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 java.io.IOException;
021import java.util.ArrayList;
022import java.util.Base64;
023import java.util.Base64.Decoder;
024import java.util.List;
025import org.apache.hadoop.hbase.client.Result;
026import org.apache.hadoop.hbase.filter.Filter;
027import org.apache.hadoop.hbase.filter.ParseFilter;
028import org.apache.hadoop.hbase.rest.model.CellSetModel;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import org.apache.hbase.thirdparty.javax.ws.rs.Encoded;
035import org.apache.hbase.thirdparty.javax.ws.rs.GET;
036import org.apache.hbase.thirdparty.javax.ws.rs.HeaderParam;
037import org.apache.hbase.thirdparty.javax.ws.rs.Produces;
038import org.apache.hbase.thirdparty.javax.ws.rs.QueryParam;
039import org.apache.hbase.thirdparty.javax.ws.rs.core.Context;
040import org.apache.hbase.thirdparty.javax.ws.rs.core.MultivaluedMap;
041import org.apache.hbase.thirdparty.javax.ws.rs.core.Response;
042import org.apache.hbase.thirdparty.javax.ws.rs.core.UriInfo;
043
044@InterfaceAudience.Private
045public class MultiRowResource extends ResourceBase implements Constants {
046  private static final Logger LOG = LoggerFactory.getLogger(MultiRowResource.class);
047
048  private static final Decoder base64Urldecoder = Base64.getUrlDecoder();
049
050  TableResource tableResource;
051  Integer versions = null;
052  String[] columns = null;
053
054  /**
055   * Constructor
056   */
057  public MultiRowResource(TableResource tableResource, String versions, String columnsStr)
058    throws IOException {
059    super();
060    this.tableResource = tableResource;
061
062    if (columnsStr != null && !columnsStr.equals("")) {
063      this.columns = columnsStr.split(",");
064    }
065
066    if (versions != null) {
067      this.versions = Integer.valueOf(versions);
068
069    }
070  }
071
072  @GET
073  @Produces({ MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF })
074  public Response get(final @Context UriInfo uriInfo,
075    final @HeaderParam("Encoding") String keyEncodingHeader,
076    @QueryParam(Constants.FILTER_B64) @Encoded String paramFilterB64,
077    @QueryParam(Constants.FILTER) String paramFilter) {
078    MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
079    String keyEncoding = (keyEncodingHeader != null)
080      ? keyEncodingHeader
081      : params.getFirst(KEY_ENCODING_QUERY_PARAM_NAME);
082
083    servlet.getMetrics().incrementRequests(1);
084
085    byte[] filterBytes = null;
086    if (paramFilterB64 != null) {
087      filterBytes = base64Urldecoder.decode(paramFilterB64);
088    } else if (paramFilter != null) {
089      filterBytes = paramFilter.getBytes();
090    }
091
092    try {
093      Filter parsedParamFilter = null;
094      if (filterBytes != null) {
095        // Note that this is a completely different representation of the filters
096        // than the JSON one used in the /table/scanner endpoint
097        ParseFilter pf = new ParseFilter();
098        parsedParamFilter = pf.parseFilterString(filterBytes);
099      }
100      List<RowSpec> rowSpecs = new ArrayList<>();
101      if (params.containsKey(ROW_KEYS_PARAM_NAME)) {
102        for (String rk : params.get(ROW_KEYS_PARAM_NAME)) {
103          RowSpec rowSpec = new RowSpec(rk, keyEncoding);
104
105          if (this.versions != null) {
106            rowSpec.setMaxVersions(this.versions);
107          }
108
109          if (this.columns != null) {
110            for (int i = 0; i < this.columns.length; i++) {
111              rowSpec.addColumn(Bytes.toBytes(this.columns[i]));
112            }
113          }
114          rowSpecs.add(rowSpec);
115        }
116      }
117
118      MultiRowResultReader reader = new MultiRowResultReader(this.tableResource.getName(), rowSpecs,
119        parsedParamFilter, !params.containsKey(NOCACHE_PARAM_NAME));
120
121      CellSetModel model = new CellSetModel();
122      for (Result r : reader.getResults()) {
123        if (r.isEmpty()) {
124          continue;
125        }
126        model.addRow(RestUtil.createRowModelFromResult(r));
127      }
128      if (model.getRows().isEmpty()) {
129        // If no rows found.
130        servlet.getMetrics().incrementFailedGetRequests(1);
131        return Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT)
132          .entity("No rows found." + CRLF).build();
133      } else {
134        servlet.getMetrics().incrementSucessfulGetRequests(1);
135        return Response.ok(model).build();
136      }
137    } catch (IOException e) {
138      servlet.getMetrics().incrementFailedGetRequests(1);
139      return processException(e);
140    }
141  }
142}