Commit a24a08d13eb05ff407db49b38ddb8f5e9862bde6
1 parent
bc6de58b
Melhoria no Rest, uso de cache-control
Showing
6 changed files
with
139 additions
and
72 deletions
Show diff stats
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/Cache.java
0 → 100644
| ... | ... | @@ -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.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=9223372036854775807"; | |
| 29 | +} | ... | ... |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/Cors.java
0 → 100644
| ... | ... | @@ -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.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 Cors { | |
| 26 | +} | ... | ... |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/ValidatePayload.java
0 → 100644
| ... | ... | @@ -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.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 | +} | |
| 0 | 26 | \ No newline at end of file | ... | ... |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/filter/JaxRsFilter.java
| ... | ... | @@ -9,16 +9,16 @@ package org.demoiselle.jee.ws.jaxrs.filter; |
| 9 | 9 | import java.util.logging.Logger; |
| 10 | 10 | import javax.annotation.PostConstruct; |
| 11 | 11 | import javax.inject.Inject; |
| 12 | -import javax.ws.rs.client.ClientRequestContext; | |
| 13 | -import javax.ws.rs.client.ClientRequestFilter; | |
| 14 | -import javax.ws.rs.client.ClientResponseContext; | |
| 15 | -import javax.ws.rs.client.ClientResponseFilter; | |
| 16 | 12 | import javax.ws.rs.container.ContainerRequestContext; |
| 17 | 13 | import javax.ws.rs.container.ContainerRequestFilter; |
| 18 | 14 | import javax.ws.rs.container.ContainerResponseContext; |
| 19 | 15 | import javax.ws.rs.container.ContainerResponseFilter; |
| 20 | 16 | import javax.ws.rs.container.PreMatching; |
| 17 | +import javax.ws.rs.container.ResourceInfo; | |
| 18 | +import javax.ws.rs.core.Context; | |
| 21 | 19 | import javax.ws.rs.ext.Provider; |
| 20 | +import org.demoiselle.jee.ws.jaxrs.annotation.Cache; | |
| 21 | +import org.demoiselle.jee.ws.jaxrs.annotation.Cors; | |
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * |
| ... | ... | @@ -26,18 +26,13 @@ import javax.ws.rs.ext.Provider; |
| 26 | 26 | */ |
| 27 | 27 | @Provider |
| 28 | 28 | @PreMatching |
| 29 | -public class JaxRsFilter implements ClientRequestFilter, ClientResponseFilter, ContainerRequestFilter, ContainerResponseFilter { | |
| 29 | +public class JaxRsFilter implements ContainerRequestFilter, ContainerResponseFilter { | |
| 30 | 30 | |
| 31 | 31 | @Inject |
| 32 | 32 | private Logger LOG; |
| 33 | 33 | |
| 34 | - @Override | |
| 35 | - public void filter(ClientRequestContext requestContext) { | |
| 36 | - } | |
| 37 | - | |
| 38 | - @Override | |
| 39 | - public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) { | |
| 40 | - } | |
| 34 | + @Context | |
| 35 | + private ResourceInfo info; | |
| 41 | 36 | |
| 42 | 37 | @Override |
| 43 | 38 | public void filter(ContainerRequestContext requestContext) { |
| ... | ... | @@ -45,6 +40,19 @@ public class JaxRsFilter implements ClientRequestFilter, ClientResponseFilter, C |
| 45 | 40 | |
| 46 | 41 | @Override |
| 47 | 42 | public void filter(ContainerRequestContext requestContext, ContainerResponseContext response) { |
| 43 | + | |
| 44 | + if (requestContext.getMethod().equals("GET")) { | |
| 45 | + Cache max = info.getResourceMethod().getAnnotation(Cache.class); | |
| 46 | + if (max != null) { | |
| 47 | + response.getHeaders().putSingle("Cache-Control", max.value()); | |
| 48 | + } | |
| 49 | + } | |
| 50 | + | |
| 51 | +// Cors cors = info.getResourceMethod().getAnnotation(Cors.class); | |
| 52 | +// if (cors != null) { | |
| 53 | +// response.getHeaders().putSingle("Cache-Control", max.value()); | |
| 54 | +// } | |
| 55 | + | |
| 48 | 56 | response.getHeaders().putSingle("Demoiselle", "3.0.0"); |
| 49 | 57 | response.getHeaders().putSingle("Access-Control-Allow-Origin", "*"); |
| 50 | 58 | response.getHeaders().putSingle("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE"); | ... | ... |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/interceptor/ValidatePayload.java
| ... | ... | @@ -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.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 | 0 | \ No newline at end of file |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/interceptor/ValidatePayloadInterceptor.java
| ... | ... | @@ -6,12 +6,15 @@ |
| 6 | 6 | */ |
| 7 | 7 | package org.demoiselle.jee.ws.jaxrs.interceptor; |
| 8 | 8 | |
| 9 | +import org.demoiselle.jee.ws.jaxrs.annotation.ValidatePayload; | |
| 9 | 10 | import java.io.Serializable; |
| 10 | 11 | import java.util.HashSet; |
| 11 | 12 | import java.util.Set; |
| 13 | +import javax.annotation.Priority; | |
| 12 | 14 | |
| 13 | 15 | import javax.interceptor.AroundInvoke; |
| 14 | 16 | import javax.interceptor.Interceptor; |
| 17 | +import static javax.interceptor.Interceptor.Priority.APPLICATION; | |
| 15 | 18 | import javax.interceptor.InvocationContext; |
| 16 | 19 | import javax.validation.ConstraintViolation; |
| 17 | 20 | import javax.validation.UnexpectedTypeException; |
| ... | ... | @@ -24,40 +27,41 @@ import org.demoiselle.jee.ws.jaxrs.exception.DemoiselleRESTException; |
| 24 | 27 | |
| 25 | 28 | @Interceptor |
| 26 | 29 | @ValidatePayload |
| 30 | +@Priority(APPLICATION) | |
| 27 | 31 | public class ValidatePayloadInterceptor implements Serializable { |
| 28 | 32 | |
| 29 | - private static final long serialVersionUID = 1L; | |
| 30 | - | |
| 31 | - @AroundInvoke | |
| 32 | - public Object manage(final InvocationContext ic) throws Exception { | |
| 33 | - DemoiselleRESTException ex = new DemoiselleRESTException(); | |
| 34 | - Set<ConstraintViolation<?>> violations = new HashSet<>(); | |
| 35 | - for (Object params : ic.getParameters()) { | |
| 36 | - if (params != null) { | |
| 37 | - ValidatorFactory dfv = buildDefaultValidatorFactory(); | |
| 38 | - Validator validator = dfv.getValidator(); | |
| 39 | - try { | |
| 40 | - violations.addAll(validator.validate(params)); | |
| 41 | - for (ConstraintViolation<?> violation : violations) { | |
| 42 | - String field = (violation.getRootBeanClass().getSimpleName() + "_" | |
| 43 | - + violation.getPropertyPath()).toLowerCase(); | |
| 44 | - // GPMessage msg = | |
| 45 | - // GPMessage.INVALID_FIELD_P1.setSufix(violation.getConstraintDescriptor() | |
| 46 | - // .getAnnotation().annotationType().getSimpleName().toLowerCase()); | |
| 47 | - | |
| 48 | - ex.addMessage(field, violation.getMessage()); | |
| 49 | - } | |
| 50 | - } catch (UnexpectedTypeException cause) { | |
| 51 | - // GPMessage msg = GPMessage.GENERAL_ERROR_P1; | |
| 52 | - // msg.setParam(cause.getMessage()); | |
| 53 | - throw new DemoiselleRESTException("ERRO GENERICO -> ALTERAR"); | |
| 54 | - } | |
| 55 | - } | |
| 56 | - } | |
| 57 | - | |
| 58 | - if (!violations.isEmpty() && !ex.getMessages().isEmpty()) { | |
| 59 | - throw ex; | |
| 60 | - } | |
| 61 | - return ic.proceed(); | |
| 62 | - } | |
| 63 | -} | |
| 64 | 33 | \ No newline at end of file |
| 34 | + private static final long serialVersionUID = 1L; | |
| 35 | + | |
| 36 | + @AroundInvoke | |
| 37 | + public Object manage(final InvocationContext ic) throws Exception { | |
| 38 | + DemoiselleRESTException ex = new DemoiselleRESTException(); | |
| 39 | + Set<ConstraintViolation<?>> violations = new HashSet<>(); | |
| 40 | + for (Object params : ic.getParameters()) { | |
| 41 | + if (params != null) { | |
| 42 | + ValidatorFactory dfv = buildDefaultValidatorFactory(); | |
| 43 | + Validator validator = dfv.getValidator(); | |
| 44 | + try { | |
| 45 | + violations.addAll(validator.validate(params)); | |
| 46 | + for (ConstraintViolation<?> violation : violations) { | |
| 47 | + String field = (violation.getRootBeanClass().getSimpleName() + "_" | |
| 48 | + + violation.getPropertyPath()).toLowerCase(); | |
| 49 | + // GPMessage msg = | |
| 50 | + // GPMessage.INVALID_FIELD_P1.setSufix(violation.getConstraintDescriptor() | |
| 51 | + // .getAnnotation().annotationType().getSimpleName().toLowerCase()); | |
| 52 | + | |
| 53 | + ex.addMessage(field, violation.getMessage()); | |
| 54 | + } | |
| 55 | + } catch (UnexpectedTypeException cause) { | |
| 56 | + // GPMessage msg = GPMessage.GENERAL_ERROR_P1; | |
| 57 | + // msg.setParam(cause.getMessage()); | |
| 58 | + throw new DemoiselleRESTException("ERRO GENERICO -> ALTERAR"); | |
| 59 | + } | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + if (!violations.isEmpty() && !ex.getMessages().isEmpty()) { | |
| 64 | + throw ex; | |
| 65 | + } | |
| 66 | + return ic.proceed(); | |
| 67 | + } | |
| 68 | +} | ... | ... |