1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.rest; |
35 |
|
|
36 |
|
import static com.google.common.base.CaseFormat.*; |
37 |
|
import static javax.ws.rs.core.Response.Status.*; |
38 |
|
|
39 |
|
import javax.jcr.AccessDeniedException; |
40 |
|
import javax.jcr.NoSuchWorkspaceException; |
41 |
|
import javax.jcr.PathNotFoundException; |
42 |
|
import javax.jcr.RepositoryException; |
43 |
|
import javax.jcr.ValueFormatException; |
44 |
|
import javax.jcr.nodetype.NoSuchNodeTypeException; |
45 |
|
import javax.jcr.query.InvalidQueryException; |
46 |
|
import javax.ws.rs.core.Response; |
47 |
|
import javax.ws.rs.core.Response.Status; |
48 |
|
import javax.ws.rs.ext.ExceptionMapper; |
49 |
|
import javax.ws.rs.ext.Provider; |
50 |
|
|
51 |
|
import org.apache.commons.lang3.StringUtils; |
52 |
|
import org.slf4j.Logger; |
53 |
|
import org.slf4j.LoggerFactory; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@Provider |
|
|
| 76.3% |
Uncovered Elements: 9 (38) |
Complexity: 7 |
Complexity Density: 0.28 |
|
59 |
|
public class RepositoryExceptionMapper implements ExceptionMapper<RepositoryException> { |
60 |
|
|
61 |
|
private static final Logger log = LoggerFactory.getLogger(RepositoryExceptionMapper.class); |
62 |
|
|
|
|
| 75.7% |
Uncovered Elements: 9 (37) |
Complexity: 7 |
Complexity Density: 0.28 |
|
63 |
5 |
@Override... |
64 |
|
public Response toResponse(RepositoryException exception) { |
65 |
5 |
Status status = INTERNAL_SERVER_ERROR; |
66 |
5 |
String errorCode = "unknown"; |
67 |
5 |
String errorMessage = exception.getMessage(); |
68 |
|
|
69 |
5 |
if (exception instanceof AccessDeniedException) { |
70 |
1 |
status = FORBIDDEN; |
71 |
1 |
errorCode = "accessDenied"; |
72 |
4 |
} else if (exception instanceof NoSuchNodeTypeException) { |
73 |
0 |
status = INTERNAL_SERVER_ERROR; |
74 |
0 |
errorCode = "noSuchNodeType"; |
75 |
4 |
} else if (exception instanceof NoSuchWorkspaceException) { |
76 |
0 |
status = INTERNAL_SERVER_ERROR; |
77 |
0 |
errorCode = "noSuchWorkspace"; |
78 |
4 |
} else if (exception instanceof ValueFormatException) { |
79 |
0 |
status = BAD_REQUEST; |
80 |
0 |
errorCode = "invalidValueFormat"; |
81 |
4 |
} else if (exception instanceof InvalidQueryException) { |
82 |
1 |
status = BAD_REQUEST; |
83 |
1 |
errorCode = "invalidQuery"; |
84 |
1 |
String errorName = StringUtils.substringBefore(exception.getClass().getSimpleName(), "Exception"); |
85 |
1 |
errorMessage = StringUtils.capitalize(UPPER_CAMEL.to(LOWER_UNDERSCORE, errorName)).replaceAll("_", " "); |
86 |
3 |
} else if (exception instanceof PathNotFoundException) { |
87 |
1 |
status = NOT_FOUND; |
88 |
1 |
errorCode = "pathNotFound"; |
89 |
|
} |
90 |
|
|
91 |
5 |
log.error("Exception thrown executing REST endpoint, returning {}", status.getStatusCode(), exception); |
92 |
|
|
93 |
5 |
return Response.status(status.getStatusCode()) |
94 |
|
.entity(new RestError(errorCode, errorMessage)) |
95 |
|
.build(); |
96 |
|
} |
97 |
|
} |