Example usage for org.apache.commons.httpclient HttpMethodBase setDoAuthentication

List of usage examples for org.apache.commons.httpclient HttpMethodBase setDoAuthentication

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodBase setDoAuthentication.

Prototype

@Override
public void setDoAuthentication(boolean doAuthentication) 

Source Link

Document

Sets whether or not the HTTP method should automatically handle HTTP authentication challenges (status code 401, etc.)

Usage

From source file:org.rhq.enterprise.server.plugins.url.HttpProvider.java

/**
 * Given a client and the method to be used by that client, this will prepare those objects
 * so they can be used to get the remote content.
 * //from  w  ww .j  ava2  s.c  om
 * @param client
 * @param method
 * 
 * @throws Exception if the client cannot be prepared successfully
 */
protected void prepareHttpClient(HttpClient client, HttpMethodBase method) throws Exception {

    // prepare the client with proxy info, if appropriate
    configureProxy(client);

    // setup the authentication
    method.setFollowRedirects(true);

    if (this.username != null) {
        method.setDoAuthentication(true);

        org.apache.commons.httpclient.URI fullUri = method.getURI();
        AuthScope authScope = new AuthScope(fullUri.getHost(), fullUri.getPort(), AuthScope.ANY_REALM);
        Credentials credentials = new UsernamePasswordCredentials(this.username, this.password);
        client.getState().setCredentials(authScope, credentials);
    }

    return;
}

From source file:org.rssowl.core.internal.connection.DefaultProtocolHandler.java

private void setupAuthentication(URI link, String realm, HttpClient client, HttpMethodBase method)
        throws URIException, CredentialsException {
    ICredentials authCredentials = Owl.getConnectionService().getAuthCredentials(link, realm);
    if (authCredentials != null) {
        client.getParams().setAuthenticationPreemptive(true);

        /* Require Host */
        String host = method.getURI().getHost();

        /* Create the UsernamePasswordCredentials */
        NTCredentials userPwCreds = new NTCredentials(authCredentials.getUsername(),
                authCredentials.getPassword(), host,
                (authCredentials.getDomain() != null) ? authCredentials.getDomain() : ""); //$NON-NLS-1$

        /* Authenticate to the Server */
        client.getState().setCredentials(AuthScope.ANY, userPwCreds);
        method.setDoAuthentication(true);
    }/*from  w w w  . j  a v  a2s  .  c o  m*/
}

From source file:org.tinygroup.httpvisit.impl.HttpVisitorImpl.java

String execute(HttpMethodBase method) {
    try {/*from  w  ww  .  j av  a 2s .  co  m*/
        if (client == null) {
            init();
        }
        LOGGER.logMessage(LogLevel.DEBUG, "?:{}", method.getURI().toString());
        if (!("ISO-8859-1").equals(requestCharset)) {
            method.addRequestHeader("Content-Type", "text/html; charset=" + requestCharset);
        }
        method.setDoAuthentication(authEnabled);
        int iGetResultCode = client.executeMethod(method);
        if (iGetResultCode == HttpStatus.SC_OK) {
            LOGGER.logMessage(LogLevel.DEBUG, "?");
            Header responseHeader = method.getResponseHeader("Content-Encoding");
            if (responseHeader != null) {
                String acceptEncoding = responseHeader.getValue();
                if (acceptEncoding != null && ("gzip").equals(acceptEncoding)) {
                    //gzip?
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                            method.getResponseBody());
                    GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);
                    return IOUtils.readFromInputStream(gzipInputStream, responseCharset);
                }
            }
            return new String(method.getResponseBody(), responseCharset);
        }
        LOGGER.logMessage(LogLevel.ERROR, "{}",
                method.getStatusLine().toString());
        throw new RuntimeException(method.getStatusLine().toString());
    } catch (Exception e) {
        LOGGER.logMessage(LogLevel.DEBUG, "{}", e.getMessage());
        throw new RuntimeException(e);
    } finally {
        method.releaseConnection();
    }
}