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