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.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 SecurityException("Unauthorized" + CRLF +
54                StringUtils.stringifyException(exp) + CRLF);
55          }
56          curr = curr.getCause();
57        }
58      }
59      //TableNotFound may also be buried one level deep
60      if (exp instanceof TableNotFoundException ||
61          exp.getCause() instanceof TableNotFoundException) {
62        throw new WebApplicationException(
63          Response.status(Response.Status.NOT_FOUND)
64            .type(MIMETYPE_TEXT).entity("Not found" + CRLF +
65               StringUtils.stringifyException(exp) + CRLF)
66            .build());
67      }
68      if (exp instanceof NoSuchColumnFamilyException){
69        throw new WebApplicationException(
70          Response.status(Response.Status.NOT_FOUND)
71            .type(MIMETYPE_TEXT).entity("Not found" + CRLF +
72               StringUtils.stringifyException(exp) + CRLF)
73            .build());
74      }
75      if (exp instanceof RuntimeException) {
76        throw new WebApplicationException(
77            Response.status(Response.Status.BAD_REQUEST)
78              .type(MIMETYPE_TEXT).entity("Bad request" + CRLF +
79                StringUtils.stringifyException(exp) + CRLF)
80              .build());
81      }
82      if (exp instanceof RetriesExhaustedWithDetailsException) {
83        RetriesExhaustedWithDetailsException retryException =
84            (RetriesExhaustedWithDetailsException) exp;
85        processException(retryException.getCause(0));
86      }
87      throw new WebApplicationException(
88        Response.status(Response.Status.SERVICE_UNAVAILABLE)
89          .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF +
90            StringUtils.stringifyException(exp) + CRLF)
91          .build());
92    }
93  }