-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FORGE-885: Implemented bean-validation addon
- Loading branch information
Showing
16 changed files
with
659 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
== bean-validation | ||
:idprefix: id_ | ||
|
||
This addon provides support of link:http://beanvalidation.org/1.1/(Bean Validation 1.1) features for dependent addons. | ||
|
||
=== Depends on | ||
|
||
[options="header"] | ||
|=== | ||
|Addon |Exported |Optional | ||
|
||
|org.jboss.forge.furnace.container:cdi | ||
|yes | ||
|no | ||
|
||
|=== | ||
|
||
== Setup | ||
|
||
This Addon requires the following installation steps. | ||
|
||
=== Add configuration to pom.xml | ||
|
||
To use this addon, you must add it as a dependency in the *pom.xml* of your `forge-addon` classified artifact: | ||
|
||
[source,xml] | ||
---- | ||
<dependency> | ||
<groupId>org.jboss.forge.addon</groupId> | ||
<artifactId>bean-validation</artifactId> | ||
<classifier>forge-addon</classifier> | ||
<version>${version}</version> | ||
</dependency> | ||
---- | ||
|
||
== Features | ||
|
||
Method validation:: | ||
link::http://beanvalidation.org/1.1/spec/#d0e267(Method validation) is provided out-of-box for usage in addon methods. | ||
+ | ||
[source,java] | ||
---- | ||
public class MyService { | ||
public void sayHello(@NotNull String name) { | ||
} | ||
} | ||
@Inject private MyService service; | ||
service.sayHello(null); // will throw ValidationException | ||
---- | ||
+ | ||
[TIP] | ||
==== | ||
This feature only works with addons that use the container-cdi | ||
==== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jboss.forge.addon</groupId> | ||
<artifactId>bean-validation-parent</artifactId> | ||
<version>2.0.0-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>bean-validation</artifactId> | ||
<name>Forge - Bean Validation Addon</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jboss.forge.addon</groupId> | ||
<artifactId>bean-validation-api</artifactId> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.forge.addon</groupId> | ||
<artifactId>bean-validation-impl</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.forge.addon</groupId> | ||
<artifactId>ui</artifactId> | ||
<classifier>forge-addon</classifier> | ||
<scope>provided</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<!-- Furnace Container --> | ||
<dependency> | ||
<groupId>org.jboss.forge.furnace.container</groupId> | ||
<artifactId>cdi</artifactId> | ||
<classifier>forge-addon</classifier> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.jboss.forge.furnace</groupId> | ||
<artifactId>furnace-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>generate-dot</id> | ||
<phase>prepare-package</phase> | ||
<goals> | ||
<goal>generate-dot</goal> | ||
</goals> | ||
<configuration> | ||
<attach>true</attach> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>create-forge-addon</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
<inherited>false</inherited> | ||
<configuration> | ||
<classifier>forge-addon</classifier> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
</project> |
8 changes: 8 additions & 0 deletions
8
bean-validation/addon/src/main/java/org/jboss/forge/addon/validation/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.validation; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jboss.forge.addon</groupId> | ||
<artifactId>bean-validation-parent</artifactId> | ||
<version>2.0.0-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>bean-validation-api</artifactId> | ||
<name>Forge - Bean Validation API</name> | ||
|
||
<dependencies> | ||
<!-- Bean Validation 1.1 API --> | ||
<dependency> | ||
<groupId>javax.validation</groupId> | ||
<artifactId>validation-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.forge.furnace</groupId> | ||
<artifactId>furnace-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jboss.forge.addon</groupId> | ||
<artifactId>bean-validation-parent</artifactId> | ||
<version>2.0.0-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>bean-validation-impl</artifactId> | ||
<name>Forge - Bean Validation Impl</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.jboss.forge.furnace.container</groupId> | ||
<artifactId>cdi-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.jboss.forge.addon</groupId> | ||
<artifactId>bean-validation-api</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.hibernate</groupId> | ||
<artifactId>hibernate-validator-cdi</artifactId> | ||
</dependency> | ||
|
||
|
||
<dependency> | ||
<groupId>org.jboss.forge.addon</groupId> | ||
<artifactId>ui</artifactId> | ||
<classifier>forge-addon</classifier> | ||
<scope>provided</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
</project> |
58 changes: 58 additions & 0 deletions
58
...pl/src/main/java/org/jboss/addon/validation/constraintvalidators/DelegatingValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.addon.validation.constraintvalidators; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
import org.jboss.forge.addon.ui.input.InputComponent; | ||
import org.jboss.forge.addon.ui.input.ManyValued; | ||
import org.jboss.forge.addon.ui.util.InputComponents; | ||
|
||
/** | ||
* A {@link ConstraintValidator} for InputComponents | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public abstract class DelegatingValidator<T extends Annotation> implements | ||
ConstraintValidator<T, InputComponent<Object, Object>> | ||
{ | ||
|
||
private final ConstraintValidator<T, ?> singleValidator; | ||
private final ConstraintValidator<T, ?> manyValidator; | ||
|
||
public DelegatingValidator(ConstraintValidator<T, ?> singleValidator, ConstraintValidator<T, ?> manyValidator) | ||
{ | ||
super(); | ||
this.singleValidator = singleValidator; | ||
this.manyValidator = manyValidator; | ||
} | ||
|
||
@Override | ||
public void initialize(T constraintAnnotation) | ||
{ | ||
singleValidator.initialize(constraintAnnotation); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public boolean isValid(InputComponent<Object, Object> value, ConstraintValidatorContext context) | ||
{ | ||
Object valueFor = InputComponents.getValueFor(value); | ||
if (value instanceof ManyValued) | ||
{ | ||
return ((ConstraintValidator<T, Object>) manyValidator).isValid(valueFor, context); | ||
} | ||
else | ||
{ | ||
return ((ConstraintValidator<T, Object>) singleValidator).isValid(valueFor, context); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ion/impl/src/main/java/org/jboss/addon/validation/constraintvalidators/SizeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.addon.validation.constraintvalidators; | ||
|
||
import javax.validation.constraints.Size; | ||
|
||
import org.hibernate.validator.internal.constraintvalidators.SizeValidatorForCharSequence; | ||
import org.hibernate.validator.internal.constraintvalidators.SizeValidatorForCollection; | ||
import org.jboss.forge.addon.ui.util.InputComponents; | ||
|
||
/** | ||
* Size Validator for {@link InputComponents} | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class SizeValidator extends DelegatingValidator<Size> | ||
{ | ||
public SizeValidator() | ||
{ | ||
super(new SizeValidatorForCharSequence(), new SizeValidatorForCollection()); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
bean-validation/impl/src/main/java/org/jboss/addon/validation/ui/UIValidationAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.addon.validation.ui; | ||
|
||
import java.util.Set; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validator; | ||
|
||
import org.jboss.forge.addon.ui.UICommand; | ||
import org.jboss.forge.addon.ui.UIValidator; | ||
import org.jboss.forge.addon.ui.context.UIValidationContext; | ||
import org.jboss.forge.addon.ui.input.InputComponent; | ||
import org.jboss.forge.addon.ui.util.InputComponents; | ||
|
||
/** | ||
* A {@link UIValidator} adapter that validates using Bean Validation 1.1 | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class UIValidationAdapter implements UIValidator | ||
{ | ||
private final Validator validator; | ||
private final InputComponent<?, ?> input; | ||
private final Class<UICommand> commandClass; | ||
|
||
public UIValidationAdapter(Validator validator, InputComponent<?, ?> input, Class<UICommand> commandClass) | ||
{ | ||
super(); | ||
this.validator = validator; | ||
this.input = input; | ||
this.commandClass = commandClass; | ||
} | ||
|
||
@Override | ||
public void validate(UIValidationContext context) | ||
{ | ||
Object value = InputComponents.getValueFor(input); | ||
Set<ConstraintViolation<UICommand>> violations = validator.validateValue(commandClass, input.getName(), value); | ||
for (ConstraintViolation<UICommand> violation : violations) | ||
{ | ||
context.addValidationError(input, violation.getMessage()); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...on/impl/src/main/java/org/jboss/addon/validation/ui/ValidationInputComponentEnricher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.addon.validation.ui; | ||
|
||
import javax.enterprise.inject.spi.InjectionPoint; | ||
import javax.inject.Inject; | ||
import javax.validation.Validator; | ||
|
||
import org.jboss.forge.addon.ui.UICommand; | ||
import org.jboss.forge.addon.ui.UIValidator; | ||
import org.jboss.forge.addon.ui.input.InputComponent; | ||
import org.jboss.forge.addon.ui.input.InputComponentInjectionEnricher; | ||
|
||
/** | ||
* Enables Bean Validation 1.1 as a {@link UIValidator} object in the {@link InputComponent} | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class ValidationInputComponentEnricher implements InputComponentInjectionEnricher | ||
{ | ||
@Inject | ||
private Validator validator; | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void enrich(InjectionPoint injectionPoint, InputComponent<?, ?> input) | ||
{ | ||
Class<?> beanType = injectionPoint.getBean().getBeanClass(); | ||
UIValidationAdapter adapter = new UIValidationAdapter(validator, input, (Class<UICommand>) beanType); | ||
input.setValidator(adapter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?xml version="1.0"?> | ||
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd" /> |
Oops, something went wrong.