Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SEAMVALIDATE-22, extend the helloworld example #15

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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<String> getBannedNames() {
Collection<String> bannedNames = new HashSet<String> ();
bannedNames.add("root");
bannedNames.add("admin");
bannedNames.add("foo bar");

return bannedNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "!";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,15 +34,21 @@ public class HelloWorldServlet extends HttpServlet {

@Inject
private HelloWorldService service;

@Inject
@BannedNames
private Collection<String> 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("<h1>" + service.sayHello(name) + "</h1>");
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("<h1>" + service.sayHello(service.composeName(firstName, middleName, lastName)) + "</h1>");
} else {
response.getWriter().println("<?xml version=\"1.0\" ?>");
response.getWriter()
Expand All @@ -51,11 +58,22 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
response.getWriter().println(" <head><title>Seam Validation Module Example</title></head>");
response.getWriter().println(" <body>");
response.getWriter().println(" <h1>Seam Validation Module Example - Method Validation</h1>");
response.getWriter().println(" <p>Hi, what's your name? Enter at least three characters.</p>");
response.getWriter().println(" <p>Hi, what's your name? </p>");
response.getWriter().println(" <p>Your First Name must not be empty.</p>");
response.getWriter().println(" <p>Your whole name must have at least three and at most sixteen characters.</p>");
response.getWriter().println(" <p>Your name must not contain any of the banned names list (see below).</p>");
response.getWriter().println(" <form action=\"HelloWorld\">");
response.getWriter().println(" Name: <input name=\"name\" type=\"text\" size=\"30\">");
response.getWriter().println(" <input type=\"submit\" value=\" OK \">");
response.getWriter().println(" <div>First Name: <input name=\"firstName\" type=\"text\" size=\"15\"></div>");
response.getWriter().println(" <div>Middle Name: <input name=\"middleName\" type=\"text\" size=\"15\"></div>");
response.getWriter().println(" <div>Last Name: <input name=\"lastName\" type=\"text\" size=\"15\"></div>");
response.getWriter().println(" <div><input type=\"submit\" value=\" OK \"></div>");
response.getWriter().println(" </form>");

response.getWriter().println("<h2>Banned names list</h2>");
for (String name : bannedNames) {
response.getWriter().println("<div>" + name + "</div>");
}

response.getWriter().println(" </body>");
response.getWriter().println("</html>");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -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<NotBanned, String> {

/*
* 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<String> 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();
}
}
10 changes: 10 additions & 0 deletions examples/helloworld/src/main/resources/META-INF/validation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd">

<constraint-validator-factory>
org.jboss.seam.validation.InjectingConstraintValidatorFactory
</constraint-validator-factory>

</validation-config>