From 37e9a86396a0f07c7e83e2844e2c6e719fe7bd96 Mon Sep 17 00:00:00 2001 From: ndimiduk Date: Thu, 12 Sep 2013 14:14:02 +0000 Subject: [PATCH] HBASE-9347 Support for enabling servlet filters for REST service (Vandana Ayyalasomayajula) git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1522586 13f79535-47bb-0310-9956-ffa450edef68 --- hbase-common/src/main/resources/hbase-default.xml | 7 +++++++ .../org/apache/hadoop/hbase/rest/Constants.java | 1 + .../org/apache/hadoop/hbase/rest/RESTServer.java | 9 ++++++++- .../hbase/rest/HBaseRESTTestingUtility.java | 10 +++++++++- .../apache/hadoop/hbase/rest/TestGzipFilter.java | 15 +++++++++++---- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/hbase-common/src/main/resources/hbase-default.xml b/hbase-common/src/main/resources/hbase-default.xml index 8ccc01bc615..a858582dddd 100644 --- a/hbase-common/src/main/resources/hbase-default.xml +++ b/hbase-common/src/main/resources/hbase-default.xml @@ -1041,4 +1041,11 @@ possible configurations would overwhelm and obscure the important. Possible values are 'simple' (no authentication), and 'kerberos'. + + hbase.rest.filter.classes + org.apache.hadoop.hbase.rest.filter.GzipFilter + + Servlet filters for REST service. + + diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/Constants.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/Constants.java index 6abdc6c0a74..ec99e08a04e 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/Constants.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/Constants.java @@ -57,4 +57,5 @@ public interface Constants { static final String REST_DNS_NAMESERVER = "hbase.rest.dns.nameserver"; static final String REST_DNS_INTERFACE = "hbase.rest.dns.interface"; + public static final String FILTER_CLASSES = "hbase.rest.filter.classes"; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/RESTServer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/RESTServer.java index 48bd1ac7696..7de4d70230a 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/RESTServer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/RESTServer.java @@ -28,6 +28,7 @@ import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; +import org.apache.commons.lang.ArrayUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.classification.InterfaceAudience; @@ -226,7 +227,13 @@ public class RESTServer implements Constants { context.addFilter(authFilter, "/*", 1); } - context.addFilter(GzipFilter.class, "/*", 0); + // Load filters from configuration. + String[] filterClasses = servlet.getConfiguration().getStrings(FILTER_CLASSES, + ArrayUtils.EMPTY_STRING_ARRAY); + for (String filter : filterClasses) { + filter = filter.trim(); + context.addFilter(Class.forName(filter), "/*", 0); + } // Put up info server. int port = conf.getInt("hbase.rest.info.port", 8085); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/HBaseRESTTestingUtility.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/HBaseRESTTestingUtility.java index 3a1b4793b3b..8c316dd35ab 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/HBaseRESTTestingUtility.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/HBaseRESTTestingUtility.java @@ -18,6 +18,7 @@ */ package org.apache.hadoop.hbase.rest; +import org.apache.commons.lang.ArrayUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; @@ -67,7 +68,14 @@ public class HBaseRESTTestingUtility { // set up context Context context = new Context(server, "/", Context.SESSIONS); context.addServlet(sh, "/*"); - context.addFilter(GzipFilter.class, "/*", 0); + // Load filters specified from configuration. + String[] filterClasses = conf.getStrings(Constants.FILTER_CLASSES, + ArrayUtils.EMPTY_STRING_ARRAY); + for (String filter : filterClasses) { + filter = filter.trim(); + context.addFilter(Class.forName(filter), "/*", 0); + } + LOG.info("Loaded filter classes :" + filterClasses); // start the server server.start(); // get the port diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestGzipFilter.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestGzipFilter.java index ea0ad9b47d1..23da0ecbedd 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestGzipFilter.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestGzipFilter.java @@ -19,13 +19,21 @@ package org.apache.hadoop.hbase.rest; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.apache.commons.httpclient.Header; -import org.apache.hadoop.hbase.*; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HColumnDescriptor; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.MediumTests; +import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; @@ -33,10 +41,8 @@ import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.rest.client.Client; import org.apache.hadoop.hbase.rest.client.Cluster; import org.apache.hadoop.hbase.rest.client.Response; +import org.apache.hadoop.hbase.rest.filter.GzipFilter; import org.apache.hadoop.hbase.util.Bytes; - -import static org.junit.Assert.*; - import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -115,6 +121,7 @@ public class TestGzipFilter { is.read(value, 0, VALUE_1.length); assertTrue(Bytes.equals(value, VALUE_1)); is.close(); + table.close(); testScannerResultCodes(); }