1 | package eu.clarin.cmdi.virtualcollectionregistry.rest; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import javax.ws.rs.core.MediaType; |
---|
7 | import javax.ws.rs.core.Response; |
---|
8 | import javax.ws.rs.core.Response.Status; |
---|
9 | import javax.ws.rs.ext.ExceptionMapper; |
---|
10 | import javax.ws.rs.ext.Provider; |
---|
11 | import javax.xml.stream.XMLStreamException; |
---|
12 | |
---|
13 | import org.xml.sax.SAXParseException; |
---|
14 | |
---|
15 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionNotFoundException; |
---|
16 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException; |
---|
17 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryPermissionException; |
---|
18 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryUsageException; |
---|
19 | |
---|
20 | @Provider |
---|
21 | public class VirtualCollectionRegistryExceptionMapper implements |
---|
22 | ExceptionMapper<VirtualCollectionRegistryException> { |
---|
23 | |
---|
24 | public Response toResponse(VirtualCollectionRegistryException e) { |
---|
25 | Status status = null; |
---|
26 | List<String> errors = null; |
---|
27 | if (e instanceof VirtualCollectionRegistryUsageException) { |
---|
28 | status = Status.BAD_REQUEST; |
---|
29 | errors = getErrors(e.getCause()); |
---|
30 | } else if (e instanceof VirtualCollectionRegistryPermissionException) { |
---|
31 | status = Status.FORBIDDEN; |
---|
32 | } else if (e instanceof VirtualCollectionNotFoundException) { |
---|
33 | status = Status.NOT_FOUND; |
---|
34 | } else { |
---|
35 | if (hasCause(e.getCause(), XMLStreamException.class)) { |
---|
36 | status = Status.BAD_REQUEST; |
---|
37 | } else { |
---|
38 | status = Status.INTERNAL_SERVER_ERROR; |
---|
39 | } |
---|
40 | errors = getErrors(e.getCause()); |
---|
41 | } |
---|
42 | RestResponse response = new RestResponse(); |
---|
43 | response.setIsSuccess(false); |
---|
44 | response.setInfo(e.getMessage()); |
---|
45 | if (errors != null) { |
---|
46 | response.setError(errors); |
---|
47 | } |
---|
48 | return Response |
---|
49 | .status(status) |
---|
50 | .type(MediaType.APPLICATION_XML) |
---|
51 | .entity(response) |
---|
52 | .build(); |
---|
53 | } |
---|
54 | |
---|
55 | private static List<String> getErrors(Throwable t) { |
---|
56 | ArrayList<String> errors = new ArrayList<String>(); |
---|
57 | while (t != null) { |
---|
58 | if (t instanceof SAXParseException) { |
---|
59 | final SAXParseException e = (SAXParseException) t; |
---|
60 | String message = e.getMessage(); |
---|
61 | if (message.startsWith("cvc")) { |
---|
62 | int pos = message.indexOf(':'); |
---|
63 | if (pos != 0) { |
---|
64 | message = message.substring(pos + 1); |
---|
65 | } |
---|
66 | } |
---|
67 | errors.add(message + |
---|
68 | " [line = " + e.getLineNumber() + |
---|
69 | ", column = " + e.getColumnNumber() + "]"); |
---|
70 | } else if (t instanceof XMLStreamException) { |
---|
71 | final XMLStreamException e = (XMLStreamException) t; |
---|
72 | String message = e.getMessage(); |
---|
73 | if (message.startsWith("ParseError")) { |
---|
74 | int pos = message.indexOf(':'); |
---|
75 | if (pos != -1) { |
---|
76 | pos = message.indexOf(':', pos + 1); |
---|
77 | if (pos != -1) { |
---|
78 | message = message.substring(pos + 1); |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | if (e.getLocation() != null) { |
---|
83 | message = message + |
---|
84 | " [line = " + e.getLocation().getLineNumber() + |
---|
85 | ", column = " + e.getLocation().getColumnNumber() + |
---|
86 | "]"; |
---|
87 | } |
---|
88 | errors.add(message); |
---|
89 | } else { |
---|
90 | errors.add(t.getMessage()); |
---|
91 | } |
---|
92 | t = t.getCause(); |
---|
93 | }; |
---|
94 | return errors; |
---|
95 | } |
---|
96 | |
---|
97 | private static boolean hasCause(Throwable t, Class<?> clazz) { |
---|
98 | while (t != null) { |
---|
99 | if (clazz.isInstance(t)) { |
---|
100 | return true; |
---|
101 | } |
---|
102 | t = t.getCause(); |
---|
103 | } |
---|
104 | return false; |
---|
105 | } |
---|
106 | |
---|
107 | } // class VirtualCollectionRegistryExceptionMapper |
---|