1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rest;
22
23 import java.io.IOException;
24
25 import javax.ws.rs.GET;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.PathParam;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.core.CacheControl;
30 import javax.ws.rs.core.Context;
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.UriInfo;
33 import javax.ws.rs.core.Response.ResponseBuilder;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 import org.apache.hadoop.hbase.HTableDescriptor;
39 import org.apache.hadoop.hbase.rest.model.TableListModel;
40 import org.apache.hadoop.hbase.rest.model.TableModel;
41
42 @Path("/")
43 public class RootResource extends ResourceBase {
44 private static final Log LOG = LogFactory.getLog(RootResource.class);
45
46 static CacheControl cacheControl;
47 static {
48 cacheControl = new CacheControl();
49 cacheControl.setNoCache(true);
50 cacheControl.setNoTransform(false);
51 }
52
53
54
55
56
57 public RootResource() throws IOException {
58 super();
59 }
60
61 private final TableListModel getTableList() throws IOException {
62 TableListModel tableList = new TableListModel();
63 HTableDescriptor[] list = servlet.getAdmin().listTables();
64 for (HTableDescriptor htd: list) {
65 tableList.add(new TableModel(htd.getNameAsString()));
66 }
67 return tableList;
68 }
69
70 @GET
71 @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
72 MIMETYPE_PROTOBUF_IETF})
73 public Response get(final @Context UriInfo uriInfo) {
74 if (LOG.isDebugEnabled()) {
75 LOG.debug("GET " + uriInfo.getAbsolutePath());
76 }
77 servlet.getMetrics().incrementRequests(1);
78 try {
79 ResponseBuilder response = Response.ok(getTableList());
80 response.cacheControl(cacheControl);
81 servlet.getMetrics().incrementSucessfulGetRequests(1);
82 return response.build();
83 } catch (IOException e) {
84 servlet.getMetrics().incrementFailedGetRequests(1);
85 return Response.status(Response.Status.SERVICE_UNAVAILABLE)
86 .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
87 .build();
88 }
89 }
90
91 @Path("status/cluster")
92 public StorageClusterStatusResource getClusterStatusResource()
93 throws IOException {
94 return new StorageClusterStatusResource();
95 }
96
97 @Path("version")
98 public VersionResource getVersionResource() throws IOException {
99 return new VersionResource();
100 }
101
102 @Path("{table}")
103 public TableResource getTableResource(
104 final @PathParam("table") String table) throws IOException {
105 return new TableResource(table);
106 }
107 }