View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest;
22  
23  import java.io.IOException;
24  import java.net.URI;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import javax.ws.rs.Consumes;
30  import javax.ws.rs.POST;
31  import javax.ws.rs.PUT;
32  import javax.ws.rs.Path;
33  import javax.ws.rs.PathParam;
34  import javax.ws.rs.WebApplicationException;
35  import javax.ws.rs.core.Context;
36  import javax.ws.rs.core.Response;
37  import javax.ws.rs.core.UriBuilder;
38  import javax.ws.rs.core.UriInfo;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
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  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 void delete(final String id) {
67      ScannerInstanceResource instance = scanners.remove(id);
68      if (instance != null) {
69        instance.generator.close();
70      }
71    }
72  
73    Response update(final ScannerModel model, final boolean replace,
74        final UriInfo uriInfo) {
75      servlet.getMetrics().incrementRequests(1);
76      if (servlet.isReadOnly()) {
77        throw new WebApplicationException(Response.Status.FORBIDDEN);
78      }
79      byte[] endRow = model.hasEndRow() ? model.getEndRow() : null;
80      RowSpec spec = new RowSpec(model.getStartRow(), endRow,
81        model.getColumns(), model.getStartTime(), model.getEndTime(),
82        model.getMaxVersions());
83      try {
84        Filter filter = ScannerResultGenerator.buildFilterFromModel(model);
85        String tableName = tableResource.getName();
86        ScannerResultGenerator gen =
87          new ScannerResultGenerator(tableName, spec, filter);
88        String id = gen.getID();
89        ScannerInstanceResource instance =
90          new ScannerInstanceResource(tableName, id, gen, model.getBatch());
91        scanners.put(id, instance);
92        if (LOG.isDebugEnabled()) {
93          LOG.debug("new scanner: " + id);
94        }
95        UriBuilder builder = uriInfo.getAbsolutePathBuilder();
96        URI uri = builder.path(id).build();
97        return Response.created(uri).build();
98      } catch (IOException e) {
99        throw new WebApplicationException(e,
100               Response.Status.SERVICE_UNAVAILABLE);
101     } catch (RuntimeException e) {
102       if (e.getCause() instanceof TableNotFoundException) {
103         throw new WebApplicationException(e, Response.Status.NOT_FOUND);
104       }
105       throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
106     } catch (Exception e) {
107       throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
108     }
109   }
110 
111   @PUT
112   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF})
113   public Response put(final ScannerModel model, 
114       final @Context UriInfo uriInfo) {
115     if (LOG.isDebugEnabled()) {
116       LOG.debug("PUT " + uriInfo.getAbsolutePath());
117     }
118     return update(model, true, uriInfo);
119   }
120 
121   @POST
122   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF})
123   public Response post(final ScannerModel model,
124       final @Context UriInfo uriInfo) {
125     if (LOG.isDebugEnabled()) {
126       LOG.debug("POST " + uriInfo.getAbsolutePath());
127     }
128     return update(model, false, uriInfo);
129   }
130 
131   @Path("{scanner: .+}")
132   public ScannerInstanceResource getScannerInstanceResource(
133       final @PathParam("scanner") String id) {
134     ScannerInstanceResource instance = scanners.get(id);
135     if (instance == null) {
136       throw new WebApplicationException(Response.Status.NOT_FOUND);
137     }
138     return instance;
139   }
140 }