Commit f116ffba646d6617d480108a2ab9a0035163f0dd
1 parent
7ce177ac
Exists in
master
FWK-206: Logando as exceptions tratadas automaticamente pelos
ExceptionMappers internos Task-Url: https://demoiselle.atlassian.net/browse/FWK-206
Showing
11 changed files
with
208 additions
and
46 deletions
Show diff stats
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/AuthenticationExceptionMapper.java
1 | 1 | package br.gov.frameworkdemoiselle.internal.implementation; |
2 | 2 | |
3 | +import static java.util.logging.Level.FINE; | |
3 | 4 | import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; |
4 | 5 | |
5 | 6 | import java.util.logging.Logger; |
... | ... | @@ -11,21 +12,35 @@ import javax.ws.rs.ext.Provider; |
11 | 12 | import br.gov.frameworkdemoiselle.security.AuthenticationException; |
12 | 13 | import br.gov.frameworkdemoiselle.util.Beans; |
13 | 14 | import br.gov.frameworkdemoiselle.util.NameQualifier; |
15 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
14 | 16 | |
15 | 17 | @Provider |
16 | 18 | public class AuthenticationExceptionMapper implements ExceptionMapper<AuthenticationException> { |
17 | 19 | |
20 | + private transient ResourceBundle bundle; | |
21 | + | |
18 | 22 | private transient Logger logger; |
19 | 23 | |
20 | 24 | @Override |
21 | 25 | public Response toResponse(AuthenticationException exception) { |
22 | - getLogger().warning(exception.getMessage()); | |
23 | - return Response.status(SC_UNAUTHORIZED).entity(exception.getMessage()).type("text/plain").build(); | |
26 | + int status = SC_UNAUTHORIZED; | |
27 | + String message = getBundle().getString("mapping-violations", status); | |
28 | + getLogger().log(FINE, message, exception); | |
29 | + | |
30 | + return Response.status(status).build(); | |
31 | + } | |
32 | + | |
33 | + private ResourceBundle getBundle() { | |
34 | + if (bundle == null) { | |
35 | + bundle = Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-rest-bundle")); | |
36 | + } | |
37 | + | |
38 | + return bundle; | |
24 | 39 | } |
25 | 40 | |
26 | 41 | private Logger getLogger() { |
27 | 42 | if (logger == null) { |
28 | - logger = Beans.getReference(Logger.class, new NameQualifier(AuthenticationExceptionMapper.class.getName())); | |
43 | + logger = Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.exception")); | |
29 | 44 | } |
30 | 45 | |
31 | 46 | return logger; | ... | ... |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/AuthorizationExceptionMapper.java
1 | 1 | package br.gov.frameworkdemoiselle.internal.implementation; |
2 | 2 | |
3 | +import static java.util.logging.Level.FINE; | |
3 | 4 | import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN; |
4 | 5 | |
5 | 6 | import java.util.logging.Logger; |
... | ... | @@ -11,21 +12,35 @@ import javax.ws.rs.ext.Provider; |
11 | 12 | import br.gov.frameworkdemoiselle.security.AuthorizationException; |
12 | 13 | import br.gov.frameworkdemoiselle.util.Beans; |
13 | 14 | import br.gov.frameworkdemoiselle.util.NameQualifier; |
15 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
14 | 16 | |
15 | 17 | @Provider |
16 | 18 | public class AuthorizationExceptionMapper implements ExceptionMapper<AuthorizationException> { |
17 | 19 | |
20 | + private transient ResourceBundle bundle; | |
21 | + | |
18 | 22 | private transient Logger logger; |
19 | 23 | |
20 | 24 | @Override |
21 | 25 | public Response toResponse(AuthorizationException exception) { |
22 | - getLogger().warning(exception.getMessage()); | |
23 | - return Response.status(SC_FORBIDDEN).build(); | |
26 | + int status = SC_FORBIDDEN; | |
27 | + String message = getBundle().getString("mapping-violations", status); | |
28 | + getLogger().log(FINE, message, exception); | |
29 | + | |
30 | + return Response.status(status).build(); | |
31 | + } | |
32 | + | |
33 | + private ResourceBundle getBundle() { | |
34 | + if (bundle == null) { | |
35 | + bundle = Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-rest-bundle")); | |
36 | + } | |
37 | + | |
38 | + return bundle; | |
24 | 39 | } |
25 | 40 | |
26 | 41 | private Logger getLogger() { |
27 | 42 | if (logger == null) { |
28 | - logger = Beans.getReference(Logger.class, new NameQualifier(AuthorizationExceptionMapper.class.getName())); | |
43 | + logger = Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.exception")); | |
29 | 44 | } |
30 | 45 | |
31 | 46 | return logger; | ... | ... |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConstraintViolationExceptionMapper.java
... | ... | @@ -32,6 +32,7 @@ public class ConstraintViolationExceptionMapper implements ExceptionMapper<Const |
32 | 32 | } |
33 | 33 | |
34 | 34 | getLogger().fine(getBundle().getString("mapping-violations", status, failed.getViolations().toString())); |
35 | + | |
35 | 36 | return Response.status(status).entity(failed.getViolations()).build(); |
36 | 37 | } |
37 | 38 | |
... | ... | @@ -45,7 +46,7 @@ public class ConstraintViolationExceptionMapper implements ExceptionMapper<Const |
45 | 46 | |
46 | 47 | private Logger getLogger() { |
47 | 48 | if (logger == null) { |
48 | - logger = Beans.getReference(Logger.class, new NameQualifier(HttpViolationExceptionMapper.class.getName())); | |
49 | + logger = Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.exception")); | |
49 | 50 | } |
50 | 51 | |
51 | 52 | return logger; | ... | ... |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/DefaultExceptionMapper.java
... | ... | @@ -22,7 +22,7 @@ public class DefaultExceptionMapper implements ExceptionMapper<Throwable> { |
22 | 22 | |
23 | 23 | @Override |
24 | 24 | public Response toResponse(Throwable exception) { |
25 | - String message = getBundle().getString("internal.server.error"); | |
25 | + String message = getBundle().getString("internal-server-error"); | |
26 | 26 | getLogger().log(SEVERE, message, exception); |
27 | 27 | |
28 | 28 | return Response.status(INTERNAL_SERVER_ERROR).entity(message).build(); |
... | ... | @@ -38,7 +38,7 @@ public class DefaultExceptionMapper implements ExceptionMapper<Throwable> { |
38 | 38 | |
39 | 39 | private Logger getLogger() { |
40 | 40 | if (logger == null) { |
41 | - logger = Beans.getReference(Logger.class, new NameQualifier(DefaultExceptionMapper.class.getName())); | |
41 | + logger = Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.exception")); | |
42 | 42 | } |
43 | 43 | |
44 | 44 | return logger; | ... | ... |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/EOFExceptionMapper.java
0 → 100644
... | ... | @@ -0,0 +1,48 @@ |
1 | +package br.gov.frameworkdemoiselle.internal.implementation; | |
2 | + | |
3 | +import static java.util.logging.Level.FINE; | |
4 | +import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; | |
5 | + | |
6 | +import java.io.EOFException; | |
7 | +import java.util.logging.Logger; | |
8 | + | |
9 | +import javax.ws.rs.core.Response; | |
10 | +import javax.ws.rs.ext.ExceptionMapper; | |
11 | +import javax.ws.rs.ext.Provider; | |
12 | + | |
13 | +import br.gov.frameworkdemoiselle.util.Beans; | |
14 | +import br.gov.frameworkdemoiselle.util.NameQualifier; | |
15 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
16 | + | |
17 | +@Provider | |
18 | +public class EOFExceptionMapper implements ExceptionMapper<EOFException> { | |
19 | + | |
20 | + private transient ResourceBundle bundle; | |
21 | + | |
22 | + private transient Logger logger; | |
23 | + | |
24 | + @Override | |
25 | + public Response toResponse(EOFException exception) { | |
26 | + int status = SC_BAD_REQUEST; | |
27 | + String message = getBundle().getString("mapping-violations", status); | |
28 | + getLogger().log(FINE, message, exception); | |
29 | + | |
30 | + return Response.status(status).build(); | |
31 | + } | |
32 | + | |
33 | + private ResourceBundle getBundle() { | |
34 | + if (bundle == null) { | |
35 | + bundle = Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-rest-bundle")); | |
36 | + } | |
37 | + | |
38 | + return bundle; | |
39 | + } | |
40 | + | |
41 | + private Logger getLogger() { | |
42 | + if (logger == null) { | |
43 | + logger = Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.exception")); | |
44 | + } | |
45 | + | |
46 | + return logger; | |
47 | + } | |
48 | +} | ... | ... |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/HttpViolationExceptionMapper.java
1 | 1 | package br.gov.frameworkdemoiselle.internal.implementation; |
2 | 2 | |
3 | +import static java.util.logging.Level.FINE; | |
4 | + | |
3 | 5 | import java.util.Set; |
4 | 6 | import java.util.logging.Logger; |
5 | 7 | |
... | ... | @@ -28,7 +30,7 @@ public class HttpViolationExceptionMapper implements ExceptionMapper<HttpViolati |
28 | 30 | if (violations.isEmpty()) { |
29 | 31 | violations = null; |
30 | 32 | } else { |
31 | - getLogger().fine(getBundle().getString("mapping-violations", status, violations.toString())); | |
33 | + getLogger().log(FINE, getBundle().getString("mapping-violations", status), exception); | |
32 | 34 | } |
33 | 35 | |
34 | 36 | return Response.status(status).entity(violations).build(); |
... | ... | @@ -44,7 +46,7 @@ public class HttpViolationExceptionMapper implements ExceptionMapper<HttpViolati |
44 | 46 | |
45 | 47 | private Logger getLogger() { |
46 | 48 | if (logger == null) { |
47 | - logger = Beans.getReference(Logger.class, new NameQualifier(HttpViolationExceptionMapper.class.getName())); | |
49 | + logger = Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.exception")); | |
48 | 50 | } |
49 | 51 | |
50 | 52 | return logger; | ... | ... |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/IllegalArgumentExceptionMapper.java
0 → 100644
... | ... | @@ -0,0 +1,83 @@ |
1 | +/* | |
2 | + * SERPRO Artifacts | |
3 | + * Copyright (C) 2014 SERPRO | |
4 | + * ---------------------------------------------------------------------------- | |
5 | + * This file is part of SERPRO Artifacts. | |
6 | + * | |
7 | + * SERPRO Artifacts is free software; you can redistribute it and/or | |
8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | |
9 | + * as published by the Free Software Foundation. | |
10 | + * | |
11 | + * This program is distributed in the hope that it will be useful, | |
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | + * GNU General Public License for more details. | |
15 | + * | |
16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | |
17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | |
18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | + * ---------------------------------------------------------------------------- | |
21 | + * Este arquivo é parte do SERPRO Artifacts. | |
22 | + * | |
23 | + * O SERPRO Artifacts é um software livre; você pode redistribuí-lo e/ou | |
24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
25 | + * do Software Livre (FSF). | |
26 | + * | |
27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
30 | + * para maiores detalhes. | |
31 | + * | |
32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
36 | + */ | |
37 | +package br.gov.frameworkdemoiselle.internal.implementation; | |
38 | + | |
39 | +import static java.util.logging.Level.FINE; | |
40 | +import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; | |
41 | + | |
42 | +import java.util.logging.Logger; | |
43 | + | |
44 | +import javax.ws.rs.core.Response; | |
45 | +import javax.ws.rs.ext.ExceptionMapper; | |
46 | +import javax.ws.rs.ext.Provider; | |
47 | + | |
48 | +import br.gov.frameworkdemoiselle.util.Beans; | |
49 | +import br.gov.frameworkdemoiselle.util.NameQualifier; | |
50 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
51 | + | |
52 | +@Provider | |
53 | +public class IllegalArgumentExceptionMapper implements ExceptionMapper<IllegalArgumentException> { | |
54 | + | |
55 | + private transient ResourceBundle bundle; | |
56 | + | |
57 | + private transient Logger logger; | |
58 | + | |
59 | + @Override | |
60 | + public Response toResponse(IllegalArgumentException exception) { | |
61 | + int status = SC_BAD_REQUEST; | |
62 | + String message = getBundle().getString("mapping-violations", status); | |
63 | + getLogger().log(FINE, message, exception); | |
64 | + | |
65 | + return Response.status(status).build(); | |
66 | + } | |
67 | + | |
68 | + private ResourceBundle getBundle() { | |
69 | + if (bundle == null) { | |
70 | + bundle = Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-rest-bundle")); | |
71 | + } | |
72 | + | |
73 | + return bundle; | |
74 | + } | |
75 | + | |
76 | + private Logger getLogger() { | |
77 | + if (logger == null) { | |
78 | + logger = Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.exception")); | |
79 | + } | |
80 | + | |
81 | + return logger; | |
82 | + } | |
83 | +} | ... | ... |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/NotLoggedInExceptionMapper.java
... | ... | @@ -1,31 +0,0 @@ |
1 | -package br.gov.frameworkdemoiselle.internal.implementation; | |
2 | - | |
3 | -import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; | |
4 | - | |
5 | -import javax.ws.rs.core.Response; | |
6 | -import javax.ws.rs.ext.ExceptionMapper; | |
7 | -import javax.ws.rs.ext.Provider; | |
8 | - | |
9 | -import br.gov.frameworkdemoiselle.security.NotLoggedInException; | |
10 | - | |
11 | -@Provider | |
12 | -public class NotLoggedInExceptionMapper implements ExceptionMapper<NotLoggedInException> { | |
13 | - | |
14 | - @Override | |
15 | - public Response toResponse(NotLoggedInException exception) { | |
16 | - // HttpServletRequest request = Beans.getReference(HttpServletRequest.class); | |
17 | - // String path = request.getRequestURI().substring(request.getContextPath().length()); | |
18 | - // | |
19 | - // Response response; | |
20 | - // | |
21 | - // if (path.indexOf("/api") > -1) { | |
22 | - // response = Response.status(SC_UNAUTHORIZED).header("WWW-Authenticate", "Basic realm=default").build(); | |
23 | - // } else { | |
24 | - // response = Response.status(SC_UNAUTHORIZED).build(); | |
25 | - // } | |
26 | - // | |
27 | - // return response; | |
28 | - | |
29 | - return Response.status(SC_UNAUTHORIZED).build(); | |
30 | - } | |
31 | -} |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/security/AbstractHTTPAuthorizationFilter.java
... | ... | @@ -36,11 +36,13 @@ |
36 | 36 | */ |
37 | 37 | package br.gov.frameworkdemoiselle.security; |
38 | 38 | |
39 | +import static java.util.logging.Level.FINE; | |
39 | 40 | import static java.util.regex.Pattern.CASE_INSENSITIVE; |
40 | 41 | import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; |
41 | 42 | |
42 | 43 | import java.io.IOException; |
43 | 44 | import java.util.Enumeration; |
45 | +import java.util.logging.Logger; | |
44 | 46 | import java.util.regex.Matcher; |
45 | 47 | import java.util.regex.Pattern; |
46 | 48 | |
... | ... | @@ -54,10 +56,16 @@ import javax.servlet.http.HttpServletRequest; |
54 | 56 | import javax.servlet.http.HttpServletResponse; |
55 | 57 | |
56 | 58 | import br.gov.frameworkdemoiselle.util.Beans; |
59 | +import br.gov.frameworkdemoiselle.util.NameQualifier; | |
60 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
57 | 61 | import br.gov.frameworkdemoiselle.util.Strings; |
58 | 62 | |
59 | 63 | public abstract class AbstractHTTPAuthorizationFilter implements Filter { |
60 | 64 | |
65 | + private transient ResourceBundle bundle; | |
66 | + | |
67 | + private transient Logger logger; | |
68 | + | |
61 | 69 | @Override |
62 | 70 | public void init(FilterConfig filterConfig) throws ServletException { |
63 | 71 | } |
... | ... | @@ -86,6 +94,9 @@ public abstract class AbstractHTTPAuthorizationFilter implements Filter { |
86 | 94 | performLogout(request, response); |
87 | 95 | |
88 | 96 | } catch (InvalidCredentialsException cause) { |
97 | + String message = getBundle().getString("authentication-failed"); | |
98 | + getLogger().log(FINE, message, cause); | |
99 | + | |
89 | 100 | setUnauthorizedStatus(response, cause); |
90 | 101 | } |
91 | 102 | |
... | ... | @@ -151,4 +162,20 @@ public abstract class AbstractHTTPAuthorizationFilter implements Filter { |
151 | 162 | response.setContentType("text/plain; charset=UTF-8"); |
152 | 163 | response.getWriter().write(cause.getMessage()); |
153 | 164 | } |
165 | + | |
166 | + private ResourceBundle getBundle() { | |
167 | + if (bundle == null) { | |
168 | + bundle = Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-rest-bundle")); | |
169 | + } | |
170 | + | |
171 | + return bundle; | |
172 | + } | |
173 | + | |
174 | + private Logger getLogger() { | |
175 | + if (logger == null) { | |
176 | + logger = Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.security")); | |
177 | + } | |
178 | + | |
179 | + return logger; | |
180 | + } | |
154 | 181 | } | ... | ... |
impl/extension/rest/src/main/resources/demoiselle-rest-bundle.properties
... | ... | @@ -33,5 +33,6 @@ |
33 | 33 | # ou escreva para a Fundação do Software Livre (FSF) Inc., |
34 | 34 | # 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. |
35 | 35 | |
36 | -internal.server.error=Erro interno do servidor | |
37 | -mapping-violations=Mapeando viola\u00E7\u00F5es com o status HTTP {0}\: {1} | |
36 | +internal-server-error=Erro interno do servidor | |
37 | +mapping-violations=Mapeando viola\u00E7\u00F5es com o status HTTP {0} | |
38 | +authentication-failed=Falha na autentica\u00E7\u00E3o | ... | ... |
impl/extension/rest/src/test/java/test/Tests.java
... | ... | @@ -56,7 +56,7 @@ import br.gov.frameworkdemoiselle.internal.implementation.AuthorizationException |
56 | 56 | import br.gov.frameworkdemoiselle.internal.implementation.ConstraintViolationExceptionMapper; |
57 | 57 | import br.gov.frameworkdemoiselle.internal.implementation.DefaultExceptionMapper; |
58 | 58 | import br.gov.frameworkdemoiselle.internal.implementation.HttpViolationExceptionMapper; |
59 | -import br.gov.frameworkdemoiselle.internal.implementation.NotLoggedInExceptionMapper; | |
59 | +import br.gov.frameworkdemoiselle.internal.implementation.IllegalArgumentExceptionMapper; | |
60 | 60 | import br.gov.frameworkdemoiselle.internal.implementation.SessionNotPermittedListener; |
61 | 61 | import br.gov.frameworkdemoiselle.security.AbstractHTTPAuthorizationFilter; |
62 | 62 | import br.gov.frameworkdemoiselle.security.BasicAuthFilter; |
... | ... | @@ -94,9 +94,10 @@ public final class Tests { |
94 | 94 | .addClass(AuthenticationExceptionMapper.class) |
95 | 95 | .addClass(AuthorizationExceptionMapper.class) |
96 | 96 | .addClass(ConstraintViolationExceptionMapper.class) |
97 | + .addClass(ConstraintViolationExceptionMapper.class) | |
98 | + .addClass(IllegalArgumentExceptionMapper.class) | |
97 | 99 | .addClass(DefaultExceptionMapper.class) |
98 | 100 | .addClass(HttpViolationExceptionMapper.class) |
99 | - .addClass(NotLoggedInExceptionMapper.class) | |
100 | 101 | .addClass(SessionNotPermittedListener.class) |
101 | 102 | .addClass(AbstractHTTPAuthorizationFilter.class) |
102 | 103 | .addClass(BasicAuthFilter.class) | ... | ... |