View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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     * Constructor
57     * @throws IOException
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 }