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