Index: src/test/java/org/jboss/resteasy/test/resource/generic/StudentWriter.java =================================================================== --- src/test/java/org/jboss/resteasy/test/resource/generic/StudentWriter.java (revision 0) +++ src/test/java/org/jboss/resteasy/test/resource/generic/StudentWriter.java (revision 0) @@ -0,0 +1,37 @@ +package org.jboss.resteasy.test.resource.generic; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; + +@Provider +@Produces("application/student") +public class StudentWriter implements MessageBodyWriter +{ + + public long getSize(Student t, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) + { + return t.getName().length(); + } + + public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) + { + return true; + } + + public void writeTo(Student t, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException + { + OutputStreamWriter writer = new OutputStreamWriter(entityStream); + writer.write(t.getName()); + writer.flush(); + } +} Index: src/test/java/org/jboss/resteasy/test/resource/generic/GenericResourceTest.java =================================================================== --- src/test/java/org/jboss/resteasy/test/resource/generic/GenericResourceTest.java (revision 0) +++ src/test/java/org/jboss/resteasy/test/resource/generic/GenericResourceTest.java (revision 0) @@ -0,0 +1,36 @@ +package org.jboss.resteasy.test.resource.generic; + +import org.jboss.resteasy.client.ProxyFactory; +import org.jboss.resteasy.test.BaseResourceTest; +import org.junit.Before; +import org.junit.Test; + +import static org.jboss.resteasy.test.TestPortProvider.generateBaseUrl; +import static junit.framework.Assert.assertTrue; + +public class GenericResourceTest extends BaseResourceTest +{ + StudentInterface proxy; + + @Before + public void setUp() { + addPerRequestResource(StudentCrudResource.class); + getProviderFactory().registerProvider(StudentReader.class); + getProviderFactory().registerProvider(StudentWriter.class); + + proxy = ProxyFactory.create(StudentInterface.class, generateBaseUrl()); + } + + @Test + public void testGet() + { + assertTrue(proxy.get(1).getName().equals("Jozef Hartinger")); + } + + @Test + public void testPut() + { + proxy.put(2, new Student("John Doe")); + assertTrue(proxy.get(2).getName().equals("John Doe")); + } +} Index: src/test/java/org/jboss/resteasy/test/resource/generic/CrudResource.java =================================================================== --- src/test/java/org/jboss/resteasy/test/resource/generic/CrudResource.java (revision 0) +++ src/test/java/org/jboss/resteasy/test/resource/generic/CrudResource.java (revision 0) @@ -0,0 +1,33 @@ +package org.jboss.resteasy.test.resource.generic; + +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +/** + * This is a sample of a CRUD resource template which can be reused for different entities. + * @author Jozef Hartinger + * + * @param Type of the entity which CRUD operations are performed on. + * @param Type of the entity identified i.e. java.lang.Long + */ +public abstract class CrudResource +{ + abstract ENTITY_TYPE getEntity(ENTITY_IDENTIFIER_TYPE id); + abstract void setEntity(ENTITY_IDENTIFIER_TYPE id, ENTITY_TYPE entity); + + @GET + @Path("/{id}") + public ENTITY_TYPE get(@PathParam("id") ENTITY_IDENTIFIER_TYPE id) + { + return getEntity(id); + } + + @PUT + @Path("/{id}") + public void put(@PathParam("id") ENTITY_IDENTIFIER_TYPE id, ENTITY_TYPE entity) + { + setEntity(id, entity); + } +} Index: src/test/java/org/jboss/resteasy/test/resource/generic/Student.java =================================================================== --- src/test/java/org/jboss/resteasy/test/resource/generic/Student.java (revision 0) +++ src/test/java/org/jboss/resteasy/test/resource/generic/Student.java (revision 0) @@ -0,0 +1,31 @@ +package org.jboss.resteasy.test.resource.generic; + +public class Student +{ + private String name; + + public Student() + { + } + + public Student(String name) + { + this.name = name; + } + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + @Override + public String toString() + { + return "Student: " + name; + } +} Index: src/test/java/org/jboss/resteasy/test/resource/generic/StudentInterface.java =================================================================== --- src/test/java/org/jboss/resteasy/test/resource/generic/StudentInterface.java (revision 0) +++ src/test/java/org/jboss/resteasy/test/resource/generic/StudentInterface.java (revision 0) @@ -0,0 +1,25 @@ +package org.jboss.resteasy.test.resource.generic; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; + +/** + * This interface is used on the client side only. + * @author Jozef Hartinger + * + */ + +@Path("/student/{id}") +@Produces("application/student") +@Consumes("application/student") +public interface StudentInterface +{ + @GET + Student get(@PathParam("id") Integer id); + @PUT + void put(@PathParam("id") Integer id, Student entity); +} Index: src/test/java/org/jboss/resteasy/test/resource/generic/StudentReader.java =================================================================== --- src/test/java/org/jboss/resteasy/test/resource/generic/StudentReader.java (revision 0) +++ src/test/java/org/jboss/resteasy/test/resource/generic/StudentReader.java (revision 0) @@ -0,0 +1,40 @@ +package org.jboss.resteasy.test.resource.generic; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.Consumes; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.Provider; + +@Provider +@Consumes("application/student") +public class StudentReader implements MessageBodyReader +{ + + public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) + { + return true; + } + + public Student readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException + { + BufferedReader br = null; + try + { + br = new BufferedReader(new InputStreamReader(entityStream)); + return new Student(br.readLine()); + } + catch (Exception e) + { + throw new RuntimeException("Unable to parse student.", e); + } + } +} Index: src/test/java/org/jboss/resteasy/test/resource/generic/StudentCrudResource.java =================================================================== --- src/test/java/org/jboss/resteasy/test/resource/generic/StudentCrudResource.java (revision 0) +++ src/test/java/org/jboss/resteasy/test/resource/generic/StudentCrudResource.java (revision 0) @@ -0,0 +1,39 @@ +package org.jboss.resteasy.test.resource.generic; + +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.Consumes; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +/** + * RESTEasy should be able to use type parameter values (Student, Integer) for (de)marshalling parameters/entity body. + * @author Jozef Hartinger + * + */ +@Path("/student") +@Produces("application/student") +@Consumes("application/student") +public class StudentCrudResource extends CrudResource +{ + + private static Map students = new HashMap(); + + public StudentCrudResource() + { + students.put(1, new Student("Jozef Hartinger")); + } + + @Override + Student getEntity(Integer id) + { + return students.get(id); + } + + @Override + void setEntity(Integer id, Student entity) + { + students.put(id, entity); + } +}