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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  import java.util.zip.GZIPInputStream;
29  import java.util.zip.GZIPOutputStream;
30  
31  import org.apache.commons.httpclient.Header;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HColumnDescriptor;
34  import org.apache.hadoop.hbase.HTableDescriptor;
35  import org.apache.hadoop.hbase.testclassification.MediumTests;
36  import org.apache.hadoop.hbase.TableName;
37  import org.apache.hadoop.hbase.client.Get;
38  import org.apache.hadoop.hbase.client.HBaseAdmin;
39  import org.apache.hadoop.hbase.client.HTable;
40  import org.apache.hadoop.hbase.client.Result;
41  import org.apache.hadoop.hbase.rest.client.Client;
42  import org.apache.hadoop.hbase.rest.client.Cluster;
43  import org.apache.hadoop.hbase.rest.client.Response;
44  import org.apache.hadoop.hbase.util.Bytes;
45  import org.junit.AfterClass;
46  import org.junit.BeforeClass;
47  import org.junit.Test;
48  import org.junit.experimental.categories.Category;
49  
50  @Category(MediumTests.class)
51  public class TestGzipFilter {
52    private static final String TABLE = "TestGzipFilter";
53    private static final String CFA = "a";
54    private static final String COLUMN_1 = CFA + ":1";
55    private static final String COLUMN_2 = CFA + ":2";
56    private static final String ROW_1 = "testrow1";
57    private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
58  
59    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
60    private static final HBaseRESTTestingUtility REST_TEST_UTIL =
61      new HBaseRESTTestingUtility();
62    private static Client client;
63  
64    @BeforeClass
65    public static void setUpBeforeClass() throws Exception {
66      TEST_UTIL.startMiniCluster();
67      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
68      client = new Client(new Cluster().add("localhost",
69        REST_TEST_UTIL.getServletPort()));
70      HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
71      if (admin.tableExists(TABLE)) {
72        return;
73      }
74      HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(TABLE));
75      htd.addFamily(new HColumnDescriptor(CFA));
76      admin.createTable(htd);
77    }
78  
79    @AfterClass
80    public static void tearDownAfterClass() throws Exception {
81      REST_TEST_UTIL.shutdownServletContainer();
82      TEST_UTIL.shutdownMiniCluster();
83    }
84  
85    @Test
86    public void testGzipFilter() throws Exception {
87      String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
88  
89      ByteArrayOutputStream bos = new ByteArrayOutputStream();
90      GZIPOutputStream os = new GZIPOutputStream(bos);
91      os.write(VALUE_1);
92      os.close();
93      byte[] value_1_gzip = bos.toByteArray();
94  
95      // input side filter
96  
97      Header[] headers = new Header[2];
98      headers[0] = new Header("Content-Type", Constants.MIMETYPE_BINARY);
99      headers[1] = new Header("Content-Encoding", "gzip");
100     Response response = client.put(path, headers, value_1_gzip);
101     assertEquals(response.getCode(), 200);
102 
103     HTable table = new HTable(TEST_UTIL.getConfiguration(), TABLE);
104     Get get = new Get(Bytes.toBytes(ROW_1));
105     get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
106     Result result = table.get(get);
107     byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
108     assertNotNull(value);
109     assertTrue(Bytes.equals(value, VALUE_1));
110 
111     // output side filter
112 
113     headers[0] = new Header("Accept", Constants.MIMETYPE_BINARY);
114     headers[1] = new Header("Accept-Encoding", "gzip");
115     response = client.get(path, headers);
116     assertEquals(response.getCode(), 200);
117     ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
118     GZIPInputStream is = new GZIPInputStream(bis);
119     value = new byte[VALUE_1.length];
120     is.read(value, 0, VALUE_1.length);
121     assertTrue(Bytes.equals(value, VALUE_1));
122     is.close();
123     table.close();
124 
125     testScannerResultCodes();
126   }
127 
128   @Test
129   public void testErrorNotGzipped() throws Exception {
130     Header[] headers = new Header[2];
131     headers[0] = new Header("Accept", Constants.MIMETYPE_BINARY);
132     headers[1] = new Header("Accept-Encoding", "gzip");
133     Response response = client.get("/" + TABLE + "/" + ROW_1 + "/" + COLUMN_2, headers);
134     assertEquals(response.getCode(), 404);
135     String contentEncoding = response.getHeader("Content-Encoding");
136     assertTrue(contentEncoding == null || !contentEncoding.contains("gzip"));
137     response = client.get("/" + TABLE, headers);
138     assertEquals(response.getCode(), 405);
139     contentEncoding = response.getHeader("Content-Encoding");
140     assertTrue(contentEncoding == null || !contentEncoding.contains("gzip"));
141   }
142 
143   void testScannerResultCodes() throws Exception {
144     Header[] headers = new Header[3];
145     headers[0] = new Header("Content-Type", Constants.MIMETYPE_XML);
146     headers[1] = new Header("Accept", Constants.MIMETYPE_JSON);
147     headers[2] = new Header("Accept-Encoding", "gzip");
148     Response response = client.post("/" + TABLE + "/scanner", headers,
149         "<Scanner/>".getBytes());
150     assertEquals(response.getCode(), 201);
151     String scannerUrl = response.getLocation();
152     assertNotNull(scannerUrl);
153     response = client.get(scannerUrl);
154     assertEquals(response.getCode(), 200);
155     response = client.get(scannerUrl);
156     assertEquals(response.getCode(), 204);
157   }
158 
159 }
160