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.ws.rs.GET;
25  import javax.ws.rs.Produces;
26  import javax.ws.rs.core.CacheControl;
27  import javax.ws.rs.core.Context;
28  import javax.ws.rs.core.Response;
29  import javax.ws.rs.core.UriInfo;
30  import javax.ws.rs.core.Response.ResponseBuilder;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import org.apache.hadoop.hbase.classification.InterfaceAudience;
36  import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel;
37  
38  @InterfaceAudience.Private
39  public class StorageClusterVersionResource extends ResourceBase {
40    private static final Log LOG =
41      LogFactory.getLog(StorageClusterVersionResource.class);
42  
43    static CacheControl cacheControl;
44    static {
45      cacheControl = new CacheControl();
46      cacheControl.setNoCache(true);
47      cacheControl.setNoTransform(false);
48    }
49  
50    /**
51     * Constructor
52     * @throws IOException
53     */
54    public StorageClusterVersionResource() throws IOException {
55      super();
56    }
57  
58    @GET
59    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON})
60    public Response get(final @Context UriInfo uriInfo) {
61      if (LOG.isDebugEnabled()) {
62        LOG.debug("GET " + uriInfo.getAbsolutePath());
63      }
64      servlet.getMetrics().incrementRequests(1);
65      try {
66        StorageClusterVersionModel model = new StorageClusterVersionModel();
67        model.setVersion(servlet.getAdmin().getClusterStatus().getHBaseVersion());
68        ResponseBuilder response = Response.ok(model);
69        response.cacheControl(cacheControl);
70        servlet.getMetrics().incrementSucessfulGetRequests(1);
71        return response.build();
72      } catch (IOException e) {
73        servlet.getMetrics().incrementFailedGetRequests(1);
74        return Response.status(Response.Status.SERVICE_UNAVAILABLE)
75          .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
76          .build();
77      }
78    }
79  }