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.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.isDebugEnabled()) {
76        LOG.debug("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 (IOException e) {
85        servlet.getMetrics().incrementFailedGetRequests(1);
86        return Response.status(Response.Status.SERVICE_UNAVAILABLE)
87          .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
88          .build();
89      }
90    }
91  
92    @Path("status/cluster")
93    public StorageClusterStatusResource getClusterStatusResource()
94        throws IOException {
95      return new StorageClusterStatusResource();
96    }
97  
98    @Path("version")
99    public VersionResource getVersionResource() throws IOException {
100     return new VersionResource();
101   }
102 
103   @Path("{table}")
104   public TableResource getTableResource(
105       final @PathParam("table") String table) throws IOException {
106     return new TableResource(table);
107   }
108 }