View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.client.protocol;
29  
30  import java.io.IOException;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.http.HttpException;
35  import org.apache.http.HttpHost;
36  import org.apache.http.HttpResponse;
37  import org.apache.http.HttpResponseInterceptor;
38  import org.apache.http.annotation.Contract;
39  import org.apache.http.annotation.ThreadingBehavior;
40  import org.apache.http.auth.AuthScheme;
41  import org.apache.http.auth.AuthState;
42  import org.apache.http.client.AuthCache;
43  import org.apache.http.client.params.AuthPolicy;
44  import org.apache.http.conn.scheme.Scheme;
45  import org.apache.http.conn.scheme.SchemeRegistry;
46  import org.apache.http.impl.client.BasicAuthCache;
47  import org.apache.http.protocol.ExecutionContext;
48  import org.apache.http.protocol.HttpContext;
49  import org.apache.http.util.Args;
50  
51  /**
52   * Response interceptor that adds successfully completed {@link AuthScheme}s
53   * to the local {@link AuthCache} instance. Cached {@link AuthScheme}s can be
54   * re-used when executing requests against known hosts, thus avoiding
55   * additional authentication round-trips.
56   *
57   * @since 4.1
58   *
59   * @deprecated (4.2)  use {@link org.apache.http.client.AuthenticationStrategy}
60   */
61  @Contract(threading = ThreadingBehavior.IMMUTABLE)
62  @Deprecated
63  public class ResponseAuthCache implements HttpResponseInterceptor {
64  
65      private final Log log = LogFactory.getLog(getClass());
66  
67      public ResponseAuthCache() {
68          super();
69      }
70  
71      @Override
72      public void process(final HttpResponse response, final HttpContext context)
73              throws HttpException, IOException {
74          Args.notNull(response, "HTTP request");
75          Args.notNull(context, "HTTP context");
76          AuthCache../../../org/apache/http/client/AuthCache.html#AuthCache">AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
77  
78          HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
79          final AuthState/../../org/apache/http/auth/AuthState.html#AuthState">AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
80          if (target != null && targetState != null) {
81              if (this.log.isDebugEnabled()) {
82                  this.log.debug("Target auth state: " + targetState.getState());
83              }
84              if (isCachable(targetState)) {
85                  final SchemeRegistryrg/apache/http/conn/scheme/SchemeRegistry.html#SchemeRegistry">SchemeRegistry schemeRegistry = (SchemeRegistry) context.getAttribute(
86                          ClientContext.SCHEME_REGISTRY);
87                  if (target.getPort() < 0) {
88                      final Scheme scheme = schemeRegistry.getScheme(target);
89                      target = new HttpHost(target.getHostName(),
90                              scheme.resolvePort(target.getPort()), target.getSchemeName());
91                  }
92                  if (authCache == null) {
93                      authCache = new BasicAuthCache();
94                      context.setAttribute(ClientContext.AUTH_CACHE, authCache);
95                  }
96                  switch (targetState.getState()) {  // TODO add SUCCESS, UNCHALLENGED and HANDSHAKE cases
97                  case CHALLENGED:
98                      cache(authCache, target, targetState.getAuthScheme());
99                      break;
100                 case FAILURE:
101                     uncache(authCache, target, targetState.getAuthScheme());
102                 }
103             }
104         }
105 
106         final HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
107         final AuthState./../../org/apache/http/auth/AuthState.html#AuthState">AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
108         if (proxy != null && proxyState != null) {
109             if (this.log.isDebugEnabled()) {
110                 this.log.debug("Proxy auth state: " + proxyState.getState());
111             }
112             if (isCachable(proxyState)) {
113                 if (authCache == null) {
114                     authCache = new BasicAuthCache();
115                     context.setAttribute(ClientContext.AUTH_CACHE, authCache);
116                 }
117                 switch (proxyState.getState()) {  // TODO add SUCCESS, UNCHALLENGED and HANDSHAKE cases
118                 case CHALLENGED:
119                     cache(authCache, proxy, proxyState.getAuthScheme());
120                     break;
121                 case FAILURE:
122                     uncache(authCache, proxy, proxyState.getAuthScheme());
123                 }
124             }
125         }
126     }
127 
128     private boolean isCachable(final AuthState authState) {
129         final AuthScheme authScheme = authState.getAuthScheme();
130         if (authScheme == null || !authScheme.isComplete()) {
131             return false;
132         }
133         final String schemeName = authScheme.getSchemeName();
134         return schemeName.equalsIgnoreCase(AuthPolicy.BASIC) ||
135                 schemeName.equalsIgnoreCase(AuthPolicy.DIGEST);
136     }
137 
138     private void cache(final AuthCache authCache, final HttpHost host, final AuthScheme authScheme) {
139         if (this.log.isDebugEnabled()) {
140             this.log.debug("Caching '" + authScheme.getSchemeName() +
141                     "' auth scheme for " + host);
142         }
143         authCache.put(host, authScheme);
144     }
145 
146     private void uncache(final AuthCache authCache, final HttpHost host, final AuthScheme authScheme) {
147         if (this.log.isDebugEnabled()) {
148             this.log.debug("Removing from cache '" + authScheme.getSchemeName() +
149                     "' auth scheme for " + host);
150         }
151         authCache.remove(host);
152     }
153 }