From 26b476c1d491a5860c8a4df3d29874f50d7175a2 Mon Sep 17 00:00:00 2001 From: Marek Schmidt Date: Thu, 18 Aug 2011 12:47:45 +0200 Subject: [PATCH] SEAMVALIDATE-22, extend the helloworld example to demonstrate 1) validation of a method return value, 2) DI for constraint validators --- .../jboss/seam/validation/BannedNames.java | 40 +++++++++++++ .../seam/validation/BannedNamesList.java | 40 +++++++++++++ .../seam/validation/HelloWorldService.java | 22 ++++++- .../seam/validation/HelloWorldServlet.java | 30 ++++++++-- .../org/jboss/seam/validation/NotBanned.java | 40 +++++++++++++ .../seam/validation/NotBannedValidator.java | 58 +++++++++++++++++++ .../main/resources/META-INF/validation.xml | 10 ++++ 7 files changed, 232 insertions(+), 8 deletions(-) create mode 100644 examples/helloworld/src/main/java/org/jboss/seam/validation/BannedNames.java create mode 100644 examples/helloworld/src/main/java/org/jboss/seam/validation/BannedNamesList.java create mode 100644 examples/helloworld/src/main/java/org/jboss/seam/validation/NotBanned.java create mode 100644 examples/helloworld/src/main/java/org/jboss/seam/validation/NotBannedValidator.java create mode 100644 examples/helloworld/src/main/resources/META-INF/validation.xml diff --git a/examples/helloworld/src/main/java/org/jboss/seam/validation/BannedNames.java b/examples/helloworld/src/main/java/org/jboss/seam/validation/BannedNames.java new file mode 100644 index 0000000..8de938e --- /dev/null +++ b/examples/helloworld/src/main/java/org/jboss/seam/validation/BannedNames.java @@ -0,0 +1,40 @@ +/** + * JBoss, Home of Professional Open Source + * + * Copyright 2011, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.seam.validation; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Qualifier +@Target({ TYPE, METHOD, PARAMETER, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface BannedNames { + +} diff --git a/examples/helloworld/src/main/java/org/jboss/seam/validation/BannedNamesList.java b/examples/helloworld/src/main/java/org/jboss/seam/validation/BannedNamesList.java new file mode 100644 index 0000000..c37770c --- /dev/null +++ b/examples/helloworld/src/main/java/org/jboss/seam/validation/BannedNamesList.java @@ -0,0 +1,40 @@ +/** + * JBoss, Home of Professional Open Source + * + * Copyright 2011, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.seam.validation; + +import java.util.Collection; +import java.util.HashSet; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Produces; + +public class BannedNamesList { + @Produces + @BannedNames + @ApplicationScoped + Collection getBannedNames() { + Collection bannedNames = new HashSet (); + bannedNames.add("root"); + bannedNames.add("admin"); + bannedNames.add("foo bar"); + + return bannedNames; + } +} diff --git a/examples/helloworld/src/main/java/org/jboss/seam/validation/HelloWorldService.java b/examples/helloworld/src/main/java/org/jboss/seam/validation/HelloWorldService.java index 214c7af..e81a472 100644 --- a/examples/helloworld/src/main/java/org/jboss/seam/validation/HelloWorldService.java +++ b/examples/helloworld/src/main/java/org/jboss/seam/validation/HelloWorldService.java @@ -23,15 +23,33 @@ import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; +import org.hibernate.validator.constraints.NotEmpty; + @ApplicationScoped @AutoValidating public class HelloWorldService { public HelloWorldService() { } + + @Size(max = 16) + public String composeName(@NotNull @NotEmpty String firstName, @NotNull String middleName, @NotNull String lastName) { + StringBuffer name = new StringBuffer(firstName); + + if (middleName.length() > 0) { + name.append(' '); + name.append(middleName); + } + + if (lastName.length() > 0) { + name.append(' '); + name.append(lastName); + } + + return name.toString(); + } - public String sayHello(@NotNull @Size(min = 3) String name) { + public String sayHello(@NotNull @Size(min = 3) @NotBanned String name) { return "Hello, " + name + "!"; } - } diff --git a/examples/helloworld/src/main/java/org/jboss/seam/validation/HelloWorldServlet.java b/examples/helloworld/src/main/java/org/jboss/seam/validation/HelloWorldServlet.java index 9623755..3609c58 100644 --- a/examples/helloworld/src/main/java/org/jboss/seam/validation/HelloWorldServlet.java +++ b/examples/helloworld/src/main/java/org/jboss/seam/validation/HelloWorldServlet.java @@ -20,6 +20,7 @@ package org.jboss.seam.validation; import java.io.IOException; +import java.util.Collection; import javax.inject.Inject; import javax.servlet.ServletException; @@ -33,15 +34,21 @@ public class HelloWorldServlet extends HttpServlet { @Inject private HelloWorldService service; + + @Inject + @BannedNames + private Collection bannedNames; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); - String name = request.getParameter("name"); - if (name != null) { - response.getWriter().println("

" + service.sayHello(name) + "

"); + String firstName = request.getParameter("firstName"); + String middleName = request.getParameter("middleName"); + String lastName = request.getParameter("lastName"); + if (firstName != null && middleName != null && lastName != null) { + response.getWriter().println("

" + service.sayHello(service.composeName(firstName, middleName, lastName)) + "

"); } else { response.getWriter().println(""); response.getWriter() @@ -51,11 +58,22 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t response.getWriter().println(" Seam Validation Module Example"); response.getWriter().println(" "); response.getWriter().println("

Seam Validation Module Example - Method Validation

"); - response.getWriter().println("

Hi, what's your name? Enter at least three characters.

"); + response.getWriter().println("

Hi, what's your name?

"); + response.getWriter().println("

Your First Name must not be empty.

"); + response.getWriter().println("

Your whole name must have at least three and at most sixteen characters.

"); + response.getWriter().println("

Your name must not contain any of the banned names list (see below).

"); response.getWriter().println("
"); - response.getWriter().println(" Name: "); - response.getWriter().println(" "); + response.getWriter().println("
First Name:
"); + response.getWriter().println("
Middle Name:
"); + response.getWriter().println("
Last Name:
"); + response.getWriter().println("
"); response.getWriter().println("
"); + + response.getWriter().println("

Banned names list

"); + for (String name : bannedNames) { + response.getWriter().println("
" + name + "
"); + } + response.getWriter().println(" "); response.getWriter().println(""); } diff --git a/examples/helloworld/src/main/java/org/jboss/seam/validation/NotBanned.java b/examples/helloworld/src/main/java/org/jboss/seam/validation/NotBanned.java new file mode 100644 index 0000000..58d8741 --- /dev/null +++ b/examples/helloworld/src/main/java/org/jboss/seam/validation/NotBanned.java @@ -0,0 +1,40 @@ +/** + * JBoss, Home of Professional Open Source + * + * Copyright 2011, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.seam.validation; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.*; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.validation.Constraint; +import javax.validation.Payload; + +@Target( { METHOD, FIELD, ANNOTATION_TYPE, PARAMETER }) +@Retention(RUNTIME) +@Constraint(validatedBy = NotBannedValidator.class) +@Documented +public @interface NotBanned { + String message() default "Fails the banned filter"; + Class[] groups() default {}; + Class[] payload() default {}; +} diff --git a/examples/helloworld/src/main/java/org/jboss/seam/validation/NotBannedValidator.java b/examples/helloworld/src/main/java/org/jboss/seam/validation/NotBannedValidator.java new file mode 100644 index 0000000..d5fc979 --- /dev/null +++ b/examples/helloworld/src/main/java/org/jboss/seam/validation/NotBannedValidator.java @@ -0,0 +1,58 @@ +/** + * JBoss, Home of Professional Open Source + * + * Copyright 2011, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.seam.validation; + +import java.util.Collection; +import java.util.regex.Pattern; + +import javax.inject.Inject; +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +public class NotBannedValidator implements ConstraintValidator { + + /* + * Note that the Seam Validation constraint factory (org.jboss.seam.validation.InjectingConstraintValidatorFactory) + * must be enabled in META-INF/validation.xml for the dependency injection to work in constraint validators. + */ + @Inject + @BannedNames + private Collection bannedNames; + + private Pattern pattern; + + @Override + public void initialize(NotBanned annotation) { + // We build a pattern matching all the banned names + StringBuilder regex = new StringBuilder(); + for (String name : bannedNames) { + if (regex.length() > 0) { + regex.append('|'); + } + regex.append(name); + } + pattern = Pattern.compile(regex.toString(), Pattern.CASE_INSENSITIVE); + } + + @Override + public boolean isValid(String name, ConstraintValidatorContext context) { + return !pattern.matcher(name).find(); + } +} diff --git a/examples/helloworld/src/main/resources/META-INF/validation.xml b/examples/helloworld/src/main/resources/META-INF/validation.xml new file mode 100644 index 0000000..c25d1a5 --- /dev/null +++ b/examples/helloworld/src/main/resources/META-INF/validation.xml @@ -0,0 +1,10 @@ + + + + + org.jboss.seam.validation.InjectingConstraintValidatorFactory + + + \ No newline at end of file