001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.TableNotFoundException;
022import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
023import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
024import org.apache.hadoop.util.StringUtils;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.javax.ws.rs.WebApplicationException;
028import org.apache.hbase.thirdparty.javax.ws.rs.core.Response;
029
030@InterfaceAudience.Private
031public class ResourceBase implements Constants {
032
033  RESTServlet servlet;
034  Class<?> accessDeniedClazz;
035
036  public ResourceBase() throws IOException {
037    servlet = RESTServlet.getInstance();
038    try {
039      accessDeniedClazz = Class.forName("org.apache.hadoop.hbase.security.AccessDeniedException");
040    } catch (ClassNotFoundException e) {
041    }
042  }
043
044  protected Response processException(Throwable exp) {
045    Throwable curr = exp;
046    if (accessDeniedClazz != null) {
047      // some access denied exceptions are buried
048      while (curr != null) {
049        if (accessDeniedClazz.isAssignableFrom(curr.getClass())) {
050          throw new WebApplicationException(
051            Response.status(Response.Status.FORBIDDEN).type(MIMETYPE_TEXT)
052              .entity("Forbidden" + CRLF + StringUtils.stringifyException(exp) + CRLF).build());
053        }
054        curr = curr.getCause();
055      }
056    }
057    // TableNotFound may also be buried one level deep
058    if (exp instanceof TableNotFoundException || exp.getCause() instanceof TableNotFoundException) {
059      throw new WebApplicationException(
060        Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT)
061          .entity("Not found" + CRLF + StringUtils.stringifyException(exp) + CRLF).build());
062    }
063    if (exp instanceof NoSuchColumnFamilyException) {
064      throw new WebApplicationException(
065        Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT)
066          .entity("Not found" + CRLF + StringUtils.stringifyException(exp) + CRLF).build());
067    }
068    if (exp instanceof RuntimeException) {
069      throw new WebApplicationException(
070        Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT)
071          .entity("Bad request" + CRLF + StringUtils.stringifyException(exp) + CRLF).build());
072    }
073    if (exp instanceof RetriesExhaustedWithDetailsException) {
074      RetriesExhaustedWithDetailsException retryException =
075        (RetriesExhaustedWithDetailsException) exp;
076      processException(retryException.getCause(0));
077    }
078    throw new WebApplicationException(
079      Response.status(Response.Status.SERVICE_UNAVAILABLE).type(MIMETYPE_TEXT)
080        .entity("Unavailable" + CRLF + StringUtils.stringifyException(exp) + CRLF).build());
081  }
082}