From f524cb57ed4696cb27d951b457d1c063926352d7 Mon Sep 17 00:00:00 2001 From: Cleverson Sacramento Date: Tue, 3 Jun 2014 15:08:41 -0300 Subject: [PATCH] Movendo o validador de payload para a extensão rest --- archetype/html-rest/src/main/resources/archetype-resources/src/main/java/entity/Bookmark.java | 2 +- archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java | 6 +++--- archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/ValidationMessages.properties | 29 +---------------------------- archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/demoiselle.properties | 6 +----- archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/messages.properties | 45 --------------------------------------------- archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/messages_en.properties | 45 --------------------------------------------- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/beans.xml | 2 +- impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/Validate.java | 55 ------------------------------------------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/ValidateInterceptor.java | 80 -------------------------------------------------------------------------------- impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/util/ValidatePayload.java | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/util/ValidatePayloadInterceptor.java | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/extension/rest/src/main/resources/META-INF/beans.xml | 40 ++++++++++++++++++++++++++++++++++++++++ 12 files changed, 183 insertions(+), 263 deletions(-) mode change 100755 => 100644 archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/demoiselle.properties delete mode 100644 archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/messages_en.properties delete mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/Validate.java delete mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/ValidateInterceptor.java create mode 100644 impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/util/ValidatePayload.java create mode 100644 impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/util/ValidatePayloadInterceptor.java create mode 100644 impl/extension/rest/src/main/resources/META-INF/beans.xml diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/entity/Bookmark.java b/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/entity/Bookmark.java index d323013..1ec7914 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/entity/Bookmark.java +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/entity/Bookmark.java @@ -26,7 +26,7 @@ public class Bookmark implements Serializable { private String description; @NotNull - @Pattern(regexp = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$", message = "formato inválido") + @Pattern(regexp = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$", message = "{invalid.url}") private String link; public Bookmark() { diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java b/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java index 5f5da16..a8e0c86 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java @@ -21,7 +21,7 @@ import ${package}.entity.Bookmark; import br.gov.frameworkdemoiselle.BadRequestException; import br.gov.frameworkdemoiselle.NotFoundException; import br.gov.frameworkdemoiselle.transaction.Transactional; -import br.gov.frameworkdemoiselle.validation.Validate; +import br.gov.frameworkdemoiselle.util.ValidatePayload; @Path("bookmark") public class BookmarkREST { @@ -49,8 +49,8 @@ public class BookmarkREST { } @POST - @Validate @Transactional + @ValidatePayload @Produces("text/plain") @Consumes("application/json") public Response insert(Bookmark entity, @Context UriInfo uriInfo) { @@ -63,9 +63,9 @@ public class BookmarkREST { } @PUT - @Validate @Path("{id}") @Transactional + @ValidatePayload @Produces("application/json") @Consumes("application/json") public void update(@PathParam("id") Long id, Bookmark entity) { diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/ValidationMessages.properties b/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/ValidationMessages.properties index 0d63b4b..6c4ab0b 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/ValidationMessages.properties +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/ValidationMessages.properties @@ -1,28 +1 @@ -br.gov.frameworkdemoiselle.pispasep=Informe um PIS/PASEP v\u00E1lido. -br.gov.frameworkdemoiselle.inscricaoestadual=Informe uma Inscri\u00E7\u00E3o Estadual v\u00E1lida. -br.gov.frameworkdemoiselle.cpf=Informe um CPF v\u00E1lido. -br.gov.frameworkdemoiselle.cnpj=Informe um CNPJ v\u00E1lido. -br.gov.frameworkdemoiselle.cep=Informe um CEP v\u00E1lido. - -javax.validation.constraints.AssertFalse.message=Este campo deve conter o valor falso. -javax.validation.constraints.AssertTrue.message=Este campo deve conter o valor verdadeiro. -javax.validation.constraints.DecimalMax.message=O valor deve ser menor ou igual a {value}. -javax.validation.constraints.DecimalMin.message=O valor deve ser maior ou igual a {value}. -javax.validation.constraints.Digits.message=Valor num\u00E9rico incorreto. (<{integer} d\u00EDgitos>.<{fraction} d\u00EDgitos> esperado). -javax.validation.constraints.Future.message=Deve ser uma data no futuro. -javax.validation.constraints.Max.message=O valor deve ser menor ou igual a {value}. -javax.validation.constraints.Min.message=O valor deve ser maior ou igual a {value}. -javax.validation.constraints.NotNull.message=N\u00E3o pode ser nulo. -javax.validation.constraints.Null.message=Deve ser nulo. -javax.validation.constraints.Past.message=Deve ser uma data no passado. -javax.validation.constraints.Pattern.message=O valor deve seguir o padr\u00E3o "{regexp}". -javax.validation.constraints.Size.message=O tamanho deve ser entre {min} e {max}. - -org.hibernate.validator.constraints.Email.message=E-mail inv\u00E1lido. -org.hibernate.validator.constraints.Length.message=O tamanho deve ser entre {min} e {max}. -org.hibernate.validator.constraints.NotBlank.message=N\u00E3o pode ser vazio. -org.hibernate.validator.constraints.NotEmpty.message=N\u00E3o pode ser vazio. -org.hibernate.validator.constraints.Range.message=Deve ser um valor entre {min} e {max}. -org.hibernate.validator.constraints.URL.message=Endere\u00E7o de Internet inv\u00E1lido. -org.hibernate.validator.constraints.CreditCardNumber.message=N\u00FAmero de cart\u00E3o de cr\u00E9dito inv\u00E1lido. -org.hibernate.validator.constraints.ScriptAssert.message=Express\u00E3o de script "{script}" n\u00E3o avaliada como verdadeira. +invalid.url=formato inv\u00E1lido diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/demoiselle.properties b/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/demoiselle.properties old mode 100755 new mode 100644 index 71ae24e..fa7e806 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/demoiselle.properties +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/demoiselle.properties @@ -1,5 +1 @@ -# Enables JPA transaction strategy, automatically detected if demoiselle-jpa component is detected. Use only if you need to overwrite the default behaviour -#frameworkdemoiselle.transaction.class=br.gov.frameworkdemoiselle.transaction.JPATransaction - -# Enables JTA transaction strategy, automatically detected if demoiselle-jta component is detected. Use only if you need to overwrite the default behaviour -#frameworkdemoiselle.transaction.class=br.gov.frameworkdemoiselle.transaction.JTATransaction +# See http://demoiselle.sourceforge.net/docs/framework/reference/2.4.0/html_single/#propriedades diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/messages.properties b/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/messages.properties index a5bcd2c..e69de29 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/messages.properties +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/messages.properties @@ -1,45 +0,0 @@ -menu.bookmark=Bookmarks -bookmark.label=Bookmark - -bookmark-delete-ok=Bookmark exclu\u00EDdo\: {0} -bookmark-insert-ok=Bookmark inserido: {0} -bookmark-update-ok=Bookmark atualizado: {0} - -bookmark.list.table.title=Lista de Links - -bookmark.label.id=ID -bookmark.label.link=Link -bookmark.label.description=Descri\u00E7\u00E3o - -bookmark.alt.id=ID -bookmark.alt.link=Link -bookmark.alt.description=Descri\u00E7\u00E3o - -button.add.new=Incluir Novo -button.back=Voltar -button.delete=Excluir -button.dialog.no=N\u00E3o, desculpe\! -button.dialog.yes=Sim, claro! -button.edit=Editar -button.new=Novo -button.save=Salvar - -label.action=A\u00E7\u00E3o -label.dialog.alert=Alerta -label.dialog.delete=Excluir -label.confirm.delete=Confirma? -label.date.pattern=dd/MM/yyyy - -main.app.title=Bookmarks -main.app.welcome=Bem-vindo \u00E0 aplica\u00E7\u00E3o de exemplo Bookmark. Este \u00E9 o seu ponto de partida, portanto sinta-se \u00E0 vontade para modificar esta aplica\u00E7\u00E3o. -main.change.skin=Mudar Pele -main.skin=Pele -main.footer.text=Aplica\u00E7\u00E3o de exemplo do Demoiselle ${parent.version} - -menu.language=Idioma -menu.menuitem.language-portuguese=Portugu\u00EAs -menu.menuitem.language-english=Ingl\u00EAs -menu.menuitem.contents=Conte\u00FAdo -menu.menuitem.list=Listar -menu.menuitem.new=Novo -menu.menuitem.quit=Sair diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/messages_en.properties b/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/messages_en.properties deleted file mode 100644 index 540894e..0000000 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/resources/messages_en.properties +++ /dev/null @@ -1,45 +0,0 @@ -menu.bookmark=Bookmarks -bookmark.label=Bookmark - -bookmark-delete-ok=Bookmark removed\: {0} -bookmark-insert-ok=Bookmark inserted\: {0} -bookmark-update-ok=Bookmark updated\: {0} - -bookmark.list.table.title=All Links - -bookmark.label.id=ID -bookmark.label.link=Link -bookmark.label.description=Description - -bookmark.alt.id=ID -bookmark.alt.link=Link -bookmark.alt.description=Description - -button.add.new=Insert New -button.back=Back -button.delete=Remove -button.dialog.no=No, sorry\! -button.dialog.yes=Yes, sure\! -button.edit=Edit -button.new=New -button.save=Save - -label.action=Action -label.dialog.alert=Alert -label.dialog.delete=Remove -label.confirm.delete=Confirm? -label.date.pattern=MM/dd/yyyy - -main.app.title=Bookmarks -main.app.welcome=Welcome to the example application Bookmark. This is your starting point, feel free to change this application. -main.change.skin=Change Skin -main.skin=Skin -main.footer.text=Example Application for Demoiselle ${parent.version} - -menu.language=Language -menu.menuitem.language-portuguese=Portuguese -menu.menuitem.language-english=English -menu.menuitem.contents=Content -menu.menuitem.list=List -menu.menuitem.new=New -menu.menuitem.quit=Quit diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/beans.xml b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/beans.xml index c80a5d8..59b54e9 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/beans.xml +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/beans.xml @@ -5,7 +5,7 @@ br.gov.frameworkdemoiselle.security.LoggedInInterceptor br.gov.frameworkdemoiselle.security.RequiredPermissionInterceptor br.gov.frameworkdemoiselle.security.RequiredRoleInterceptor - br.gov.frameworkdemoiselle.validation.ValidateInterceptor + br.gov.frameworkdemoiselle.util.ValidateInterceptor br.gov.frameworkdemoiselle.transaction.TransactionalInterceptor br.gov.frameworkdemoiselle.exception.ExceptionHandlerInterceptor diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/Validate.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/Validate.java deleted file mode 100644 index 0627ac8..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/Validate.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.validation; - -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import javax.interceptor.InterceptorBinding; - -@Inherited -@InterceptorBinding -@Target({ METHOD, TYPE }) -@Retention(RUNTIME) -public @interface Validate { - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/ValidateInterceptor.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/ValidateInterceptor.java deleted file mode 100644 index 01bc424..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/ValidateInterceptor.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.validation; - -import java.io.Serializable; -import java.util.HashSet; -import java.util.Set; - -import javax.interceptor.AroundInvoke; -import javax.interceptor.Interceptor; -import javax.interceptor.InvocationContext; -import javax.validation.ConstraintViolation; -import javax.validation.ConstraintViolationException; -import javax.validation.Validation; -import javax.validation.Validator; -import javax.validation.ValidatorFactory; - -/** - * Intercepts calls with {@code @Validate} annotations. - * - * @author SERPRO - */ -@Validate -@Interceptor -public class ValidateInterceptor implements Serializable { - - private static final long serialVersionUID = 1L; - - @AroundInvoke - public Object manage(final InvocationContext ic) throws Exception { - Set> violations = new HashSet>(); - - for (Object params : ic.getParameters()) { - ValidatorFactory dfv = Validation.buildDefaultValidatorFactory(); - Validator validator = dfv.getValidator(); - - violations.addAll(validator.validate(params)); - } - - if (!violations.isEmpty()) { - throw new ConstraintViolationException(violations); - } - - return ic.proceed(); - } -} diff --git a/impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/util/ValidatePayload.java b/impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/util/ValidatePayload.java new file mode 100644 index 0000000..ea7214c --- /dev/null +++ b/impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/util/ValidatePayload.java @@ -0,0 +1,55 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.util; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.interceptor.InterceptorBinding; + +@Inherited +@InterceptorBinding +@Target({ METHOD, TYPE }) +@Retention(RUNTIME) +public @interface ValidatePayload { + +} diff --git a/impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/util/ValidatePayloadInterceptor.java b/impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/util/ValidatePayloadInterceptor.java new file mode 100644 index 0000000..9351b81 --- /dev/null +++ b/impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/util/ValidatePayloadInterceptor.java @@ -0,0 +1,81 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.util; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +import javax.interceptor.AroundInvoke; +import javax.interceptor.Interceptor; +import javax.interceptor.InvocationContext; +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +/** + * Intercepts calls with {@code @ValidatePayload} annotations. + * + * @author SERPRO + */ +@ValidatePayload +@Interceptor +public class ValidatePayloadInterceptor implements Serializable { + + private static final long serialVersionUID = 1L; + + @AroundInvoke + public Object manage(final InvocationContext ic) throws Exception { + Set> violations = new HashSet>(); + + for (Object params : ic.getParameters()) { + if (params != null) { + ValidatorFactory dfv = Validation.buildDefaultValidatorFactory(); + Validator validator = dfv.getValidator(); + violations.addAll(validator.validate(params)); + } + } + + if (!violations.isEmpty()) { + throw new ConstraintViolationException(violations); + } + + return ic.proceed(); + } +} diff --git a/impl/extension/rest/src/main/resources/META-INF/beans.xml b/impl/extension/rest/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000..527e828 --- /dev/null +++ b/impl/extension/rest/src/main/resources/META-INF/beans.xml @@ -0,0 +1,40 @@ + + + + \ No newline at end of file -- libgit2 0.21.2