View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.util.zip.GZIPInputStream;
25  import java.util.zip.GZIPOutputStream;
26  
27  import org.apache.commons.httpclient.Header;
28  import org.apache.hadoop.hbase.*;
29  import org.apache.hadoop.hbase.client.Get;
30  import org.apache.hadoop.hbase.client.HBaseAdmin;
31  import org.apache.hadoop.hbase.client.HTable;
32  import org.apache.hadoop.hbase.client.Result;
33  import org.apache.hadoop.hbase.rest.client.Client;
34  import org.apache.hadoop.hbase.rest.client.Cluster;
35  import org.apache.hadoop.hbase.rest.client.Response;
36  import org.apache.hadoop.hbase.util.Bytes;
37  
38  import static org.junit.Assert.*;
39  
40  import org.junit.AfterClass;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  @Category(MediumTests.class)
46  public class TestGzipFilter {
47    private static final String TABLE = "TestGzipFilter";
48    private static final String CFA = "a";
49    private static final String COLUMN_1 = CFA + ":1";
50    private static final String COLUMN_2 = CFA + ":2";
51    private static final String ROW_1 = "testrow1";
52    private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
53  
54    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
55    private static final HBaseRESTTestingUtility REST_TEST_UTIL =
56      new HBaseRESTTestingUtility();
57    private static Client client;
58  
59    @BeforeClass
60    public static void setUpBeforeClass() throws Exception {
61      TEST_UTIL.startMiniCluster();
62      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
63      client = new Client(new Cluster().add("localhost",
64        REST_TEST_UTIL.getServletPort()));
65      HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
66      if (admin.tableExists(TABLE)) {
67        return;
68      }
69      HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(TABLE));
70      htd.addFamily(new HColumnDescriptor(CFA));
71      admin.createTable(htd);
72    }
73  
74    @AfterClass
75    public static void tearDownAfterClass() throws Exception {
76      REST_TEST_UTIL.shutdownServletContainer();
77      TEST_UTIL.shutdownMiniCluster();
78    }
79  
80    @Test
81    public void testGzipFilter() throws Exception {
82      String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
83  
84      ByteArrayOutputStream bos = new ByteArrayOutputStream();
85      GZIPOutputStream os = new GZIPOutputStream(bos);
86      os.write(VALUE_1);
87      os.close();
88      byte[] value_1_gzip = bos.toByteArray();
89  
90      // input side filter
91  
92      Header[] headers = new Header[2];
93      headers[0] = new Header("Content-Type", Constants.MIMETYPE_BINARY);
94      headers[1] = new Header("Content-Encoding", "gzip");
95      Response response = client.put(path, headers, value_1_gzip);
96      assertEquals(response.getCode(), 200);
97  
98      HTable table = new HTable(TEST_UTIL.getConfiguration(), TABLE);
99      Get get = new Get(Bytes.toBytes(ROW_1));
100     get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
101     Result result = table.get(get);
102     byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
103     assertNotNull(value);
104     assertTrue(Bytes.equals(value, VALUE_1));
105 
106     // output side filter
107 
108     headers[0] = new Header("Accept", Constants.MIMETYPE_BINARY);
109     headers[1] = new Header("Accept-Encoding", "gzip");
110     response = client.get(path, headers);
111     assertEquals(response.getCode(), 200);
112     ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
113     GZIPInputStream is = new GZIPInputStream(bis);
114     value = new byte[VALUE_1.length];
115     is.read(value, 0, VALUE_1.length);
116     assertTrue(Bytes.equals(value, VALUE_1));
117     is.close();
118 
119     testScannerResultCodes();
120   }
121 
122   @Test
123   public void testErrorNotGzipped() throws Exception {
124     Header[] headers = new Header[2];
125     headers[0] = new Header("Accept", Constants.MIMETYPE_BINARY);
126     headers[1] = new Header("Accept-Encoding", "gzip");
127     Response response = client.get("/" + TABLE + "/" + ROW_1 + "/" + COLUMN_2, headers);
128     assertEquals(response.getCode(), 404);
129     String contentEncoding = response.getHeader("Content-Encoding");
130     assertTrue(contentEncoding == null || !contentEncoding.contains("gzip"));
131     response = client.get("/" + TABLE, headers);
132     assertEquals(response.getCode(), 405);
133     contentEncoding = response.getHeader("Content-Encoding");
134     assertTrue(contentEncoding == null || !contentEncoding.contains("gzip"));
135   }
136 
137   void testScannerResultCodes() throws Exception {
138     Header[] headers = new Header[3];
139     headers[0] = new Header("Content-Type", Constants.MIMETYPE_XML);
140     headers[1] = new Header("Accept", Constants.MIMETYPE_JSON);
141     headers[2] = new Header("Accept-Encoding", "gzip");
142     Response response = client.post("/" + TABLE + "/scanner", headers,
143         "<Scanner/>".getBytes());
144     assertEquals(response.getCode(), 201);
145     String scannerUrl = response.getLocation();
146     assertNotNull(scannerUrl);
147     response = client.get(scannerUrl);
148     assertEquals(response.getCode(), 200);
149     response = client.get(scannerUrl);
150     assertEquals(response.getCode(), 204);
151   }
152 
153 }
154