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