Re: [Resteasy-users] validation of method parameters
Hi Dave J, You can validate the size of each element of a List by writing your own validation constraint. See the attached TestJunk program. Note that either @Form or @BeanParam would work. @Form is specific to Resteasy. -Ron package org.jboss.resteasy.test.validation.junk; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; import static org.jboss.resteasy.test.TestPortProvider.generateURL; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; import javax.validation.Constraint; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import javax.validation.Payload; import javax.validation.Valid; import javax.ws.rs.FormParam; import javax.ws.rs.POST; import javax.ws.rs.Path; import junit.framework.Assert; import org.jboss.resteasy.annotations.Form; import org.jboss.resteasy.api.validation.ViolationReport; import org.jboss.resteasy.client.ClientRequest; import org.jboss.resteasy.client.ClientResponse; import org.jboss.resteasy.core.Dispatcher; import org.jboss.resteasy.spi.ResteasyDeployment; import org.jboss.resteasy.test.EmbeddedContainer; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * @author a href=mailto:ron.si...@jboss.comRon Sigal/a * @date Jan 6, 2012 */ public class TestJunk { protected static ResteasyDeployment deployment; protected static Dispatcher dispatcher; @Path(/) public static class TestResource { @POST public void post(@Valid @Form MyParams params) // public void post(@Valid @BeanParam MyParams params) { System.out.println(length: + params.stringList.size()); for (IteratorString it = params.stringList.iterator(); it.hasNext(); ) { String s = it.next(); System.out.println(s); } } } public static class TestClassValidator implements ConstraintValidatorTestClassConstraint, MyParams { int eachLength; public void initialize(TestClassConstraint constraintAnnotation) { eachLength = constraintAnnotation.value(); } @Override public boolean isValid(MyParams value, ConstraintValidatorContext context) { for (IteratorString it = value.stringList.iterator(); it.hasNext(); ) { if (it.next().length() eachLength) { return false; } } return true; } } @Documented @Constraint(validatedBy = TestClassValidator.class) @Target({TYPE}) @Retention(RUNTIME) public @interface TestClassConstraint { String message() default Each element must have length {value}; Class?[] groups() default {}; Class? extends Payload[] payload() default {}; int value(); } @TestClassConstraint(3) public static class MyParams { @FormParam(a) public ArrayListString stringList = new ArrayListString(); } @Before public void before() throws Exception { HashtableString,String initParams = new HashtableString,String(); HashtableString,String contextParams = new HashtableString,String(); deployment = EmbeddedContainer.start(initParams, contextParams); dispatcher = deployment.getDispatcher(); deployment.getRegistry().addPerRequestResource(TestResource.class); } @After public void after() throws Exception { EmbeddedContainer.stop(); dispatcher = null; deployment = null; } @Test public void testLengthPasses() throws Exception { ClientRequest request = new ClientRequest(generateURL(/)); request.formParameter(a, x); request.formParameter(a, y); ClientResponse? response = request.post(); Assert.assertEquals(204, response.getStatus()); } @Test public void testLengthFails() throws Exception { ClientRequest request = new ClientRequest(generateURL(/)); request.formParameter(a, x); request.formParameter(a, ); ClientResponse? response = request.post(); Assert.assertEquals(400, response.getStatus()); ViolationReport report = response.getEntity(ViolationReport.class); System.out.println(violation: \r + report.getParameterViolations().iterator().next()); } } -- Dive into the World of Parallel Programming The Go Parallel Website, sponsored by Intel and developed in partnership with Slashdot Media, is your hub for all things parallel software development, from weekly thought leadership blogs to news, videos, case studies, tutorials and more. Take a look and join the conversation now. http://goparallel.sourceforge.net/___ Resteasy-users mailing
Re: [Resteasy-users] validation of method parameters
Hi Savvas, Thank you for your response; I did try it the way you suggested, and you are right: the params are bound to the ArrayList. What I found was that application of the @Size annotation as below, imposes the constraint that the length of the list be no greater than the specified max. @Size(max=1000) @FormParam(a) private ArrayListString stringList = new ArrayListString(); While that constraint is useful to me, I also wanted to impose a constraint that each individual inbound string be checked that its length falls within a specified range - in my prior example, between 8 and 10 characters. Research has led me to believe that this is nontrivial, though I did see someone had developed and shared a library containing @Each annotations ... and that there (possibly) may be improvements along these lines that would only work with Java 8. I was curious why the method-based solution didn't work (my addString method) because that actually would do the validation I had in mind. That said, appreciate the time you took to respond. Best, Dave On Sat, Feb 21, 2015 at 2:57 PM, Savvas Andreas Moysidis savvas.andreas.moysi...@gmail.com wrote: Hi Dave, Not really sure why the bean validation stuff doesn't work but in order to bind form params you probably need to configure your resource method and binding class in a different way. Try the following: @GET public my object class, not important resourceMethod(@Valid @*BeanParam* MyParams params) { ... } public class MyParams { @FormParam(a) private ArrayListString stringList = new ArrayList(); ... } This should bind your form params into your MyParams class. A minor side note: The convention many frameworks use when binding http params at method level is the POJO one (i.e. *set*This()/*get*This() rather than addThis() etc) HTF, Savvas On 19 February 2015 at 19:48, Dave J devdevdata...@gmail.com wrote: Hello, After some nontrivial research I thought I would try this list in the hopes more expert users could help me figure out where I am going wrong. I am using RESTeasy 3.0.9. I am trying to affect validation of a parameter coming in on the URL for a GET request. I use a class to aggregate validation storage of the set of parameters coming in on the URL. I start with this: @GET public my object class, not important resourceMethod(@Valid @Form MyParams params) { ... } The parameter of interest is a String, and can occur more than once. Let's name it a. I would like to validate each occurrence (I want to check that its length is within a certain range), and if valid, I would like to add the String to a list. Otherwise, stop and report the error. So within the MyParams class public class MyParams { private ArrayListString stringList = new ArrayListString(); ... // Would rather this return void, but validation doesn't like constraints on void methods @QueryParam('a) public String addString(@Size(min=8,max=10) String str) { stringList.add(str); return null; } } However in this scenario, the incoming string is neither validated nor added to the list. Is it possible to do this within RESTeasy? I'm happy to provide more info if needed. Thanks. -- Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server from Actuate! Instantly Supercharge Your Business Reports and Dashboards with Interactivity, Sharing, Native Excel Exports, App Integration more Get technology previously reserved for billion-dollar corporations, FREE http://pubads.g.doubleclick.net/gampad/clk?id=190641631iu=/4140/ostg.clktrk ___ Resteasy-users mailing list Resteasy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/resteasy-users -- Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server from Actuate! Instantly Supercharge Your Business Reports and Dashboards with Interactivity, Sharing, Native Excel Exports, App Integration more Get technology previously reserved for billion-dollar corporations, FREE http://pubads.g.doubleclick.net/gampad/clk?id=190641631iu=/4140/ostg.clktrk___ Resteasy-users mailing list Resteasy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/resteasy-users
Re: [Resteasy-users] validation of method parameters
Hi Dave, Not really sure why the bean validation stuff doesn't work but in order to bind form params you probably need to configure your resource method and binding class in a different way. Try the following: @GET public my object class, not important resourceMethod(@Valid @*BeanParam* MyParams params) { ... } public class MyParams { @FormParam(a) private ArrayListString stringList = new ArrayList(); ... } This should bind your form params into your MyParams class. A minor side note: The convention many frameworks use when binding http params at method level is the POJO one (i.e. *set*This()/*get*This() rather than addThis() etc) HTF, Savvas On 19 February 2015 at 19:48, Dave J devdevdata...@gmail.com wrote: Hello, After some nontrivial research I thought I would try this list in the hopes more expert users could help me figure out where I am going wrong. I am using RESTeasy 3.0.9. I am trying to affect validation of a parameter coming in on the URL for a GET request. I use a class to aggregate validation storage of the set of parameters coming in on the URL. I start with this: @GET public my object class, not important resourceMethod(@Valid @Form MyParams params) { ... } The parameter of interest is a String, and can occur more than once. Let's name it a. I would like to validate each occurrence (I want to check that its length is within a certain range), and if valid, I would like to add the String to a list. Otherwise, stop and report the error. So within the MyParams class public class MyParams { private ArrayListString stringList = new ArrayListString(); ... // Would rather this return void, but validation doesn't like constraints on void methods @QueryParam('a) public String addString(@Size(min=8,max=10) String str) { stringList.add(str); return null; } } However in this scenario, the incoming string is neither validated nor added to the list. Is it possible to do this within RESTeasy? I'm happy to provide more info if needed. Thanks. -- Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server from Actuate! Instantly Supercharge Your Business Reports and Dashboards with Interactivity, Sharing, Native Excel Exports, App Integration more Get technology previously reserved for billion-dollar corporations, FREE http://pubads.g.doubleclick.net/gampad/clk?id=190641631iu=/4140/ostg.clktrk ___ Resteasy-users mailing list Resteasy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/resteasy-users -- Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server from Actuate! Instantly Supercharge Your Business Reports and Dashboards with Interactivity, Sharing, Native Excel Exports, App Integration more Get technology previously reserved for billion-dollar corporations, FREE http://pubads.g.doubleclick.net/gampad/clk?id=190641631iu=/4140/ostg.clktrk___ Resteasy-users mailing list Resteasy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/resteasy-users