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.Response;
35  import javax.ws.rs.core.UriBuilder;
36  import javax.ws.rs.core.UriInfo;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  import org.apache.hadoop.classification.InterfaceAudience;
42  import org.apache.hadoop.hbase.TableNotFoundException;
43  import org.apache.hadoop.hbase.filter.Filter;
44  import org.apache.hadoop.hbase.rest.model.ScannerModel;
45  
46  @InterfaceAudience.Private
47  public class ScannerResource extends ResourceBase {
48  
49    private static final Log LOG = LogFactory.getLog(ScannerResource.class);
50  
51    static final Map<String,ScannerInstanceResource> scanners =
52     Collections.synchronizedMap(new HashMap<String,ScannerInstanceResource>());
53  
54    TableResource tableResource;
55  
56    /**
57     * Constructor
58     * @param tableResource
59     * @throws IOException
60     */
61    public ScannerResource(TableResource tableResource)throws IOException {
62      super();
63      this.tableResource = tableResource;
64    }
65  
66    static boolean delete(final String id) {
67      ScannerInstanceResource instance = scanners.remove(id);
68      if (instance != null) {
69        instance.generator.close();
70        return true;
71      } else {
72        return false;
73      }
74    }
75  
76    Response update(final ScannerModel model, final boolean replace,
77        final UriInfo uriInfo) {
78      servlet.getMetrics().incrementRequests(1);
79      if (servlet.isReadOnly()) {
80        return Response.status(Response.Status.FORBIDDEN)
81          .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF)
82          .build();
83      }
84      byte[] endRow = model.hasEndRow() ? model.getEndRow() : null;
85      RowSpec spec = null;
86      if (model.getLabels() != null) {
87        spec = new RowSpec(model.getStartRow(), endRow, model.getColumns(), model.getStartTime(),
88            model.getEndTime(), model.getMaxVersions(), model.getLabels());
89      } else {
90        spec = new RowSpec(model.getStartRow(), endRow, model.getColumns(), model.getStartTime(),
91            model.getEndTime(), model.getMaxVersions());
92      }
93      try {
94        Filter filter = ScannerResultGenerator.buildFilterFromModel(model);
95        String tableName = tableResource.getName();
96        ScannerResultGenerator gen =
97          new ScannerResultGenerator(tableName, spec, filter, model.getCaching());
98        String id = gen.getID();
99        ScannerInstanceResource instance =
100         new ScannerInstanceResource(tableName, id, gen, model.getBatch());
101       scanners.put(id, instance);
102       if (LOG.isDebugEnabled()) {
103         LOG.debug("new scanner: " + id);
104       }
105       UriBuilder builder = uriInfo.getAbsolutePathBuilder();
106       URI uri = builder.path(id).build();
107       servlet.getMetrics().incrementSucessfulPutRequests(1);
108       return Response.created(uri).build();
109     } catch (Exception e) {
110       servlet.getMetrics().incrementFailedPutRequests(1);
111       if (e instanceof TableNotFoundException) {
112         return Response.status(Response.Status.NOT_FOUND)
113           .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
114           .build();
115       } else if (e instanceof RuntimeException) {
116         return Response.status(Response.Status.BAD_REQUEST)
117           .type(MIMETYPE_TEXT).entity("Bad request" + CRLF)
118           .build();
119       }
120       return Response.status(Response.Status.SERVICE_UNAVAILABLE)
121         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
122         .build();
123     }
124   }
125 
126   @PUT
127   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
128     MIMETYPE_PROTOBUF_IETF})
129   public Response put(final ScannerModel model, 
130       final @Context UriInfo uriInfo) {
131     if (LOG.isDebugEnabled()) {
132       LOG.debug("PUT " + uriInfo.getAbsolutePath());
133     }
134     return update(model, true, uriInfo);
135   }
136 
137   @POST
138   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
139     MIMETYPE_PROTOBUF_IETF})
140   public Response post(final ScannerModel model,
141       final @Context UriInfo uriInfo) {
142     if (LOG.isDebugEnabled()) {
143       LOG.debug("POST " + uriInfo.getAbsolutePath());
144     }
145     return update(model, false, uriInfo);
146   }
147 
148   @Path("{scanner: .+}")
149   public ScannerInstanceResource getScannerInstanceResource(
150       final @PathParam("scanner") String id) throws IOException {
151     ScannerInstanceResource instance = scanners.get(id);
152     if (instance == null) {
153       servlet.getMetrics().incrementFailedGetRequests(1);
154       return new ScannerInstanceResource();
155     } else {
156       servlet.getMetrics().incrementSucessfulGetRequests(1);
157     }
158     return instance;
159   }
160 }