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