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.servlet.ServletContext;
26  import javax.ws.rs.GET;
27  import javax.ws.rs.Path;
28  import javax.ws.rs.Produces;
29  import javax.ws.rs.core.CacheControl;
30  import javax.ws.rs.core.Context;
31  import javax.ws.rs.core.Response;
32  import javax.ws.rs.core.UriInfo;
33  import javax.ws.rs.core.Response.ResponseBuilder;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  import org.apache.hadoop.hbase.rest.model.VersionModel;
39  
40  /**
41   * Implements REST software version reporting
42   * <p>
43   * <tt>/version/rest</tt>
44   * <p>
45   * <tt>/version</tt> (alias for <tt>/version/rest</tt>)
46   */
47  public class VersionResource extends ResourceBase {
48  
49    private static final Log LOG = LogFactory.getLog(VersionResource.class);
50  
51    static CacheControl cacheControl;
52    static {
53      cacheControl = new CacheControl();
54      cacheControl.setNoCache(true);
55      cacheControl.setNoTransform(false);
56    }
57  
58    /**
59     * Constructor
60     * @throws IOException
61     */
62    public VersionResource() throws IOException {
63      super();
64    }
65  
66    /**
67     * Build a response for a version request.
68     * @param context servlet context
69     * @param uriInfo (JAX-RS context variable) request URL
70     * @return a response for a version request 
71     */
72    @GET
73    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
74      MIMETYPE_PROTOBUF_IETF})
75    public Response get(final @Context ServletContext context, 
76        final @Context UriInfo uriInfo) {
77      if (LOG.isDebugEnabled()) {
78        LOG.debug("GET " + uriInfo.getAbsolutePath());
79      }
80      servlet.getMetrics().incrementRequests(1);
81      ResponseBuilder response = Response.ok(new VersionModel(context));
82      response.cacheControl(cacheControl);
83      servlet.getMetrics().incrementSucessfulGetRequests(1);
84      return response.build();
85    }
86  
87    /**
88     * Dispatch to StorageClusterVersionResource
89     */
90    @Path("cluster")
91    public StorageClusterVersionResource getClusterVersionResource() 
92        throws IOException {
93      return new StorageClusterVersionResource();
94    }
95  
96    /**
97     * Dispatch <tt>/version/rest</tt> to self.
98     */
99    @Path("rest")
100   public VersionResource getVersionResource() {
101     return this;
102   }
103 }