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.HBaseTestingUtil;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.Admin;
032import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
033import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
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.client.TableDescriptorBuilder;
038import org.apache.hadoop.hbase.rest.client.Client;
039import org.apache.hadoop.hbase.rest.client.Cluster;
040import org.apache.hadoop.hbase.rest.client.Response;
041import org.apache.hadoop.hbase.testclassification.MediumTests;
042import org.apache.hadoop.hbase.testclassification.RestTests;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.apache.http.Header;
045import org.apache.http.message.BasicHeader;
046import org.junit.AfterClass;
047import org.junit.BeforeClass;
048import org.junit.ClassRule;
049import org.junit.Test;
050import org.junit.experimental.categories.Category;
051
052@Category({ RestTests.class, MediumTests.class })
053public class TestGzipFilter {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestGzipFilter.class);
058
059  private static final TableName TABLE = TableName.valueOf("TestGzipFilter");
060  private static final String CFA = "a";
061  private static final String COLUMN_1 = CFA + ":1";
062  private static final String COLUMN_2 = CFA + ":2";
063  private static final String ROW_1 = "testrow1";
064  private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
065
066  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
067  private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
068  private static Client client;
069
070  @BeforeClass
071  public static void setUpBeforeClass() throws Exception {
072    TEST_UTIL.startMiniCluster();
073    REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
074    client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
075    Admin admin = TEST_UTIL.getAdmin();
076    if (admin.tableExists(TABLE)) {
077      return;
078    }
079    TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TABLE);
080    ColumnFamilyDescriptor columnFamilyDescriptor =
081      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFA)).build();
082    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
083    admin.createTable(tableDescriptorBuilder.build());
084  }
085
086  @AfterClass
087  public static void tearDownAfterClass() throws Exception {
088    REST_TEST_UTIL.shutdownServletContainer();
089    TEST_UTIL.shutdownMiniCluster();
090  }
091
092  @Test
093  public void testGzipFilter() throws Exception {
094    String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
095
096    ByteArrayOutputStream bos = new ByteArrayOutputStream();
097    GZIPOutputStream os = new GZIPOutputStream(bos);
098    os.write(VALUE_1);
099    os.close();
100    byte[] value_1_gzip = bos.toByteArray();
101
102    // input side filter
103
104    Header[] headers = new Header[2];
105    headers[0] = new BasicHeader("Content-Type", Constants.MIMETYPE_BINARY);
106    headers[1] = new BasicHeader("Content-Encoding", "gzip");
107    Response response = client.put(path, headers, value_1_gzip);
108    assertEquals(200, response.getCode());
109
110    Table table = TEST_UTIL.getConnection().getTable(TABLE);
111    Get get = new Get(Bytes.toBytes(ROW_1));
112    get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
113    Result result = table.get(get);
114    byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
115    assertNotNull(value);
116    assertTrue(Bytes.equals(value, VALUE_1));
117
118    // output side filter
119
120    headers[0] = new BasicHeader("Accept", Constants.MIMETYPE_BINARY);
121    headers[1] = new BasicHeader("Accept-Encoding", "gzip");
122    response = client.get(path, headers);
123    assertEquals(200, response.getCode());
124    ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
125    GZIPInputStream is = new GZIPInputStream(bis);
126    value = new byte[VALUE_1.length];
127    is.read(value, 0, VALUE_1.length);
128    assertTrue(Bytes.equals(value, VALUE_1));
129    is.close();
130    table.close();
131
132    testScannerResultCodes();
133  }
134
135  void testScannerResultCodes() throws Exception {
136    Header[] headers = new Header[3];
137    headers[0] = new BasicHeader("Content-Type", Constants.MIMETYPE_XML);
138    headers[1] = new BasicHeader("Accept", Constants.MIMETYPE_JSON);
139    headers[2] = new BasicHeader("Accept-Encoding", "gzip");
140    Response response = client.post("/" + TABLE + "/scanner", headers, Bytes.toBytes("<Scanner/>"));
141    assertEquals(201, response.getCode());
142    String scannerUrl = response.getLocation();
143    assertNotNull(scannerUrl);
144    response = client.get(scannerUrl);
145    assertEquals(200, response.getCode());
146    response = client.get(scannerUrl);
147    assertEquals(204, response.getCode());
148  }
149
150}