Example usage for org.apache.commons.httpclient HttpClient getState

List of usage examples for org.apache.commons.httpclient HttpClient getState

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpClient getState.

Prototype

public HttpState getState()

Source Link

Usage

From source file:org.apache.maven.plugin.jira.AbstractJiraDownloader.java

/**
 * Check and prepare for basic authentication.
 *
 * @param client The client to prepare// w  ww .j  a  v a  2 s  .  c  o  m
 */
private void prepareBasicAuthentication(HttpClient client) {
    if ((webUser != null) && (webUser.length() > 0)) {
        client.getParams().setAuthenticationPreemptive(true);

        Credentials defaultcreds = new UsernamePasswordCredentials(webUser, webPassword);

        getLog().debug("Using username: " + webUser + " for Basic Authentication.");

        client.getState().setCredentials(new AuthScope(null, AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME),
                defaultcreds);
    }
}

From source file:org.apache.maven.plugin.jira.AbstractJiraDownloader.java

/**
 * Setup proxy access if we have to.//  w w w  .  j av a  2  s. c o m
 *
 * @param client  the HttpClient
 */
private void determineProxy(String jiraUrl, HttpClient client) {
    // see whether there is any proxy defined in maven
    Proxy proxy = null;

    String proxyHost = null;

    int proxyPort = 0;

    String proxyUser = null;

    String proxyPass = null;

    if (project == null) {
        getLog().error("No project set. No proxy info available.");

        return;
    }

    if (settings != null) {
        proxy = settings.getActiveProxy();
    }

    if (proxy != null) {

        ProxyInfo proxyInfo = new ProxyInfo();
        proxyInfo.setNonProxyHosts(proxy.getNonProxyHosts());

        // Get the host out of the JIRA URL
        URL url = null;
        try {
            url = new URL(jiraUrl);
        } catch (MalformedURLException e) {
            getLog().error("Invalid JIRA URL: " + jiraUrl + ". " + e.getMessage());
        }
        String jiraHost = null;
        if (url != null) {
            jiraHost = url.getHost();
        }

        // Validation of proxy method copied from org.apache.maven.wagon.proxy.ProxyUtils.
        // @todo Can use original when maven-changes-plugin requires a more recent version of Maven

        //if ( ProxyUtils.validateNonProxyHosts( proxyInfo, jiraHost ) )
        if (JiraHelper.validateNonProxyHosts(proxyInfo, jiraHost)) {
            return;
        }

        proxyHost = settings.getActiveProxy().getHost();

        proxyPort = settings.getActiveProxy().getPort();

        proxyUser = settings.getActiveProxy().getUsername();

        proxyPass = settings.getActiveProxy().getPassword();

        getLog().debug(proxyPass);
    }

    if (proxyHost != null) {
        client.getHostConfiguration().setProxy(proxyHost, proxyPort);

        getLog().debug("Using proxy: " + proxyHost + " at port " + proxyPort);

        if (proxyUser != null) {
            getLog().debug("Using proxy user: " + proxyUser);

            client.getState().setProxyCredentials(
                    new AuthScope(null, AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME),
                    new UsernamePasswordCredentials(proxyUser, proxyPass));
        }
    }
}

From source file:org.apache.maven.plugin.jira.ClassicJiraDownloader.java

/**
 * Setup proxy access if we have to./*from w w w  .j a v  a2s . c o  m*/
 *
 * @param client  the HttpClient
 */
private void determineProxy(String jiraUrl, HttpClient client) {
    // see whether there is any proxy defined in maven

    getProxyInfo(jiraUrl);

    if (proxyHost != null) {
        client.getHostConfiguration().setProxy(proxyHost, proxyPort);

        getLog().debug("Using proxy: " + proxyHost + " at port " + proxyPort);

        if (proxyUser != null) {
            getLog().debug("Using proxy user: " + proxyUser);

            client.getState().setProxyCredentials(
                    new AuthScope(null, AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME),
                    new UsernamePasswordCredentials(proxyUser, proxyPass));
        }
    }
}

From source file:org.apache.maven.proxy.config.HttpRepoConfiguration.java

