/
ConfigurationProviderHTTP.java
296 lines (272 loc) · 11.7 KB
/
ConfigurationProviderHTTP.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package net.spy.memcached.vbucket;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.net.URI;
import java.net.InetSocketAddress;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.ParseException;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.compat.SpyObject;
import net.spy.memcached.vbucket.config.Bucket;
import net.spy.memcached.vbucket.config.Config;
import net.spy.memcached.vbucket.config.Pool;
import net.spy.memcached.vbucket.config.ConfigurationParserJSON;
import net.spy.memcached.vbucket.config.ConfigurationParser;
public class ConfigurationProviderHTTP extends SpyObject implements ConfigurationProvider {
/**
* Configuration management class that provides methods for retrieving vbucket configuration and receiving
* configuration updates.
*/
private static final String DEFAULT_POOL_NAME = "default";
private static final String ANONYMOUS_AUTH_BUCKET = "default";
/**
* The specification version which this client meets. This will be included
* in requests to the server.
*/
public static final String CLIENT_SPEC_VER = "1.0";
private List<URI> baseList;
private String restUsr;
private String restPwd;
private URI loadedBaseUri;
// map of <bucketname, bucket> currently loaded
private Map<String, Bucket> buckets = new ConcurrentHashMap<String, Bucket>();
// map of <poolname, pool> currently loaded
//private Map<String, Pool> pools = new ConcurrentHashMap<String, Pool>();
private ConfigurationParser configurationParser = new ConfigurationParserJSON();
private Map<String, BucketMonitor> monitors = new HashMap<String, BucketMonitor>();
/**
* Constructs a configuration provider with disabled authentication for the REST service
* @param baseList list of urls to treat as base
* @throws IOException
*/
public ConfigurationProviderHTTP(List<URI> baseList) throws IOException {
this(baseList, null, null);
}
/**
* Constructs a configuration provider with a given credentials for the REST service
* @param baseList list of urls to treat as base
* @param restUsr username
* @param restPwd password
* @throws IOException
*/
public ConfigurationProviderHTTP(List<URI> baseList, String restUsr, String restPwd) throws IOException {
this.baseList = baseList;
this.restUsr = restUsr;
this.restPwd = restPwd;
}
/**
* Connects to the REST service and retrieves the bucket configuration from the first pool available
* @param bucketname bucketname
* @return vbucket configuration
* @throws ConfigurationException
*/
public Bucket getBucketConfiguration(final String bucketname) throws ConfigurationException {
if (bucketname == null || bucketname.isEmpty()) {
throw new IllegalArgumentException("Bucket name can not be blank.");
}
Bucket bucket = this.buckets.get(bucketname);
if (bucket == null) {
readPools(bucketname);
}
return this.buckets.get(bucketname);
}
/**
* For a given bucket to be found, walk the URIs in the baselist until the
* bucket needed is found.
*
* @param bucketToFind
* @throws ConfigurationException
*/
private void readPools(String bucketToFind) throws ConfigurationException {
// the intent with this method is to encapsulate all of the walking of URIs
// and populating an internal object model of the configuration to one place
for (URI baseUri : baseList) {
try {
// get and parse the response from the current base uri
URLConnection baseConnection = urlConnBuilder(null, baseUri);
String base = readToString(baseConnection);
if ("".equals(base)) {
getLogger().warn("Provided URI " + baseUri + " has an empty response... skipping");
continue;
}
Map<String, Pool> pools = this.configurationParser.parseBase(base);
// check for the default pool name
if (!pools.containsKey(DEFAULT_POOL_NAME)) {
getLogger().warn("Provided URI " + baseUri + " has no default pool... skipping");
continue;
}
// load pools
for (Pool pool : pools.values()) {
URLConnection poolConnection = urlConnBuilder(baseUri, pool.getUri());
String poolString = readToString(poolConnection);
configurationParser.loadPool(pool, poolString);
URLConnection poolBucketsConnection = urlConnBuilder(baseUri, pool.getBucketsUri());
String sBuckets = readToString(poolBucketsConnection);
Map<String, Bucket> bucketsForPool = configurationParser.parseBuckets(sBuckets);
pool.replaceBuckets(bucketsForPool);
}
// did we found our bucket?
boolean bucketFound = false;
for (Pool pool : pools.values()) {
if (pool.hasBucket(bucketToFind)) {
bucketFound = true;
break;
}
}
if (bucketFound) {
for (Pool pool : pools.values()) {
for (Map.Entry<String, Bucket> bucketEntry : pool.getROBuckets().entrySet()) {
this.buckets.put(bucketEntry.getKey(), bucketEntry.getValue());
}
}
this.loadedBaseUri = baseUri;
return;
}
} catch (ParseException e) {
getLogger().warn("Provided URI " + baseUri + " has an unparsable response...skipping", e);
} catch (IOException e) {
getLogger().warn("Connection problems with URI " + baseUri + " ...skipping", e);
}
throw new ConfigurationException("Configuration for bucket " + bucketToFind + " was not found.");
}
}
public List<InetSocketAddress> getServerList(final String bucketname) throws ConfigurationException {
Bucket bucket = getBucketConfiguration(bucketname);
List<String> servers = bucket.getConfig().getServers();
StringBuilder serversString = new StringBuilder();
for (String server : servers) {
serversString.append(server).append(' ');
}
return AddrUtil.getAddresses(serversString.toString());
}
/**
* Subscribes for configuration updates
* @param bucketName bucket name to receive configuration for
* @param rec reconfigurable that will receive updates
* @throws ConfigurationException
*/
public void subscribe(String bucketName, Reconfigurable rec) throws ConfigurationException {
Bucket bucket = getBucketConfiguration(bucketName);
ReconfigurableObserver obs = new ReconfigurableObserver(rec);
BucketMonitor monitor = this.monitors.get(bucketName);
if (monitor == null) {
URI streamingURI = bucket.getStreamingURI();
monitor = new BucketMonitor(this.loadedBaseUri.resolve(streamingURI), bucketName, this.restUsr, this.restPwd, configurationParser);
this.monitors.put(bucketName, monitor);
monitor.addObserver(obs);
monitor.startMonitor();
} else {
monitor.addObserver(obs);
}
}
/**
* Unsubscribe from updates on a given bucket and given reconfigurable
* @param vbucketName bucket name
* @param rec reconfigurable
*/
public void unsubscribe(String vbucketName, Reconfigurable rec) {
BucketMonitor monitor = this.monitors.get(vbucketName);
if (monitor != null) {
monitor.deleteObserver(new ReconfigurableObserver(rec));
}
}
public Config getLatestConfig(String bucketname) throws ConfigurationException {
Bucket bucket = getBucketConfiguration(bucketname);
return bucket.getConfig();
}
public String getAnonymousAuthBucket() {
return ANONYMOUS_AUTH_BUCKET;
}
/**
* Shutdowns a monitor connections to the REST service
*/
public void shutdown() {
for (BucketMonitor monitor : this.monitors.values()) {
monitor.shutdown();
}
}
/**
* Create a URL which has the appropriate headers to interact with the
* service. Most exception handling is up to the caller.
*
* @param resource the URI either absolute or relative to the base for this ClientManager
* @return
* @throws java.io.IOException
*/
private URLConnection urlConnBuilder(URI base, URI resource) throws IOException {
if (!resource.isAbsolute() && base != null) {
resource = base.resolve(resource);
}
URL specURL = resource.toURL();
URLConnection connection = specURL.openConnection();
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("user-agent", "spymemcached vbucket client");
connection.setRequestProperty("X-memcachekv-Store-Client-Specification-Version", CLIENT_SPEC_VER);
if (restUsr != null) {
connection.setRequestProperty("Authorization", buildAuthHeader(restUsr, restPwd));
}
return connection;
}
/**
* Helper method that reads content from url connection to the string
* @param connection a given url connection
* @return content string
* @throws IOException
*/
private String readToString(URLConnection connection) throws IOException {
BufferedReader reader = null;
try {
InputStream inStream = connection.getInputStream();
if (connection instanceof java.net.HttpURLConnection) {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
if (httpConnection.getResponseCode() == 403) {
throw new IOException("Service does not accept the authentication credentials: "
+ httpConnection.getResponseCode() + httpConnection.getResponseMessage());
} else if (httpConnection.getResponseCode() >= 400) {
throw new IOException("Service responded with a failure code: "
+ httpConnection.getResponseCode() + httpConnection.getResponseMessage());
}
} else {
throw new IOException("Unexpected URI type encountered");
}
reader = new BufferedReader(new InputStreamReader(inStream));
String str;
StringBuilder buffer = new StringBuilder();
while ((str = reader.readLine()) != null) {
buffer.append(str);
}
return buffer.toString();
} finally {
reader.close();
}
}
/**
* Oddly, lots of things that do HTTP seem to not know how to do this and
* Authenticator caches for the process. Since we only need Basic at the
* moment simply, add the header.
*
* @return a value for an HTTP Basic Auth Header
*/
protected static String buildAuthHeader(String username, String password) {
// apparently netty isn't familiar with HTTP Basic Auth
StringBuilder clearText = new StringBuilder(username);
clearText.append(':');
if (password != null) {
clearText.append(password);
}
// and apache base64 codec has extra \n\l we have to strip off
String encodedText = org.apache.commons.codec.binary.Base64.encodeBase64String(clearText.toString().getBytes());
char[] encodedWoNewline = new char[encodedText.length() - 2];
encodedText.getChars(0, encodedText.length() - 2, encodedWoNewline, 0);
String authVal = "Basic " + new String(encodedWoNewline);
return authVal;
}
}