Commit 4724a696d7bf3ab7ce267eae58848ec21514c35f
1 parent
50340724
Estrutura de pacotes do REST renomeada.
Showing
22 changed files
with
411 additions
and
410 deletions
Show diff stats
demoiselle-core/src/main/java/org/demoiselle/jee/core/message/DemoiselleMessage.java
@@ -46,7 +46,7 @@ public interface DemoiselleMessage { | @@ -46,7 +46,7 @@ public interface DemoiselleMessage { | ||
46 | String takingOff(); | 46 | String takingOff(); |
47 | 47 | ||
48 | @MessageTemplate("{engine-off}") | 48 | @MessageTemplate("{engine-off}") |
49 | - String engineOff(String ex); | 49 | + String engineOff(); |
50 | 50 | ||
51 | @MessageTemplate("{setting-up-bean-manager}") | 51 | @MessageTemplate("{setting-up-bean-manager}") |
52 | String settingUpBeanManagerException(String util); | 52 | String settingUpBeanManagerException(String util); |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/annotation/Cache.java
0 → 100644
@@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
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.rest.annotation; | ||
7 | + | ||
8 | +import static java.lang.annotation.ElementType.METHOD; | ||
9 | +import static java.lang.annotation.ElementType.TYPE; | ||
10 | +import java.lang.annotation.Inherited; | ||
11 | +import java.lang.annotation.Retention; | ||
12 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | +import java.lang.annotation.Target; | ||
14 | +import javax.enterprise.util.Nonbinding; | ||
15 | +import javax.interceptor.InterceptorBinding; | ||
16 | + | ||
17 | +/** | ||
18 | + * | ||
19 | + * @author 70744416353 | ||
20 | + */ | ||
21 | +@Inherited | ||
22 | +@InterceptorBinding | ||
23 | +@Target({ METHOD, TYPE }) | ||
24 | +@Retention(RUNTIME) | ||
25 | +public @interface Cache { | ||
26 | + | ||
27 | + @Nonbinding | ||
28 | + String value() default "max-age=0"; | ||
29 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/annotation/CorsAllowMethods.java
0 → 100644
@@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
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.rest.annotation; | ||
7 | + | ||
8 | +import static java.lang.annotation.ElementType.METHOD; | ||
9 | +import static java.lang.annotation.ElementType.TYPE; | ||
10 | +import java.lang.annotation.Inherited; | ||
11 | +import java.lang.annotation.Retention; | ||
12 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | +import java.lang.annotation.Target; | ||
14 | +import javax.enterprise.util.Nonbinding; | ||
15 | +import javax.interceptor.InterceptorBinding; | ||
16 | + | ||
17 | +/** | ||
18 | + * | ||
19 | + * @author 70744416353 | ||
20 | + */ | ||
21 | +@Inherited | ||
22 | +@InterceptorBinding | ||
23 | +@Target({METHOD, TYPE}) | ||
24 | +@Retention(RUNTIME) | ||
25 | +public @interface CorsAllowMethods { | ||
26 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/annotation/CorsAllowOrigin.java
0 → 100644
@@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
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.rest.annotation; | ||
7 | + | ||
8 | +import static java.lang.annotation.ElementType.METHOD; | ||
9 | +import static java.lang.annotation.ElementType.TYPE; | ||
10 | +import java.lang.annotation.Inherited; | ||
11 | +import java.lang.annotation.Retention; | ||
12 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | +import java.lang.annotation.Target; | ||
14 | +import javax.enterprise.util.Nonbinding; | ||
15 | +import javax.interceptor.InterceptorBinding; | ||
16 | + | ||
17 | +/** | ||
18 | + * | ||
19 | + * @author 70744416353 | ||
20 | + */ | ||
21 | +@Inherited | ||
22 | +@InterceptorBinding | ||
23 | +@Target({METHOD, TYPE}) | ||
24 | +@Retention(RUNTIME) | ||
25 | +public @interface CorsAllowOrigin { | ||
26 | + | ||
27 | + @Nonbinding | ||
28 | + String value() default "*"; | ||
29 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/annotation/CorsMaxAge.java
0 → 100644
@@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
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.rest.annotation; | ||
7 | + | ||
8 | +import static java.lang.annotation.ElementType.METHOD; | ||
9 | +import static java.lang.annotation.ElementType.TYPE; | ||
10 | +import java.lang.annotation.Inherited; | ||
11 | +import java.lang.annotation.Retention; | ||
12 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | +import java.lang.annotation.Target; | ||
14 | +import javax.enterprise.util.Nonbinding; | ||
15 | +import javax.interceptor.InterceptorBinding; | ||
16 | + | ||
17 | +/** | ||
18 | + * | ||
19 | + * @author 70744416353 | ||
20 | + */ | ||
21 | +@Inherited | ||
22 | +@InterceptorBinding | ||
23 | +@Target({METHOD, TYPE}) | ||
24 | +@Retention(RUNTIME) | ||
25 | +public @interface CorsMaxAge { | ||
26 | + | ||
27 | + @Nonbinding | ||
28 | + String value() default "0"; | ||
29 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/annotation/ValidatePayload.java
0 → 100644
@@ -0,0 +1,25 @@ | @@ -0,0 +1,25 @@ | ||
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.rest.annotation; | ||
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 | +} | ||
0 | \ No newline at end of file | 26 | \ No newline at end of file |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/crud/package-info.java
0 → 100644
@@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
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.rest.crud; | ||
0 | \ No newline at end of file | 12 | \ No newline at end of file |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/exception/DemoiselleRESTException.java
0 → 100644
@@ -0,0 +1,41 @@ | @@ -0,0 +1,41 @@ | ||
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.rest.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<>(); | ||
18 | + | ||
19 | + private int statusCode; | ||
20 | + | ||
21 | + public DemoiselleRESTException() { | ||
22 | + | ||
23 | + } | ||
24 | + | ||
25 | + public DemoiselleRESTException(String string) { | ||
26 | + super(string); | ||
27 | + } | ||
28 | + | ||
29 | + public int getStatusCode() { | ||
30 | + return statusCode; | ||
31 | + } | ||
32 | + | ||
33 | + public void addMessage(String field, String msg) { | ||
34 | + this.statusCode = 422; | ||
35 | + messages.put(field, msg); | ||
36 | + } | ||
37 | + | ||
38 | + public HashMap<String, String> getMessages() { | ||
39 | + return messages; | ||
40 | + } | ||
41 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/exception/mapper/GenericExceptionMapper.java
0 → 100644
@@ -0,0 +1,78 @@ | @@ -0,0 +1,78 @@ | ||
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.rest.exception.mapper; | ||
8 | + | ||
9 | +import java.io.PrintWriter; | ||
10 | +import java.io.StringWriter; | ||
11 | +import java.util.HashMap; | ||
12 | + | ||
13 | +import javax.ws.rs.WebApplicationException; | ||
14 | +import javax.ws.rs.core.MediaType; | ||
15 | +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
16 | +import javax.ws.rs.core.Response; | ||
17 | +import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; | ||
18 | +import static javax.ws.rs.core.Response.status; | ||
19 | +import static javax.ws.rs.core.Response.status; | ||
20 | +import static javax.ws.rs.core.Response.status; | ||
21 | +import static javax.ws.rs.core.Response.status; | ||
22 | +import javax.ws.rs.ext.ExceptionMapper; | ||
23 | +import javax.ws.rs.ext.Provider; | ||
24 | + | ||
25 | +import org.demoiselle.jee.rest.exception.DemoiselleRESTException; | ||
26 | + | ||
27 | +@Provider | ||
28 | +public class GenericExceptionMapper implements ExceptionMapper<Exception> { | ||
29 | + | ||
30 | + public Response toResponse(Exception ex) { | ||
31 | + | ||
32 | + StringWriter errorStackTrace = new StringWriter(); | ||
33 | + ex.printStackTrace(new PrintWriter(errorStackTrace)); | ||
34 | + | ||
35 | + // Verifica se a exception é de validação de PAYLOAD do REST | ||
36 | + if (ex instanceof DemoiselleRESTException) { | ||
37 | + DemoiselleRESTException exDemoiselleREST = (DemoiselleRESTException) ex; | ||
38 | + if (!exDemoiselleREST.getMessages().isEmpty()) { | ||
39 | + return status(exDemoiselleREST.getStatusCode()).entity(exDemoiselleREST.getMessages()) | ||
40 | + .type(APPLICATION_JSON).build(); | ||
41 | + } else if (exDemoiselleREST.getStatusCode() > 0){ | ||
42 | + return status(exDemoiselleREST.getStatusCode()).entity(exDemoiselleREST.getMessage()) | ||
43 | + .type(APPLICATION_JSON).build(); | ||
44 | + } | ||
45 | + | ||
46 | + } | ||
47 | + | ||
48 | + HashMap<String, String> entity = new HashMap<>(); | ||
49 | + | ||
50 | + // No caso de existir message ele mostra a MESSAGE da Exception | ||
51 | + if (ex.getMessage() != null) { | ||
52 | + entity.put("error", ex.getMessage()); | ||
53 | + | ||
54 | + // Pega toda as mensagens da stacktrace | ||
55 | + int level = 1; | ||
56 | + while (ex.getCause() != null) { | ||
57 | + ex = (Exception) ex.getCause(); | ||
58 | + if (!ex.getMessage().isEmpty()) { | ||
59 | + entity.put("inner_cause_" + level, ex.getMessage()); | ||
60 | + } | ||
61 | + level += 1; | ||
62 | + } | ||
63 | + | ||
64 | + // Por padrão retorna SERVER ERROR, mas tenta encontrar o status do RESPONSE se for WebApplicationException | ||
65 | + // http://docs.oracle.com/javaee/7/api/javax/ws/rs/WebApplicationException.html | ||
66 | + int responseCode = INTERNAL_SERVER_ERROR.getStatusCode(); | ||
67 | + if (ex instanceof WebApplicationException) { | ||
68 | + responseCode = ((WebApplicationException) ex).getResponse().getStatus(); | ||
69 | + } | ||
70 | + | ||
71 | + return status(responseCode).entity(entity).type(APPLICATION_JSON).build(); | ||
72 | + } | ||
73 | + | ||
74 | + entity.put("error", "Erro interno desconhecido no servidor."); | ||
75 | + return status(500).entity(entity).type(APPLICATION_JSON).build(); | ||
76 | + } | ||
77 | + | ||
78 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/filter/JaxRsFilter.java
0 → 100644
@@ -0,0 +1,74 @@ | @@ -0,0 +1,74 @@ | ||
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.rest.filter; | ||
8 | + | ||
9 | +import java.util.logging.Logger; | ||
10 | +import javax.annotation.PostConstruct; | ||
11 | +import javax.inject.Inject; | ||
12 | +import javax.ws.rs.container.ContainerRequestContext; | ||
13 | +import javax.ws.rs.container.ContainerRequestFilter; | ||
14 | +import javax.ws.rs.container.ContainerResponseContext; | ||
15 | +import javax.ws.rs.container.ContainerResponseFilter; | ||
16 | +import javax.ws.rs.container.PreMatching; | ||
17 | +import javax.ws.rs.container.ResourceInfo; | ||
18 | +import javax.ws.rs.core.Context; | ||
19 | +import javax.ws.rs.ext.Provider; | ||
20 | + | ||
21 | +import org.demoiselle.jee.rest.annotation.Cache; | ||
22 | +import org.demoiselle.jee.rest.annotation.CorsAllowMethods; | ||
23 | +import org.demoiselle.jee.rest.annotation.CorsAllowOrigin; | ||
24 | + | ||
25 | +/** | ||
26 | + * | ||
27 | + * @author 70744416353 | ||
28 | + */ | ||
29 | +@Provider | ||
30 | +@PreMatching | ||
31 | +public class JaxRsFilter implements ContainerRequestFilter, ContainerResponseFilter { | ||
32 | + | ||
33 | + @Inject | ||
34 | + private Logger LOG; | ||
35 | + | ||
36 | + @Context | ||
37 | + private ResourceInfo info; | ||
38 | + | ||
39 | + @Override | ||
40 | + public void filter(ContainerRequestContext requestContext) { | ||
41 | + } | ||
42 | + | ||
43 | + @Override | ||
44 | + public void filter(ContainerRequestContext requestContext, ContainerResponseContext response) { | ||
45 | + | ||
46 | + response.getHeaders().putSingle("Demoiselle", "3.0.0"); | ||
47 | + response.getHeaders().putSingle("Access-Control-Allow-Origin", "*"); | ||
48 | + response.getHeaders().putSingle("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE"); | ||
49 | + response.getHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type"); | ||
50 | + | ||
51 | + if (requestContext.getMethod().equals("GET")) { | ||
52 | + Cache max = info.getResourceMethod().getAnnotation(Cache.class); | ||
53 | + if (max != null) { | ||
54 | + response.getHeaders().putSingle("Cache-Control", max.value()); | ||
55 | + } | ||
56 | + } | ||
57 | + | ||
58 | +// CorsAllowMethods corsAllowMethods = info.getResourceMethod().getAnnotation(CorsAllowMethods.class); | ||
59 | +// if (corsAllowMethods != null) { | ||
60 | +// response.getHeaders().putSingle("Access-Control-Allow-Methods", requestContext.getMethod()); | ||
61 | +// } | ||
62 | +// | ||
63 | +// CorsAllowOrigin corsAllowOrigin = info.getResourceMethod().getAnnotation(CorsAllowOrigin.class); | ||
64 | +// if (corsAllowOrigin != null) { | ||
65 | +// response.getHeaders().putSingle("Access-Control-Allow-Origin", corsAllowOrigin.value()); | ||
66 | +// } | ||
67 | + } | ||
68 | + | ||
69 | + @PostConstruct | ||
70 | + public void init() { | ||
71 | + LOG.info("Demoiselle Module - Rest"); | ||
72 | + } | ||
73 | + | ||
74 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/interceptor/ValidatePayloadInterceptor.java
0 → 100644
@@ -0,0 +1,67 @@ | @@ -0,0 +1,67 @@ | ||
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.rest.interceptor; | ||
8 | + | ||
9 | +import static javax.interceptor.Interceptor.Priority.APPLICATION; | ||
10 | +import static javax.validation.Validation.buildDefaultValidatorFactory; | ||
11 | + | ||
12 | +import java.io.Serializable; | ||
13 | +import java.util.HashSet; | ||
14 | +import java.util.Set; | ||
15 | + | ||
16 | +import javax.annotation.Priority; | ||
17 | +import javax.interceptor.AroundInvoke; | ||
18 | +import javax.interceptor.Interceptor; | ||
19 | +import javax.interceptor.InvocationContext; | ||
20 | +import javax.validation.ConstraintViolation; | ||
21 | +import javax.validation.UnexpectedTypeException; | ||
22 | +import javax.validation.Validator; | ||
23 | +import javax.validation.ValidatorFactory; | ||
24 | + | ||
25 | +import org.demoiselle.jee.rest.annotation.ValidatePayload; | ||
26 | +import org.demoiselle.jee.rest.exception.DemoiselleRESTException; | ||
27 | + | ||
28 | +@Interceptor | ||
29 | +@ValidatePayload | ||
30 | +@Priority(APPLICATION) | ||
31 | +public class ValidatePayloadInterceptor implements Serializable { | ||
32 | + | ||
33 | + private static final long serialVersionUID = 1L; | ||
34 | + | ||
35 | + @AroundInvoke | ||
36 | + public Object manage(final InvocationContext ic) throws Exception { | ||
37 | + DemoiselleRESTException ex = new DemoiselleRESTException(); | ||
38 | + Set<ConstraintViolation<?>> violations = new HashSet<>(); | ||
39 | + for (Object params : ic.getParameters()) { | ||
40 | + if (params != null) { | ||
41 | + ValidatorFactory dfv = buildDefaultValidatorFactory(); | ||
42 | + Validator validator = dfv.getValidator(); | ||
43 | + try { | ||
44 | + violations.addAll(validator.validate(params)); | ||
45 | + for (ConstraintViolation<?> violation : violations) { | ||
46 | + String field = (violation.getRootBeanClass().getSimpleName() + "_" | ||
47 | + + violation.getPropertyPath()).toLowerCase(); | ||
48 | + // GPMessage msg = | ||
49 | + // GPMessage.INVALID_FIELD_P1.setSufix(violation.getConstraintDescriptor() | ||
50 | + // .getAnnotation().annotationType().getSimpleName().toLowerCase()); | ||
51 | + | ||
52 | + ex.addMessage(field, violation.getMessage()); | ||
53 | + } | ||
54 | + } catch (UnexpectedTypeException cause) { | ||
55 | + // GPMessage msg = GPMessage.GENERAL_ERROR_P1; | ||
56 | + // msg.setParam(cause.getMessage()); | ||
57 | + throw new DemoiselleRESTException("ERRO GENERICO -> ALTERAR"); | ||
58 | + } | ||
59 | + } | ||
60 | + } | ||
61 | + | ||
62 | + if (!violations.isEmpty() && !ex.getMessages().isEmpty()) { | ||
63 | + throw ex; | ||
64 | + } | ||
65 | + return ic.proceed(); | ||
66 | + } | ||
67 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/Cache.java
@@ -1,29 +0,0 @@ | @@ -1,29 +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.jaxrs.annotation; | ||
7 | - | ||
8 | -import static java.lang.annotation.ElementType.METHOD; | ||
9 | -import static java.lang.annotation.ElementType.TYPE; | ||
10 | -import java.lang.annotation.Inherited; | ||
11 | -import java.lang.annotation.Retention; | ||
12 | -import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | -import java.lang.annotation.Target; | ||
14 | -import javax.enterprise.util.Nonbinding; | ||
15 | -import javax.interceptor.InterceptorBinding; | ||
16 | - | ||
17 | -/** | ||
18 | - * | ||
19 | - * @author 70744416353 | ||
20 | - */ | ||
21 | -@Inherited | ||
22 | -@InterceptorBinding | ||
23 | -@Target({ METHOD, TYPE }) | ||
24 | -@Retention(RUNTIME) | ||
25 | -public @interface Cache { | ||
26 | - | ||
27 | - @Nonbinding | ||
28 | - String value() default "max-age=0"; | ||
29 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/CorsAllowMethods.java
@@ -1,26 +0,0 @@ | @@ -1,26 +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.jaxrs.annotation; | ||
7 | - | ||
8 | -import static java.lang.annotation.ElementType.METHOD; | ||
9 | -import static java.lang.annotation.ElementType.TYPE; | ||
10 | -import java.lang.annotation.Inherited; | ||
11 | -import java.lang.annotation.Retention; | ||
12 | -import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | -import java.lang.annotation.Target; | ||
14 | -import javax.enterprise.util.Nonbinding; | ||
15 | -import javax.interceptor.InterceptorBinding; | ||
16 | - | ||
17 | -/** | ||
18 | - * | ||
19 | - * @author 70744416353 | ||
20 | - */ | ||
21 | -@Inherited | ||
22 | -@InterceptorBinding | ||
23 | -@Target({METHOD, TYPE}) | ||
24 | -@Retention(RUNTIME) | ||
25 | -public @interface CorsAllowMethods { | ||
26 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/CorsAllowOrigin.java
@@ -1,29 +0,0 @@ | @@ -1,29 +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.jaxrs.annotation; | ||
7 | - | ||
8 | -import static java.lang.annotation.ElementType.METHOD; | ||
9 | -import static java.lang.annotation.ElementType.TYPE; | ||
10 | -import java.lang.annotation.Inherited; | ||
11 | -import java.lang.annotation.Retention; | ||
12 | -import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | -import java.lang.annotation.Target; | ||
14 | -import javax.enterprise.util.Nonbinding; | ||
15 | -import javax.interceptor.InterceptorBinding; | ||
16 | - | ||
17 | -/** | ||
18 | - * | ||
19 | - * @author 70744416353 | ||
20 | - */ | ||
21 | -@Inherited | ||
22 | -@InterceptorBinding | ||
23 | -@Target({METHOD, TYPE}) | ||
24 | -@Retention(RUNTIME) | ||
25 | -public @interface CorsAllowOrigin { | ||
26 | - | ||
27 | - @Nonbinding | ||
28 | - String value() default "*"; | ||
29 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/CorsMaxAge.java
@@ -1,29 +0,0 @@ | @@ -1,29 +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.jaxrs.annotation; | ||
7 | - | ||
8 | -import static java.lang.annotation.ElementType.METHOD; | ||
9 | -import static java.lang.annotation.ElementType.TYPE; | ||
10 | -import java.lang.annotation.Inherited; | ||
11 | -import java.lang.annotation.Retention; | ||
12 | -import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | -import java.lang.annotation.Target; | ||
14 | -import javax.enterprise.util.Nonbinding; | ||
15 | -import javax.interceptor.InterceptorBinding; | ||
16 | - | ||
17 | -/** | ||
18 | - * | ||
19 | - * @author 70744416353 | ||
20 | - */ | ||
21 | -@Inherited | ||
22 | -@InterceptorBinding | ||
23 | -@Target({METHOD, TYPE}) | ||
24 | -@Retention(RUNTIME) | ||
25 | -public @interface CorsMaxAge { | ||
26 | - | ||
27 | - @Nonbinding | ||
28 | - String value() default "0"; | ||
29 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/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.jaxrs.annotation; | ||
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 |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/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.jaxrs.crud; | ||
12 | \ No newline at end of file | 0 | \ No newline at end of file |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/exception/DemoiselleRESTException.java
@@ -1,41 +0,0 @@ | @@ -1,41 +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.jaxrs.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<>(); | ||
18 | - | ||
19 | - private int statusCode; | ||
20 | - | ||
21 | - public DemoiselleRESTException() { | ||
22 | - | ||
23 | - } | ||
24 | - | ||
25 | - public DemoiselleRESTException(String string) { | ||
26 | - super(string); | ||
27 | - } | ||
28 | - | ||
29 | - public int getStatusCode() { | ||
30 | - return statusCode; | ||
31 | - } | ||
32 | - | ||
33 | - public void addMessage(String field, String msg) { | ||
34 | - this.statusCode = 422; | ||
35 | - messages.put(field, msg); | ||
36 | - } | ||
37 | - | ||
38 | - public HashMap<String, String> getMessages() { | ||
39 | - return messages; | ||
40 | - } | ||
41 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/exception/mapper/GenericExceptionMapper.java
@@ -1,78 +0,0 @@ | @@ -1,78 +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.jaxrs.exception.mapper; | ||
8 | - | ||
9 | -import java.io.PrintWriter; | ||
10 | -import java.io.StringWriter; | ||
11 | -import java.util.HashMap; | ||
12 | - | ||
13 | -import javax.ws.rs.WebApplicationException; | ||
14 | -import javax.ws.rs.core.MediaType; | ||
15 | -import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
16 | -import javax.ws.rs.core.Response; | ||
17 | -import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; | ||
18 | -import static javax.ws.rs.core.Response.status; | ||
19 | -import static javax.ws.rs.core.Response.status; | ||
20 | -import static javax.ws.rs.core.Response.status; | ||
21 | -import static javax.ws.rs.core.Response.status; | ||
22 | -import javax.ws.rs.ext.ExceptionMapper; | ||
23 | -import javax.ws.rs.ext.Provider; | ||
24 | - | ||
25 | -import org.demoiselle.jee.ws.jaxrs.exception.DemoiselleRESTException; | ||
26 | - | ||
27 | -@Provider | ||
28 | -public class GenericExceptionMapper implements ExceptionMapper<Exception> { | ||
29 | - | ||
30 | - public Response toResponse(Exception ex) { | ||
31 | - | ||
32 | - StringWriter errorStackTrace = new StringWriter(); | ||
33 | - ex.printStackTrace(new PrintWriter(errorStackTrace)); | ||
34 | - | ||
35 | - // Verifica se a exception é de validação de PAYLOAD do REST | ||
36 | - if (ex instanceof DemoiselleRESTException) { | ||
37 | - DemoiselleRESTException exDemoiselleREST = (DemoiselleRESTException) ex; | ||
38 | - if (!exDemoiselleREST.getMessages().isEmpty()) { | ||
39 | - return status(exDemoiselleREST.getStatusCode()).entity(exDemoiselleREST.getMessages()) | ||
40 | - .type(APPLICATION_JSON).build(); | ||
41 | - } else if (exDemoiselleREST.getStatusCode() > 0){ | ||
42 | - return status(exDemoiselleREST.getStatusCode()).entity(exDemoiselleREST.getMessage()) | ||
43 | - .type(APPLICATION_JSON).build(); | ||
44 | - } | ||
45 | - | ||
46 | - } | ||
47 | - | ||
48 | - HashMap<String, String> entity = new HashMap<>(); | ||
49 | - | ||
50 | - // No caso de existir message ele mostra a MESSAGE da Exception | ||
51 | - if (ex.getMessage() != null) { | ||
52 | - entity.put("error", ex.getMessage()); | ||
53 | - | ||
54 | - // Pega toda as mensagens da stacktrace | ||
55 | - int level = 1; | ||
56 | - while (ex.getCause() != null) { | ||
57 | - ex = (Exception) ex.getCause(); | ||
58 | - if (!ex.getMessage().isEmpty()) { | ||
59 | - entity.put("inner_cause_" + level, ex.getMessage()); | ||
60 | - } | ||
61 | - level += 1; | ||
62 | - } | ||
63 | - | ||
64 | - // Por padrão retorna SERVER ERROR, mas tenta encontrar o status do RESPONSE se for WebApplicationException | ||
65 | - // http://docs.oracle.com/javaee/7/api/javax/ws/rs/WebApplicationException.html | ||
66 | - int responseCode = INTERNAL_SERVER_ERROR.getStatusCode(); | ||
67 | - if (ex instanceof WebApplicationException) { | ||
68 | - responseCode = ((WebApplicationException) ex).getResponse().getStatus(); | ||
69 | - } | ||
70 | - | ||
71 | - return status(responseCode).entity(entity).type(APPLICATION_JSON).build(); | ||
72 | - } | ||
73 | - | ||
74 | - entity.put("error", "Erro interno desconhecido no servidor."); | ||
75 | - return status(500).entity(entity).type(APPLICATION_JSON).build(); | ||
76 | - } | ||
77 | - | ||
78 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/filter/JaxRsFilter.java
@@ -1,73 +0,0 @@ | @@ -1,73 +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.jaxrs.filter; | ||
8 | - | ||
9 | -import java.util.logging.Logger; | ||
10 | -import javax.annotation.PostConstruct; | ||
11 | -import javax.inject.Inject; | ||
12 | -import javax.ws.rs.container.ContainerRequestContext; | ||
13 | -import javax.ws.rs.container.ContainerRequestFilter; | ||
14 | -import javax.ws.rs.container.ContainerResponseContext; | ||
15 | -import javax.ws.rs.container.ContainerResponseFilter; | ||
16 | -import javax.ws.rs.container.PreMatching; | ||
17 | -import javax.ws.rs.container.ResourceInfo; | ||
18 | -import javax.ws.rs.core.Context; | ||
19 | -import javax.ws.rs.ext.Provider; | ||
20 | -import org.demoiselle.jee.ws.jaxrs.annotation.Cache; | ||
21 | -import org.demoiselle.jee.ws.jaxrs.annotation.CorsAllowMethods; | ||
22 | -import org.demoiselle.jee.ws.jaxrs.annotation.CorsAllowOrigin; | ||
23 | - | ||
24 | -/** | ||
25 | - * | ||
26 | - * @author 70744416353 | ||
27 | - */ | ||
28 | -@Provider | ||
29 | -@PreMatching | ||
30 | -public class JaxRsFilter implements ContainerRequestFilter, ContainerResponseFilter { | ||
31 | - | ||
32 | - @Inject | ||
33 | - private Logger LOG; | ||
34 | - | ||
35 | - @Context | ||
36 | - private ResourceInfo info; | ||
37 | - | ||
38 | - @Override | ||
39 | - public void filter(ContainerRequestContext requestContext) { | ||
40 | - } | ||
41 | - | ||
42 | - @Override | ||
43 | - public void filter(ContainerRequestContext requestContext, ContainerResponseContext response) { | ||
44 | - | ||
45 | - response.getHeaders().putSingle("Demoiselle", "3.0.0"); | ||
46 | - response.getHeaders().putSingle("Access-Control-Allow-Origin", "*"); | ||
47 | - response.getHeaders().putSingle("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE"); | ||
48 | - response.getHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type"); | ||
49 | - | ||
50 | - if (requestContext.getMethod().equals("GET")) { | ||
51 | - Cache max = info.getResourceMethod().getAnnotation(Cache.class); | ||
52 | - if (max != null) { | ||
53 | - response.getHeaders().putSingle("Cache-Control", max.value()); | ||
54 | - } | ||
55 | - } | ||
56 | - | ||
57 | -// CorsAllowMethods corsAllowMethods = info.getResourceMethod().getAnnotation(CorsAllowMethods.class); | ||
58 | -// if (corsAllowMethods != null) { | ||
59 | -// response.getHeaders().putSingle("Access-Control-Allow-Methods", requestContext.getMethod()); | ||
60 | -// } | ||
61 | -// | ||
62 | -// CorsAllowOrigin corsAllowOrigin = info.getResourceMethod().getAnnotation(CorsAllowOrigin.class); | ||
63 | -// if (corsAllowOrigin != null) { | ||
64 | -// response.getHeaders().putSingle("Access-Control-Allow-Origin", corsAllowOrigin.value()); | ||
65 | -// } | ||
66 | - } | ||
67 | - | ||
68 | - @PostConstruct | ||
69 | - public void init() { | ||
70 | - LOG.info("Demoiselle Module - Rest"); | ||
71 | - } | ||
72 | - | ||
73 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/interceptor/ValidatePayloadInterceptor.java
@@ -1,67 +0,0 @@ | @@ -1,67 +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.jaxrs.interceptor; | ||
8 | - | ||
9 | -import org.demoiselle.jee.ws.jaxrs.annotation.ValidatePayload; | ||
10 | -import java.io.Serializable; | ||
11 | -import java.util.HashSet; | ||
12 | -import java.util.Set; | ||
13 | -import javax.annotation.Priority; | ||
14 | - | ||
15 | -import javax.interceptor.AroundInvoke; | ||
16 | -import javax.interceptor.Interceptor; | ||
17 | -import static javax.interceptor.Interceptor.Priority.APPLICATION; | ||
18 | -import javax.interceptor.InvocationContext; | ||
19 | -import javax.validation.ConstraintViolation; | ||
20 | -import javax.validation.UnexpectedTypeException; | ||
21 | -import javax.validation.Validation; | ||
22 | -import static javax.validation.Validation.buildDefaultValidatorFactory; | ||
23 | -import javax.validation.Validator; | ||
24 | -import javax.validation.ValidatorFactory; | ||
25 | - | ||
26 | -import org.demoiselle.jee.ws.jaxrs.exception.DemoiselleRESTException; | ||
27 | - | ||
28 | -@Interceptor | ||
29 | -@ValidatePayload | ||
30 | -@Priority(APPLICATION) | ||
31 | -public class ValidatePayloadInterceptor implements Serializable { | ||
32 | - | ||
33 | - private static final long serialVersionUID = 1L; | ||
34 | - | ||
35 | - @AroundInvoke | ||
36 | - public Object manage(final InvocationContext ic) throws Exception { | ||
37 | - DemoiselleRESTException ex = new DemoiselleRESTException(); | ||
38 | - Set<ConstraintViolation<?>> violations = new HashSet<>(); | ||
39 | - for (Object params : ic.getParameters()) { | ||
40 | - if (params != null) { | ||
41 | - ValidatorFactory dfv = buildDefaultValidatorFactory(); | ||
42 | - Validator validator = dfv.getValidator(); | ||
43 | - try { | ||
44 | - violations.addAll(validator.validate(params)); | ||
45 | - for (ConstraintViolation<?> violation : violations) { | ||
46 | - String field = (violation.getRootBeanClass().getSimpleName() + "_" | ||
47 | - + violation.getPropertyPath()).toLowerCase(); | ||
48 | - // GPMessage msg = | ||
49 | - // GPMessage.INVALID_FIELD_P1.setSufix(violation.getConstraintDescriptor() | ||
50 | - // .getAnnotation().annotationType().getSimpleName().toLowerCase()); | ||
51 | - | ||
52 | - ex.addMessage(field, violation.getMessage()); | ||
53 | - } | ||
54 | - } catch (UnexpectedTypeException cause) { | ||
55 | - // GPMessage msg = GPMessage.GENERAL_ERROR_P1; | ||
56 | - // msg.setParam(cause.getMessage()); | ||
57 | - throw new DemoiselleRESTException("ERRO GENERICO -> ALTERAR"); | ||
58 | - } | ||
59 | - } | ||
60 | - } | ||
61 | - | ||
62 | - if (!violations.isEmpty() && !ex.getMessages().isEmpty()) { | ||
63 | - throw ex; | ||
64 | - } | ||
65 | - return ic.proceed(); | ||
66 | - } | ||
67 | -} |
demoiselle-security/src/main/java/org/demoiselle/jee/security/exception/DemoiselleSecurityException.java
@@ -8,7 +8,7 @@ package org.demoiselle.jee.security.exception; | @@ -8,7 +8,7 @@ package org.demoiselle.jee.security.exception; | ||
8 | 8 | ||
9 | import java.util.HashMap; | 9 | import java.util.HashMap; |
10 | 10 | ||
11 | -import org.demoiselle.jee.ws.jaxrs.exception.DemoiselleRESTException; | 11 | +import org.demoiselle.jee.rest.exception.DemoiselleRESTException; |
12 | 12 | ||
13 | public class DemoiselleSecurityException extends DemoiselleRESTException { | 13 | public class DemoiselleSecurityException extends DemoiselleRESTException { |
14 | 14 |