mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/protocol/RequestAuthCache.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/protocol/RequestAuthCache.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.client.protocol;
    1.32 +
    1.33 +import java.io.IOException;
    1.34 +
    1.35 +import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
    1.36 +/* LogFactory removed by HttpClient for Android script. */
    1.37 +import ch.boye.httpclientandroidlib.HttpException;
    1.38 +import ch.boye.httpclientandroidlib.HttpHost;
    1.39 +import ch.boye.httpclientandroidlib.HttpRequest;
    1.40 +import ch.boye.httpclientandroidlib.HttpRequestInterceptor;
    1.41 +import ch.boye.httpclientandroidlib.annotation.Immutable;
    1.42 +import ch.boye.httpclientandroidlib.auth.AuthScheme;
    1.43 +import ch.boye.httpclientandroidlib.auth.AuthScope;
    1.44 +import ch.boye.httpclientandroidlib.auth.AuthState;
    1.45 +import ch.boye.httpclientandroidlib.auth.Credentials;
    1.46 +import ch.boye.httpclientandroidlib.client.AuthCache;
    1.47 +import ch.boye.httpclientandroidlib.client.CredentialsProvider;
    1.48 +import ch.boye.httpclientandroidlib.protocol.ExecutionContext;
    1.49 +import ch.boye.httpclientandroidlib.protocol.HttpContext;
    1.50 +
    1.51 +/**
    1.52 + * Request interceptor that can preemptively authenticate against known hosts,
    1.53 + * if there is a cached {@link AuthScheme} instance in the local
    1.54 + * {@link AuthCache} associated with the given target or proxy host.
    1.55 + *
    1.56 + * @since 4.1
    1.57 + */
    1.58 +@Immutable
    1.59 +public class RequestAuthCache implements HttpRequestInterceptor {
    1.60 +
    1.61 +    public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
    1.62 +
    1.63 +    public RequestAuthCache() {
    1.64 +        super();
    1.65 +    }
    1.66 +
    1.67 +    public void process(final HttpRequest request, final HttpContext context)
    1.68 +            throws HttpException, IOException {
    1.69 +        if (request == null) {
    1.70 +            throw new IllegalArgumentException("HTTP request may not be null");
    1.71 +        }
    1.72 +        if (context == null) {
    1.73 +            throw new IllegalArgumentException("HTTP context may not be null");
    1.74 +        }
    1.75 +
    1.76 +        AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
    1.77 +        if (authCache == null) {
    1.78 +            this.log.debug("Auth cache not set in the context");
    1.79 +            return;
    1.80 +        }
    1.81 +
    1.82 +        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
    1.83 +                ClientContext.CREDS_PROVIDER);
    1.84 +        if (credsProvider == null) {
    1.85 +            this.log.debug("Credentials provider not set in the context");
    1.86 +            return;
    1.87 +        }
    1.88 +
    1.89 +        HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    1.90 +        AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    1.91 +        if (target != null && targetState != null && targetState.getAuthScheme() == null) {
    1.92 +            AuthScheme authScheme = authCache.get(target);
    1.93 +            if (authScheme != null) {
    1.94 +                doPreemptiveAuth(target, authScheme, targetState, credsProvider);
    1.95 +            }
    1.96 +        }
    1.97 +
    1.98 +        HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
    1.99 +        AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
   1.100 +        if (proxy != null && proxyState != null && proxyState.getAuthScheme() == null) {
   1.101 +            AuthScheme authScheme = authCache.get(proxy);
   1.102 +            if (authScheme != null) {
   1.103 +                doPreemptiveAuth(proxy, authScheme, proxyState, credsProvider);
   1.104 +            }
   1.105 +        }
   1.106 +    }
   1.107 +
   1.108 +    private void doPreemptiveAuth(
   1.109 +            final HttpHost host,
   1.110 +            final AuthScheme authScheme,
   1.111 +            final AuthState authState,
   1.112 +            final CredentialsProvider credsProvider) {
   1.113 +        String schemeName = authScheme.getSchemeName();
   1.114 +        if (this.log.isDebugEnabled()) {
   1.115 +            this.log.debug("Re-using cached '" + schemeName + "' auth scheme for " + host);
   1.116 +        }
   1.117 +
   1.118 +        AuthScope authScope = new AuthScope(host.getHostName(), host.getPort(),
   1.119 +                AuthScope.ANY_REALM, schemeName);
   1.120 +        Credentials creds = credsProvider.getCredentials(authScope);
   1.121 +
   1.122 +        if (creds != null) {
   1.123 +            authState.setAuthScheme(authScheme);
   1.124 +            authState.setCredentials(creds);
   1.125 +        } else {
   1.126 +            this.log.debug("No credentials for preemptive authentication");
   1.127 +        }
   1.128 +    }
   1.129 +
   1.130 +}

mercurial