Example usage for org.apache.http.client.protocol HttpClientContext HttpClientContext

List of usage examples for org.apache.http.client.protocol HttpClientContext HttpClientContext

Introduction

In this page you can find the example usage for org.apache.http.client.protocol HttpClientContext HttpClientContext.

Prototype

public HttpClientContext(final HttpContext context) 

Source Link

Usage

From source file:org.dcache.srm.client.HttpClientSender.java

/**
 * Creates the HttpContext for a particular call to a SOAP server.
 *
 * Called once per session./*from w  w w . j  av a 2  s  . co m*/
 */
protected HttpClientContext createHttpContext(MessageContext msgContext, URI uri) {
    HttpClientContext context = new HttpClientContext(new BasicHttpContext());
    // if UserID is not part of the context, but is in the URL, use
    // the one in the URL.
    String userID = msgContext.getUsername();
    String passwd = msgContext.getPassword();
    if ((userID == null) && (uri.getUserInfo() != null)) {
        String info = uri.getUserInfo();
        int sep = info.indexOf(':');
        if ((sep >= 0) && (sep + 1 < info.length())) {
            userID = info.substring(0, sep);
            passwd = info.substring(sep + 1);
        } else {
            userID = info;
        }
    }
    if (userID != null) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        // if the username is in the form "user\domain"
        // then use NTCredentials instead.
        int domainIndex = userID.indexOf('\\');
        if (domainIndex > 0 && userID.length() > domainIndex + 1) {
            String domain = userID.substring(0, domainIndex);
            String user = userID.substring(domainIndex + 1);
            credsProvider.setCredentials(AuthScope.ANY,
                    new NTCredentials(user, passwd, NetworkUtils.getLocalHostname(), domain));
        } else {
            credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userID, passwd));
        }
        context.setCredentialsProvider(credsProvider);
    }
    context.setAttribute(HttpClientTransport.TRANSPORT_HTTP_CREDENTIALS,
            msgContext.getProperty(HttpClientTransport.TRANSPORT_HTTP_CREDENTIALS));
    return context;
}