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.Produces;
028import javax.ws.rs.core.CacheControl;
029import javax.ws.rs.core.Context;
030import javax.ws.rs.core.Response;
031import javax.ws.rs.core.UriInfo;
032import javax.ws.rs.core.Response.ResponseBuilder;
033
034import org.apache.yetus.audience.InterfaceAudience;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037import org.apache.hadoop.hbase.rest.model.VersionModel;
038
039/**
040 * Implements REST software version reporting
041 * <p>
042 * <tt>/version/rest</tt>
043 * <p>
044 * <tt>/version</tt> (alias for <tt>/version/rest</tt>)
045 */
046@InterfaceAudience.Private
047public class VersionResource extends ResourceBase {
048
049  private static final Logger LOG = LoggerFactory.getLogger(VersionResource.class);
050
051  static CacheControl cacheControl;
052  static {
053    cacheControl = new CacheControl();
054    cacheControl.setNoCache(true);
055    cacheControl.setNoTransform(false);
056  }
057
058  /**
059   * Constructor
060   * @throws IOException
061   */
062  public VersionResource() throws IOException {
063    super();
064  }
065
066  /**
067   * Build a response for a version request.
068   * @param context servlet context
069   * @param uriInfo (JAX-RS context variable) request URL
070   * @return a response for a version request
071   */
072  @GET
073  @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
074    MIMETYPE_PROTOBUF_IETF})
075  public Response get(final @Context ServletContext context,
076      final @Context UriInfo uriInfo) {
077    if (LOG.isTraceEnabled()) {
078      LOG.trace("GET " + uriInfo.getAbsolutePath());
079    }
080    servlet.getMetrics().incrementRequests(1);
081    ResponseBuilder response = Response.ok(new VersionModel(context));
082    response.cacheControl(cacheControl);
083    servlet.getMetrics().incrementSucessfulGetRequests(1);
084    return response.build();
085  }
086
087  /**
088   * Dispatch to StorageClusterVersionResource
089   */
090  @Path("cluster")
091  public StorageClusterVersionResource getClusterVersionResource()
092      throws IOException {
093    return new StorageClusterVersionResource();
094  }
095
096  /**
097   * Dispatch <tt>/version/rest</tt> to self.
098   */
099  @Path("rest")
100  public VersionResource getVersionResource() {
101    return this;
102  }
103}