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.hadoop.classification.InterfaceAudience;
33  
34  @InterfaceAudience.Private
35  public class ExistsResource extends ResourceBase {
36  
37    static CacheControl cacheControl;
38    static {
39      cacheControl = new CacheControl();
40      cacheControl.setNoCache(true);
41      cacheControl.setNoTransform(false);
42    }
43  
44    TableResource tableResource;
45  
46    /**
47     * Constructor
48     * @param tableResource
49     * @throws IOException
50     */
51    public ExistsResource(TableResource tableResource) throws IOException {
52      super();
53      this.tableResource = tableResource;
54    }
55  
56    @GET
57    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
58      MIMETYPE_PROTOBUF_IETF, MIMETYPE_BINARY})
59    public Response get(final @Context UriInfo uriInfo) {
60      try {
61        if (!tableResource.exists()) {
62          return Response.status(Response.Status.NOT_FOUND)
63            .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
64            .build();
65        }
66      } catch (IOException e) {
67        return Response.status(Response.Status.SERVICE_UNAVAILABLE)
68          .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
69          .build();
70      }
71      ResponseBuilder response = Response.ok();
72      response.cacheControl(cacheControl);
73      return response.build();
74    }
75  }