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.WebApplicationException;
025import javax.ws.rs.core.Response;
026
027import org.apache.yetus.audience.InterfaceAudience;
028import org.apache.hadoop.hbase.TableNotFoundException;
029import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
030import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
031import org.apache.hadoop.util.StringUtils;
032
033@InterfaceAudience.Private
034public class ResourceBase implements Constants {
035
036  RESTServlet servlet;
037  Class<?>  accessDeniedClazz;
038
039  public ResourceBase() throws IOException {
040    servlet = RESTServlet.getInstance();
041    try {
042      accessDeniedClazz = Class.forName("org.apache.hadoop.hbase.security.AccessDeniedException");
043    } catch (ClassNotFoundException e) {
044    }
045  }
046  
047  protected Response processException(Throwable exp) {
048    Throwable curr = exp;
049    if(accessDeniedClazz != null) {
050      //some access denied exceptions are buried
051      while (curr != null) {
052        if(accessDeniedClazz.isAssignableFrom(curr.getClass())) {
053          throw new WebApplicationException(
054              Response.status(Response.Status.FORBIDDEN)
055                .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF +
056                   StringUtils.stringifyException(exp) + CRLF)
057                .build());
058        }
059        curr = curr.getCause();
060      }
061    }
062    //TableNotFound may also be buried one level deep
063    if (exp instanceof TableNotFoundException ||
064        exp.getCause() instanceof TableNotFoundException) {
065      throw new WebApplicationException(
066        Response.status(Response.Status.NOT_FOUND)
067          .type(MIMETYPE_TEXT).entity("Not found" + CRLF +
068             StringUtils.stringifyException(exp) + CRLF)
069          .build());
070    }
071    if (exp instanceof NoSuchColumnFamilyException){
072      throw new WebApplicationException(
073        Response.status(Response.Status.NOT_FOUND)
074          .type(MIMETYPE_TEXT).entity("Not found" + CRLF +
075             StringUtils.stringifyException(exp) + CRLF)
076          .build());
077    }
078    if (exp instanceof RuntimeException) {
079      throw new WebApplicationException(
080          Response.status(Response.Status.BAD_REQUEST)
081            .type(MIMETYPE_TEXT).entity("Bad request" + CRLF +
082              StringUtils.stringifyException(exp) + CRLF)
083            .build());
084    }
085    if (exp instanceof RetriesExhaustedWithDetailsException) {
086      RetriesExhaustedWithDetailsException retryException =
087          (RetriesExhaustedWithDetailsException) exp;
088      processException(retryException.getCause(0));
089    }
090    throw new WebApplicationException(
091      Response.status(Response.Status.SERVICE_UNAVAILABLE)
092        .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF +
093          StringUtils.stringifyException(exp) + CRLF)
094        .build());
095  }
096}