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.auth;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.ObjectInputStream;
33 import java.io.ObjectOutputStream;
34 import java.io.Serializable;
35 import java.util.Map;
36 import java.util.concurrent.ConcurrentHashMap;
37
38 import org.apache.hc.client5.http.SchemePortResolver;
39 import org.apache.hc.client5.http.auth.AuthCache;
40 import org.apache.hc.client5.http.auth.AuthScheme;
41 import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
42 import org.apache.hc.client5.http.routing.RoutingSupport;
43 import org.apache.hc.core5.annotation.Contract;
44 import org.apache.hc.core5.annotation.ThreadingBehavior;
45 import org.apache.hc.core5.http.HttpHost;
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
60 @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
61 public class BasicAuthCache implements AuthCache {
62
63 private static final Logger LOG = LoggerFactory.getLogger(BasicAuthCache.class);
64
65 private final Map<HttpHost, byte[]> map;
66 private final SchemePortResolver schemePortResolver;
67
68
69
70
71
72
73 public BasicAuthCache(final SchemePortResolver schemePortResolver) {
74 super();
75 this.map = new ConcurrentHashMap<>();
76 this.schemePortResolver = schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE;
77 }
78
79 public BasicAuthCache() {
80 this(null);
81 }
82
83 @Override
84 public void put(final HttpHost host, final AuthScheme authScheme) {
85 Args.notNull(host, "HTTP host");
86 if (authScheme == null) {
87 return;
88 }
89 if (authScheme instanceof Serializable) {
90 try {
91 final ByteArrayOutputStream buf = new ByteArrayOutputStream();
92 try (final ObjectOutputStream out = new ObjectOutputStream(buf)) {
93 out.writeObject(authScheme);
94 }
95 final HttpHost key = RoutingSupport.normalize(host, schemePortResolver);
96 this.map.put(key, buf.toByteArray());
97 } catch (final IOException ex) {
98 if (LOG.isWarnEnabled()) {
99 LOG.warn("Unexpected I/O error while serializing auth scheme", ex);
100 }
101 }
102 } else {
103 if (LOG.isDebugEnabled()) {
104 LOG.debug("Auth scheme {} is not serializable", authScheme.getClass());
105 }
106 }
107 }
108
109 @Override
110 public AuthScheme get(final HttpHost host) {
111 Args.notNull(host, "HTTP host");
112 final HttpHost key = RoutingSupport.normalize(host, schemePortResolver);
113 final byte[] bytes = this.map.get(key);
114 if (bytes != null) {
115 try {
116 final ByteArrayInputStream buf = new ByteArrayInputStream(bytes);
117 try (final ObjectInputStream in = new ObjectInputStream(buf)) {
118 return (AuthScheme) in.readObject();
119 }
120 } catch (final IOException ex) {
121 if (LOG.isWarnEnabled()) {
122 LOG.warn("Unexpected I/O error while de-serializing auth scheme", ex);
123 }
124 } catch (final ClassNotFoundException ex) {
125 if (LOG.isWarnEnabled()) {
126 LOG.warn("Unexpected error while de-serializing auth scheme", ex);
127 }
128 }
129 }
130 return null;
131 }
132
133 @Override
134 public void remove(final HttpHost host) {
135 Args.notNull(host, "HTTP host");
136 final HttpHost key = RoutingSupport.normalize(host, schemePortResolver);
137 this.map.remove(key);
138 }
139
140 @Override
141 public void clear() {
142 this.map.clear();
143 }
144
145 @Override
146 public String toString() {
147 return this.map.toString();
148 }
149
150 }