HBASE-3279 [rest] Filter for gzip content encoding that wraps both input and output side
git-svn-id: https://svn.apache.org/repos/asf/hbase/branches/0.20@1039506 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index af4ea5e..e5ca8d2 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -20,6 +20,9 @@
HBASE-2988 Support alternate compression for major compactions
HBASE-2907 [rest/stargate] Improve error response when trying to create a
scanner on a nonexistant table
+ HBASE-3279 [rest] Filter for gzip content encoding that wraps both input
+ and output side.
+
Release 0.20.6 - July 30, 2010
BUG FIXES
diff --git a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/Main.java b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/Main.java
index 2fc92eb..3c0b670 100644
--- a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/Main.java
+++ b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/Main.java
@@ -24,11 +24,11 @@
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
+import org.apache.hadoop.hbase.stargate.filter.GzipFilter;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
-import org.mortbay.servlet.GzipFilter;
import com.sun.jersey.spi.container.servlet.ServletContainer;
diff --git a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/client/Response.java b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/client/Response.java
index 11637a4..8dac1a3 100644
--- a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/client/Response.java
+++ b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/client/Response.java
@@ -73,12 +73,9 @@
return headers;
}
- /**
- * @return the value of the Location header
- */
- public String getLocation() {
+ public String getHeader(String key) {
for (Header header: headers) {
- if (header.getName().equals("Location")) {
+ if (header.getName().equalsIgnoreCase(key)) {
return header.getValue();
}
}
@@ -86,6 +83,13 @@
}
/**
+ * @return the value of the Location header
+ */
+ public String getLocation() {
+ return getHeader("Location");
+ }
+
+ /**
* @return true if a response body was sent
*/
public boolean hasBody() {
diff --git a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPRequestStream.java b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPRequestStream.java
new file mode 100644
index 0000000..5e60c51
--- /dev/null
+++ b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPRequestStream.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.stargate.filter;
+
+import java.io.IOException;
+import java.util.zip.GZIPInputStream;
+
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+
+public class GZIPRequestStream extends ServletInputStream
+{
+ private GZIPInputStream in;
+
+ public GZIPRequestStream(HttpServletRequest request) throws IOException {
+ this.in = new GZIPInputStream(request.getInputStream());
+ }
+
+ @Override
+ public int read() throws IOException {
+ return in.read();
+ }
+
+ @Override
+ public int read(byte[] b) throws IOException {
+ return in.read(b);
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ return in.read(b, off, len);
+ }
+
+ @Override
+ public void close() throws IOException {
+ in.close();
+ }
+}
\ No newline at end of file
diff --git a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPRequestWrapper.java b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPRequestWrapper.java
new file mode 100644
index 0000000..b721cbc
--- /dev/null
+++ b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPRequestWrapper.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.stargate.filter;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+
+public class GZIPRequestWrapper extends HttpServletRequestWrapper {
+ private ServletInputStream is;
+ private BufferedReader reader;
+
+ public GZIPRequestWrapper(HttpServletRequest request) throws IOException {
+ super(request);
+ this.is = new GZIPRequestStream(request);
+ this.reader = new BufferedReader(new InputStreamReader(this.is));
+ }
+
+ @Override
+ public ServletInputStream getInputStream() throws IOException {
+ return is;
+ }
+
+ @Override
+ public BufferedReader getReader() throws IOException {
+ return reader;
+ }
+}
\ No newline at end of file
diff --git a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPResponseStream.java b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPResponseStream.java
new file mode 100644
index 0000000..70ce6f4
--- /dev/null
+++ b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPResponseStream.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.stargate.filter;
+
+import java.io.IOException;
+import java.util.zip.GZIPOutputStream;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+
+public class GZIPResponseStream extends ServletOutputStream
+{
+ private HttpServletResponse response;
+ private GZIPOutputStream out;
+
+ public GZIPResponseStream(HttpServletResponse response) throws IOException {
+ this.response = response;
+ this.out = new GZIPOutputStream(response.getOutputStream());
+ response.addHeader("Content-Encoding", "gzip");
+ }
+
+ public void resetBuffer() {
+ if (out != null && !response.isCommitted()) {
+ response.setHeader("Content-Encoding", null);
+ }
+ out = null;
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ out.write(b);
+ }
+
+ @Override
+ public void write(byte[] b) throws IOException {
+ out.write(b);
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException {
+ out.write(b, off, len);
+ }
+
+ @Override
+ public void close() throws IOException {
+ finish();
+ out.close();
+ }
+
+ @Override
+ public void flush() throws IOException {
+ out.flush();
+ }
+
+ public void finish() throws IOException {
+ out.finish();
+ }
+}
\ No newline at end of file
diff --git a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPResponseWrapper.java b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPResponseWrapper.java
new file mode 100644
index 0000000..1ecdd43
--- /dev/null
+++ b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GZIPResponseWrapper.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.stargate.filter;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+public class GZIPResponseWrapper extends HttpServletResponseWrapper {
+ private HttpServletResponse response;
+ private GZIPResponseStream os;
+ private PrintWriter writer;
+ private boolean compress = true;
+
+ public GZIPResponseWrapper(HttpServletResponse response) {
+ super(response);
+ this.response = response;
+ }
+
+ @Override
+ public void setStatus(int status) {
+ if (status < 200 || status >= 300) {
+ compress = false;
+ }
+ }
+
+ @Override
+ public void addHeader(String name, String value) {
+ if (!"content-length".equalsIgnoreCase(name)) {
+ super.addHeader(name, value);
+ }
+ }
+
+ @Override
+ public void setContentLength(int length) {
+ // do nothing
+ }
+
+ @Override
+ public void setIntHeader(String name, int value) {
+ if (!"content-length".equalsIgnoreCase(name)) {
+ super.setIntHeader(name, value);
+ }
+ }
+
+ @Override
+ public void setHeader(String name, String value) {
+ if (!"content-length".equalsIgnoreCase(name)) {
+ super.setHeader(name, value);
+ }
+ }
+
+ @Override
+ public void flushBuffer() throws IOException {
+ if (writer != null) {
+ writer.flush();
+ }
+ if (os != null) {
+ os.finish();
+ } else {
+ getResponse().flushBuffer();
+ }
+ }
+
+ @Override
+ public void reset() {
+ super.reset();
+ if (os != null) {
+ os.resetBuffer();
+ }
+ writer = null;
+ os = null;
+ compress = true;
+ }
+
+ @Override
+ public void resetBuffer() {
+ super.resetBuffer();
+ if (os != null) {
+ os.resetBuffer();
+ }
+ writer = null;
+ os = null;
+ }
+
+ @Override
+ public void sendError(int status, String msg) throws IOException {
+ resetBuffer();
+ super.sendError(status, msg);
+ }
+
+ @Override
+ public void sendError(int status) throws IOException {
+ resetBuffer();
+ super.sendError(status);
+ }
+
+ @Override
+ public void sendRedirect(String location) throws IOException {
+ resetBuffer();
+ super.sendRedirect(location);
+ }
+
+ @Override
+ public ServletOutputStream getOutputStream() throws IOException {
+ if (!response.isCommitted() && compress) {
+ if (os == null) {
+ os = new GZIPResponseStream(response);
+ }
+ return os;
+ } else {
+ return response.getOutputStream();
+ }
+ }
+
+ @Override
+ public PrintWriter getWriter() throws IOException {
+ if (writer == null) {
+ writer = new PrintWriter(getOutputStream());
+ }
+ return writer;
+ }
+}
\ No newline at end of file
diff --git a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GzipFilter.java b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GzipFilter.java
new file mode 100644
index 0000000..5c8a6ef
--- /dev/null
+++ b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/filter/GzipFilter.java
@@ -0,0 +1,57 @@
+package org.apache.hadoop.hbase.stargate.filter;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class GzipFilter implements Filter {
+ private Set<String> mimeTypes = new HashSet<String>();
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ String s = filterConfig.getInitParameter("mimeTypes");
+ if (s != null) {
+ StringTokenizer tok = new StringTokenizer(s, ",", false);
+ while (tok.hasMoreTokens()) {
+ mimeTypes.add(tok.nextToken());
+ }
+ }
+ }
+
+ @Override
+ public void destroy() {
+ }
+
+ @Override
+ public void doFilter(ServletRequest req, ServletResponse rsp,
+ FilterChain chain) throws IOException, ServletException {
+ HttpServletRequest request = (HttpServletRequest)req;
+ HttpServletResponse response = (HttpServletResponse)rsp;
+ String contentEncoding = request.getHeader("content-encoding");
+ String acceptEncoding = request.getHeader("accept-encoding");
+ String contentType = request.getHeader("content-type");
+ if ((contentEncoding != null) &&
+ (contentEncoding.toLowerCase().indexOf("gzip") > -1)) {
+ request = new GZIPRequestWrapper(request);
+ }
+ if (((acceptEncoding != null) &&
+ (acceptEncoding.toLowerCase().indexOf("gzip") > -1)) ||
+ ((contentType != null) && mimeTypes.contains(contentType))) {
+ response = new GZIPResponseWrapper(response);
+ }
+ chain.doFilter(request, response);
+ if ((response instanceof GZIPResponseWrapper)) {
+ ((GZIPResponseStream)response.getOutputStream()).finish();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/MiniClusterTestCase.java b/src/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/MiniClusterTestCase.java
index 80a8162..85329ef 100644
--- a/src/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/MiniClusterTestCase.java
+++ b/src/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/MiniClusterTestCase.java
@@ -33,6 +33,7 @@
import org.apache.hadoop.hbase.MiniHBaseCluster;
import org.apache.hadoop.hbase.MiniZooKeeperCluster;
import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.stargate.filter.GzipFilter;
import org.apache.hadoop.hbase.util.FSUtils;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.util.StringUtils;
@@ -170,6 +171,7 @@
// set up context
Context context = new Context(server, "/", Context.SESSIONS);
context.addServlet(sh, "/*");
+ context.addFilter(GzipFilter.class, "/*", 0);
// start the server
server.start();
diff --git a/src/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestGzipFilter.java b/src/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestGzipFilter.java
new file mode 100644
index 0000000..641cb39
--- /dev/null
+++ b/src/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestGzipFilter.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.stargate;
+
+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.HColumnDescriptor;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.client.Get;
+import org.apache.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.client.Result;
+import org.apache.hadoop.hbase.stargate.client.Client;
+import org.apache.hadoop.hbase.stargate.client.Cluster;
+import org.apache.hadoop.hbase.stargate.client.Response;
+import org.apache.hadoop.hbase.util.Bytes;
+
+public class TestGzipFilter extends MiniClusterTestCase {
+ private static final String TABLE = "TestGzipFilter";
+ private static final String CFA = "a";
+ private static final String COLUMN_1 = CFA + ":1";
+ private static final String COLUMN_2 = CFA + ":2";
+ private static final String ROW_1 = "testrow1";
+ private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
+
+ Client client;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ HBaseAdmin admin = new HBaseAdmin(conf);
+ if (admin.tableExists(TABLE)) {
+ return;
+ }
+ HTableDescriptor htd = new HTableDescriptor(TABLE);
+ htd.addFamily(new HColumnDescriptor(CFA));
+ admin.createTable(htd);
+ client = new Client(new Cluster().add("localhost", testServletPort));
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ client.shutdown();
+ super.tearDown();
+ }
+
+ public void testGzipFilter() throws Exception {
+ String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ GZIPOutputStream os = new GZIPOutputStream(bos);
+ os.write(VALUE_1);
+ os.close();
+ byte[] value_1_gzip = bos.toByteArray();
+
+ // input side filter
+
+ Header[] headers = new Header[2];
+ headers[0] = new Header("Content-Type", Constants.MIMETYPE_BINARY);
+ headers[1] = new Header("Content-Encoding", "gzip");
+ Response response = client.put(path, headers, value_1_gzip);
+ assertEquals(response.getCode(), 200);
+
+ HTable table = new HTable(conf, TABLE);
+ Get get = new Get(Bytes.toBytes(ROW_1));
+ get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
+ Result result = table.get(get);
+ byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
+ assertNotNull(value);
+ assertTrue(Bytes.equals(value, VALUE_1));
+
+ // output side filter
+
+ headers[0] = new Header("Accept", Constants.MIMETYPE_BINARY);
+ headers[1] = new Header("Accept-Encoding", "gzip");
+ response = client.get(path, headers);
+ assertEquals(response.getCode(), 200);
+ ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
+ GZIPInputStream is = new GZIPInputStream(bis);
+ value = new byte[VALUE_1.length];
+ is.read(value, 0, VALUE_1.length);
+ assertTrue(Bytes.equals(value, VALUE_1));
+ is.close();
+
+ // errors should not be compressed
+
+ String badPath = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_2;
+ headers[0] = new Header("Accept", Constants.MIMETYPE_BINARY);
+ headers[1] = new Header("Accept-Encoding", "gzip");
+ response = client.get(badPath, headers);
+ assertEquals(response.getCode(), 404);
+ String contentEncoding = response.getHeader("Content-Encoding");
+ assertTrue(contentEncoding == null || !contentEncoding.contains("gzip"));
+ }
+}
\ No newline at end of file