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.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.UriInfo;
030import javax.ws.rs.core.Response.ResponseBuilder;
031
032import org.apache.yetus.audience.InterfaceAudience;
033
034@InterfaceAudience.Private
035public class ExistsResource extends ResourceBase {
036
037  static CacheControl cacheControl;
038  static {
039    cacheControl = new CacheControl();
040    cacheControl.setNoCache(true);
041    cacheControl.setNoTransform(false);
042  }
043
044  TableResource tableResource;
045
046  /**
047   * Constructor
048   * @param tableResource
049   * @throws IOException
050   */
051  public ExistsResource(TableResource tableResource) throws IOException {
052    super();
053    this.tableResource = tableResource;
054  }
055
056  @GET
057  @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
058    MIMETYPE_PROTOBUF_IETF, MIMETYPE_BINARY})
059  public Response get(final @Context UriInfo uriInfo) {
060    try {
061      if (!tableResource.exists()) {
062        return Response.status(Response.Status.NOT_FOUND)
063          .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
064          .build();
065      }
066    } catch (IOException e) {
067      return Response.status(Response.Status.SERVICE_UNAVAILABLE)
068        .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
069        .build();
070    }
071    ResponseBuilder response = Response.ok();
072    response.cacheControl(cacheControl);
073    return response.build();
074  }
075}