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