View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.IOException;
23  import java.net.URI;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.ws.rs.Consumes;
29  import javax.ws.rs.POST;
30  import javax.ws.rs.PUT;
31  import javax.ws.rs.Path;
32  import javax.ws.rs.PathParam;
33  import javax.ws.rs.core.Context;
34  import javax.ws.rs.core.MultivaluedMap;
35  import javax.ws.rs.core.Response;
36  import javax.ws.rs.core.UriBuilder;
37  import javax.ws.rs.core.UriInfo;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  import org.apache.hadoop.hbase.classification.InterfaceAudience;
43  import org.apache.hadoop.hbase.TableNotFoundException;
44  import org.apache.hadoop.hbase.filter.Filter;
45  import org.apache.hadoop.hbase.rest.model.ScannerModel;
46  
47  @InterfaceAudience.Private
48  public class ScannerResource extends ResourceBase {
49  
50    private static final Log LOG = LogFactory.getLog(ScannerResource.class);
51  
52    static final Map<String,ScannerInstanceResource> scanners =
53     Collections.synchronizedMap(new HashMap<String,ScannerInstanceResource>());
54  
55    TableResource tableResource;
56  
57    /**
58     * Constructor
59     * @param tableResource
60     * @throws IOException
61     */
62    public ScannerResource(TableResource tableResource)throws IOException {
63      super();
64      this.tableResource = tableResource;
65    }
66  
67    static boolean delete(final String id) {
68      ScannerInstanceResource instance = scanners.remove(id);
69      if (instance != null) {
70        instance.generator.close();
71        return true;
72      } else {
73        return false;
74      }
75    }
76  
77    Response update(final ScannerModel model, final boolean replace,
78        final UriInfo uriInfo) {
79      servlet.getMetrics().incrementRequests(1);
80      if (servlet.isReadOnly()) {
81        return Response.status(Response.Status.FORBIDDEN)
82          .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF)
83          .build();
84      }
85      byte[] endRow = model.hasEndRow() ? model.getEndRow() : null;
86      RowSpec spec = null;
87      if (model.getLabels() != null) {
88        spec = new RowSpec(model.getStartRow(), endRow, model.getColumns(), model.getStartTime(),
89            model.getEndTime(), model.getMaxVersions(), model.getLabels());
90      } else {
91        spec = new RowSpec(model.getStartRow(), endRow, model.getColumns(), model.getStartTime(),
92            model.getEndTime(), model.getMaxVersions());
93      }
94
95      try {
96        Filter filter = ScannerResultGenerator.buildFilterFromModel(model);
97        String tableName = tableResource.getName();
98        ScannerResultGenerator gen =
99          new ScannerResultGenerator(tableName, spec, filter, model.getCaching(),
100           model.getCacheBlocks());
101       String id = gen.getID();
102       ScannerInstanceResource instance =
103         new ScannerInstanceResource(tableName, id, gen, model.getBatch());
104       scanners.put(id, instance);
105       if (LOG.isDebugEnabled()) {
106         LOG.debug("new scanner: " + id);
107       }
108       UriBuilder builder = uriInfo.getAbsolutePathBuilder();
109       URI uri = builder.path(id).build();
110       servlet.getMetrics().incrementSucessfulPutRequests(1);
111       return Response.created(uri).build();
112     } catch (Exception e) {
113       servlet.getMetrics().incrementFailedPutRequests(1);
114       if (e instanceof TableNotFoundException) {
115         return Response.status(Response.Status.NOT_FOUND)
116           .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
117           .build();
118       } else if (e instanceof RuntimeException) {
119         return Response.status(Response.Status.BAD_REQUEST)
120           .type(MIMETYPE_TEXT).entity("Bad request" + CRLF)
121           .build();
122       }
123       return Response.status(Response.Status.SERVICE_UNAVAILABLE)
124         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
125         .build();
126     }
127   }
128
129   @PUT
130   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
131     MIMETYPE_PROTOBUF_IETF})
132   public Response put(final ScannerModel model,
133       final @Context UriInfo uriInfo) {
134     if (LOG.isDebugEnabled()) {
135       LOG.debug("PUT " + uriInfo.getAbsolutePath());
136     }
137     return update(model, true, uriInfo);
138   }
139
140   @POST
141   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
142     MIMETYPE_PROTOBUF_IETF})
143   public Response post(final ScannerModel model,
144       final @Context UriInfo uriInfo) {
145     if (LOG.isDebugEnabled()) {
146       LOG.debug("POST " + uriInfo.getAbsolutePath());
147     }
148     return update(model, false, uriInfo);
149   }
150
151   @Path("{scanner: .+}")
152   public ScannerInstanceResource getScannerInstanceResource(
153       final @PathParam("scanner") String id) throws IOException {
154     ScannerInstanceResource instance = scanners.get(id);
155     if (instance == null) {
156       servlet.getMetrics().incrementFailedGetRequests(1);
157       return new ScannerInstanceResource();
158     } else {
159       servlet.getMetrics().incrementSucessfulGetRequests(1);
160     }
161     return instance;
162   }
163 }