1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rest.client;
22
23 import java.io.IOException;
24
25 import org.apache.commons.httpclient.Header;
26 import org.apache.commons.httpclient.HttpClient;
27 import org.apache.commons.httpclient.HttpMethod;
28 import org.apache.commons.httpclient.HttpVersion;
29 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
30 import org.apache.commons.httpclient.URI;
31 import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
32 import org.apache.commons.httpclient.methods.DeleteMethod;
33 import org.apache.commons.httpclient.methods.GetMethod;
34 import org.apache.commons.httpclient.methods.HeadMethod;
35 import org.apache.commons.httpclient.methods.PostMethod;
36 import org.apache.commons.httpclient.methods.PutMethod;
37 import org.apache.commons.httpclient.params.HttpClientParams;
38 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42
43
44
45
46 public class Client {
47 public static final Header[] EMPTY_HEADER_ARRAY = new Header[0];
48
49 private static final Log LOG = LogFactory.getLog(Client.class);
50
51 private HttpClient httpClient;
52 private Cluster cluster;
53
54
55
56
57 public Client() {
58 this(null);
59 }
60
61
62
63
64
65 public Client(Cluster cluster) {
66 this.cluster = cluster;
67 MultiThreadedHttpConnectionManager manager =
68 new MultiThreadedHttpConnectionManager();
69 HttpConnectionManagerParams managerParams = manager.getParams();
70 managerParams.setConnectionTimeout(2000);
71 managerParams.setDefaultMaxConnectionsPerHost(10);
72 managerParams.setMaxTotalConnections(100);
73 this.httpClient = new HttpClient(manager);
74 HttpClientParams clientParams = httpClient.getParams();
75 clientParams.setVersion(HttpVersion.HTTP_1_1);
76 }
77
78
79
80
81 public void shutdown() {
82 MultiThreadedHttpConnectionManager manager =
83 (MultiThreadedHttpConnectionManager) httpClient.getHttpConnectionManager();
84 manager.shutdown();
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public int executePathOnly(Cluster cluster, HttpMethod method,
101 Header[] headers, String path) throws IOException {
102 IOException lastException;
103 if (cluster.nodes.size() < 1) {
104 throw new IOException("Cluster is empty");
105 }
106 int start = (int)Math.round((cluster.nodes.size() - 1) * Math.random());
107 int i = start;
108 do {
109 cluster.lastHost = cluster.nodes.get(i);
110 try {
111 StringBuilder sb = new StringBuilder();
112 sb.append("http://");
113 sb.append(cluster.lastHost);
114 sb.append(path);
115 URI uri = new URI(sb.toString(), true);
116 return executeURI(method, headers, uri.toString());
117 } catch (IOException e) {
118 lastException = e;
119 }
120 } while (++i != start && i < cluster.nodes.size());
121 throw lastException;
122 }
123
124
125
126
127
128
129
130
131
132 public int executeURI(HttpMethod method, Header[] headers, String uri)
133 throws IOException {
134 method.setURI(new URI(uri, true));
135 if (headers != null) {
136 for (Header header: headers) {
137 method.addRequestHeader(header);
138 }
139 }
140 long startTime = System.currentTimeMillis();
141 int code = httpClient.executeMethod(method);
142 long endTime = System.currentTimeMillis();
143 if (LOG.isDebugEnabled()) {
144 LOG.debug(method.getName() + " " + uri + " " + code + " " +
145 method.getStatusText() + " in " + (endTime - startTime) + " ms");
146 }
147 return code;
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161 public int execute(Cluster cluster, HttpMethod method, Header[] headers,
162 String path) throws IOException {
163 if (path.startsWith("/")) {
164 return executePathOnly(cluster, method, headers, path);
165 }
166 return executeURI(method, headers, path);
167 }
168
169
170
171
172 public Cluster getCluster() {
173 return cluster;
174 }
175
176
177
178
179 public void setCluster(Cluster cluster) {
180 this.cluster = cluster;
181 }
182
183
184
185
186
187
188
189 public Response head(String path) throws IOException {
190 return head(cluster, path, null);
191 }
192
193
194
195
196
197
198
199
200
201 public Response head(Cluster cluster, String path, Header[] headers)
202 throws IOException {
203 HeadMethod method = new HeadMethod();
204 try {
205 int code = execute(cluster, method, null, path);
206 headers = method.getResponseHeaders();
207 return new Response(code, headers, null);
208 } finally {
209 method.releaseConnection();
210 }
211 }
212
213
214
215
216
217
218
219 public Response get(String path) throws IOException {
220 return get(cluster, path);
221 }
222
223
224
225
226
227
228
229
230 public Response get(Cluster cluster, String path) throws IOException {
231 return get(cluster, path, EMPTY_HEADER_ARRAY);
232 }
233
234
235
236
237
238
239
240
241 public Response get(String path, String accept) throws IOException {
242 return get(cluster, path, accept);
243 }
244
245
246
247
248
249
250
251
252
253 public Response get(Cluster cluster, String path, String accept)
254 throws IOException {
255 Header[] headers = new Header[1];
256 headers[0] = new Header("Accept", accept);
257 return get(cluster, path, headers);
258 }
259
260
261
262
263
264
265
266
267
268 public Response get(String path, Header[] headers) throws IOException {
269 return get(cluster, path, headers);
270 }
271
272
273
274
275
276
277
278
279
280 public Response get(Cluster c, String path, Header[] headers)
281 throws IOException {
282 GetMethod method = new GetMethod();
283 try {
284 int code = execute(c, method, headers, path);
285 headers = method.getResponseHeaders();
286 byte[] body = method.getResponseBody();
287 return new Response(code, headers, body);
288 } finally {
289 method.releaseConnection();
290 }
291 }
292
293
294
295
296
297
298
299
300
301 public Response put(String path, String contentType, byte[] content)
302 throws IOException {
303 return put(cluster, path, contentType, content);
304 }
305
306
307
308
309
310
311
312
313
314
315 public Response put(Cluster cluster, String path, String contentType,
316 byte[] content) throws IOException {
317 Header[] headers = new Header[1];
318 headers[0] = new Header("Content-Type", contentType);
319 return put(cluster, path, headers, content);
320 }
321
322
323
324
325
326
327
328
329
330
331 public Response put(String path, Header[] headers, byte[] content)
332 throws IOException {
333 return put(cluster, path, headers, content);
334 }
335
336
337
338
339
340
341
342
343
344
345
346 public Response put(Cluster cluster, String path, Header[] headers,
347 byte[] content) throws IOException {
348 PutMethod method = new PutMethod();
349 try {
350 method.setRequestEntity(new ByteArrayRequestEntity(content));
351 int code = execute(cluster, method, headers, path);
352 headers = method.getResponseHeaders();
353 content = method.getResponseBody();
354 return new Response(code, headers, content);
355 } finally {
356 method.releaseConnection();
357 }
358 }
359
360
361
362
363
364
365
366
367
368 public Response post(String path, String contentType, byte[] content)
369 throws IOException {
370 return post(cluster, path, contentType, content);
371 }
372
373
374
375
376
377
378
379
380
381
382 public Response post(Cluster cluster, String path, String contentType,
383 byte[] content) throws IOException {
384 Header[] headers = new Header[1];
385 headers[0] = new Header("Content-Type", contentType);
386 return post(cluster, path, headers, content);
387 }
388
389
390
391
392
393
394
395
396
397
398 public Response post(String path, Header[] headers, byte[] content)
399 throws IOException {
400 return post(cluster, path, headers, content);
401 }
402
403
404
405
406
407
408
409
410
411
412
413 public Response post(Cluster cluster, String path, Header[] headers,
414 byte[] content) throws IOException {
415 PostMethod method = new PostMethod();
416 try {
417 method.setRequestEntity(new ByteArrayRequestEntity(content));
418 int code = execute(cluster, method, headers, path);
419 headers = method.getResponseHeaders();
420 content = method.getResponseBody();
421 return new Response(code, headers, content);
422 } finally {
423 method.releaseConnection();
424 }
425 }
426
427
428
429
430
431
432
433 public Response delete(String path) throws IOException {
434 return delete(cluster, path);
435 }
436
437
438
439
440
441
442
443
444 public Response delete(Cluster cluster, String path) throws IOException {
445 DeleteMethod method = new DeleteMethod();
446 try {
447 int code = execute(cluster, method, null, path);
448 Header[] headers = method.getResponseHeaders();
449 byte[] content = method.getResponseBody();
450 return new Response(code, headers, content);
451 } finally {
452 method.releaseConnection();
453 }
454 }
455
456 }