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.WebApplicationException;
25  import javax.ws.rs.core.Response;
26  
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.TableNotFoundException;
29  import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
30  import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
31  import org.apache.hadoop.util.StringUtils;
32  
33  @InterfaceAudience.Private
34  public class ResourceBase implements Constants {
35  
36    RESTServlet servlet;
37    Class<?>  accessDeniedClazz;
38  
39    public ResourceBase() throws IOException {
40      servlet = RESTServlet.getInstance();
41      try {
42        accessDeniedClazz = Class.forName("org.apache.hadoop.hbase.security.AccessDeniedException");
43      } catch (ClassNotFoundException e) {
44      }
45    }
46    
47    protected Response processException(Throwable exp) {
48      Throwable curr = exp;
49      if(accessDeniedClazz != null) {
50        //some access denied exceptions are buried
51        while (curr != null) {
52          if(accessDeniedClazz.isAssignableFrom(curr.getClass())) {
53            throw new WebApplicationException(
54                Response.status(Response.Status.FORBIDDEN)
55                  .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF +
56                     StringUtils.stringifyException(exp) + CRLF)
57                  .build());
58          }
59          curr = curr.getCause();
60        }
61      }
62      //TableNotFound may also be buried one level deep
63      if (exp instanceof TableNotFoundException ||
64          exp.getCause() instanceof TableNotFoundException) {
65        throw new WebApplicationException(
66          Response.status(Response.Status.NOT_FOUND)
67            .type(MIMETYPE_TEXT).entity("Not found" + CRLF +
68               StringUtils.stringifyException(exp) + CRLF)
69            .build());
70      }
71      if (exp instanceof NoSuchColumnFamilyException){
72        throw new WebApplicationException(
73          Response.status(Response.Status.NOT_FOUND)
74            .type(MIMETYPE_TEXT).entity("Not found" + CRLF +
75               StringUtils.stringifyException(exp) + CRLF)
76            .build());
77      }
78      if (exp instanceof RuntimeException) {
79        throw new WebApplicationException(
80            Response.status(Response.Status.BAD_REQUEST)
81              .type(MIMETYPE_TEXT).entity("Bad request" + CRLF +
82                StringUtils.stringifyException(exp) + CRLF)
83              .build());
84      }
85      if (exp instanceof RetriesExhaustedWithDetailsException) {
86        RetriesExhaustedWithDetailsException retryException =
87            (RetriesExhaustedWithDetailsException) exp;
88        processException(retryException.getCause(0));
89      }
90      throw new WebApplicationException(
91        Response.status(Response.Status.SERVICE_UNAVAILABLE)
92          .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF +
93            StringUtils.stringifyException(exp) + CRLF)
94          .build());
95    }
96  }