1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }