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