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 = new RowSpec(model.getStartRow(), endRow,
86        model.getColumns(), model.getStartTime(), model.getEndTime(),
87        model.getMaxVersions());
88      try {
89        Filter filter = ScannerResultGenerator.buildFilterFromModel(model);
90        String tableName = tableResource.getName();
91        ScannerResultGenerator gen =
92          new ScannerResultGenerator(tableName, spec, filter, model.getCaching());
93        String id = gen.getID();
94        ScannerInstanceResource instance =
95          new ScannerInstanceResource(tableName, id, gen, model.getBatch());
96        scanners.put(id, instance);
97        if (LOG.isDebugEnabled()) {
98          LOG.debug("new scanner: " + id);
99        }
100       UriBuilder builder = uriInfo.getAbsolutePathBuilder();
101       URI uri = builder.path(id).build();
102       servlet.getMetrics().incrementSucessfulPutRequests(1);
103       return Response.created(uri).build();
104     } catch (RuntimeException e) {
105       servlet.getMetrics().incrementFailedPutRequests(1);
106       if (e.getCause() instanceof TableNotFoundException) {
107         return Response.status(Response.Status.NOT_FOUND)
108           .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
109           .build();
110       }
111       return Response.status(Response.Status.BAD_REQUEST)
112         .type(MIMETYPE_TEXT).entity("Bad request" + CRLF)
113         .build();
114     } catch (Exception e) {
115       servlet.getMetrics().incrementFailedPutRequests(1);
116       return Response.status(Response.Status.SERVICE_UNAVAILABLE)
117         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
118         .build();
119     }
120   }
121 
122   @PUT
123   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
124     MIMETYPE_PROTOBUF_IETF})
125   public Response put(final ScannerModel model, 
126       final @Context UriInfo uriInfo) {
127     if (LOG.isDebugEnabled()) {
128       LOG.debug("PUT " + uriInfo.getAbsolutePath());
129     }
130     return update(model, true, uriInfo);
131   }
132 
133   @POST
134   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
135     MIMETYPE_PROTOBUF_IETF})
136   public Response post(final ScannerModel model,
137       final @Context UriInfo uriInfo) {
138     if (LOG.isDebugEnabled()) {
139       LOG.debug("POST " + uriInfo.getAbsolutePath());
140     }
141     return update(model, false, uriInfo);
142   }
143 
144   @Path("{scanner: .+}")
145   public ScannerInstanceResource getScannerInstanceResource(
146       final @PathParam("scanner") String id) throws IOException {
147     ScannerInstanceResource instance = scanners.get(id);
148     if (instance == null) {
149       servlet.getMetrics().incrementFailedGetRequests(1);
150       return new ScannerInstanceResource();
151     } else {
152       servlet.getMetrics().incrementSucessfulGetRequests(1);
153     }
154     return instance;
155   }
156 }