Java 类org.apache.http.auth.AuthScheme 实例源码

项目:remote-files-sync    文件:AuthenticationStrategyImpl.java   
public void authSucceeded(
        final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    Args.notNull(authhost, "Host");
    Args.notNull(authScheme, "Auth scheme");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    if (isCachable(authScheme)) {
        AuthCache authCache = clientContext.getAuthCache();
        if (authCache == null) {
            authCache = new BasicAuthCache();
            clientContext.setAuthCache(authCache);
        }
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "Caching '" + authScheme.getSchemeName() +
                    "' auth scheme for " + authhost);
        }
        authCache.put(authhost, authScheme);
    }
}
项目:lams    文件:RequestAuthCache.java   
private void doPreemptiveAuth(
        final HttpHost host,
        final AuthScheme authScheme,
        final AuthState authState,
        final CredentialsProvider credsProvider) {
    String schemeName = authScheme.getSchemeName();
    if (this.log.isDebugEnabled()) {
        this.log.debug("Re-using cached '" + schemeName + "' auth scheme for " + host);
    }

    AuthScope authScope = new AuthScope(host, AuthScope.ANY_REALM, schemeName);
    Credentials creds = credsProvider.getCredentials(authScope);

    if (creds != null) {
        authState.setState(AuthProtocolState.SUCCESS);
        authState.update(authScheme, creds);
    } else {
        this.log.debug("No credentials for preemptive authentication");
    }
}
项目:lams    文件:AuthenticationStrategyImpl.java   
public void authSucceeded(
        final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    if (authhost == null) {
        throw new IllegalArgumentException("Host may not be null");
    }
    if (authScheme == null) {
        throw new IllegalArgumentException("Auth scheme may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    if (isCachable(authScheme)) {
        AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
        if (authCache == null) {
            authCache = new BasicAuthCache();
            context.setAttribute(ClientContext.AUTH_CACHE, authCache);
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("Caching '" + authScheme.getSchemeName() +
                    "' auth scheme for " + authhost);
        }
        authCache.put(authhost, authScheme);
    }
}
项目:lams    文件:AuthenticationStrategyImpl.java   
public void authFailed(
        final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    if (authhost == null) {
        throw new IllegalArgumentException("Host may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
    if (authCache != null) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Clearing cached auth scheme for " + authhost);
        }
        authCache.remove(authhost);
    }
}
项目:cyberduck    文件:CallbackProxyAuthenticationStrategy.java   
@Override
public void authSucceeded(final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    final HttpClientContext clientContext = HttpClientContext.adapt(context);
    final Credentials credentials = clientContext.getAttribute(PROXY_CREDENTIALS_INPUT_ID, Credentials.class);
    if(null != credentials) {
        clientContext.removeAttribute(PROXY_CREDENTIALS_INPUT_ID);
        if(log.isInfoEnabled()) {
            log.info(String.format("Save passphrase for proxy %s", authhost));
        }
        keychain.addCredentials(authhost.getHostName(), credentials.getUsername(), credentials.getPassword());
    }
    super.authSucceeded(authhost, authScheme, context);
}
项目:beyondj    文件:HttpSender.java   
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {

    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }

}
项目:remote-files-sync    文件:RequestAuthCache.java   
private void doPreemptiveAuth(
        final HttpHost host,
        final AuthScheme authScheme,
        final AuthStateHC4 authState,
        final CredentialsProvider credsProvider) {
    final String schemeName = authScheme.getSchemeName();
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "Re-using cached '" + schemeName + "' auth scheme for " + host);
    }

    final AuthScope authScope = new AuthScope(host.getHostName(), host.getPort(), AuthScope.ANY_REALM, schemeName);
    final Credentials creds = credsProvider.getCredentials(authScope);

    if (creds != null) {
        if ("BASIC".equalsIgnoreCase(authScheme.getSchemeName())) {
            authState.setState(AuthProtocolState.CHALLENGED);
        } else {
            authState.setState(AuthProtocolState.SUCCESS);
        }
        authState.update(authScheme, creds);
    } else {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "No credentials for preemptive authentication");
        }
    }
}
项目:remote-files-sync    文件:AuthenticationStrategyImpl.java   
public void authFailed(
        final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    Args.notNull(authhost, "Host");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    final AuthCache authCache = clientContext.getAuthCache();
    if (authCache != null) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "Clearing cached auth scheme for " + authhost);
        }
        authCache.remove(authhost);
    }
}
项目:jenkinsfile-maven-plugin    文件:PreemptiveAuth.java   
@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {

    AuthState authState = (AuthState) httpContext.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) httpContext.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) httpContext
                .getAttribute(HttpClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) httpContext.getAttribute(HttpClientContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            Credentials creds = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.update(authScheme, creds);
        }
    }
}
项目:Camel    文件:PreemptiveAuthInterceptor.java   
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.update(authScheme, creds);
        }
    }

}
项目:purecloud-iot    文件:AuthenticationStrategyAdaptor.java   
@Override
public void authSucceeded(
        final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
    if (isCachable(authScheme)) {
        if (authCache == null) {
            authCache = new BasicAuthCache();
            context.setAttribute(ClientContext.AUTH_CACHE, authCache);
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("Caching '" + authScheme.getSchemeName() +
                    "' auth scheme for " + authhost);
        }
        authCache.put(authhost, authScheme);
    }
}
项目:purecloud-iot    文件:RequestAuthCache.java   
private void doPreemptiveAuth(
        final HttpHost host,
        final AuthScheme authScheme,
        final AuthState authState,
        final CredentialsProvider credsProvider) {
    final String schemeName = authScheme.getSchemeName();
    if (this.log.isDebugEnabled()) {
        this.log.debug("Re-using cached '" + schemeName + "' auth scheme for " + host);
    }

    final AuthScope authScope = new AuthScope(host, AuthScope.ANY_REALM, schemeName);
    final Credentials creds = credsProvider.getCredentials(authScope);

    if (creds != null) {
        if ("BASIC".equalsIgnoreCase(authScheme.getSchemeName())) {
            authState.setState(AuthProtocolState.CHALLENGED);
        } else {
            authState.setState(AuthProtocolState.SUCCESS);
        }
        authState.update(authScheme, creds);
    } else {
        this.log.debug("No credentials for preemptive authentication");
    }
}
项目:purecloud-iot    文件:AuthenticationStrategyImpl.java   
@Override
public void authSucceeded(
        final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    Args.notNull(authhost, "Host");
    Args.notNull(authScheme, "Auth scheme");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    if (isCachable(authScheme)) {
        AuthCache authCache = clientContext.getAuthCache();
        if (authCache == null) {
            authCache = new BasicAuthCache();
            clientContext.setAuthCache(authCache);
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("Caching '" + authScheme.getSchemeName() +
                    "' auth scheme for " + authhost);
        }
        authCache.put(authhost, authScheme);
    }
}
项目:purecloud-iot    文件:AuthenticationStrategyImpl.java   
@Override
public void authFailed(
        final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    Args.notNull(authhost, "Host");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    final AuthCache authCache = clientContext.getAuthCache();
    if (authCache != null) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Clearing cached auth scheme for " + authhost);
        }
        authCache.remove(authhost);
    }
}
项目:purecloud-iot    文件:BasicAuthCache.java   
@Override
public void put(final HttpHost host, final AuthScheme authScheme) {
    Args.notNull(host, "HTTP host");
    if (authScheme == null) {
        return;
    }
    if (authScheme instanceof Serializable) {
        try {
            final ByteArrayOutputStream buf = new ByteArrayOutputStream();
            final ObjectOutputStream out = new ObjectOutputStream(buf);
            out.writeObject(authScheme);
            out.close();
            this.map.put(getKey(host), buf.toByteArray());
        } catch (final IOException ex) {
            if (log.isWarnEnabled()) {
                log.warn("Unexpected I/O error while serializing auth scheme", ex);
            }
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Auth scheme " + authScheme.getClass() + " is not serializable");
        }
    }
}
项目:purecloud-iot    文件:TestAuthenticationStrategy.java   
@Test
public void testAuthScemeNonCacheable() throws Exception {
    final TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
    final HttpHost authhost = new HttpHost("somehost", 80);
    final AuthScheme authScheme = Mockito.mock(AuthScheme.class);
    Mockito.when(authScheme.isComplete()).thenReturn(true);
    Mockito.when(authScheme.getSchemeName()).thenReturn("whatever");

    final AuthCache authCache = Mockito.mock(AuthCache.class);

    final HttpClientContext context = HttpClientContext.create();
    context.setAuthCache(authCache);

    authStrategy.authSucceeded(authhost, authScheme, context);
    Mockito.verify(authCache, Mockito.never()).put(authhost, authScheme);
}
项目:Visit    文件:RequestAuthCache.java   
private void doPreemptiveAuth(
        final HttpHost host,
        final AuthScheme authScheme,
        final AuthStateHC4 authState,
        final CredentialsProvider credsProvider) {
    final String schemeName = authScheme.getSchemeName();
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "Re-using cached '" + schemeName + "' auth scheme for " + host);
    }

    final AuthScope authScope = new AuthScope(host.getHostName(), host.getPort(), AuthScope.ANY_REALM, schemeName);
    final Credentials creds = credsProvider.getCredentials(authScope);

    if (creds != null) {
        if ("BASIC".equalsIgnoreCase(authScheme.getSchemeName())) {
            authState.setState(AuthProtocolState.CHALLENGED);
        } else {
            authState.setState(AuthProtocolState.SUCCESS);
        }
        authState.update(authScheme, creds);
    } else {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "No credentials for preemptive authentication");
        }
    }
}
项目:Visit    文件:AuthenticationStrategyImpl.java   
public void authSucceeded(
        final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    Args.notNull(authhost, "Host");
    Args.notNull(authScheme, "Auth scheme");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    if (isCachable(authScheme)) {
        AuthCache authCache = clientContext.getAuthCache();
        if (authCache == null) {
            authCache = new BasicAuthCache();
            clientContext.setAuthCache(authCache);
        }
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "Caching '" + authScheme.getSchemeName() +
                    "' auth scheme for " + authhost);
        }
        authCache.put(authhost, authScheme);
    }
}
项目:Wilma    文件:BrowserMobHttpClient.java   
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {

    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    // If no auth scheme available yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds != null) {
                authState.setAuthScheme(authScheme);
                authState.setCredentials(creds);
            }
        }
    }
}
项目:activemq-artemis    文件:UriStrategy.java   
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
   AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);

   // If no auth scheme available yet, try to initialize it preemptively
   if (authState.getAuthScheme() == null) {
      AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
      CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
      HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
      if (authScheme != null) {
         Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
         if (creds == null) {
            throw new HttpException("No credentials for preemptive authentication");
         }
         authState.update(authScheme, creds);
      }
   }
}
项目:verigreen    文件:PreemptiveAuth.java   
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    Credentials creds;

    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider =
                (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost =
                (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            creds =
                    credsProvider.getCredentials(new AuthScope(
                            targetHost.getHostName(),
                            targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.update(authScheme, creds);
        }
    }
}
项目:java-http-signature    文件:HttpSignatureAuthenticationStrategy.java   
@Override
public void authFailed(final HttpHost authhost,
                       final AuthScheme authScheme,
                       final HttpContext context) {
    Objects.requireNonNull(authhost, "Authentication host must be present");
    Objects.requireNonNull(context, "HTTP context must be present");

    LOG.debug("HTTP Signature authentication failed");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    final AuthCache authCache = clientContext.getAuthCache();
    if (authCache != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Clearing cached auth scheme for " + authhost);
        }
        authCache.remove(authhost);
    }
}
项目:hql-builder    文件:HqlWebServiceClientFactory.java   
@SuppressWarnings("deprecation")
protected org.jboss.resteasy.client.ClientExecutor setupClientExecutor() {
    AuthCache authCache = new BasicAuthCache();
    AuthScheme basicAuth = new BasicScheme();
    authCache.put(new HttpHost(getServiceUrl()), basicAuth);

    BasicHttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(URI.create(getServiceUrl()).getHost(), authCache);

    // BasicCredentialsProvider credentialsProvider = new
    // BasicCredentialsProvider();
    // Credentials credentials = new
    // UsernamePasswordCredentials("hqlbuilder", "hqlbuilder");
    // credentialsProvider.setCredentials(new
    // AuthScope(URI.create(getServiceUrl()).getHost(),
    // URI.create(getServiceUrl()).getPort()),
    // credentials);

    HttpClient httpClient = HttpClientBuilder.create()//
            // .setDefaultCredentialsProvider(credentialsProvider)//
            .build();//

    return new ApacheHttpClient4Executor(httpClient, localContext);
}
项目:FullRobolectricTestSample    文件:DefaultRequestDirector.java   
private void processChallenges(
    final Map<String, Header> challenges,
    final AuthState authState,
    final AuthenticationHandler authHandler,
    final HttpResponse response,
    final HttpContext context)
      throws MalformedChallengeException, AuthenticationException {

  AuthScheme authScheme = authState.getAuthScheme();
  if (authScheme == null) {
    // Authentication not attempted before
    authScheme = authHandler.selectScheme(challenges, response, context);
    authState.setAuthScheme(authScheme);
  }
  String id = authScheme.getSchemeName();

  Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
  if (challenge == null) {
    throw new AuthenticationException(id +
      " authorization challenge expected, but not found");
  }
  authScheme.processChallenge(challenge);
  this.log.debug("Authorization challenge processed");
}
项目:cJUnit-mc626    文件:DefaultRequestDirector.java   
private void processChallenges(
        final Map<String, Header> challenges, 
        final AuthState authState,
        final AuthenticationHandler authHandler,
        final HttpResponse response, 
        final HttpContext context) 
            throws MalformedChallengeException, AuthenticationException {

    AuthScheme authScheme = authState.getAuthScheme();
    if (authScheme == null) {
        // Authentication not attempted before
        authScheme = authHandler.selectScheme(challenges, response, context);
        authState.setAuthScheme(authScheme);
    }
    String id = authScheme.getSchemeName();

    Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
    if (challenge == null) {
        throw new AuthenticationException(id + 
            " authorization challenge expected, but not found");
    }
    authScheme.processChallenge(challenge);
    this.log.debug("Authorization challenge processed");
}
项目:SoundcloudAPI    文件:OAuth2HttpRequestInterceptor.java   
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    if (request == null) throw new IllegalArgumentException("HTTP request may not be null");
    if (context == null) throw new IllegalArgumentException("HTTP context may not be null");

    if (!request.getRequestLine().getMethod().equalsIgnoreCase("CONNECT")) {
        AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
        if (authState != null) {
            AuthScheme authScheme = authState.getAuthScheme();
            if (authScheme != null && !authScheme.isConnectionBased()) {
                try {
                    request.setHeader(authScheme.authenticate(null, request));
                } catch (AuthenticationException ignored) {
                    // ignored
                }
            }
        }
    }
}
项目:stash-pullrequest-jenkins    文件:JenkinsJobTrigger.java   
@SuppressWarnings("deprecation")
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    // Get the AuthState
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    // If no auth scheme available yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost
                    .getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }

}
项目:build-flow-http-extension    文件:PreemptiveHttpClient.java   
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {

            AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

            // If no auth scheme available yet, try to initialize it preemptively
            if (authState.getAuthScheme() == null) {
                AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
                CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
                        ClientContext.CREDS_PROVIDER);
                HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
                if (authScheme != null) {
                    Credentials creds = credsProvider.getCredentials(
                            new AuthScope(targetHost.getHostName(), targetHost.getPort()));
                    if (creds == null) {
                        throw new HttpException("No credentials for preemptive authentication");
                    }
                    authState.setAuthScheme(authScheme);
                    authState.setCredentials(creds);
                }
            }
        }
