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