001/* 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.apache.hadoop.hbase.rest; 021 022import java.io.IOException; 023 024import javax.ws.rs.GET; 025import javax.ws.rs.Path; 026import javax.ws.rs.PathParam; 027import javax.ws.rs.Produces; 028import javax.ws.rs.core.CacheControl; 029import javax.ws.rs.core.Context; 030import javax.ws.rs.core.Response; 031import javax.ws.rs.core.UriInfo; 032import javax.ws.rs.core.Response.ResponseBuilder; 033 034import org.apache.yetus.audience.InterfaceAudience; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037import org.apache.hadoop.hbase.TableName; 038import org.apache.hadoop.hbase.rest.model.TableListModel; 039import org.apache.hadoop.hbase.rest.model.TableModel; 040 041@Path("/") 042@InterfaceAudience.Private 043public class RootResource extends ResourceBase { 044 private static final Logger LOG = LoggerFactory.getLogger(RootResource.class); 045 046 static CacheControl cacheControl; 047 static { 048 cacheControl = new CacheControl(); 049 cacheControl.setNoCache(true); 050 cacheControl.setNoTransform(false); 051 } 052 053 /** 054 * Constructor 055 * @throws IOException 056 */ 057 public RootResource() throws IOException { 058 super(); 059 } 060 061 private final TableListModel getTableList() throws IOException { 062 TableListModel tableList = new TableListModel(); 063 TableName[] tableNames = servlet.getAdmin().listTableNames(); 064 for (TableName name: tableNames) { 065 tableList.add(new TableModel(name.getNameAsString())); 066 } 067 return tableList; 068 } 069 070 @GET 071 @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, 072 MIMETYPE_PROTOBUF_IETF}) 073 public Response get(final @Context UriInfo uriInfo) { 074 if (LOG.isTraceEnabled()) { 075 LOG.trace("GET " + uriInfo.getAbsolutePath()); 076 } 077 servlet.getMetrics().incrementRequests(1); 078 try { 079 ResponseBuilder response = Response.ok(getTableList()); 080 response.cacheControl(cacheControl); 081 servlet.getMetrics().incrementSucessfulGetRequests(1); 082 return response.build(); 083 } catch (Exception e) { 084 servlet.getMetrics().incrementFailedGetRequests(1); 085 return processException(e); 086 } 087 } 088 089 @Path("status/cluster") 090 public StorageClusterStatusResource getClusterStatusResource() 091 throws IOException { 092 return new StorageClusterStatusResource(); 093 } 094 095 @Path("version") 096 public VersionResource getVersionResource() throws IOException { 097 return new VersionResource(); 098 } 099 100 @Path("{table}") 101 public TableResource getTableResource( 102 final @PathParam("table") String table) throws IOException { 103 return new TableResource(table); 104 } 105 106 @Path("namespaces") 107 public NamespacesResource getNamespaceResource() throws IOException { 108 return new NamespacesResource(); 109 } 110}