Revert "HBASE-26523 Upgrade hbase-thirdparty dependency to 4.0.0 (#3910)"
[hbase.git] / hbase-rest / src / main / java / org / apache / hadoop / hbase / rest / RegionsResource.java
blob6d6293fb164729b1951e88eced7794012c08ac59
1 /*
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;
23 import java.util.List;
24 import javax.ws.rs.GET;
25 import javax.ws.rs.Produces;
26 import javax.ws.rs.core.CacheControl;
27 import javax.ws.rs.core.Context;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.ResponseBuilder;
30 import javax.ws.rs.core.UriInfo;
31 import org.apache.hadoop.hbase.HRegionLocation;
32 import org.apache.hadoop.hbase.ServerName;
33 import org.apache.hadoop.hbase.TableName;
34 import org.apache.hadoop.hbase.TableNotFoundException;
35 import org.apache.hadoop.hbase.client.Connection;
36 import org.apache.hadoop.hbase.client.ConnectionFactory;
37 import org.apache.hadoop.hbase.client.RegionInfo;
38 import org.apache.hadoop.hbase.client.RegionLocator;
39 import org.apache.hadoop.hbase.rest.model.TableInfoModel;
40 import org.apache.hadoop.hbase.rest.model.TableRegionModel;
41 import org.apache.yetus.audience.InterfaceAudience;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 @InterfaceAudience.Private
46 public class RegionsResource extends ResourceBase {
47 private static final Logger LOG = LoggerFactory.getLogger(RegionsResource.class);
49 static CacheControl cacheControl;
50 static {
51 cacheControl = new CacheControl();
52 cacheControl.setNoCache(true);
53 cacheControl.setNoTransform(false);
56 TableResource tableResource;
58 /**
59 * Constructor
60 * @param tableResource
61 * @throws IOException
63 public RegionsResource(TableResource tableResource) throws IOException {
64 super();
65 this.tableResource = tableResource;
68 @GET
69 @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
70 MIMETYPE_PROTOBUF_IETF})
71 public Response get(final @Context UriInfo uriInfo) {
72 if (LOG.isTraceEnabled()) {
73 LOG.trace("GET " + uriInfo.getAbsolutePath());
75 servlet.getMetrics().incrementRequests(1);
76 try {
77 TableName tableName = TableName.valueOf(tableResource.getName());
78 if (!tableResource.exists()) {
79 throw new TableNotFoundException(tableName);
81 TableInfoModel model = new TableInfoModel(tableName.getNameAsString());
83 List<HRegionLocation> locs;
84 try (Connection connection = ConnectionFactory.createConnection(servlet.getConfiguration());
85 RegionLocator locator = connection.getRegionLocator(tableName)) {
86 locs = locator.getAllRegionLocations();
88 for (HRegionLocation loc : locs) {
89 RegionInfo hri = loc.getRegion();
90 ServerName addr = loc.getServerName();
91 model.add(new TableRegionModel(tableName.getNameAsString(), hri.getRegionId(),
92 hri.getStartKey(), hri.getEndKey(), addr.getAddress().toString()));
94 ResponseBuilder response = Response.ok(model);
95 response.cacheControl(cacheControl);
96 servlet.getMetrics().incrementSucessfulGetRequests(1);
97 return response.build();
98 } catch (TableNotFoundException e) {
99 servlet.getMetrics().incrementFailedGetRequests(1);
100 return Response.status(Response.Status.NOT_FOUND)
101 .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
102 .build();
103 } catch (IOException e) {
104 servlet.getMetrics().incrementFailedGetRequests(1);
105 return Response.status(Response.Status.SERVICE_UNAVAILABLE)
106 .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
107 .build();