1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.AuthenticationStrategy;
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
50
51
52
53
54
55
56
57
58
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 public void process(final HttpResponse response, final HttpContext context)
71 throws HttpException, IOException {
72 if (response == null) {
73 throw new IllegalArgumentException("HTTP request may not be null");
74 }
75 if (context == null) {
76 throw new IllegalArgumentException("HTTP context may not be null");
77 }
78 AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
79
80 HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
81 AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
82 if (target != null && targetState != null) {
83 if (this.log.isDebugEnabled()) {
84 this.log.debug("Target auth state: " + targetState.getState());
85 }
86 if (isCachable(targetState)) {
87 SchemeRegistry schemeRegistry = (SchemeRegistry) context.getAttribute(
88 ClientContext.SCHEME_REGISTRY);
89 if (target.getPort() < 0) {
90 Scheme scheme = schemeRegistry.getScheme(target);
91 target = new HttpHost(target.getHostName(),
92 scheme.resolvePort(target.getPort()), target.getSchemeName());
93 }
94 if (authCache == null) {
95 authCache = new BasicAuthCache();
96 context.setAttribute(ClientContext.AUTH_CACHE, authCache);
97 }
98 switch (targetState.getState()) {
99 case CHALLENGED:
100 cache(authCache, target, targetState.getAuthScheme());
101 break;
102 case FAILURE:
103 uncache(authCache, target, targetState.getAuthScheme());
104 }
105 }
106 }
107
108 HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
109 AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
110 if (proxy != null && proxyState != null) {
111 if (this.log.isDebugEnabled()) {
112 this.log.debug("Proxy auth state: " + proxyState.getState());
113 }
114 if (isCachable(proxyState)) {
115 if (authCache == null) {
116 authCache = new BasicAuthCache();
117 context.setAttribute(ClientContext.AUTH_CACHE, authCache);
118 }
119 switch (proxyState.getState()) {
120 case CHALLENGED:
121 cache(authCache, proxy, proxyState.getAuthScheme());
122 break;
123 case FAILURE:
124 uncache(authCache, proxy, proxyState.getAuthScheme());
125 }
126 }
127 }
128 }
129
130 private boolean isCachable(final AuthState authState) {
131 AuthScheme authScheme = authState.getAuthScheme();
132 if (authScheme == null || !authScheme.isComplete()) {
133 return false;
134 }
135 String schemeName = authScheme.getSchemeName();
136 return schemeName.equalsIgnoreCase(AuthPolicy.BASIC) ||
137 schemeName.equalsIgnoreCase(AuthPolicy.DIGEST);
138 }
139
140 private void cache(final AuthCache authCache, final HttpHost host, final AuthScheme authScheme) {
141 if (this.log.isDebugEnabled()) {
142 this.log.debug("Caching '" + authScheme.getSchemeName() +
143 "' auth scheme for " + host);
144 }
145 authCache.put(host, authScheme);
146 }
147
148 private void uncache(final AuthCache authCache, final HttpHost host, final AuthScheme authScheme) {
149 if (this.log.isDebugEnabled()) {
150 this.log.debug("Removing from cache '" + authScheme.getSchemeName() +
151 "' auth scheme for " + host);
152 }
153 authCache.remove(host);
154 }
155 }