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.WebApplicationException;
30 import javax.ws.rs.core.CacheControl;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.Response;
33 import javax.ws.rs.core.UriInfo;
34 import javax.ws.rs.core.Response.ResponseBuilder;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import org.apache.hadoop.hbase.HTableDescriptor;
40 import org.apache.hadoop.hbase.client.HBaseAdmin;
41 import org.apache.hadoop.hbase.rest.model.TableListModel;
42 import org.apache.hadoop.hbase.rest.model.TableModel;
43
44 @Path("/")
45 public class RootResource extends ResourceBase {
46 private static final Log LOG = LogFactory.getLog(RootResource.class);
47
48 static CacheControl cacheControl;
49 static {
50 cacheControl = new CacheControl();
51 cacheControl.setNoCache(true);
52 cacheControl.setNoTransform(false);
53 }
54
55
56
57
58
59 public RootResource() throws IOException {
60 super();
61 }
62
63 private final TableListModel getTableList() throws IOException {
64 TableListModel tableList = new TableListModel();
65 HBaseAdmin admin = new HBaseAdmin(servlet.getConfiguration());
66 HTableDescriptor[] list = admin.listTables();
67 for (HTableDescriptor htd: list) {
68 tableList.add(new TableModel(htd.getNameAsString()));
69 }
70 return tableList;
71 }
72
73 @GET
74 @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF})
75 public Response get(final @Context UriInfo uriInfo) {
76 if (LOG.isDebugEnabled()) {
77 LOG.debug("GET " + uriInfo.getAbsolutePath());
78 }
79 servlet.getMetrics().incrementRequests(1);
80 try {
81 ResponseBuilder response = Response.ok(getTableList());
82 response.cacheControl(cacheControl);
83 return response.build();
84 } catch (IOException e) {
85 throw new WebApplicationException(e,
86 Response.Status.SERVICE_UNAVAILABLE);
87 }
88 }
89
90 @Path("status/cluster")
91 public StorageClusterStatusResource getClusterStatusResource()
92 throws IOException {
93 return new StorageClusterStatusResource();
94 }
95
96 @Path("version")
97 public VersionResource getVersionResource() throws IOException {
98 return new VersionResource();
99 }
100
101 @Path("{table}")
102 public TableResource getTableResource(
103 final @PathParam("table") String table) throws IOException {
104 return new TableResource(table);
105 }
106 }