Skip to content

Commit 46b1a55

Browse files
committed
Added @ValidateRegExp annotation
1 parent fd382e7 commit 46b1a55

7 files changed

Lines changed: 159 additions & 3 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.ocpsoft.prettyfaces.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Inherited
11+
@Documented
12+
@Target(ElementType.FIELD)
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface ValidateRegExp
15+
{
16+
String value();
17+
}

‎annotations/src/main/java/org/ocpsoft/prettyfaces/annotation/handlers/ParameterBindingHandler.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.ocpsoft.rewrite.config.Visitor;
1515
import org.ocpsoft.rewrite.el.El;
1616
import org.ocpsoft.rewrite.faces.config.PhaseBinding;
17+
import org.ocpsoft.rewrite.param.Parameter;
1718
import org.ocpsoft.rewrite.param.Parameterized;
1819

1920
public class ParameterBindingHandler extends FieldAnnotationHandler<ParameterBinding>
@@ -105,10 +106,15 @@ else if (beforePhase != null && afterPhase == null) {
105106
try {
106107

107108
// add the parameter and the binding
108-
parameterized.where(param).bindsTo(deferredBinding);
109+
Parameter parameter = parameterized.where(param);
109110

110-
// register the binding builder in the field context
111+
// TODO: is this a good or a bad pattern? :)
112+
context.put(Parameter.class, parameter);
113+
114+
// add the binding
115+
parameter.bindsTo(deferredBinding);
111116
context.setBindingBuilder(elBinding);
117+
112118

113119
if (log.isDebugEnabled()) {
114120
log.debug("Added binding for parameter [{}] to: {}", param, parameterized.getClass().getSimpleName());
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.ocpsoft.prettyfaces.annotation.handlers;
2+
3+
import java.lang.reflect.Field;
4+
5+
import org.ocpsoft.logging.Logger;
6+
import org.ocpsoft.prettyfaces.annotation.ValidateRegExp;
7+
import org.ocpsoft.rewrite.annotation.api.FieldContext;
8+
import org.ocpsoft.rewrite.annotation.spi.FieldAnnotationHandler;
9+
import org.ocpsoft.rewrite.param.Parameter;
10+
import org.ocpsoft.rewrite.param.RegexConstraint;
11+
12+
/**
13+
* @author Christian Kaltepoth
14+
*/
15+
public class ValidateRegExpHandler extends FieldAnnotationHandler<ValidateRegExp>
16+
{
17+
private final Logger log = Logger.getLogger(ValidateRegExpHandler.class);
18+
19+
@Override
20+
public Class<ValidateRegExp> handles()
21+
{
22+
return ValidateRegExp.class;
23+
}
24+
25+
@Override
26+
public int priority()
27+
{
28+
return HandlerConstants.WEIGHT_TYPE_ENRICHING;
29+
}
30+
31+
@Override
32+
@SuppressWarnings({ "rawtypes", "unchecked" })
33+
public void process(FieldContext context, Field field, ValidateRegExp annotation)
34+
{
35+
36+
// obtain the parameter for the current field
37+
Parameter parameter = (Parameter) context.get(Parameter.class);
38+
if (parameter == null) {
39+
throw new IllegalStateException("Cound not find any binding for field: " + field.getName());
40+
}
41+
42+
// add a corresponding RegexConstraint
43+
String expr = annotation.value();
44+
parameter.constrainedBy(new RegexConstraint(expr));
45+
46+
if (log.isTraceEnabled()) {
47+
log.trace("Parameter [{}] has been constrained by [{}]", parameter.getName(), expr);
48+
}
49+
50+
}
51+
52+
}

‎annotations/src/main/resources/META-INF/services/org.ocpsoft.rewrite.annotation.spi.AnnotationHandler‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ org.ocpsoft.prettyfaces.annotation.handlers.JSFConverterHandler
99
org.ocpsoft.prettyfaces.annotation.handlers.RolesRequiredHandler
1010
org.ocpsoft.prettyfaces.annotation.handlers.QueryParameterBindingHandler
1111
org.ocpsoft.prettyfaces.annotation.handlers.JoinHandler
12-
org.ocpsoft.prettyfaces.annotation.handlers.HostnameHandler
12+
org.ocpsoft.prettyfaces.annotation.handlers.HostnameHandler
13+
org.ocpsoft.prettyfaces.annotation.handlers.ValidateRegExpHandler
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.regex;
2+
3+
import javax.faces.bean.ManagedBean;
4+
import javax.faces.bean.RequestScoped;
5+
6+
import org.ocpsoft.prettyfaces.annotation.ForwardTo;
7+
import org.ocpsoft.prettyfaces.annotation.ParameterBinding;
8+
import org.ocpsoft.prettyfaces.annotation.URLPattern;
9+
import org.ocpsoft.prettyfaces.annotation.ValidateRegExp;
10+
11+
@ManagedBean
12+
@RequestScoped
13+
@URLPattern("/check/{value}")
14+
@ForwardTo("/faces/validate-regexp.xhtml")
15+
public class ValidateRegExpBean
16+
{
17+
18+
@ParameterBinding
19+
@ValidateRegExp("[a-z]{2}")
20+
private String value;
21+
22+
public String getValue()
23+
{
24+
return value;
25+
}
26+
27+
public void setValue(String value)
28+
{
29+
this.value = value;
30+
}
31+
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.regex;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.apache.http.client.methods.HttpGet;
6+
import org.jboss.arquillian.container.test.api.Deployment;
7+
import org.jboss.arquillian.junit.Arquillian;
8+
import org.jboss.shrinkwrap.api.spec.WebArchive;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.ocpsoft.prettyfaces.test.PrettyFacesTest;
12+
import org.ocpsoft.rewrite.test.HttpAction;
13+
14+
@RunWith(Arquillian.class)
15+
public class ValidateRegExpTest extends PrettyFacesTest
16+
{
17+
18+
@Deployment(testable = false)
19+
public static WebArchive getDeployment()
20+
{
21+
return getBaseDeployment()
22+
.addClass(ValidateRegExpBean.class)
23+
.addAsWebResource("basic/validate-regexp.xhtml", "validate-regexp.xhtml");
24+
}
25+
26+
@Test
27+
public void testMatchingExpression() throws Exception
28+
{
29+
HttpAction<HttpGet> action = get("/check/ab");
30+
assertEquals(200, action.getStatusCode());
31+
}
32+
33+
@Test
34+
public void testNotMatchingExpression() throws Exception
35+
{
36+
HttpAction<HttpGet> action = get("/check/abc");
37+
assertEquals(404, action.getStatusCode());
38+
}
39+
40+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets">
3+
<body>
4+
5+
<p>Some content</p>
6+
7+
</body>
8+
</html>

0 commit comments

Comments
 (0)