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;
023import java.util.EnumSet;
024import javax.ws.rs.GET;
025import javax.ws.rs.Produces;
026import javax.ws.rs.core.CacheControl;
027import javax.ws.rs.core.Context;
028import javax.ws.rs.core.Response;
029import javax.ws.rs.core.Response.ResponseBuilder;
030import javax.ws.rs.core.UriInfo;
031import org.apache.hadoop.hbase.ClusterMetrics.Option;
032import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel;
033import org.apache.yetus.audience.InterfaceAudience;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037@InterfaceAudience.Private
038public class StorageClusterVersionResource extends ResourceBase {
039  private static final Logger LOG =
040    LoggerFactory.getLogger(StorageClusterVersionResource.class);
041
042  static CacheControl cacheControl;
043  static {
044    cacheControl = new CacheControl();
045    cacheControl.setNoCache(true);
046    cacheControl.setNoTransform(false);
047  }
048
049  /**
050   * Constructor
051   * @throws IOException
052   */
053  public StorageClusterVersionResource() throws IOException {
054    super();
055  }
056
057  @GET
058  @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON})
059  public Response get(final @Context UriInfo uriInfo) {
060    if (LOG.isTraceEnabled()) {
061      LOG.trace("GET " + uriInfo.getAbsolutePath());
062    }
063    servlet.getMetrics().incrementRequests(1);
064    try {
065      StorageClusterVersionModel model = new StorageClusterVersionModel();
066      model.setVersion(
067        servlet.getAdmin().getClusterMetrics(EnumSet.of(Option.HBASE_VERSION))
068            .getHBaseVersion());
069      ResponseBuilder response = Response.ok(model);
070      response.cacheControl(cacheControl);
071      servlet.getMetrics().incrementSucessfulGetRequests(1);
072      return response.build();
073    } catch (IOException e) {
074      servlet.getMetrics().incrementFailedGetRequests(1);
075      return Response.status(Response.Status.SERVICE_UNAVAILABLE)
076        .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
077        .build();
078    }
079  }
080}