View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest;
22  
23  import java.io.IOException;
24  
25  import javax.ws.rs.GET;
26  import javax.ws.rs.Produces;
27  import javax.ws.rs.core.CacheControl;
28  import javax.ws.rs.core.Context;
29  import javax.ws.rs.core.Response;
30  import javax.ws.rs.core.UriInfo;
31  import javax.ws.rs.core.Response.ResponseBuilder;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel;
37  
38  public class StorageClusterVersionResource extends ResourceBase {
39    private static final Log LOG =
40      LogFactory.getLog(StorageClusterVersionResource.class);
41  
42    static CacheControl cacheControl;
43    static {
44      cacheControl = new CacheControl();
45      cacheControl.setNoCache(true);
46      cacheControl.setNoTransform(false);
47    }
48  
49    /**
50     * Constructor
51     * @throws IOException
52     */
53    public StorageClusterVersionResource() throws IOException {
54      super();
55    }
56  
57    @GET
58    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON})
59    public Response get(final @Context UriInfo uriInfo) {
60      if (LOG.isDebugEnabled()) {
61        LOG.debug("GET " + uriInfo.getAbsolutePath());
62      }
63      servlet.getMetrics().incrementRequests(1);
64      try {
65        StorageClusterVersionModel model = new StorageClusterVersionModel();
66        model.setVersion(servlet.getAdmin().getClusterStatus().getHBaseVersion());
67        ResponseBuilder response = Response.ok(model);
68        response.cacheControl(cacheControl);
69        servlet.getMetrics().incrementSucessfulGetRequests(1);
70        return response.build();
71      } catch (IOException e) {
72        servlet.getMetrics().incrementFailedGetRequests(1);
73        return Response.status(Response.Status.SERVICE_UNAVAILABLE)
74          .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
75          .build();
76      }
77    }
78  }