Commit 05a7d42bac3d229ce16d6634cb138b6eeb98cd95
1 parent
1ff1e4c3
Ajustes para início
Showing
11 changed files
with
43 additions
and
263 deletions
Show diff stats
core/pom.xml
@@ -13,12 +13,6 @@ | @@ -13,12 +13,6 @@ | ||
13 | </properties> | 13 | </properties> |
14 | <dependencies> | 14 | <dependencies> |
15 | <dependency> | 15 | <dependency> |
16 | - <groupId>org.apache.commons</groupId> | ||
17 | - <artifactId>commons-configuration2</artifactId> | ||
18 | - <version>2.0</version> | ||
19 | - <type>jar</type> | ||
20 | - </dependency> | ||
21 | - <dependency> | ||
22 | <groupId>javax</groupId> | 16 | <groupId>javax</groupId> |
23 | <artifactId>javaee-web-api</artifactId> | 17 | <artifactId>javaee-web-api</artifactId> |
24 | <version>7.0</version> | 18 | <version>7.0</version> |
pom.xml
@@ -23,6 +23,7 @@ | @@ -23,6 +23,7 @@ | ||
23 | <module>persistence</module> | 23 | <module>persistence</module> |
24 | <module>ws</module> | 24 | <module>ws</module> |
25 | <!--<module>swagger</module>--> | 25 | <!--<module>swagger</module>--> |
26 | + <module>security</module> | ||
26 | </modules> | 27 | </modules> |
27 | 28 | ||
28 | <dependencyManagement> | 29 | <dependencyManagement> |
security/pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> | 1 | <?xml version="1.0" encoding="UTF-8"?> |
2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
3 | <modelVersion>4.0.0</modelVersion> | 3 | <modelVersion>4.0.0</modelVersion> |
4 | - <groupId>org.demoiselle.jee.security</groupId> | 4 | + <groupId>org.demoiselle.jee</groupId> |
5 | <artifactId>security</artifactId> | 5 | <artifactId>security</artifactId> |
6 | <version>3.0.0-SNAPSHOT</version> | 6 | <version>3.0.0-SNAPSHOT</version> |
7 | <packaging>jar</packaging> | 7 | <packaging>jar</packaging> |
security/src/main/java/org/demoiselle/jee/security/exception/DemoiselleSecurityException.java
0 → 100644
@@ -0,0 +1,37 @@ | @@ -0,0 +1,37 @@ | ||
1 | +/* | ||
2 | + * Demoiselle Framework | ||
3 | + * | ||
4 | + * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | + * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | + */ | ||
7 | +package org.demoiselle.jee.security.exception; | ||
8 | + | ||
9 | +import java.util.HashMap; | ||
10 | + | ||
11 | +import org.demoiselle.jee.core.exception.DemoiselleException; | ||
12 | + | ||
13 | +public class DemoiselleSecurityException extends DemoiselleException { | ||
14 | + | ||
15 | + private static final long serialVersionUID = 519965615171844237L; | ||
16 | + | ||
17 | + private HashMap<String, String> messages = new HashMap<String, String>(); | ||
18 | + | ||
19 | + private int statusCode; | ||
20 | + | ||
21 | + public DemoiselleSecurityException(String string) { | ||
22 | + super(string); | ||
23 | + } | ||
24 | + | ||
25 | + public int getStatusCode() { | ||
26 | + return statusCode; | ||
27 | + } | ||
28 | + | ||
29 | + public void addMessage(String field, String msg) { | ||
30 | + this.statusCode = 422; | ||
31 | + messages.put(field, msg); | ||
32 | + } | ||
33 | + | ||
34 | + public HashMap<String, String> getMessages() { | ||
35 | + return messages; | ||
36 | + } | ||
37 | +} |
security/src/main/java/org/demoiselle/jee/ws/JaxRsFilter.java
@@ -1,59 +0,0 @@ | @@ -1,59 +0,0 @@ | ||
1 | -/* | ||
2 | - * To change this license header, choose License Headers in Project Properties. | ||
3 | - * To change this template file, choose Tools | Templates | ||
4 | - * and open the template in the editor. | ||
5 | - */ | ||
6 | -package org.demoiselle.jee.ws; | ||
7 | - | ||
8 | -import java.util.logging.Logger; | ||
9 | -import javax.annotation.PostConstruct; | ||
10 | -import javax.inject.Inject; | ||
11 | -import javax.ws.rs.client.ClientRequestContext; | ||
12 | -import javax.ws.rs.client.ClientRequestFilter; | ||
13 | -import javax.ws.rs.client.ClientResponseContext; | ||
14 | -import javax.ws.rs.client.ClientResponseFilter; | ||
15 | -import javax.ws.rs.container.ContainerRequestContext; | ||
16 | -import javax.ws.rs.container.ContainerRequestFilter; | ||
17 | -import javax.ws.rs.container.ContainerResponseContext; | ||
18 | -import javax.ws.rs.container.ContainerResponseFilter; | ||
19 | -import javax.ws.rs.container.PreMatching; | ||
20 | -import javax.ws.rs.ext.Provider; | ||
21 | - | ||
22 | -/** | ||
23 | - * | ||
24 | - * @author 70744416353 | ||
25 | - */ | ||
26 | -@Provider | ||
27 | -@PreMatching | ||
28 | -public class JaxRsFilter implements ClientRequestFilter, ClientResponseFilter, ContainerRequestFilter, ContainerResponseFilter { | ||
29 | - | ||
30 | - @Inject | ||
31 | - private Logger LOG; | ||
32 | - | ||
33 | - @Override | ||
34 | - public void filter(ClientRequestContext requestContext) { | ||
35 | - } | ||
36 | - | ||
37 | - @Override | ||
38 | - public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) { | ||
39 | - } | ||
40 | - | ||
41 | - @Override | ||
42 | - public void filter(ContainerRequestContext requestContext) { | ||
43 | - } | ||
44 | - | ||
45 | - @Override | ||
46 | - public void filter(ContainerRequestContext requestContext, ContainerResponseContext response) { | ||
47 | - | ||
48 | - response.getHeaders().putSingle("Access-Control-Allow-Origin", "*"); | ||
49 | - response.getHeaders().putSingle("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE"); | ||
50 | - response.getHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type"); | ||
51 | - | ||
52 | - } | ||
53 | - | ||
54 | - @PostConstruct | ||
55 | - public void init() { | ||
56 | - LOG.info("Demoiselle Module - Rest"); | ||
57 | - } | ||
58 | - | ||
59 | -} |
security/src/main/java/org/demoiselle/jee/ws/crud/package-info.java
@@ -1,11 +0,0 @@ | @@ -1,11 +0,0 @@ | ||
1 | -/* | ||
2 | - * Demoiselle Framework | ||
3 | - * | ||
4 | - * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | - * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | - */ | ||
7 | -/** | ||
8 | - * Esta pacote tem o objetivo de conter as classes relacionadas aos | ||
9 | - * facilitadores de CRUD do framework Demoiselle. | ||
10 | - */ | ||
11 | -package org.demoiselle.jee.ws.crud; | ||
12 | \ No newline at end of file | 0 | \ No newline at end of file |
security/src/main/java/org/demoiselle/jee/ws/exception/DemoiselleRESTException.java
@@ -1,37 +0,0 @@ | @@ -1,37 +0,0 @@ | ||
1 | -/* | ||
2 | - * Demoiselle Framework | ||
3 | - * | ||
4 | - * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | - * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | - */ | ||
7 | -package org.demoiselle.jee.ws.exception; | ||
8 | - | ||
9 | -import java.util.HashMap; | ||
10 | - | ||
11 | -import org.demoiselle.jee.core.exception.DemoiselleException; | ||
12 | - | ||
13 | -public class DemoiselleRESTException extends DemoiselleException { | ||
14 | - | ||
15 | - private static final long serialVersionUID = 519965615171844237L; | ||
16 | - | ||
17 | - private HashMap<String, String> messages = new HashMap<String, String>(); | ||
18 | - | ||
19 | - private int statusCode; | ||
20 | - | ||
21 | - public DemoiselleRESTException(String string) { | ||
22 | - super(string); | ||
23 | - } | ||
24 | - | ||
25 | - public int getStatusCode() { | ||
26 | - return statusCode; | ||
27 | - } | ||
28 | - | ||
29 | - public void addMessage(String field, String msg) { | ||
30 | - this.statusCode = 422; | ||
31 | - messages.put(field, msg); | ||
32 | - } | ||
33 | - | ||
34 | - public HashMap<String, String> getMessages() { | ||
35 | - return messages; | ||
36 | - } | ||
37 | -} |
security/src/main/java/org/demoiselle/jee/ws/exception/mapper/GenericExceptionMapper.java
@@ -1,62 +0,0 @@ | @@ -1,62 +0,0 @@ | ||
1 | -package org.demoiselle.jee.ws.exception.mapper; | ||
2 | - | ||
3 | -import java.io.PrintWriter; | ||
4 | -import java.io.StringWriter; | ||
5 | -import java.util.HashMap; | ||
6 | - | ||
7 | -import javax.ws.rs.WebApplicationException; | ||
8 | -import javax.ws.rs.core.MediaType; | ||
9 | -import javax.ws.rs.core.Response; | ||
10 | -import javax.ws.rs.ext.ExceptionMapper; | ||
11 | -import javax.ws.rs.ext.Provider; | ||
12 | - | ||
13 | -import org.demoiselle.jee.ws.exception.DemoiselleRESTException; | ||
14 | - | ||
15 | -@Provider | ||
16 | -public class GenericExceptionMapper implements ExceptionMapper<Exception> { | ||
17 | - | ||
18 | - public Response toResponse(Exception ex) { | ||
19 | - | ||
20 | - StringWriter errorStackTrace = new StringWriter(); | ||
21 | - ex.printStackTrace(new PrintWriter(errorStackTrace)); | ||
22 | - | ||
23 | - // Verifica se a exception é de validação de PAYLOAD do REST | ||
24 | - if (ex.getCause() instanceof DemoiselleRESTException) { | ||
25 | - DemoiselleRESTException exDemoiselleREST = (DemoiselleRESTException) ex.getCause(); | ||
26 | - if (!exDemoiselleREST.getMessages().isEmpty()) { | ||
27 | - return Response.status(exDemoiselleREST.getStatusCode()).entity(exDemoiselleREST.getMessages()) | ||
28 | - .type(MediaType.APPLICATION_JSON).build(); | ||
29 | - } | ||
30 | - } | ||
31 | - | ||
32 | - HashMap<String, String> entity = new HashMap<String, String>(); | ||
33 | - | ||
34 | - // No caso de existir message ele mostra a MESSAGE da Exception | ||
35 | - if (ex.getMessage() != null) { | ||
36 | - entity.put("error", ex.getMessage()); | ||
37 | - | ||
38 | - // Pega toda as mensagens da stacktrace | ||
39 | - int level = 1; | ||
40 | - while (ex.getCause() != null) { | ||
41 | - ex = (Exception) ex.getCause(); | ||
42 | - if (!ex.getMessage().isEmpty()) { | ||
43 | - entity.put("inner_cause_" + level, ex.getMessage()); | ||
44 | - } | ||
45 | - level += 1; | ||
46 | - } | ||
47 | - | ||
48 | - // Por padrão retorna SERVER ERROR, mas tenta encontrar o status do RESPONSE se for WebApplicationException | ||
49 | - // http://docs.oracle.com/javaee/7/api/javax/ws/rs/WebApplicationException.html | ||
50 | - int responseCode = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(); | ||
51 | - if (ex instanceof WebApplicationException) { | ||
52 | - responseCode = ((WebApplicationException)ex).getResponse().getStatus(); | ||
53 | - } | ||
54 | - | ||
55 | - return Response.status(responseCode).entity(entity).type(MediaType.APPLICATION_JSON).build(); | ||
56 | - } | ||
57 | - | ||
58 | - entity.put("error", "Erro interno desconhecido no servidor."); | ||
59 | - return Response.status(500).entity(entity).type(MediaType.APPLICATION_JSON).build(); | ||
60 | - } | ||
61 | - | ||
62 | -} |
security/src/main/java/org/demoiselle/jee/ws/interceptor/ValidatePayload.java
@@ -1,25 +0,0 @@ | @@ -1,25 +0,0 @@ | ||
1 | -/* | ||
2 | - * Demoiselle Framework | ||
3 | - * | ||
4 | - * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | - * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | - */ | ||
7 | -package org.demoiselle.jee.ws.interceptor; | ||
8 | - | ||
9 | -import static java.lang.annotation.ElementType.METHOD; | ||
10 | -import static java.lang.annotation.ElementType.TYPE; | ||
11 | -import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
12 | - | ||
13 | -import java.lang.annotation.Inherited; | ||
14 | -import java.lang.annotation.Retention; | ||
15 | -import java.lang.annotation.Target; | ||
16 | - | ||
17 | -import javax.interceptor.InterceptorBinding; | ||
18 | - | ||
19 | -@Inherited | ||
20 | -@InterceptorBinding | ||
21 | -@Target({ METHOD, TYPE }) | ||
22 | -@Retention(RUNTIME) | ||
23 | -public @interface ValidatePayload { | ||
24 | - | ||
25 | -} | ||
26 | \ No newline at end of file | 0 | \ No newline at end of file |
security/src/main/java/org/demoiselle/jee/ws/interceptor/ValidatePayloadInterceptor.java
@@ -1,62 +0,0 @@ | @@ -1,62 +0,0 @@ | ||
1 | -/* | ||
2 | - * Demoiselle Framework | ||
3 | - * | ||
4 | - * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | - * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | - */ | ||
7 | -package org.demoiselle.jee.ws.interceptor; | ||
8 | - | ||
9 | -import java.io.Serializable; | ||
10 | -import java.util.HashSet; | ||
11 | -import java.util.Set; | ||
12 | - | ||
13 | -import javax.interceptor.AroundInvoke; | ||
14 | -import javax.interceptor.Interceptor; | ||
15 | -import javax.interceptor.InvocationContext; | ||
16 | -import javax.validation.ConstraintViolation; | ||
17 | -import javax.validation.UnexpectedTypeException; | ||
18 | -import javax.validation.Validation; | ||
19 | -import javax.validation.Validator; | ||
20 | -import javax.validation.ValidatorFactory; | ||
21 | - | ||
22 | -import org.demoiselle.jee.ws.exception.DemoiselleRESTException; | ||
23 | - | ||
24 | -@Interceptor | ||
25 | -@ValidatePayload | ||
26 | -public class ValidatePayloadInterceptor implements Serializable { | ||
27 | - | ||
28 | - private static final long serialVersionUID = 1L; | ||
29 | - | ||
30 | - @AroundInvoke | ||
31 | - public Object manage(final InvocationContext ic) throws Exception { | ||
32 | - DemoiselleRESTException ex = new DemoiselleRESTException("Erro de validação de entidade."); | ||
33 | - Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>(); | ||
34 | - for (Object params : ic.getParameters()) { | ||
35 | - if (params != null) { | ||
36 | - ValidatorFactory dfv = Validation.buildDefaultValidatorFactory(); | ||
37 | - Validator validator = dfv.getValidator(); | ||
38 | - try { | ||
39 | - violations.addAll(validator.validate(params)); | ||
40 | - for (ConstraintViolation<?> violation : violations) { | ||
41 | - String field = (violation.getRootBeanClass().getSimpleName() + "_" | ||
42 | - + violation.getPropertyPath()).toLowerCase(); | ||
43 | - // GPMessage msg = | ||
44 | - // GPMessage.INVALID_FIELD_P1.setSufix(violation.getConstraintDescriptor() | ||
45 | - // .getAnnotation().annotationType().getSimpleName().toLowerCase()); | ||
46 | - | ||
47 | - ex.addMessage(field, violation.getMessage()); | ||
48 | - } | ||
49 | - } catch (UnexpectedTypeException cause) { | ||
50 | - // GPMessage msg = GPMessage.GENERAL_ERROR_P1; | ||
51 | - // msg.setParam(cause.getMessage()); | ||
52 | - throw new DemoiselleRESTException("ERRO GENERICO -> ALTERAR"); | ||
53 | - } | ||
54 | - } | ||
55 | - } | ||
56 | - | ||
57 | - if (!violations.isEmpty() && !ex.getMessages().isEmpty()) { | ||
58 | - throw ex; | ||
59 | - } | ||
60 | - return ic.proceed(); | ||
61 | - } | ||
62 | -} | ||
63 | \ No newline at end of file | 0 | \ No newline at end of file |