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.servlet.ServletContext;
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.core.Context;
30  import javax.ws.rs.core.Response;
31  import javax.ws.rs.core.UriInfo;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.hadoop.hbase.classification.InterfaceAudience;
36  import org.apache.hadoop.hbase.rest.model.NamespacesModel;
37  
38  /**
39   * Implements REST GET list of all namespaces.
40   * <p>
41   * <tt>/namespaces</tt>
42   * <p>
43   */
44  @InterfaceAudience.Private
45  public class NamespacesResource extends ResourceBase {
46  
47    private static final Log LOG = LogFactory.getLog(NamespacesResource.class);
48  
49    /**
50     * Constructor
51     * @throws IOException
52     */
53    public NamespacesResource() throws IOException {
54      super();
55    }
56  
57    /**
58     * Build a response for a list of all namespaces request.
59     * @param context servlet context
60     * @param uriInfo (JAX-RS context variable) request URL
61     * @return a response for a version request
62     */
63    @GET
64    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
65      MIMETYPE_PROTOBUF_IETF})
66    public Response get(final @Context ServletContext context, final @Context UriInfo uriInfo) {
67      if (LOG.isTraceEnabled()) {
68        LOG.trace("GET " + uriInfo.getAbsolutePath());
69      }
70      servlet.getMetrics().incrementRequests(1);
71      try {
72        NamespacesModel rowModel = null;
73        rowModel = new NamespacesModel(servlet.getAdmin());
74        servlet.getMetrics().incrementSucessfulGetRequests(1);
75        return Response.ok(rowModel).build();
76      } catch (IOException e) {
77        servlet.getMetrics().incrementFailedGetRequests(1);
78        throw new RuntimeException("Cannot retrieve list of namespaces.");
79      }
80    }
81  
82    /**
83     * Dispatch to NamespaceInstanceResource
84     */
85    @Path("{namespace}")
86    public NamespacesInstanceResource getNamespaceInstanceResource(
87        final @PathParam("namespace") String namespace) throws IOException {
88      return new NamespacesInstanceResource(namespace);
89    }
90  }