View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.rest;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.DataInputStream;
26  import java.io.EOFException;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.Serializable;
30  import java.net.URLEncoder;
31  import java.nio.charset.StandardCharsets;
32  import java.util.ArrayList;
33  import java.util.Collections;
34  import java.util.List;
35  
36  import javax.ws.rs.core.MediaType;
37  import javax.xml.bind.JAXBContext;
38  import javax.xml.bind.JAXBException;
39  import javax.xml.bind.Unmarshaller;
40  import javax.xml.bind.annotation.XmlAccessType;
41  import javax.xml.bind.annotation.XmlAccessorType;
42  import javax.xml.bind.annotation.XmlElement;
43  import javax.xml.bind.annotation.XmlRootElement;
44  import javax.xml.parsers.SAXParserFactory;
45  import javax.xml.stream.XMLStreamException;
46  
47  import org.apache.hadoop.conf.Configuration;
48  import org.apache.hadoop.hbase.HBaseTestingUtility;
49  import org.apache.hadoop.hbase.HColumnDescriptor;
50  import org.apache.hadoop.hbase.HTableDescriptor;
51  import org.apache.hadoop.hbase.testclassification.MediumTests;
52  import org.apache.hadoop.hbase.TableName;
53  import org.apache.hadoop.hbase.client.Admin;
54  import org.apache.hadoop.hbase.filter.Filter;
55  import org.apache.hadoop.hbase.filter.ParseFilter;
56  import org.apache.hadoop.hbase.filter.PrefixFilter;
57  import org.apache.hadoop.hbase.rest.client.Client;
58  import org.apache.hadoop.hbase.rest.client.Cluster;
59  import org.apache.hadoop.hbase.rest.client.Response;
60  import org.apache.hadoop.hbase.rest.model.CellModel;
61  import org.apache.hadoop.hbase.rest.model.CellSetModel;
62  import org.apache.hadoop.hbase.rest.model.RowModel;
63  import org.apache.hadoop.hbase.rest.provider.JacksonProvider;
64  import org.apache.hadoop.hbase.util.Bytes;
65  import org.codehaus.jackson.JsonFactory;
66  import org.codehaus.jackson.JsonParser;
67  import org.codehaus.jackson.JsonToken;
68  import org.codehaus.jackson.map.ObjectMapper;
69  import org.junit.AfterClass;
70  import org.junit.BeforeClass;
71  import org.junit.Test;
72  import org.junit.experimental.categories.Category;
73  import org.xml.sax.InputSource;
74  import org.xml.sax.XMLReader;
75  
76  @Category(MediumTests.class)
77  public class TestTableScan {
78  
79    private static final TableName TABLE = TableName.valueOf("TestScanResource");
80    private static final String CFA = "a";
81    private static final String CFB = "b";
82    private static final String COLUMN_1 = CFA + ":1";
83    private static final String COLUMN_2 = CFB + ":2";
84    private static Client client;
85    private static int expectedRows1;
86    private static int expectedRows2;
87    private static Configuration conf;
88  
89    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
90    private static final HBaseRESTTestingUtility REST_TEST_UTIL =
91      new HBaseRESTTestingUtility();
92  
93    @BeforeClass
94    public static void setUpBeforeClass() throws Exception {
95      conf = TEST_UTIL.getConfiguration();
96      conf.set(Constants.CUSTOM_FILTERS, "CustomFilter:" + CustomFilter.class.getName()); 
97      TEST_UTIL.startMiniCluster();
98      REST_TEST_UTIL.startServletContainer(conf);
99      client = new Client(new Cluster().add("localhost",
100       REST_TEST_UTIL.getServletPort()));
101     Admin admin = TEST_UTIL.getHBaseAdmin();
102     if (!admin.tableExists(TABLE)) {
103     HTableDescriptor htd = new HTableDescriptor(TABLE);
104     htd.addFamily(new HColumnDescriptor(CFA));
105     htd.addFamily(new HColumnDescriptor(CFB));
106     admin.createTable(htd);
107     expectedRows1 = TestScannerResource.insertData(conf, TABLE, COLUMN_1, 1.0);
108     expectedRows2 = TestScannerResource.insertData(conf, TABLE, COLUMN_2, 0.5);
109     }
110   }
111 
112   @AfterClass
113   public static void tearDownAfterClass() throws Exception {
114     TEST_UTIL.getHBaseAdmin().disableTable(TABLE);
115     TEST_UTIL.getHBaseAdmin().deleteTable(TABLE);
116     REST_TEST_UTIL.shutdownServletContainer();
117     TEST_UTIL.shutdownMiniCluster();
118   }
119 
120   @Test
121   public void testSimpleScannerXML() throws IOException, JAXBException, XMLStreamException {
122     // Test scanning particular columns
123     StringBuilder builder = new StringBuilder();
124     builder.append("/*");
125     builder.append("?");
126     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
127     builder.append("&");
128     builder.append(Constants.SCAN_LIMIT + "=10");
129     Response response = client.get("/" + TABLE + builder.toString(),
130       Constants.MIMETYPE_XML);
131     assertEquals(200, response.getCode());
132     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
133     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
134     Unmarshaller ush = ctx.createUnmarshaller();
135     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
136     int count = TestScannerResource.countCellSet(model);
137     assertEquals(10, count);
138     checkRowsNotNull(model);
139 
140     //Test with no limit.
141     builder = new StringBuilder();
142     builder.append("/*");
143     builder.append("?");
144     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
145     response = client.get("/" + TABLE + builder.toString(),
146       Constants.MIMETYPE_XML);
147     assertEquals(200, response.getCode());
148     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); 
149     model = (CellSetModel) ush.unmarshal(response.getStream());
150     count = TestScannerResource.countCellSet(model);
151     assertEquals(expectedRows1, count);
152     checkRowsNotNull(model);
153 
154     //Test with start and end row.
155     builder = new StringBuilder();
156     builder.append("/*");
157     builder.append("?");
158     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
159     builder.append("&");
160     builder.append(Constants.SCAN_START_ROW + "=aaa");
161     builder.append("&");
162     builder.append(Constants.SCAN_END_ROW + "=aay");
163     response = client.get("/" + TABLE + builder.toString(),
164       Constants.MIMETYPE_XML);
165     assertEquals(200, response.getCode());
166     model = (CellSetModel) ush.unmarshal(response.getStream());
167     count = TestScannerResource.countCellSet(model);
168     RowModel startRow = model.getRows().get(0);
169     assertEquals("aaa", Bytes.toString(startRow.getKey()));
170     RowModel endRow = model.getRows().get(model.getRows().size() - 1);
171     assertEquals("aax", Bytes.toString(endRow.getKey()));
172     assertEquals(24, count);
173     checkRowsNotNull(model);
174 
175     //Test with start row and limit.
176     builder = new StringBuilder();
177     builder.append("/*");
178     builder.append("?");
179     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
180     builder.append("&");
181     builder.append(Constants.SCAN_START_ROW + "=aaa");
182     builder.append("&");
183     builder.append(Constants.SCAN_LIMIT + "=15");
184     response = client.get("/" + TABLE + builder.toString(),
185       Constants.MIMETYPE_XML);
186     assertEquals(200, response.getCode());
187     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
188     model = (CellSetModel) ush.unmarshal(response.getStream());
189     startRow = model.getRows().get(0);
190     assertEquals("aaa", Bytes.toString(startRow.getKey()));
191     count = TestScannerResource.countCellSet(model);
192     assertEquals(15, count);
193     checkRowsNotNull(model);
194   }
195 
196   @Test
197   public void testSimpleScannerJson() throws IOException, JAXBException {
198     // Test scanning particular columns with limit.
199     StringBuilder builder = new StringBuilder();
200     builder.append("/*");
201     builder.append("?");
202     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
203     builder.append("&");
204     builder.append(Constants.SCAN_LIMIT + "=20");
205     Response response = client.get("/" + TABLE + builder.toString(),
206       Constants.MIMETYPE_JSON);
207     assertEquals(200, response.getCode());
208     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
209     ObjectMapper mapper = new JacksonProvider()
210         .locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
211     CellSetModel model = mapper.readValue(response.getStream(), CellSetModel.class);
212     int count = TestScannerResource.countCellSet(model);
213     assertEquals(20, count);
214     checkRowsNotNull(model);
215 
216     //Test scanning with no limit.
217     builder = new StringBuilder();
218     builder.append("/*");
219     builder.append("?");
220     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_2);
221     response = client.get("/" + TABLE + builder.toString(),
222       Constants.MIMETYPE_JSON);
223     assertEquals(200, response.getCode());
224     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
225     model = mapper.readValue(response.getStream(), CellSetModel.class);
226     count = TestScannerResource.countCellSet(model);
227     assertEquals(expectedRows2, count);
228     checkRowsNotNull(model);
229 
230     //Test with start row and end row.
231     builder = new StringBuilder();
232     builder.append("/*");
233     builder.append("?");
234     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
235     builder.append("&");
236     builder.append(Constants.SCAN_START_ROW + "=aaa");
237     builder.append("&");
238     builder.append(Constants.SCAN_END_ROW + "=aay");
239     response = client.get("/" + TABLE + builder.toString(),
240       Constants.MIMETYPE_JSON);
241     assertEquals(200, response.getCode());
242     model = mapper.readValue(response.getStream(), CellSetModel.class);
243     RowModel startRow = model.getRows().get(0);
244     assertEquals("aaa", Bytes.toString(startRow.getKey()));
245     RowModel endRow = model.getRows().get(model.getRows().size() - 1);
246     assertEquals("aax", Bytes.toString(endRow.getKey()));
247     count = TestScannerResource.countCellSet(model);
248     assertEquals(24, count);
249     checkRowsNotNull(model);
250   }
251 
252   /**
253    * An example to scan using listener in unmarshaller for XML.
254    * @throws Exception the exception
255    */
256   @Test
257   public void testScanUsingListenerUnmarshallerXML() throws Exception {
258     StringBuilder builder = new StringBuilder();
259     builder.append("/*");
260     builder.append("?");
261     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
262     builder.append("&");
263     builder.append(Constants.SCAN_LIMIT + "=10");
264     Response response = client.get("/" + TABLE + builder.toString(),
265       Constants.MIMETYPE_XML);
266     assertEquals(200, response.getCode());
267     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
268     JAXBContext context = JAXBContext.newInstance(ClientSideCellSetModel.class, RowModel.class,
269       CellModel.class);
270     Unmarshaller unmarshaller = context.createUnmarshaller();
271 
272     final ClientSideCellSetModel.Listener listener = new ClientSideCellSetModel.Listener() {
273       @Override
274       public void handleRowModel(ClientSideCellSetModel helper, RowModel row) {
275         assertTrue(row.getKey() != null);
276         assertTrue(row.getCells().size() > 0);
277       }
278     };
279 
280     // install the callback on all ClientSideCellSetModel instances
281     unmarshaller.setListener(new Unmarshaller.Listener() {
282         @Override
283         public void beforeUnmarshal(Object target, Object parent) {
284             if (target instanceof ClientSideCellSetModel) {
285                 ((ClientSideCellSetModel) target).setCellSetModelListener(listener);
286             }
287         }
288 
289         @Override
290         public void afterUnmarshal(Object target, Object parent) {
291             if (target instanceof ClientSideCellSetModel) {
292                 ((ClientSideCellSetModel) target).setCellSetModelListener(null);
293             }
294         }
295     });
296 
297     // create a new XML parser
298     SAXParserFactory factory = SAXParserFactory.newInstance();
299     factory.setNamespaceAware(true);
300     XMLReader reader = factory.newSAXParser().getXMLReader();
301     reader.setContentHandler(unmarshaller.getUnmarshallerHandler());
302     assertFalse(ClientSideCellSetModel.listenerInvoked);
303     reader.parse(new InputSource(response.getStream()));
304     assertTrue(ClientSideCellSetModel.listenerInvoked);
305 
306   }
307 
308   @Test
309   public void testStreamingJSON() throws Exception {
310     // Test scanning particular columns with limit.
311     StringBuilder builder = new StringBuilder();
312     builder.append("/*");
313     builder.append("?");
314     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
315     builder.append("&");
316     builder.append(Constants.SCAN_LIMIT + "=20");
317     Response response = client.get("/" + TABLE + builder.toString(),
318       Constants.MIMETYPE_JSON);
319     assertEquals(200, response.getCode());
320     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
321     ObjectMapper mapper = new JacksonProvider()
322         .locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
323     CellSetModel model = mapper.readValue(response.getStream(), CellSetModel.class);
324     int count = TestScannerResource.countCellSet(model);
325     assertEquals(20, count);
326     checkRowsNotNull(model);
327 
328     //Test scanning with no limit.
329     builder = new StringBuilder();
330     builder.append("/*");
331     builder.append("?");
332     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_2);
333     response = client.get("/" + TABLE + builder.toString(),
334       Constants.MIMETYPE_JSON);
335     assertEquals(200, response.getCode());
336     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
337     model = mapper.readValue(response.getStream(), CellSetModel.class);
338     count = TestScannerResource.countCellSet(model);
339     assertEquals(expectedRows2, count);
340     checkRowsNotNull(model);
341 
342     //Test with start row and end row.
343     builder = new StringBuilder();
344     builder.append("/*");
345     builder.append("?");
346     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
347     builder.append("&");
348     builder.append(Constants.SCAN_START_ROW + "=aaa");
349     builder.append("&");
350     builder.append(Constants.SCAN_END_ROW + "=aay");
351     response = client.get("/" + TABLE + builder.toString(),
352       Constants.MIMETYPE_JSON);
353     assertEquals(200, response.getCode());
354 
355     count = 0;
356     JsonFactory jfactory = new JsonFactory(mapper);
357     JsonParser jParser = jfactory.createJsonParser(response.getStream());
358     boolean found = false;
359     while (jParser.nextToken() != JsonToken.END_OBJECT) {
360       if(jParser.getCurrentToken() == JsonToken.START_OBJECT && found) {
361         RowModel row = jParser.readValueAs(RowModel.class);
362         assertNotNull(row.getKey());
363         for (int i = 0; i < row.getCells().size(); i++) {
364           if (count == 0) {
365             assertEquals("aaa", Bytes.toString(row.getKey()));
366           }
367           if (count == 23) {
368             assertEquals("aax", Bytes.toString(row.getKey()));
369           }
370           count++;
371         }
372         jParser.skipChildren();
373       } else {
374         found = jParser.getCurrentToken() == JsonToken.START_ARRAY;
375       }
376     }
377     assertEquals(24, count);
378   }
379 
380   @Test
381   public void testSimpleScannerProtobuf() throws Exception {
382     StringBuilder builder = new StringBuilder();
383     builder.append("/*");
384     builder.append("?");
385     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
386     builder.append("&");
387     builder.append(Constants.SCAN_LIMIT + "=15");
388     Response response = client.get("/" + TABLE + builder.toString(),
389       Constants.MIMETYPE_PROTOBUF);
390     assertEquals(200, response.getCode());
391     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
392     int rowCount = readProtobufStream(response.getStream());
393     assertEquals(15, rowCount);
394 
395   //Test with start row and end row.
396     builder = new StringBuilder();
397     builder.append("/*");
398     builder.append("?");
399     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
400     builder.append("&");
401     builder.append(Constants.SCAN_START_ROW + "=aaa");
402     builder.append("&");
403     builder.append(Constants.SCAN_END_ROW + "=aay");
404     response = client.get("/" + TABLE + builder.toString(),
405       Constants.MIMETYPE_PROTOBUF);
406     assertEquals(200, response.getCode());
407     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
408     rowCount = readProtobufStream(response.getStream());
409     assertEquals(24, rowCount);
410   }
411 
412   private void checkRowsNotNull(CellSetModel model) {
413     for (RowModel row: model.getRows()) {
414       assertTrue(row.getKey() != null);
415       assertTrue(row.getCells().size() > 0);
416     }
417   }
418 
419   /**
420    * Read protobuf stream.
421    * @param inputStream the input stream
422    * @return The number of rows in the cell set model.
423    * @throws IOException Signals that an I/O exception has occurred.
424    */
425   public int readProtobufStream(InputStream inputStream) throws IOException{
426     DataInputStream stream = new DataInputStream(inputStream);
427     CellSetModel model = null;
428     int rowCount = 0;
429     try {
430       while (true) {
431         byte[] lengthBytes = new byte[2];
432         int readBytes = stream.read(lengthBytes);
433         if (readBytes == -1) {
434           break;
435         }
436         assertEquals(2, readBytes);
437         int length = Bytes.toShort(lengthBytes);
438         byte[] cellset = new byte[length];
439         stream.read(cellset);
440         model = new CellSetModel();
441         model.getObjectFromMessage(cellset);
442         checkRowsNotNull(model);
443         rowCount = rowCount + TestScannerResource.countCellSet(model);
444       }
445     } catch (EOFException exp) {
446       exp.printStackTrace();
447     } finally {
448       stream.close();
449     }
450     return rowCount;
451   }
452 
453   @Test
454   public void testScanningUnknownColumnJson() throws IOException, JAXBException {
455     // Test scanning particular columns with limit.
456     StringBuilder builder = new StringBuilder();
457     builder.append("/*");
458     builder.append("?");
459     builder.append(Constants.SCAN_COLUMN + "=a:test");
460     Response response = client.get("/" + TABLE  + builder.toString(),
461       Constants.MIMETYPE_JSON);
462     assertEquals(200, response.getCode());
463     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
464     ObjectMapper mapper = new JacksonProvider().locateMapper(CellSetModel.class,
465       MediaType.APPLICATION_JSON_TYPE);
466     CellSetModel model = mapper.readValue(response.getStream(), CellSetModel.class);
467     int count = TestScannerResource.countCellSet(model);
468     assertEquals(0, count);
469   }
470   
471   @Test
472   public void testSimpleFilter() throws IOException, JAXBException {
473     StringBuilder builder = new StringBuilder();
474     builder.append("/*");
475     builder.append("?");
476     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
477     builder.append("&");
478     builder.append(Constants.SCAN_START_ROW + "=aaa");
479     builder.append("&");
480     builder.append(Constants.SCAN_END_ROW + "=aay");
481     builder.append("&");
482     builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("PrefixFilter('aab')", "UTF-8"));
483     Response response =
484         client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
485     assertEquals(200, response.getCode());
486     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
487     Unmarshaller ush = ctx.createUnmarshaller();
488     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
489     int count = TestScannerResource.countCellSet(model);
490     assertEquals(1, count);
491     assertEquals("aab", new String(model.getRows().get(0).getCells().get(0).getValue(),
492       StandardCharsets.UTF_8));
493   }
494 
495   @Test
496   public void testQualifierAndPrefixFilters() throws IOException, JAXBException {
497     StringBuilder builder = new StringBuilder();
498     builder.append("/abc*");
499     builder.append("?");
500     builder.append(Constants.SCAN_FILTER + "="
501         + URLEncoder.encode("QualifierFilter(=,'binary:1')", "UTF-8"));
502     Response response = 
503         client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
504     assertEquals(200, response.getCode());
505     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
506     Unmarshaller ush = ctx.createUnmarshaller();
507     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
508     int count = TestScannerResource.countCellSet(model);
509     assertEquals(1, count);
510     assertEquals("abc", new String(model.getRows().get(0).getCells().get(0).getValue(),
511       StandardCharsets.UTF_8));
512   }
513 
514 
515   @Test
516   public void testCompoundFilter() throws IOException, JAXBException {
517     StringBuilder builder = new StringBuilder();
518     builder.append("/*");
519     builder.append("?");
520     builder.append(Constants.SCAN_FILTER + "="
521         + URLEncoder.encode("PrefixFilter('abc') AND QualifierFilter(=,'binary:1')", "UTF-8"));
522     Response response =
523         client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
524     assertEquals(200, response.getCode());
525     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
526     Unmarshaller ush = ctx.createUnmarshaller();
527     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
528     int count = TestScannerResource.countCellSet(model);
529     assertEquals(1, count);
530     assertEquals("abc", new String(model.getRows().get(0).getCells().get(0).getValue(),
531       StandardCharsets.UTF_8));
532   }
533 
534   @Test
535   public void testCustomFilter() throws IOException, JAXBException {
536     StringBuilder builder = new StringBuilder();
537     builder.append("/a*");
538     builder.append("?");
539     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
540     builder.append("&");
541     builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("CustomFilter('abc')", "UTF-8"));
542     Response response =
543         client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
544     assertEquals(200, response.getCode());
545     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
546     Unmarshaller ush = ctx.createUnmarshaller();
547     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
548     int count = TestScannerResource.countCellSet(model);
549     assertEquals(1, count);
550     assertEquals("abc", new String(model.getRows().get(0).getCells().get(0).getValue(),
551       StandardCharsets.UTF_8));
552   }
553   
554   @Test
555   public void testNegativeCustomFilter() throws IOException, JAXBException {
556     StringBuilder builder = new StringBuilder();
557     builder.append("/b*");
558     builder.append("?");
559     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
560     builder.append("&");
561     builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("CustomFilter('abc')", "UTF-8"));
562     Response response =
563         client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
564     assertEquals(200, response.getCode());
565     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
566     Unmarshaller ush = ctx.createUnmarshaller();
567     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
568     int count = TestScannerResource.countCellSet(model);
569     // Should return no rows as the filters conflict
570     assertEquals(0, count);
571   }
572 
573   @Test
574   public void testReversed() throws IOException, JAXBException {
575     StringBuilder builder = new StringBuilder();
576     builder.append("/*");
577     builder.append("?");
578     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
579     builder.append("&");
580     builder.append(Constants.SCAN_START_ROW + "=aaa");
581     builder.append("&");
582     builder.append(Constants.SCAN_END_ROW + "=aay");
583     Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
584     assertEquals(200, response.getCode());
585     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
586     Unmarshaller ush = ctx.createUnmarshaller();
587     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
588     int count = TestScannerResource.countCellSet(model);
589     assertEquals(24, count);
590     List<RowModel> rowModels = model.getRows().subList(1, count);
591 
592     //reversed
593     builder = new StringBuilder();
594     builder.append("/*");
595     builder.append("?");
596     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
597     builder.append("&");
598     builder.append(Constants.SCAN_START_ROW + "=aay");
599     builder.append("&");
600     builder.append(Constants.SCAN_END_ROW + "=aaa");
601     builder.append("&");
602     builder.append(Constants.SCAN_REVERSED + "=true");
603     response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
604     assertEquals(200, response.getCode());
605     model = (CellSetModel) ush.unmarshal(response.getStream());
606     count = TestScannerResource.countCellSet(model);
607     assertEquals(24, count);
608     List<RowModel> reversedRowModels = model.getRows().subList(1, count);
609 
610     Collections.reverse(reversedRowModels);
611     assertEquals(rowModels.size(), reversedRowModels.size());
612     for (int i = 0; i < rowModels.size(); i++) {
613       RowModel rowModel = rowModels.get(i);
614       RowModel reversedRowModel = reversedRowModels.get(i);
615 
616       assertEquals(new String(rowModel.getKey(), StandardCharsets.UTF_8),
617           new String(reversedRowModel.getKey(), StandardCharsets.UTF_8));
618       assertEquals(new String(rowModel.getCells().get(0).getValue(), StandardCharsets.UTF_8),
619           new String(reversedRowModel.getCells().get(0).getValue(), StandardCharsets.UTF_8));
620     }
621   }
622 
623   public static class CustomFilter extends PrefixFilter {
624     private byte[] key = null;
625 
626     public CustomFilter(byte[] key) {
627       super(key);
628     }
629     
630     @Override
631     public boolean filterRowKey(byte[] buffer, int offset, int length) {
632       int cmp = Bytes.compareTo(buffer, offset, length, this.key, 0, this.key.length);
633       return cmp != 0;
634     }
635 
636     public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
637       byte[] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
638       return new CustomFilter(prefix);
639     }
640   }
641 
642   /**
643    * The Class ClientSideCellSetModel which mimics cell set model, and contains listener to perform
644    * user defined operations on the row model.
645    */
646   @XmlRootElement(name = "CellSet")
647   @XmlAccessorType(XmlAccessType.FIELD)
648   public static class ClientSideCellSetModel implements Serializable {
649 
650     private static final long serialVersionUID = 1L;
651 
652     /**
653      * This list is not a real list; instead it will notify a listener whenever JAXB has
654      * unmarshalled the next row.
655      */
656     @XmlElement(name="Row")
657     private List<RowModel> row;
658 
659     static boolean listenerInvoked = false;
660 
661     /**
662      * Install a listener for row model on this object. If l is null, the listener
663      * is removed again.
664      */
665     public void setCellSetModelListener(final Listener l) {
666         row = (l == null) ? null : new ArrayList<RowModel>() {
667         private static final long serialVersionUID = 1L;
668             @Override
669             public boolean add(RowModel o) {
670                 l.handleRowModel(ClientSideCellSetModel.this, o);
671                 listenerInvoked = true;
672                 return false;
673             }
674         };
675     }
676 
677     /**
678      * This listener is invoked every time a new row model is unmarshalled.
679      */
680     public static interface Listener {
681         void handleRowModel(ClientSideCellSetModel helper, RowModel rowModel);
682     }
683   }
684 }
685 
686 
687