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 =
068    new HBaseRESTTestingUtility();
069  private static Client client;
070
071  @BeforeClass
072  public static void setUpBeforeClass() throws Exception {
073    TEST_UTIL.startMiniCluster();
074    REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
075    client = new Client(new Cluster().add("localhost",
076      REST_TEST_UTIL.getServletPort()));
077    Admin admin = TEST_UTIL.getAdmin();
078    if (admin.tableExists(TABLE)) {
079      return;
080    }
081    TableDescriptorBuilder tableDescriptorBuilder =
082      TableDescriptorBuilder.newBuilder(TABLE);
083    ColumnFamilyDescriptor columnFamilyDescriptor =
084      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFA)).build();
085    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
086    admin.createTable(tableDescriptorBuilder.build());
087  }
088
089  @AfterClass
090  public static void tearDownAfterClass() throws Exception {
091    REST_TEST_UTIL.shutdownServletContainer();
092    TEST_UTIL.shutdownMiniCluster();
093  }
094
095  @Test
096  public void testGzipFilter() throws Exception {
097    String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
098
099    ByteArrayOutputStream bos = new ByteArrayOutputStream();
100    GZIPOutputStream os = new GZIPOutputStream(bos);
101    os.write(VALUE_1);
102    os.close();
103    byte[] value_1_gzip = bos.toByteArray();
104
105    // input side filter
106
107    Header[] headers = new Header[2];
108    headers[0] = new BasicHeader("Content-Type", Constants.MIMETYPE_BINARY);
109    headers[1] = new BasicHeader("Content-Encoding", "gzip");
110    Response response = client.put(path, headers, value_1_gzip);
111    assertEquals(200, response.getCode());
112
113    Table table = TEST_UTIL.getConnection().getTable(TABLE);
114    Get get = new Get(Bytes.toBytes(ROW_1));
115    get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
116    Result result = table.get(get);
117    byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
118    assertNotNull(value);
119    assertTrue(Bytes.equals(value, VALUE_1));
120
121    // output side filter
122
123    headers[0] = new BasicHeader("Accept", Constants.MIMETYPE_BINARY);
124    headers[1] = new BasicHeader("Accept-Encoding", "gzip");
125    response = client.get(path, headers);
126    assertEquals(200, response.getCode());
127    ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
128    GZIPInputStream is = new GZIPInputStream(bis);
129    value = new byte[VALUE_1.length];
130    is.read(value, 0, VALUE_1.length);
131    assertTrue(Bytes.equals(value, VALUE_1));
132    is.close();
133    table.close();
134
135    testScannerResultCodes();
136  }
137
138  void testScannerResultCodes() throws Exception {
139    Header[] headers = new Header[3];
140    headers[0] = new BasicHeader("Content-Type", Constants.MIMETYPE_XML);
141    headers[1] = new BasicHeader("Accept", Constants.MIMETYPE_JSON);
142    headers[2] = new BasicHeader("Accept-Encoding", "gzip");
143    Response response = client.post("/" + TABLE + "/scanner", headers, Bytes.toBytes("<Scanner/>"));
144    assertEquals(201, response.getCode());
145    String scannerUrl = response.getLocation();
146    assertNotNull(scannerUrl);
147    response = client.get(scannerUrl);
148    assertEquals(200, response.getCode());
149    response = client.get(scannerUrl);
150    assertEquals(204, response.getCode());
151  }
152
153}
154