View Javadoc

1   /*
2    *
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
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
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.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.IOException;
23  
24  import javax.ws.rs.GET;
25  import javax.ws.rs.Path;
26  import javax.ws.rs.PathParam;
27  import javax.ws.rs.Produces;
28  import javax.ws.rs.core.CacheControl;
29  import javax.ws.rs.core.Context;
30  import javax.ws.rs.core.Response;
31  import javax.ws.rs.core.UriInfo;
32  import javax.ws.rs.core.Response.ResponseBuilder;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import org.apache.hadoop.hbase.classification.InterfaceAudience;
38  import org.apache.hadoop.hbase.TableName;
39  import org.apache.hadoop.hbase.rest.model.TableListModel;
40  import org.apache.hadoop.hbase.rest.model.TableModel;
41  
42  @Path("/")
43  @InterfaceAudience.Private
44  public class RootResource extends ResourceBase {
45    private static final Log LOG = LogFactory.getLog(RootResource.class);
46  
47    static CacheControl cacheControl;
48    static {
49      cacheControl = new CacheControl();
50      cacheControl.setNoCache(true);
51      cacheControl.setNoTransform(false);
52    }
53  
54    /**
55     * Constructor
56     * @throws IOException
57     */
58    public RootResource() throws IOException {
59      super();
60    }
61  
62    private final TableListModel getTableList() throws IOException {
63      TableListModel tableList = new TableListModel();
64      TableName[] tableNames = servlet.getAdmin().listTableNames();
65      for (TableName name: tableNames) {
66        tableList.add(new TableModel(name.getNameAsString()));
67      }
68      return tableList;
69    }
70  
71    @GET
72    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
73      MIMETYPE_PROTOBUF_IETF})
74    public Response get(final @Context UriInfo uriInfo) {
75      if (LOG.isTraceEnabled()) {
76        LOG.trace("GET " + uriInfo.getAbsolutePath());
77      }
78      servlet.getMetrics().incrementRequests(1);
79      try {
80        ResponseBuilder response = Response.ok(getTableList());
81        response.cacheControl(cacheControl);
82        servlet.getMetrics().incrementSucessfulGetRequests(1);
83        return response.build();
84      } catch (Exception e) {
85        servlet.getMetrics().incrementFailedGetRequests(1);
86        return processException(e);
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 }