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