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