From 5671b5ca862d1b773f8e5a2fac4ef4cad6438a2e Mon Sep 17 00:00:00 2001 From: jxiang Date: Wed, 8 Aug 2012 02:32:24 +0000 Subject: [PATCH] HBASE-6444 Expose the ability to set custom HTTP Request Headers for the REST client used by RemoteHTable git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1370635 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop/hbase/rest/client/Client.java | 47 ++++++++++++++++++- .../hbase/rest/client/RemoteHTable.java | 2 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/Client.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/Client.java index 72f63b2ce36..7991366d7dd 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/Client.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/Client.java @@ -21,6 +21,9 @@ package org.apache.hadoop.hbase.rest.client; import java.io.IOException; +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; @@ -55,6 +58,8 @@ public class Client { private HttpClient httpClient; private Cluster cluster; + private Map extraHeaders; + /** * Default Constructor */ @@ -74,6 +79,7 @@ public class Client { managerParams.setConnectionTimeout(2000); // 2 s managerParams.setDefaultMaxConnectionsPerHost(10); managerParams.setMaxTotalConnections(100); + extraHeaders = new ConcurrentHashMap(); this.httpClient = new HttpClient(manager); HttpClientParams clientParams = httpClient.getParams(); clientParams.setVersion(HttpVersion.HTTP_1_1); @@ -88,6 +94,43 @@ public class Client { manager.shutdown(); } + /** + * @return the wrapped HttpClient + */ + public HttpClient getHttpClient() { + return httpClient; + } + + /** + * Add extra headers. These extra headers will be applied to all http + * methods before they are removed. If any header is not used any more, + * client needs to remove it explicitly. + */ + public void addExtraHeader(final String name, final String value) { + extraHeaders.put(name, value); + } + + /** + * Get an extra header value. + */ + public String getExtraHeader(final String name) { + return extraHeaders.get(name); + } + + /** + * Get all extra headers (read-only). + */ + public Map getExtraHeaders() { + return Collections.unmodifiableMap(extraHeaders); + } + + /** + * Remove an extra header. + */ + public void removeExtraHeader(final String name) { + extraHeaders.remove(name); + } + /** * Execute a transaction method given only the path. Will select at random * one of the members of the supplied cluster definition and iterate through @@ -136,6 +179,9 @@ public class Client { public int executeURI(HttpMethod method, Header[] headers, String uri) throws IOException { method.setURI(new URI(uri, true)); + for (Map.Entry e: extraHeaders.entrySet()) { + method.addRequestHeader(e.getKey(), e.getValue()); + } if (headers != null) { for (Header header: headers) { method.addRequestHeader(header); @@ -456,5 +502,4 @@ public class Client { method.releaseConnection(); } } - } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java index a4d43777bf2..acaa4458efa 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java @@ -80,7 +80,7 @@ public class RemoteHTable implements HTableInterface { final int maxRetries; final long sleepTime; - @SuppressWarnings("unchecked") + @SuppressWarnings("rawtypes") protected String buildRowSpec(final byte[] row, final Map familyMap, final long startTime, final long endTime, final int maxVersions) { StringBuffer sb = new StringBuffer();