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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 package org
.apache
.hadoop
.hbase
.rest
;
22 import java
.io
.IOException
;
24 import java
.util
.Collections
;
25 import java
.util
.HashMap
;
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
;
38 import org
.apache
.yetus
.audience
.InterfaceAudience
;
39 import org
.slf4j
.Logger
;
40 import org
.slf4j
.LoggerFactory
;
42 import com
.fasterxml
.jackson
.core
.JsonParseException
;
43 import com
.fasterxml
.jackson
.databind
.JsonMappingException
;
45 import org
.apache
.hadoop
.hbase
.TableNotFoundException
;
46 import org
.apache
.hadoop
.hbase
.filter
.Filter
;
47 import org
.apache
.hadoop
.hbase
.rest
.model
.ScannerModel
;
49 @InterfaceAudience.Private
50 public class ScannerResource
extends ResourceBase
{
52 private static final Logger LOG
= LoggerFactory
.getLogger(ScannerResource
.class);
54 static final Map
<String
,ScannerInstanceResource
> scanners
=
55 Collections
.synchronizedMap(new HashMap
<String
,ScannerInstanceResource
>());
57 TableResource tableResource
;
61 * @param tableResource
64 public ScannerResource(TableResource tableResource
)throws IOException
{
66 this.tableResource
= tableResource
;
69 static boolean delete(final String id
) {
70 ScannerInstanceResource instance
= scanners
.remove(id
);
71 if (instance
!= null) {
72 instance
.generator
.close();
79 Response
update(final ScannerModel model
, final boolean replace
,
80 final UriInfo uriInfo
) {
81 servlet
.getMetrics().incrementRequests(1);
82 if (servlet
.isReadOnly()) {
83 return Response
.status(Response
.Status
.FORBIDDEN
)
84 .type(MIMETYPE_TEXT
).entity("Forbidden" + CRLF
)
87 byte[] endRow
= model
.hasEndRow() ? model
.getEndRow() : null;
89 if (model
.getLabels() != null) {
90 spec
= new RowSpec(model
.getStartRow(), endRow
, model
.getColumns(), model
.getStartTime(),
91 model
.getEndTime(), model
.getMaxVersions(), model
.getLabels());
93 spec
= new RowSpec(model
.getStartRow(), endRow
, model
.getColumns(), model
.getStartTime(),
94 model
.getEndTime(), model
.getMaxVersions());
98 Filter filter
= ScannerResultGenerator
.buildFilterFromModel(model
);
99 String tableName
= tableResource
.getName();
100 ScannerResultGenerator gen
=
101 new ScannerResultGenerator(tableName
, spec
, filter
, model
.getCaching(),
102 model
.getCacheBlocks(), model
.getLimit());
103 String id
= gen
.getID();
104 ScannerInstanceResource instance
=
105 new ScannerInstanceResource(tableName
, id
, gen
, model
.getBatch());
106 scanners
.put(id
, instance
);
107 if (LOG
.isTraceEnabled()) {
108 LOG
.trace("new scanner: " + id
);
110 UriBuilder builder
= uriInfo
.getAbsolutePathBuilder();
111 URI uri
= builder
.path(id
).build();
112 servlet
.getMetrics().incrementSucessfulPutRequests(1);
113 return Response
.created(uri
).build();
114 } catch (Exception e
) {
115 LOG
.error("Exception occurred while processing " + uriInfo
.getAbsolutePath() + " : ", e
);
116 servlet
.getMetrics().incrementFailedPutRequests(1);
117 if (e
instanceof TableNotFoundException
) {
118 return Response
.status(Response
.Status
.NOT_FOUND
)
119 .type(MIMETYPE_TEXT
).entity("Not found" + CRLF
)
121 } else if (e
instanceof RuntimeException
122 || e
instanceof JsonMappingException
| e
instanceof JsonParseException
) {
123 return Response
.status(Response
.Status
.BAD_REQUEST
)
124 .type(MIMETYPE_TEXT
).entity("Bad request" + CRLF
)
127 return Response
.status(Response
.Status
.SERVICE_UNAVAILABLE
)
128 .type(MIMETYPE_TEXT
).entity("Unavailable" + CRLF
)
134 @Consumes({MIMETYPE_XML
, MIMETYPE_JSON
, MIMETYPE_PROTOBUF
,
135 MIMETYPE_PROTOBUF_IETF
})
136 public Response
put(final ScannerModel model
,
137 final @Context UriInfo uriInfo
) {
138 if (LOG
.isTraceEnabled()) {
139 LOG
.trace("PUT " + uriInfo
.getAbsolutePath());
141 return update(model
, true, uriInfo
);
145 @Consumes({MIMETYPE_XML
, MIMETYPE_JSON
, MIMETYPE_PROTOBUF
,
146 MIMETYPE_PROTOBUF_IETF
})
147 public Response
post(final ScannerModel model
,
148 final @Context UriInfo uriInfo
) {
149 if (LOG
.isTraceEnabled()) {
150 LOG
.trace("POST " + uriInfo
.getAbsolutePath());
152 return update(model
, false, uriInfo
);
155 @Path("{scanner: .+}")
156 public ScannerInstanceResource
getScannerInstanceResource(
157 final @PathParam("scanner") String id
) throws IOException
{
158 ScannerInstanceResource instance
= scanners
.get(id
);
159 if (instance
== null) {
160 servlet
.getMetrics().incrementFailedGetRequests(1);
161 return new ScannerInstanceResource();
163 servlet
.getMetrics().incrementSucessfulGetRequests(1);