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.com">Ron 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 (Iterator<String> it = params.stringList.iterator(); it.hasNext(); )
         {
            String s = it.next();
            System.out.println(s);
         }
      }
   }
   
   public static class TestClassValidator implements ConstraintValidator<TestClassConstraint, MyParams>
   {
      int eachLength;

      public void initialize(TestClassConstraint constraintAnnotation)
      {
         eachLength = constraintAnnotation.value();
      }

      @Override
      public boolean isValid(MyParams value, ConstraintValidatorContext context)
      {
         for (Iterator<String> 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 ArrayList<String> stringList = new ArrayList<String>();
   }

   @Before
   public void before() throws Exception
   {
      Hashtable<String,String> initParams = new Hashtable<String,String>();
      Hashtable<String,String> contextParams = new Hashtable<String,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", "yyyy");
      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 list
Resteasy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/resteasy-users

Reply via email to