1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }