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 package org.apache.hc.client5.http.impl;
28
29 import java.security.Principal;
30
31 import javax.net.ssl.SSLSession;
32
33 import org.apache.hc.client5.http.HttpRoute;
34 import org.apache.hc.client5.http.UserTokenHandler;
35 import org.apache.hc.client5.http.auth.AuthExchange;
36 import org.apache.hc.client5.http.auth.AuthScheme;
37 import org.apache.hc.client5.http.protocol.HttpClientContext;
38 import org.apache.hc.core5.annotation.Contract;
39 import org.apache.hc.core5.annotation.ThreadingBehavior;
40 import org.apache.hc.core5.http.protocol.HttpContext;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 @Contract(threading = ThreadingBehavior.STATELESS)
58 public class DefaultUserTokenHandler implements UserTokenHandler {
59
60 public static final DefaultUserTokenHandlererTokenHandler.html#DefaultUserTokenHandler">DefaultUserTokenHandler INSTANCE = new DefaultUserTokenHandler();
61
62 @Override
63 public Object getUserToken(final HttpRoute route, final HttpContext context) {
64
65 final HttpClientContext clientContext = HttpClientContext.adapt(context);
66
67 Principal userPrincipal = null;
68
69 final AuthExchange targetAuthExchnage = clientContext.getAuthExchange(route.getTargetHost());
70 if (targetAuthExchnage != null) {
71 userPrincipal = getAuthPrincipal(targetAuthExchnage);
72 if (userPrincipal == null && route.getProxyHost() != null) {
73 final AuthExchange proxyAuthExchange = clientContext.getAuthExchange(route.getProxyHost());
74 userPrincipal = getAuthPrincipal(proxyAuthExchange);
75 }
76 }
77
78 if (userPrincipal == null) {
79 final SSLSession sslSession = clientContext.getSSLSession();
80 if (sslSession != null) {
81 userPrincipal = sslSession.getLocalPrincipal();
82 }
83 }
84
85 return userPrincipal;
86 }
87
88 private static Principal getAuthPrincipal(final AuthExchange authExchange) {
89 final AuthScheme scheme = authExchange.getAuthScheme();
90 if (scheme != null && scheme.isConnectionBased()) {
91 return scheme.getPrincipal();
92 }
93 return null;
94 }
95
96 }