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