private HttpClient createHttpClient() {
    HttpClient client = new HttpClient();
    HostConfiguration hostConf = new HostConfiguration();
    ProxyConfiguration proxy = getProxy();

    if (proxy != null) {
        hostConf.setProxy(proxy.getHost(), proxy.getPort());
        client.setHostConfiguration(hostConf);
        if (proxy.getUsername() != null) {
            Credentials creds = new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword());
            client.getState().setProxyCredentials(null, null, creds);
        }/*from w  w  w .j a  va 2 s  .  c  om*/
    }
    return client;
}

From source file:org.apache.nutch.indexer.solr.SolrUtils.java

public static CommonsHttpSolrServer getCommonsHttpSolrServer(JobConf job) throws MalformedURLException {
    HttpClient client = new HttpClient();

    // Check for username/password
    if (job.getBoolean(SolrConstants.USE_AUTH, false)) {
        String username = job.get(SolrConstants.USERNAME);

        LOG.info("Authenticating as: " + username);

        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM,
                AuthScope.ANY_SCHEME);//  w  w w  .j  av a  2s.c  o  m

        client.getState().setCredentials(scope,
                new UsernamePasswordCredentials(username, job.get(SolrConstants.PASSWORD)));

        HttpClientParams params = client.getParams();
        params.setAuthenticationPreemptive(true);

        client.setParams(params);
    }

    return new CommonsHttpSolrServer(job.get(SolrConstants.SERVER_URL), client);
}

From source file:org.apache.nutch.indexwriter.solr.SolrUtils.java

public static CommonsHttpSolrServer getCommonsHttpSolrServer(JobConf job) throws MalformedURLException {
    HttpClient client = new HttpClient();

    // Check for username/password
    if (job.getBoolean(SolrConstants.USE_AUTH, false)) {
        String username = job.get(SolrConstants.USERNAME);

        LOG.info("Authenticating as: " + username);

        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM,
                AuthScope.ANY_SCHEME);// w w  w  .j  ava2s.  co  m

        client.getState().setCredentials(scope,
                new UsernamePasswordCredentials(username, job.get(SolrConstants.PASSWORD)));

        HttpClientParams params = client.getParams();
        params.setAuthenticationPreemptive(true);

        client.setParams(params);
    }

    String serverURL = job.get(SolrConstants.SERVER_URL);

    return new CommonsHttpSolrServer(serverURL, client);
}

From source file:org.apache.ode.axis2.httpbinding.HttpHelper.java

public static void configure(HttpClient client, URI targetURI, Element authPart, HttpParams params)
        throws URIException {
    if (log.isDebugEnabled())
        log.debug("Configuring http client...");

    /* Do not forget to wire params so that endpoint properties are passed around
       Down the road, when the request will be executed, the hierarchy of parameters will be the following:
     (-> means "is parent of")//from   w ww .  j a v a2s. co  m
     default params -> params from endpoint properties -> HttpClient -> HostConfig -> Method
       This wiring is done by HttpClient.
    */
    client.getParams().setDefaults(params);

    // Here we make sure HttpClient will not handle the default headers.
    // Actually HttpClient *appends* default headers while we want them to be ignored if the process assign them
    client.getParams().setParameter(HostParams.DEFAULT_HEADERS, Collections.EMPTY_LIST);

    // proxy configuration
    if (ProxyConf.isProxyEnabled(params, targetURI.getHost())) {
        if (log.isDebugEnabled())
            log.debug("ProxyConf");
        ProxyConf.configure(client.getHostConfiguration(), client.getState(),
                (HttpTransportProperties.ProxyProperties) params
                        .getParameter(Properties.PROP_HTTP_PROXY_PREFIX));
    }

    // security
    // ...

    // authentication
    /*
    We're expecting the following element:
    <xs:complexType name="credentialType">
    <xs:attribute name="scheme" type="xs:string" default="server-decide" />
    <xs:attribute name="username" type="xs:string" />
    <xs:attribute name="password" type="xs:string" />
    </xs:complexType>
    <xs:element type="rest_connector:credentialType" name="credentials" />
     */
    if (authPart != null) {
        // the part must be defined with an element, so take the fist child
        Element credentialsElement = DOMUtils.getFirstChildElement(authPart);
        if (credentialsElement != null && credentialsElement.getAttributes().getLength() != 0) {
            String scheme = DOMUtils.getAttribute(credentialsElement, "scheme");
            String username = DOMUtils.getAttribute(credentialsElement, "username");
            String password = DOMUtils.getAttribute(credentialsElement, "password");

            if (scheme != null && !"server-decides".equalsIgnoreCase(scheme)
                    && !"basic".equalsIgnoreCase(scheme) && !"digest".equalsIgnoreCase(scheme)) {
                throw new IllegalArgumentException("Unknown Authentication scheme: [" + scheme
                        + "] Accepted values are: Basic, Digest, Server-Decides");
            } else {
                if (log.isDebugEnabled())
                    log.debug("credentials provided: scheme=" + scheme + " user=" + username
                            + " password=********");
                client.getState().setCredentials(
                        new AuthScope(targetURI.getHost(), targetURI.getPort(), AuthScope.ANY_REALM, scheme),
                        new UsernamePasswordCredentials(username, password));
                // save one round trip if basic
                client.getParams().setAuthenticationPreemptive("basic".equalsIgnoreCase(scheme));
            }
        }
    }
}

