forked from resteasy/resteasy
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example - builtin multi valued param converters
- Loading branch information
1 parent
b842a65
commit 08e0eb6
Showing
8 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...-jaxrs/src/main/java/org/jboss/resteasy/convertors/MultiValuedParamConverterProvider.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,35 @@ | ||
package org.jboss.resteasy.convertors; | ||
|
||
import javax.ws.rs.ext.ParamConverter; | ||
import javax.ws.rs.ext.ParamConverterProvider; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
// import java.util.Set; | ||
|
||
/** | ||
* @author Marek Kopecky mkopecky@redhat.com | ||
*/ | ||
public class MultiValuedParamConverterProvider implements ParamConverterProvider { | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { | ||
Type className = null; | ||
try { | ||
className = ((java.lang.reflect.ParameterizedType) genericType).getActualTypeArguments()[0]; | ||
} catch (Throwable t) { | ||
return null; | ||
} | ||
|
||
if (List.class.isAssignableFrom(rawType)) { | ||
return (ParamConverter<T>) new MultiValuedParamListConverter((Class<?>) className); | ||
} | ||
|
||
// if (Set.class.isAssignableFrom(rawType)) { | ||
// return (ParamConverter<T>) new MultiValuedParamSetConverter(className.getClass()); | ||
// } | ||
|
||
return null; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...easy-jaxrs/src/main/java/org/jboss/resteasy/convertors/MultiValuedParamListConverter.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,61 @@ | ||
package org.jboss.resteasy.convertors; | ||
|
||
|
||
import javax.ws.rs.ext.ParamConverter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Marek Kopecky mkopecky@redhat.com | ||
*/ | ||
public class MultiValuedParamListConverter<T> implements ParamConverter<List<T>> { | ||
private final Class<T> type; | ||
|
||
public MultiValuedParamListConverter(Class<T> type) { | ||
this.type = type; | ||
} | ||
|
||
public MultiValuedParamListConverter() { | ||
this.type = null; | ||
} | ||
|
||
|
||
@Override | ||
public List<T> fromString(String param) { | ||
if (param == null || param.trim().isEmpty()) { | ||
return null; | ||
} | ||
return parse(param.split(",")); | ||
} | ||
|
||
private List<T> parse(String[] params) { | ||
List<T> list = new ArrayList<T>(); | ||
try { | ||
for (String param : params) { | ||
T object = (T) type.getMethod("valueOf", String.class).invoke(null, param); | ||
list.add(object); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return list; | ||
} | ||
|
||
|
||
|
||
|
||
@Override | ||
public String toString(List<T> list) { | ||
if (list == null || list.isEmpty()) { | ||
return null; | ||
} | ||
return stringify(list); | ||
} | ||
private <T> String stringify(List<T> list) { | ||
StringBuffer sb = new StringBuffer(); | ||
for (T s : list) { | ||
sb.append(s).append(','); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
resteasy-jaxrs/src/main/java/org/jboss/resteasy/convertors/MultiValuedParamSetConverter.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 @@ | ||
package org.jboss.resteasy.convertors; | ||
|
||
/** | ||
* @author Marek Kopecky mkopecky@redhat.com | ||
*/ | ||
public class MultiValuedParamSetConverter { | ||
// TBD | ||
} |
85 changes: 85 additions & 0 deletions
85
...ests/src/test/java/org/jboss/resteasy/test/resource/param/MultiValueParamBuiltinTest.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,85 @@ | ||
package org.jboss.resteasy.test.resource.param; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.container.test.api.RunAsClient; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.resteasy.client.jaxrs.ResteasyClient; | ||
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; | ||
import org.jboss.resteasy.test.resource.param.resource.MultiValueParamBuiltinMyObject1; | ||
import org.jboss.resteasy.test.resource.param.resource.MultiValueParamBuiltinMyObject2; | ||
import org.jboss.resteasy.test.resource.param.resource.MultiValueParamBuiltinResource; | ||
import org.jboss.resteasy.utils.PortProviderUtil; | ||
import org.jboss.resteasy.utils.TestUtil; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
|
||
/** | ||
* @tpSubChapter Parameters | ||
* @tpChapter Integration tests | ||
* @tpTestCaseDetails Usage of builtin MultiValuedParamConverterProvider | ||
* @tpSince RESTEasy 3.6 | ||
*/ | ||
@RunWith(Arquillian.class) | ||
@RunAsClient | ||
public class MultiValueParamBuiltinTest { | ||
|
||
@Deployment | ||
public static Archive<?> deploy() { | ||
WebArchive war = TestUtil.prepareArchive(MultiValueParamBuiltinTest.class.getSimpleName()); | ||
war.addClass(MultiValueParamBuiltinMyObject1.class); | ||
war.addClass(MultiValueParamBuiltinMyObject2.class); | ||
war.addClass(MultiValueParamBuiltinResource.class); | ||
war.addAsWebInfResource(MultiValueParamBuiltinTest.class.getPackage(), "multi-value-param-provided-web.xml", "web.xml"); | ||
|
||
return war; | ||
} | ||
|
||
private String generateURL(String path) { | ||
return PortProviderUtil.generateURL(path, MultiValueParamBuiltinTest.class.getSimpleName()); | ||
} | ||
|
||
|
||
static String value1 = "1+2"; | ||
static String value2 = "3+4"; | ||
static String value3 = "5+6"; | ||
|
||
/** | ||
* @tpTestDetails Simple class test | ||
* @tpSince RESTEasy 3.6 | ||
*/ | ||
@Test | ||
public void testQueryParamSimpleClass() throws Exception { | ||
ResteasyClient client = new ResteasyClientBuilder().build(); | ||
try { | ||
Response queryParamResourceClient = client.target(generateURL("/queryParam/list1")) | ||
.queryParam("q", value1 + "," + value2 + "," + value3).request().get(); | ||
Assert.assertEquals("Wrong response", "1+2,3+4,5+6,", queryParamResourceClient.readEntity(String.class)); | ||
} finally { | ||
client.close(); | ||
} | ||
} | ||
|
||
/** | ||
* @tpTestDetails Complex class test | ||
* @tpSince RESTEasy 3.6 | ||
*/ | ||
@Test | ||
public void testQueryParamComplexClass() throws Exception { | ||
ResteasyClient client = new ResteasyClientBuilder().build(); | ||
try { | ||
Response queryParamResourceClient = client.target(generateURL("/queryParam/list2")) | ||
.queryParam("q", value1 + "," + value2 + "," + value3).request().get(); | ||
Assert.assertEquals("Wrong response", "1....2,3....4,5....6,", queryParamResourceClient.readEntity(String.class)); | ||
} finally { | ||
client.close(); | ||
} | ||
} | ||
|
||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...java/org/jboss/resteasy/test/resource/param/resource/MultiValueParamBuiltinMyObject1.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,29 @@ | ||
package org.jboss.resteasy.test.resource.param.resource; | ||
|
||
/** | ||
* @author Marek Kopecky mkopecky@redhat.com | ||
*/ | ||
public class MultiValueParamBuiltinMyObject1 { | ||
|
||
String a; | ||
|
||
|
||
public static MultiValueParamBuiltinMyObject1 valueOf(String s) { | ||
MultiValueParamBuiltinMyObject1 myObject = new MultiValueParamBuiltinMyObject1(); | ||
myObject.setA(s); | ||
return myObject; | ||
} | ||
|
||
public String getA() { | ||
return a; | ||
} | ||
|
||
public void setA(String a) { | ||
this.a = a; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getA(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...java/org/jboss/resteasy/test/resource/param/resource/MultiValueParamBuiltinMyObject2.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,44 @@ | ||
package org.jboss.resteasy.test.resource.param.resource; | ||
|
||
/** | ||
* @author Marek Kopecky mkopecky@redhat.com | ||
*/ | ||
public class MultiValueParamBuiltinMyObject2 { | ||
String a; | ||
String b; | ||
|
||
|
||
public static MultiValueParamBuiltinMyObject2 valueOf(String s) { | ||
String[] array = s.split("\\+"); | ||
|
||
MultiValueParamBuiltinMyObject2 myObject = new MultiValueParamBuiltinMyObject2(); | ||
myObject.setA(array[0]); | ||
myObject.setB(array[1]); | ||
return myObject; | ||
} | ||
|
||
public String getA() { | ||
return a; | ||
} | ||
|
||
public void setA(String a) { | ||
this.a = a; | ||
} | ||
|
||
public String getB() { | ||
return b; | ||
} | ||
|
||
public void setB(String b) { | ||
this.b = b; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getA() + "+" + getB(); | ||
} | ||
|
||
public String specialToString() { | ||
return getA() + "...." + getB(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
.../java/org/jboss/resteasy/test/resource/param/resource/MultiValueParamBuiltinResource.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,44 @@ | ||
package org.jboss.resteasy.test.resource.param.resource; | ||
|
||
import org.jboss.resteasy.annotations.jaxrs.QueryParam; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Response; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author Marek Kopecky mkopecky@redhat.com | ||
*/ | ||
@Path("queryParam") | ||
public class MultiValueParamBuiltinResource { | ||
|
||
@GET | ||
@Path("list1") | ||
public Response conversion1(@QueryParam List<MultiValueParamBuiltinMyObject1> q) { | ||
if (q == null) throw new RuntimeException("q is null"); | ||
System.out.println(q.size()); | ||
q.stream().map(myObject -> myObject == null ? "null" : myObject.getA()).forEach(s -> System.out.println(s)); | ||
return Response.ok(stringify(q.stream().map(myObject -> myObject.toString()).collect(Collectors.toList()))).build(); | ||
} | ||
|
||
@GET | ||
@Path("list2") | ||
public Response conversion2(@QueryParam List<MultiValueParamBuiltinMyObject2> q) { | ||
if (q == null) throw new RuntimeException("q is null"); | ||
System.out.println(q.size()); | ||
q.stream().map(myObject -> myObject == null ? "null" : myObject.specialToString()).forEach(s -> System.out.println(s)); | ||
return Response.ok(stringify(q.stream().map(myObject -> myObject.specialToString()).collect(Collectors.toList()))).build(); | ||
} | ||
|
||
|
||
|
||
private static String stringify(List<String> list) { | ||
StringBuffer sb = new StringBuffer(); | ||
for (String s : list) { | ||
sb.append(s).append(','); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../test/resources/org/jboss/resteasy/test/resource/param/multi-value-param-provided-web.xml
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,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<web-app> | ||
|
||
<servlet-mapping> | ||
<servlet-name>javax.ws.rs.core.Application</servlet-name> | ||
<url-pattern>/*</url-pattern> | ||
</servlet-mapping> | ||
<context-param> | ||
<param-name>resteasy.providers</param-name> | ||
<param-value>org.jboss.resteasy.convertors.MultiValuedParamConverterProvider</param-value> | ||
</context-param> | ||
|
||
</web-app> |