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.impl.client;
29
30 import java.util.LinkedList;
31 import java.util.Locale;
32 import java.util.Map;
33 import java.util.Queue;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.http.Header;
38 import org.apache.http.HttpHost;
39 import org.apache.http.HttpResponse;
40 import org.apache.http.annotation.Immutable;
41 import org.apache.http.auth.AuthOption;
42 import org.apache.http.auth.AuthScheme;
43 import org.apache.http.auth.AuthScope;
44 import org.apache.http.auth.AuthenticationException;
45 import org.apache.http.auth.Credentials;
46 import org.apache.http.auth.MalformedChallengeException;
47 import org.apache.http.client.AuthCache;
48 import org.apache.http.client.AuthenticationHandler;
49 import org.apache.http.client.AuthenticationStrategy;
50 import org.apache.http.client.CredentialsProvider;
51 import org.apache.http.client.params.AuthPolicy;
52 import org.apache.http.client.protocol.ClientContext;
53 import org.apache.http.protocol.HttpContext;
54
55
56
57
58 @Immutable
59 @Deprecated
60 class AuthenticationStrategyAdaptor implements AuthenticationStrategy {
61
62 private final Log log = LogFactory.getLog(getClass());
63
64 private final AuthenticationHandler handler;
65
66 public AuthenticationStrategyAdaptor(final AuthenticationHandler handler) {
67 super();
68 this.handler = handler;
69 }
70
71 public boolean isAuthenticationRequested(
72 final HttpHost authhost,
73 final HttpResponse response,
74 final HttpContext context) {
75 return this.handler.isAuthenticationRequested(response, context);
76 }
77
78 public Map<String, Header> getChallenges(
79 final HttpHost authhost,
80 final HttpResponse response,
81 final HttpContext context) throws MalformedChallengeException {
82 return this.handler.getChallenges(response, context);
83 }
84
85 public Queue<AuthOption> select(
86 final Map<String, Header> challenges,
87 final HttpHost authhost,
88 final HttpResponse response,
89 final HttpContext context) throws MalformedChallengeException {
90 if (challenges == null) {
91 throw new IllegalArgumentException("Map of auth challenges may not be null");
92 }
93 if (authhost == null) {
94 throw new IllegalArgumentException("Host may not be null");
95 }
96 if (response == null) {
97 throw new IllegalArgumentException("HTTP response may not be null");
98 }
99 if (context == null) {
100 throw new IllegalArgumentException("HTTP context may not be null");
101 }
102
103 Queue<AuthOption> options = new LinkedList<AuthOption>();
104 CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
105 ClientContext.CREDS_PROVIDER);
106 if (credsProvider == null) {
107 this.log.debug("Credentials provider not set in the context");
108 return options;
109 }
110
111 AuthScheme authScheme;
112 try {
113 authScheme = this.handler.selectScheme(challenges, response, context);
114 } catch (AuthenticationException ex) {
115 if (this.log.isWarnEnabled()) {
116 this.log.warn(ex.getMessage(), ex);
117 }
118 return options;
119 }
120 String id = authScheme.getSchemeName();
121 Header challenge = challenges.get(id.toLowerCase(Locale.US));
122 authScheme.processChallenge(challenge);
123
124 AuthScope authScope = new AuthScope(
125 authhost.getHostName(),
126 authhost.getPort(),
127 authScheme.getRealm(),
128 authScheme.getSchemeName());
129
130 Credentials credentials = credsProvider.getCredentials(authScope);
131 if (credentials != null) {
132 options.add(new AuthOption(authScheme, credentials));
133 }
134 return options;
135 }
136
137 public void authSucceeded(
138 final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
139 AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
140 if (isCachable(authScheme)) {
141 if (authCache == null) {
142 authCache = new BasicAuthCache();
143 context.setAttribute(ClientContext.AUTH_CACHE, authCache);
144 }
145 if (this.log.isDebugEnabled()) {
146 this.log.debug("Caching '" + authScheme.getSchemeName() +
147 "' auth scheme for " + authhost);
148 }
149 authCache.put(authhost, authScheme);
150 }
151 }
152
153 public void authFailed(
154 final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
155 AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
156 if (authCache == null) {
157 return;
158 }
159 if (this.log.isDebugEnabled()) {
160 this.log.debug("Removing from cache '" + authScheme.getSchemeName() +
161 "' auth scheme for " + authhost);
162 }
163 authCache.remove(authhost);
164 }
165
166 private boolean isCachable(final AuthScheme authScheme) {
167 if (authScheme == null || !authScheme.isComplete()) {
168 return false;
169 }
170 String schemeName = authScheme.getSchemeName();
171 return schemeName.equalsIgnoreCase(AuthPolicy.BASIC) ||
172 schemeName.equalsIgnoreCase(AuthPolicy.DIGEST);
173 }
174
175 public AuthenticationHandler getHandler() {
176 return this.handler;
177 }
178
179 }