001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.io.ByteArrayInputStream;
025import java.io.ByteArrayOutputStream;
026import java.util.zip.GZIPInputStream;
027import java.util.zip.GZIPOutputStream;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HColumnDescriptor;
031import org.apache.hadoop.hbase.HTableDescriptor;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.Admin;
034import org.apache.hadoop.hbase.client.Get;
035import org.apache.hadoop.hbase.client.Result;
036import org.apache.hadoop.hbase.client.Table;
037import org.apache.hadoop.hbase.rest.client.Client;
038import org.apache.hadoop.hbase.rest.client.Cluster;
039import org.apache.hadoop.hbase.rest.client.Response;
040import org.apache.hadoop.hbase.testclassification.MediumTests;
041import org.apache.hadoop.hbase.testclassification.RestTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.apache.http.Header;
044import org.apache.http.message.BasicHeader;
045import org.junit.AfterClass;
046import org.junit.BeforeClass;
047import org.junit.ClassRule;
048import org.junit.Test;
049import org.junit.experimental.categories.Category;
050
051@Category({ RestTests.class, MediumTests.class })
052public class TestGzipFilter {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestGzipFilter.class);
057
058  private static final TableName TABLE = TableName.valueOf("TestGzipFilter");
059  private static final String CFA = "a";
060  private static final String COLUMN_1 = CFA + ":1";
061  private static final String COLUMN_2 = CFA + ":2";
062  private static final String ROW_1 = "testrow1";
063  private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
064
065  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
066  private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
067  private static Client client;
068
069  @BeforeClass
070  public static void setUpBeforeClass() throws Exception {
071    TEST_UTIL.startMiniCluster();
072    REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
073    client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
074    Admin admin = TEST_UTIL.getAdmin();
075    if (admin.tableExists(TABLE)) {
076      return;
077    }
078    HTableDescriptor htd = new HTableDescriptor(TABLE);
079    htd.addFamily(new HColumnDescriptor(CFA));
080    admin.createTable(htd);
081  }
082
083  @AfterClass
084  public static void tearDownAfterClass() throws Exception {
085    REST_TEST_UTIL.shutdownServletContainer();
086    TEST_UTIL.shutdownMiniCluster();
087  }
088
089  @Test
090  public void testGzipFilter() throws Exception {
091    String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
092
093    ByteArrayOutputStream bos = new ByteArrayOutputStream();
094    GZIPOutputStream os = new GZIPOutputStream(bos);
095    os.write(VALUE_1);
096    os.close();
097    byte[] value_1_gzip = bos.toByteArray();
098
099    // input side filter
100
101    Header[] headers = new Header[2];
102    headers[0] = new BasicHeader("Content-Type", Constants.MIMETYPE_BINARY);
103    headers[1] = new BasicHeader("Content-Encoding", "gzip");
104    Response response = client.put(path, headers, value_1_gzip);
105    assertEquals(200, response.getCode());
106
107    Table table = TEST_UTIL.getConnection().getTable(TABLE);
108    Get get = new Get(Bytes.toBytes(ROW_1));
109    get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
110    Result result = table.get(get);
111    byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
112    assertNotNull(value);
113    assertTrue(Bytes.equals(value, VALUE_1));
114
115    // output side filter
116
117    headers[0] = new BasicHeader("Accept", Constants.MIMETYPE_BINARY);
118    headers[1] = new BasicHeader("Accept-Encoding", "gzip");
119    response = client.get(path, headers);
120    assertEquals(200, response.getCode());
121    ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
122    GZIPInputStream is = new GZIPInputStream(bis);
123    value = new byte[VALUE_1.length];
124    is.read(value, 0, VALUE_1.length);
125    assertTrue(Bytes.equals(value, VALUE_1));
126    is.close();
127    table.close();
128
129    testScannerResultCodes();
130  }
131
132  void testScannerResultCodes() throws Exception {
133    Header[] headers = new Header[3];
134    headers[0] = new BasicHeader("Content-Type", Constants.MIMETYPE_XML);
135    headers[1] = new BasicHeader("Accept", Constants.MIMETYPE_JSON);
136    headers[2] = new BasicHeader("Accept-Encoding", "gzip");
137    Response response = client.post("/" + TABLE + "/scanner", headers, Bytes.toBytes("<Scanner/>"));
138    assertEquals(201, response.getCode());
139    String scannerUrl = response.getLocation();
140    assertNotNull(scannerUrl);
141    response = client.get(scannerUrl);
142    assertEquals(200, response.getCode());
143    response = client.get(scannerUrl);
144    assertEquals(204, response.getCode());
145  }
146
147}