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.hc.client5.http.protocol;
29
30 import java.io.IOException;
31
32 import org.apache.hc.client5.http.RouteInfo;
33 import org.apache.hc.client5.http.auth.AuthCache;
34 import org.apache.hc.client5.http.auth.AuthExchange;
35 import org.apache.hc.client5.http.auth.AuthScheme;
36 import org.apache.hc.client5.http.auth.CredentialsProvider;
37 import org.apache.hc.client5.http.impl.RequestSupport;
38 import org.apache.hc.core5.annotation.Contract;
39 import org.apache.hc.core5.annotation.ThreadingBehavior;
40 import org.apache.hc.core5.http.EntityDetails;
41 import org.apache.hc.core5.http.HttpException;
42 import org.apache.hc.core5.http.HttpHost;
43 import org.apache.hc.core5.http.HttpRequest;
44 import org.apache.hc.core5.http.HttpRequestInterceptor;
45 import org.apache.hc.core5.http.protocol.HttpContext;
46 import org.apache.hc.core5.util.Args;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50
51
52
53
54
55
56
57
58
59 @Deprecated
60 @Contract(threading = ThreadingBehavior.STATELESS)
61 public class RequestAuthCache implements HttpRequestInterceptor {
62
63 private static final Logger LOG = LoggerFactory.getLogger(RequestAuthCache.class);
64
65 public RequestAuthCache() {
66 super();
67 }
68
69 @Override
70 public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context)
71 throws HttpException, IOException {
72 Args.notNull(request, "HTTP request");
73 Args.notNull(context, "HTTP context");
74
75 final HttpClientContext clientContext = HttpClientContext.cast(context);
76 final String exchangeId = clientContext.getExchangeId();
77
78 final AuthCache authCache = clientContext.getAuthCache();
79 if (authCache == null) {
80 if (LOG.isDebugEnabled()) {
81 LOG.debug("{} Auth cache not set in the context", exchangeId);
82 }
83 return;
84 }
85
86 final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
87 if (credsProvider == null) {
88 if (LOG.isDebugEnabled()) {
89 LOG.debug("{} Credentials provider not set in the context", exchangeId);
90 }
91 return;
92 }
93
94 final RouteInfo route = clientContext.getHttpRoute();
95 if (route == null) {
96 if (LOG.isDebugEnabled()) {
97 LOG.debug("{} Route info not set in the context", exchangeId);
98 }
99 return;
100 }
101
102 final HttpHost target = new HttpHost(request.getScheme(), request.getAuthority());
103 final AuthExchange targetAuthExchange = clientContext.getAuthExchange(target);
104 if (targetAuthExchange.getState() == AuthExchange.State.UNCHALLENGED) {
105 final String pathPrefix = RequestSupport.extractPathPrefix(request);
106 final AuthScheme authScheme = authCache.get(target, pathPrefix);
107 if (authScheme != null) {
108 if (LOG.isDebugEnabled()) {
109 LOG.debug("{} Re-using cached '{}' auth scheme for {}", exchangeId, authScheme.getName(), target);
110 }
111 targetAuthExchange.select(authScheme);
112 }
113 }
114
115 final HttpHost proxy = route.getProxyHost();
116 if (proxy != null) {
117 final AuthExchange proxyAuthExchange = clientContext.getAuthExchange(proxy);
118 if (proxyAuthExchange.getState() == AuthExchange.State.UNCHALLENGED) {
119 final AuthScheme authScheme = authCache.get(proxy, null);
120 if (authScheme != null) {
121 if (LOG.isDebugEnabled()) {
122 LOG.debug("{} Re-using cached '{}' auth scheme for {}", exchangeId, authScheme.getName(), proxy);
123 }
124 proxyAuthExchange.select(authScheme);
125 }
126 }
127 }
128 }
129
130 }