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.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
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()) {
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 }