Commit 8987998c9dc456d586d5ee0bd31bc8e31d15158d
1 parent
a992dbd9
Exists in
master
FWK-197: Tratar erros de parsing para retornar HTTP 400 ao invés de HTTP
500 Task-Url: https://demoiselle.atlassian.net/browse/FWK-197
Showing
1 changed file
with
99 additions
and
0 deletions
Show diff stats
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ParseExceptionMapper.java
0 → 100644
@@ -0,0 +1,99 @@ | @@ -0,0 +1,99 @@ | ||
1 | +package br.gov.frameworkdemoiselle.internal.implementation; | ||
2 | + | ||
3 | +import java.util.Arrays; | ||
4 | +import java.util.HashSet; | ||
5 | +import java.util.ResourceBundle; | ||
6 | +import java.util.Set; | ||
7 | + | ||
8 | +import javax.ws.rs.core.Response; | ||
9 | +import javax.ws.rs.ext.ExceptionMapper; | ||
10 | +import javax.ws.rs.ext.Provider; | ||
11 | + | ||
12 | +import org.slf4j.Logger; | ||
13 | + | ||
14 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
15 | +import br.gov.frameworkdemoiselle.util.NameQualifier; | ||
16 | + | ||
17 | +@Provider | ||
18 | +public class ParseExceptionMapper implements ExceptionMapper<Throwable> { | ||
19 | + | ||
20 | + private transient ResourceBundle bundle; | ||
21 | + | ||
22 | + private transient Logger logger; | ||
23 | + | ||
24 | + private Set<Class<?>> getTypes(Class<?> target) { | ||
25 | + Set<Class<?>> classesInterfaces = new HashSet<Class<?>>(); | ||
26 | + classesInterfaces.add(target); | ||
27 | + classesInterfaces.addAll(Arrays.asList(target.getInterfaces())); | ||
28 | + | ||
29 | + Class<?> superClass = target.getSuperclass(); | ||
30 | + | ||
31 | + if (superClass != null) { | ||
32 | + classesInterfaces.add(superClass); | ||
33 | + classesInterfaces.addAll(getTypes(superClass)); | ||
34 | + } | ||
35 | + | ||
36 | + return classesInterfaces; | ||
37 | + } | ||
38 | + | ||
39 | + @Override | ||
40 | + public Response toResponse(Throwable exception) { | ||
41 | + | ||
42 | + // Throwable original = exception; | ||
43 | + // | ||
44 | + // while (exception != null) { | ||
45 | + // System.out.println("xxxxxxxxxxxxxxxxxxxxxx : " + exception.getClass().getCanonicalName()); | ||
46 | + // | ||
47 | + // exception = exception.getCause(); | ||
48 | + // } | ||
49 | + // | ||
50 | + // exception = original; | ||
51 | + // | ||
52 | + // Class<> | ||
53 | + // | ||
54 | + // while (exception != null) { | ||
55 | + // System.out.println("xxxxxxxxxxxxxxxxxxxxxx : " + exception.getClass().getIgetCanonicalName()); | ||
56 | + // | ||
57 | + // exception.getClass().getSuperclass(); | ||
58 | + // | ||
59 | + // exception = exception.getP; | ||
60 | + // } | ||
61 | + | ||
62 | + for (Class<?> type : getTypes(exception.getClass())) { | ||
63 | + | ||
64 | + System.out.println("___________________ " + type.getCanonicalName()); | ||
65 | + | ||
66 | + if (type.getCanonicalName().toLowerCase().indexOf("unrecognized") > -1) { | ||
67 | + getLogger().error("XXXXXXXXXXXXXX", exception); | ||
68 | + return Response.status(400).build(); | ||
69 | + } | ||
70 | + } | ||
71 | + | ||
72 | + // System.out.println("xxxxxxxxxxxxxx : " + getTypes(exception.getClass())); | ||
73 | + | ||
74 | + // if (exception.getMessage().toLowerCase().indexOf("unrecognized") > -1) { | ||
75 | + // getLogger().error("XXXXXXXXXXXXXX", exception); | ||
76 | + // return Response.status(400).build(); | ||
77 | + // } | ||
78 | + // | ||
79 | + throw new RuntimeException(exception); | ||
80 | + | ||
81 | + // return null; | ||
82 | + } | ||
83 | + | ||
84 | + private ResourceBundle getBundle() { | ||
85 | + if (bundle == null) { | ||
86 | + bundle = Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-rest-bundle")); | ||
87 | + } | ||
88 | + | ||
89 | + return bundle; | ||
90 | + } | ||
91 | + | ||
92 | + private Logger getLogger() { | ||
93 | + if (logger == null) { | ||
94 | + logger = Beans.getReference(Logger.class, new NameQualifier(ParseExceptionMapper.class.getName())); | ||
95 | + } | ||
96 | + | ||
97 | + return logger; | ||
98 | + } | ||
99 | +} |