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.client.protocol;
29  
30  import java.util.Queue;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.http.Header;
35  import org.apache.http.HttpRequest;
36  import org.apache.http.HttpRequestInterceptor;
37  import org.apache.http.auth.AuthOption;
38  import org.apache.http.auth.AuthScheme;
39  import org.apache.http.auth.AuthState;
40  import org.apache.http.auth.AuthenticationException;
41  import org.apache.http.auth.ContextAwareAuthScheme;
42  import org.apache.http.auth.Credentials;
43  import org.apache.http.protocol.HttpContext;
44  import org.apache.http.util.Asserts;
45  
46  /**
47   * @deprecated Do not use.
48   */
49  @Deprecated
50  abstract class RequestAuthenticationBase implements HttpRequestInterceptor {
51  
52      final Log log = LogFactory.getLog(getClass());
53  
54      public RequestAuthenticationBase() {
55          super();
56      }
57  
58      void process(
59              final AuthState authState,
60              final HttpRequest request,
61              final HttpContext context) {
62          AuthScheme authScheme = authState.getAuthScheme();
63          Credentials creds = authState.getCredentials();
64          switch (authState.getState()) { // TODO add UNCHALLENGED and HANDSHAKE cases
65          case FAILURE:
66              return;
67          case SUCCESS:
68              ensureAuthScheme(authScheme);
69              if (authScheme.isConnectionBased()) {
70                  return;
71              }
72              break;
73          case CHALLENGED:
74              final Queue<AuthOption> authOptions = authState.getAuthOptions();
75              if (authOptions != null) {
76                  while (!authOptions.isEmpty()) {
77                      final AuthOption authOption = authOptions.remove();
78                      authScheme = authOption.getAuthScheme();
79                      creds = authOption.getCredentials();
80                      authState.update(authScheme, creds);
81                      if (this.log.isDebugEnabled()) {
82                          this.log.debug("Generating response to an authentication challenge using "
83                                  + authScheme.getSchemeName() + " scheme");
84                      }
85                      try {
86                          final Header header = authenticate(authScheme, creds, request, context);
87                          request.addHeader(header);
88                          break;
89                      } catch (final AuthenticationException ex) {
90                          if (this.log.isWarnEnabled()) {
91                              this.log.warn(authScheme + " authentication error: " + ex.getMessage());
92                          }
93                      }
94                  }
95                  return;
96              } else {
97                  ensureAuthScheme(authScheme);
98              }
99          }
100         if (authScheme != null) {
101             try {
102                 final Header header = authenticate(authScheme, creds, request, context);
103                 request.addHeader(header);
104             } catch (final AuthenticationException ex) {
105                 if (this.log.isErrorEnabled()) {
106                     this.log.error(authScheme + " authentication error: " + ex.getMessage());
107                 }
108             }
109         }
110     }
111 
112     private void ensureAuthScheme(final AuthScheme authScheme) {
113         Asserts.notNull(authScheme, "Auth scheme");
114     }
115 
116     private Header authenticate(
117             final AuthScheme authScheme,
118             final Credentials creds,
119             final HttpRequest request,
120             final HttpContext context) throws AuthenticationException {
121         Asserts.notNull(authScheme, "Auth scheme");
122         if (authScheme instanceof ContextAwareAuthScheme) {
123             return ((ContextAwareAuthScheme) authScheme).authenticate(creds, request, context);
124         } else {
125             return authScheme.authenticate(creds, request);
126         }
127     }
128 
129 }