项目:paperchains    文件:OAuth2HttpRequestInterceptor.java   
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    if (request == null) throw new IllegalArgumentException("HTTP request may not be null");
    if (context == null) throw new IllegalArgumentException("HTTP context may not be null");

    if (!request.getRequestLine().getMethod().equalsIgnoreCase("CONNECT")) {
        AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
        if (authState != null) {
            AuthScheme authScheme = authState.getAuthScheme();
            if (authScheme != null && !authScheme.isConnectionBased()) {
                try {
                    request.setHeader(authScheme.authenticate(null, request));
                } catch (AuthenticationException ignored) {
                    // ignored
                }
            }
        }
    }
}
项目:ZTLib    文件:RequestAuthCache.java   
private void doPreemptiveAuth(
        final HttpHost host,
        final AuthScheme authScheme,
        final AuthStateHC4 authState,
        final CredentialsProvider credsProvider) {
    final String schemeName = authScheme.getSchemeName();
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "Re-using cached '" + schemeName + "' auth scheme for " + host);
    }

    final AuthScope authScope = new AuthScope(host.getHostName(), host.getPort(), AuthScope.ANY_REALM, schemeName);
    final Credentials creds = credsProvider.getCredentials(authScope);

    if (creds != null) {
        if ("BASIC".equalsIgnoreCase(authScheme.getSchemeName())) {
            authState.setState(AuthProtocolState.CHALLENGED);
        } else {
            authState.setState(AuthProtocolState.SUCCESS);
        }
        authState.update(authScheme, creds);
    } else {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "No credentials for preemptive authentication");
        }
    }
}
项目:ZTLib    文件:AuthenticationStrategyImpl.java   
public void authSucceeded(
        final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    Args.notNull(authhost, "Host");
    Args.notNull(authScheme, "Auth scheme");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    if (isCachable(authScheme)) {
        AuthCache authCache = clientContext.getAuthCache();
        if (authCache == null) {
            authCache = new BasicAuthCache();
            clientContext.setAuthCache(authCache);
        }
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "Caching '" + authScheme.getSchemeName() +
                    "' auth scheme for " + authhost);
        }
        authCache.put(authhost, authScheme);
    }
}
项目:exchange-ws-client    文件:PreemptiveAuthInterceptor.java   
@Override
public void process(HttpRequest request, HttpContext context)
        throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {
        AuthScheme preemptiveAuthScheme = (AuthScheme) context.getAttribute(PREEMPTIVE_AUTH);
        if (preemptiveAuthScheme != null) {
            CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
            Credentials creds = credsProvider.getCredentials(authScope);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.setAuthScheme(preemptiveAuthScheme);
            authState.setCredentials(creds);
            if(log.isTraceEnabled()) {
                log.trace("successfully set credentials " + creds + " and authScheme " + preemptiveAuthScheme + " for request " + request);
            }
        } else {
            log.warn(PREEMPTIVE_AUTH + " authScheme not found in context; make sure you are using the CustomHttpComponentsMessageSender and the preemptiveAuthEnabled property is set to true");
        }
    } else {
        log.warn("context's authState attribute (" + authState + ") has non-null AuthScheme for request " + request);
    }
}
项目:YiBo    文件:LibRequestDirector.java   
private void processChallenges(
        final Map<String, Header> challenges,
        final AuthState authState,
        final AuthenticationHandler authHandler,
        final HttpResponse response,
        final HttpContext context)
            throws MalformedChallengeException, AuthenticationException {

    AuthScheme authScheme = authState.getAuthScheme();
    if (authScheme == null) {
        // Authentication not attempted before
        authScheme = authHandler.selectScheme(challenges, response, context);
        authState.setAuthScheme(authScheme);
    }
    String id = authScheme.getSchemeName();

    Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
    if (challenge == null) {
        throw new AuthenticationException(id +
            " authorization challenge expected, but not found");
    }
    authScheme.processChallenge(challenge);
    if (DEBUG) {
        Logger.debug("Authorization challenge processed");
    }
}
项目:jmeter_oauth_plugin    文件:PreemptiveAuthorizer.java   
/**
 * If no auth scheme has been selected for the given context, consider each
 * of the preferred auth schemes and select the first one for which an
 * AuthScheme and matching Credentials are available.
 */
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState != null && authState.getAuthScheme() != null) {
        return;
    }
    HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    CredentialsProvider creds = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    AuthSchemeRegistry schemes = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY);
    for (Object schemeName : (Iterable) context.getAttribute(ClientContext.AUTH_SCHEME_PREF)) {
        AuthScheme scheme = schemes.getAuthScheme(schemeName.toString(), request.getParams());
        if (scheme != null) {
            AuthScope targetScope = new AuthScope(target.getHostName(), target.getPort(), scheme.getRealm(), scheme
                    .getSchemeName());
            Credentials cred = creds.getCredentials(targetScope);
            if (cred != null) {
                authState.setAuthScheme(scheme);
                authState.setCredentials(cred);
                return;
            }
        }
    }
}
项目:cobbzilla-utils    文件:HttpRequestBean.java   
public HttpClientBuilder initClientBuilder(HttpClientBuilder clientBuilder) {
    if (!hasAuth()) return clientBuilder;
    final HttpClientContext localContext = HttpClientContext.create();
    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(getHost(), getPort()),
            new UsernamePasswordCredentials(getAuthUsername(), getAuthPassword()));

    final AuthCache authCache = new BasicAuthCache();
    final AuthScheme authScheme = getAuthType().newScheme();
    authCache.put(getHttpHost(), authScheme);

    localContext.setAuthCache(authCache);
    clientBuilder.setDefaultCredentialsProvider(credsProvider);
    return clientBuilder;
}
项目:cloudhopper-commons    文件:HttpSender.java   
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {

    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }

}
项目:yibo-library    文件:YiBoRequestDirector.java   
private void processChallenges(
        final Map<String, Header> challenges,
        final AuthState authState,
        final AuthenticationHandler authHandler,
        final HttpResponse response,
        final HttpContext context)
            throws MalformedChallengeException, AuthenticationException {

    AuthScheme authScheme = authState.getAuthScheme();
    if (authScheme == null) {
        // Authentication not attempted before
        authScheme = authHandler.selectScheme(challenges, response, context);
        authState.setAuthScheme(authScheme);
    }
    String id = authScheme.getSchemeName();

    Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
    if (challenge == null) {
        throw new AuthenticationException(id +
            " authorization challenge expected, but not found");
    }
    authScheme.processChallenge(challenge);
    if (Constants.DEBUG) {
        logger.debug("Authorization challenge processed");
    }
}
项目:net.oauth    文件:PreemptiveAuthorizer.java   
/**
 * If no auth scheme has been selected for the given context, consider each
 * of the preferred auth schemes and select the first one for which an
 * AuthScheme and matching Credentials are available.
 */
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState != null && authState.getAuthScheme() != null) {
        return;
    }
    HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    CredentialsProvider creds = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    AuthSchemeRegistry schemes = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY);
    for (Object schemeName : (Iterable) context.getAttribute(ClientContext.AUTH_SCHEME_PREF)) {
        AuthScheme scheme = schemes.getAuthScheme(schemeName.toString(), request.getParams());
        if (scheme != null) {
            AuthScope targetScope = new AuthScope(target.getHostName(), target.getPort(), scheme.getRealm(), scheme
                    .getSchemeName());
            Credentials cred = creds.getCredentials(targetScope);
            if (cred != null) {
                authState.setAuthScheme(scheme);
                authState.setCredentials(cred);
                return;
            }
        }
    }
}
项目:net.oauth    文件:PreemptiveAuthorizer.java   
/**
 * If no auth scheme has been selected for the given context, consider each
 * of the preferred auth schemes and select the first one for which an
 * AuthScheme and matching Credentials are available.
 */
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState != null && authState.getAuthScheme() != null) {
        return;
    }
    HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    CredentialsProvider creds = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    AuthSchemeRegistry schemes = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY);
    for (Object schemeName : (Iterable) context.getAttribute(ClientContext.AUTH_SCHEME_PREF)) {
        AuthScheme scheme = schemes.getAuthScheme(schemeName.toString(), request.getParams());
        if (scheme != null) {
            AuthScope targetScope = new AuthScope(target.getHostName(), target.getPort(), scheme.getRealm(), scheme
                    .getSchemeName());
            Credentials cred = creds.getCredentials(targetScope);
            if (cred != null) {
                authState.setAuthScheme(scheme);
                authState.setCredentials(cred);
                return;
            }
        }
    }
}
项目:SecureShareLib    文件:OAuth2HttpRequestInterceptor.java   
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    if (request == null) throw new IllegalArgumentException("HTTP request may not be null");
    if (context == null) throw new IllegalArgumentException("HTTP context may not be null");

    if (!request.getRequestLine().getMethod().equalsIgnoreCase("CONNECT")) {
        AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
        if (authState != null) {
            AuthScheme authScheme = authState.getAuthScheme();
            if (authScheme != null && !authScheme.isConnectionBased()) {
                try {
                    request.setHeader(authScheme.authenticate(null, request));
                } catch (AuthenticationException ignored) {
                    // ignored
                }
            }
        }
    }
}