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.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 SecurityException("Unauthorized" + CRLF +
54 StringUtils.stringifyException(exp) + CRLF);
55 }
56 curr = curr.getCause();
57 }
58 }
59
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 }