From source file:org.apache.ode.tools.sendsoap.cline.HttpSoapSender.java

public static String doSend(URL u, InputStream is, String proxyServer, int proxyPort, String username,
        String password, String soapAction) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
    StreamUtils.copy(bos, is);//  w  w  w  . j  av  a2s.com
    String now = Long.toString(System.currentTimeMillis());
    int c = 1;
    String data = new String(bos.toByteArray());
    Matcher m = SEQ.matcher(data);
    StringBuffer sb = new StringBuffer(8192);
    while (m.find()) {
        m.appendReplacement(sb, now + "-" + c++);
    }
    m.appendTail(sb);
    SimpleHttpConnectionManager mgr = new SimpleHttpConnectionManager();
    try {
        mgr.getParams().setConnectionTimeout(60000);
        mgr.getParams().setSoTimeout(60000);
        HttpClient httpClient = new HttpClient(mgr);
        PostMethod httpPostMethod = new PostMethod(u.toExternalForm());
        if (proxyServer != null && proxyServer.length() > 0) {
            httpClient.getState().setCredentials(new AuthScope(proxyServer, proxyPort),
                    new UsernamePasswordCredentials(username, password));
            httpPostMethod.setDoAuthentication(true);
        }
        if (soapAction == null)
            soapAction = "";
        httpPostMethod.setRequestHeader("SOAPAction", "\"" + soapAction + "\"");
        httpPostMethod.setRequestHeader("Content-Type", "text/xml");
        httpPostMethod.setRequestEntity(new StringRequestEntity(sb.toString()));
        httpClient.executeMethod(httpPostMethod);
        return httpPostMethod.getResponseBodyAsString() + "\n";
    } finally {
        mgr.shutdown();
    }
}

From source file:org.apache.servicemix.http.BasicAuthCredentials.java

/**
 * Applies this authentication to the given method.
 * /*from w ww. jav  a2  s.com*/
 * @param client the client on which to set the authentication information
 * @param exchange the message exchange to be used for evaluating the expression
 * @param message the normalized message to be used for evaluating the expression
 * @throws MessagingException if the correct value for username/password cannot be determined when using an expression
 */
public void applyCredentials(HttpClient client, MessageExchange exchange, NormalizedMessage message)
        throws MessagingException {
    AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
    Credentials credentials = new UsernamePasswordCredentials(
            (String) this.username.evaluate(exchange, message),
            (String) this.password.evaluate(exchange, message));
    client.getState().setCredentials(scope, credentials);
}

From source file:org.apache.servicemix.http.BasicAuthCredentials.java

/**
 * Applies this authentication to the given method.
 * //from w w  w.  j  a v a2  s. c o m
 * @param client the client on which to set the authentication information
 * @param exchange the message exchange to be used for evaluating the expression
 * @param message the normalized message to be used for evaluating the expression
 * @throws MessagingException if the correct value for user name/password cannot be determined when using an expression
 */
public void applyProxyCredentials(HttpClient client, MessageExchange exchange, NormalizedMessage message)
        throws MessagingException {
    AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
    Credentials credentials = new UsernamePasswordCredentials(
            (String) this.username.evaluate(exchange, message),
            (String) this.password.evaluate(exchange, message));
    client.getState().setProxyCredentials(scope, credentials);
}