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.servlet.ServletContext;
025import javax.ws.rs.GET;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.Produces;
029import javax.ws.rs.core.Context;
030import javax.ws.rs.core.Response;
031import javax.ws.rs.core.UriInfo;
032
033import org.apache.yetus.audience.InterfaceAudience;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036import org.apache.hadoop.hbase.rest.model.NamespacesModel;
037
038/**
039 * Implements REST GET list of all namespaces.
040 * <p>
041 * <tt>/namespaces</tt>
042 * <p>
043 */
044@InterfaceAudience.Private
045public class NamespacesResource extends ResourceBase {
046
047  private static final Logger LOG = LoggerFactory.getLogger(NamespacesResource.class);
048
049  /**
050   * Constructor
051   * @throws IOException
052   */
053  public NamespacesResource() throws IOException {
054    super();
055  }
056
057  /**
058   * Build a response for a list of all namespaces request.
059   * @param context servlet context
060   * @param uriInfo (JAX-RS context variable) request URL
061   * @return a response for a version request
062   */
063  @GET
064  @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
065    MIMETYPE_PROTOBUF_IETF})
066  public Response get(final @Context ServletContext context, final @Context UriInfo uriInfo) {
067    if (LOG.isTraceEnabled()) {
068      LOG.trace("GET " + uriInfo.getAbsolutePath());
069    }
070    servlet.getMetrics().incrementRequests(1);
071    try {
072      NamespacesModel rowModel = null;
073      rowModel = new NamespacesModel(servlet.getAdmin());
074      servlet.getMetrics().incrementSucessfulGetRequests(1);
075      return Response.ok(rowModel).build();
076    } catch (IOException e) {
077      servlet.getMetrics().incrementFailedGetRequests(1);
078      throw new RuntimeException("Cannot retrieve list of namespaces.");
079    }
080  }
081
082  /**
083   * Dispatch to NamespaceInstanceResource
084   */
085  @Path("{namespace}")
086  public NamespacesInstanceResource getNamespaceInstanceResource(
087      final @PathParam("namespace") String namespace) throws IOException {
088    return new NamespacesInstanceResource(namespace);
089  }
090}