Example usage for org.apache.http.impl.auth BasicScheme BasicScheme

List of usage examples for org.apache.http.impl.auth BasicScheme BasicScheme

Introduction

In this page you can find the example usage for org.apache.http.impl.auth BasicScheme BasicScheme.

Prototype

public BasicScheme() 

Source Link

Usage

From source file:org.artifactory.util.PreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    HttpClientContext clientContext = HttpClientContext.adapt(context);
    AuthState authState = clientContext.getTargetAuthState();

    // If there's no auth scheme available yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
        HttpHost targetHost = clientContext.getTargetHost();
        Credentials creds = credsProvider
                .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
        if (creds == null) {
            log.debug("No credentials found for host " + targetHost);
        } else {/*from   w w w  .  j a  v a  2  s. c  o m*/
            log.debug("Updating credentials for host " + targetHost);
            authState.update(new BasicScheme(), creds);
        }
    }
}

From source file:de.tsystems.mms.apm.performancesignature.viewer.rest.model.CustomJenkinsHttpClient.java

public CustomJenkinsHttpClient(final URI uri, final String username, final String password,
        final boolean verifyCertificate, final CustomProxy customProxy) {

    super(uri, addAuthentication(createHttpClientBuilder(verifyCertificate, customProxy), uri, username,
            password));//from w  w w . j  a  va 2  s .c om
    if (StringUtils.isNotBlank(username)) {
        BasicHttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute("preemptive-auth", new BasicScheme());
        super.setLocalContext(httpContext);
    }
}

From source file:de.escidoc.core.http.PreemtiveAuthHttpRequestInterceptor.java

@Override
public void process(final HttpRequest httpRequest, final HttpContext httpContext)
        throws HttpException, IOException {
    final AuthState authState = (AuthState) httpContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
    final CredentialsProvider credsProvider = (CredentialsProvider) httpContext
            .getAttribute(ClientContext.CREDS_PROVIDER);
    final HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
        final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        final Credentials creds = credsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }//from   w  w w  .  java 2s  .c  o m
    }
}

From source file:org.orbeon.oxf.resources.handler.PreemptiveAuthHttpRequestInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credentialsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        Credentials credentials = credentialsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (credentials != null) {
            authState.setAuthScheme(credentials instanceof NTCredentials ? new NTLMScheme(new JCIFSEngine())
                    : new BasicScheme());
            authState.setCredentials(credentials);
        }//from   w  w  w  .j  a v  a2 s  . co m
    }
}

From source file:org.mule.jenkins.Helper.java

public void setClientInfo() {
    client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(user, password));
    BasicScheme basicAuth = new BasicScheme();

    context.setAttribute("preemptive-auth", basicAuth);

    client.addRequestInterceptor(new PreemptiveAuth(), 0);
}

From source file:com.xebialabs.deployit.ci.server.PreemptiveAuthenticationInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request.getFirstHeader("Authorization") == null) {
        LOGGER.trace("No 'Authorization' header found for request: {}", request.getRequestLine());
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        CredentialsProvider credentialsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        if (credentialsProvider != null) {
            Credentials credentials = credentialsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (credentials != null) {
                request.setHeader(new BasicScheme().authenticate(credentials, request, context));
                LOGGER.trace("Set 'Authorization' header {} for request: {}", credentials.getUserPrincipal(),
                        request.getRequestLine());
            }/*  w  ww . j av a 2  s. com*/
        }
    }
}

From source file:org.cerberus.util.HTTPSession.java

/**
 * Start a HTTP Session with authorisation
 *
 * @param username/*w  ww . ja  v  a  2s  .  c om*/
 * @param password
 */
public void startSession(String username, String password) {
    // Create your httpclient
    client = new DefaultHttpClient();
    client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

    // Then provide the right credentials
    client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(username, password));

    // Generate BASIC scheme object and stick it to the execution context
    basicAuth = new BasicScheme();
    context = new BasicHttpContext();
    context.setAttribute("preemptive-auth", basicAuth);

    // Add as the first (because of the zero) request interceptor
    // It will first intercept the request and preemptively initialize the authentication scheme if there is not
    client.addRequestInterceptor(new PreemptiveAuth(), 0);
}

From source file:org.gbif.utils.PreemptiveAuthenticationInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    LOG.debug(request.getRequestLine().toString());

    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null && credsProvider != null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        Credentials creds = credsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (creds != null) {
            LOG.debug("Authentication used for scope " + authScope.getHost());
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }//from  w w  w.j av  a 2 s. co m
    }
}

From source file:com.sayar.requests.auth.HttpBasicAuthentication.java

public Header getHttpHeader(final HttpUriRequest request) throws AuthenticationException {
    final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(this.username, this.password);
    return new BasicScheme().authenticate(creds, request);
}

From source file:org.openlmis.UiUtils.HttpClient.java

public ResponseEntity SendJSON(String json, String url, String commMethod, String username, String password) {

    HttpHost targetHost = new HttpHost(HOST, PORT, PROTOCOL);
    AuthScope localhost = new AuthScope(HOST, PORT);

    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
    httpClient.getCredentialsProvider().setCredentials(localhost, credentials);

    AuthCache authCache = new BasicAuthCache();
    authCache.put(targetHost, new BasicScheme());

    httpContext.setAttribute(AUTH_CACHE, authCache);

    try {// ww w . ja  v  a2 s  .  c  om
        return handleRequest(commMethod, json, url, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}