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.Immutable;
39  import org.apache.http.auth.AuthScheme;
40  import org.apache.http.auth.AuthState;
41  import org.apache.http.client.AuthCache;
42  import org.apache.http.client.params.AuthPolicy;
43  import org.apache.http.conn.scheme.Scheme;
44  import org.apache.http.conn.scheme.SchemeRegistry;
45  import org.apache.http.impl.client.BasicAuthCache;
46  import org.apache.http.protocol.ExecutionContext;
47  import org.apache.http.protocol.HttpContext;
48  import org.apache.http.util.Args;
49  
50  /**
51   * Response interceptor that adds successfully completed {@link AuthScheme}s
52   * to the local {@link AuthCache} instance. Cached {@link AuthScheme}s can be
53   * re-used when executing requests against known hosts, thus avoiding
54   * additional authentication round-trips.
55   *
56   * @since 4.1
57   *
58   * @deprecated (4.2)  use {@link org.apache.http.client.AuthenticationStrategy}
59   */
60  @Immutable
61  @Deprecated
62  public class ResponseAuthCache implements HttpResponseInterceptor {
63  
64      private final Log log = LogFactory.getLog(getClass());
65  
66      public ResponseAuthCache() {
67          super();
68      }
69  
70      @Override
71      public void process(final HttpResponse response, final HttpContext context)
72              throws HttpException, IOException {
73          Args.notNull(response, "HTTP request");
74          Args.notNull(context, "HTTP context");
75          AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
76  
77          HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
78          final AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
79          if (target != null && targetState != null) {
80              if (this.log.isDebugEnabled()) {
81                  this.log.debug("Target auth state: " + targetState.getState());
82              }
83              if (isCachable(targetState)) {
84                  final SchemeRegistry schemeRegistry = (SchemeRegistry) context.getAttribute(
85                          ClientContext.SCHEME_REGISTRY);
86                  if (target.getPort() < 0) {
87                      final Scheme scheme = schemeRegistry.getScheme(target);
88                      target = new HttpHost(target.getHostName(),
89                              scheme.resolvePort(target.getPort()), target.getSchemeName());
90                  }
91                  if (authCache == null) {
92                      authCache = new BasicAuthCache();
93                      context.setAttribute(ClientContext.AUTH_CACHE, authCache);
94                  }
95                  switch (targetState.getState()) {  // TODO add SUCCESS, UNCHALLENGED and HANDSHAKE cases
96                  case CHALLENGED:
97                      cache(authCache, target, targetState.getAuthScheme());
98                      break;
99                  case FAILURE:
100                     uncache(authCache, target, targetState.getAuthScheme());
101                 }
102             }
103         }
104 
105         final HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
106         final AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
107         if (proxy != null && proxyState != null) {
108             if (this.log.isDebugEnabled()) {
109                 this.log.debug("Proxy auth state: " + proxyState.getState());
110             }
111             if (isCachable(proxyState)) {
112                 if (authCache == null) {
113                     authCache = new BasicAuthCache();
114                     context.setAttribute(ClientContext.AUTH_CACHE, authCache);
115                 }
116                 switch (proxyState.getState()) {  // TODO add SUCCESS, UNCHALLENGED and HANDSHAKE cases
117                 case CHALLENGED:
118                     cache(authCache, proxy, proxyState.getAuthScheme());
119                     break;
120                 case FAILURE:
121                     uncache(authCache, proxy, proxyState.getAuthScheme());
122                 }
123             }
124         }
125     }
126 
127     private boolean isCachable(final AuthState authState) {
128         final AuthScheme authScheme = authState.getAuthScheme();
129         if (authScheme == null || !authScheme.isComplete()) {
130             return false;
131         }
132         final String schemeName = authScheme.getSchemeName();
133         return schemeName.equalsIgnoreCase(AuthPolicy.BASIC) ||
134                 schemeName.equalsIgnoreCase(AuthPolicy.DIGEST);
135     }
136 
137     private void cache(final AuthCache authCache, final HttpHost host, final AuthScheme authScheme) {
138         if (this.log.isDebugEnabled()) {
139             this.log.debug("Caching '" + authScheme.getSchemeName() +
140                     "' auth scheme for " + host);
141         }
142         authCache.put(host, authScheme);
143     }
144 
145     private void uncache(final AuthCache authCache, final HttpHost host, final AuthScheme authScheme) {
146         if (this.log.isDebugEnabled()) {
147             this.log.debug("Removing from cache '" + authScheme.getSchemeName() +
148                     "' auth scheme for " + host);
149         }
150         authCache.remove(host);
151     }
152 }