Skip to content

Commit a9c952d

Browse files
committed
more work on issue 441, adding more param mappings
1 parent b758c1f commit a9c952d

File tree

5 files changed

+215
-162
lines changed

5 files changed

+215
-162
lines changed

modules/org.restlet.ext.jaxrs/src/org/restlet/ext/jaxrs/internal/client/JaxRsClientInvocationHandler.java

+100-63
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,22 @@
3737
import java.lang.annotation.Annotation;
3838
import java.lang.reflect.Method;
3939

40+
import javax.ws.rs.CookieParam;
41+
import javax.ws.rs.FormParam;
4042
import javax.ws.rs.HeaderParam;
43+
import javax.ws.rs.MatrixParam;
4144
import javax.ws.rs.Path;
45+
import javax.ws.rs.PathParam;
4246
import javax.ws.rs.QueryParam;
4347

4448
import org.restlet.Request;
45-
import org.restlet.engine.header.Header;
49+
import org.restlet.data.Parameter;
4650
import org.restlet.engine.resource.ClientInvocationHandler;
4751
import org.restlet.ext.jaxrs.JaxRsClientResource;
52+
import org.restlet.ext.jaxrs.internal.exceptions.IllegalMethodParamTypeException;
4853
import org.restlet.ext.jaxrs.internal.util.Util;
4954
import org.restlet.representation.Representation;
5055
import org.restlet.resource.ClientResource;
51-
import org.restlet.service.ConverterService;
52-
import org.restlet.util.Series;
5356

5457
/**
5558
* Reflection proxy invocation handler created for the
@@ -65,75 +68,109 @@
6568
*/
6669
public class JaxRsClientInvocationHandler<T> extends ClientInvocationHandler<T> {
6770

68-
private ClientResource clientResource;
71+
private ClientResource clientResource;
6972

7073
/**
71-
* Constructor.
72-
*
73-
* @param clientResource
74-
* The client resource.
75-
* @param resourceInterface
76-
* The annotated resource interface.
77-
*/
78-
public JaxRsClientInvocationHandler(ClientResource clientResource,
79-
Class<? extends T> resourceInterface) {
80-
super(clientResource, resourceInterface, JaxRsAnnotationUtils
81-
.getInstance());
82-
83-
this.clientResource = clientResource;
84-
}
85-
86-
@Override
87-
protected Request getRequest(Method javaMethod, Object[] args) {
88-
Request request = super.getRequest(javaMethod, args);
89-
90-
setRequestParams(javaMethod, args, request);
91-
92-
setRequestPathToAnnotationPath(javaMethod, request);
93-
94-
return request;
95-
}
74+
* Constructor.
75+
*
76+
* @param clientResource
77+
* The client resource.
78+
* @param resourceInterface
79+
* The annotated resource interface.
80+
*/
81+
public JaxRsClientInvocationHandler(ClientResource clientResource,
82+
Class<? extends T> resourceInterface) {
83+
super(clientResource, resourceInterface, JaxRsAnnotationUtils
84+
.getInstance());
85+
86+
this.clientResource = clientResource;
87+
}
88+
89+
@Override
90+
protected Request getRequest(Method javaMethod, Object[] args)
91+
throws Throwable {
92+
Request request = super.getRequest(javaMethod, args);
93+
94+
setRequestParams(javaMethod, args, request);
95+
96+
setRequestPathToAnnotationPath(javaMethod, request);
97+
98+
return request;
99+
}
96100

97101
private void setRequestParams(Method javaMethod, Object[] args,
98-
Request request) {
99-
Annotation[][] parameterAnnotations = javaMethod.getParameterAnnotations();
100-
for( Annotation[] annotations : parameterAnnotations ) {
101-
for( Annotation annotation : annotations ) {
102-
103-
if(annotation instanceof HeaderParam) {
104-
HeaderParam headerParam = (HeaderParam)annotation;
105-
String value = headerParam.value();
106-
Series< Header > headers = Util.getHttpHeaders( request );
107-
108-
ConverterService converterService = clientResource.getApplication().getConverterService();
109-
110-
//TODO - don't just use args[0], each param should be set
111-
Representation representation = converterService.toRepresentation( args[0] );
112-
try {
113-
headers.add( value, representation.getText() );
114-
}
115-
catch ( IOException exception ) {
116-
throw new RuntimeException(exception);
117-
}
118-
} else if(annotation instanceof QueryParam) {
119-
// TODO - encode & map QueryParam
120-
//resourceRef.addQueryParameter( new Parameter( "point", "<java.awt.Point> <x>22</x> <y>33</y> </java.awt.Point>" ) );
121-
}
122-
// TODO - other param types
123-
}
124-
}
102+
Request request) throws IllegalMethodParamTypeException {
103+
104+
int argIndex = 0;
105+
106+
Annotation[][] parameterAnnotations = javaMethod
107+
.getParameterAnnotations();
108+
for (Annotation[] annotations : parameterAnnotations) {
109+
110+
String representationAsText = getRepresentationAsText(argIndex,
111+
args);
112+
113+
for (Annotation annotation : annotations) {
114+
if (annotation instanceof HeaderParam
115+
&& representationAsText != null) {
116+
Util.getHttpHeaders(request).add(
117+
((HeaderParam) annotation).value(),
118+
representationAsText);
119+
argIndex++;
120+
} else if (annotation instanceof QueryParam) {
121+
request.getResourceRef().addQueryParameter(
122+
new Parameter(((QueryParam) annotation).value(),
123+
representationAsText));
124+
argIndex++;
125+
} else if (annotation instanceof MatrixParam
126+
&& representationAsText != null) {
127+
// TODO
128+
argIndex++;
129+
} else if (annotation instanceof CookieParam
130+
&& representationAsText != null) {
131+
// TODO
132+
argIndex++;
133+
} else if (annotation instanceof FormParam
134+
&& representationAsText != null) {
135+
// TODO
136+
argIndex++;
137+
} else if (annotation instanceof QueryParam
138+
&& representationAsText != null) {
139+
// TODO
140+
argIndex++;
141+
} else if (annotation instanceof PathParam
142+
&& representationAsText != null) {
143+
// TODO
144+
argIndex++;
145+
}
146+
}
147+
}
148+
149+
// TODO - possibly throw an exception if the arg count != processed annotations?
150+
}
151+
152+
private String getRepresentationAsText(int argIndex, Object[] args) {
153+
String representationAsText = null;
154+
Representation representation = clientResource.getApplication()
155+
.getConverterService().toRepresentation(args[argIndex]);
156+
try {
157+
representationAsText = representation.getText();
158+
} catch (IOException exception) {
159+
throw new RuntimeException(exception);
160+
}
161+
return representationAsText;
125162
}
126163

127164
private void setRequestPathToAnnotationPath(Method javaMethod,
128165
Request request) {
129166
Path methodPathAnnotation = javaMethod.getAnnotation(Path.class);
130-
if (methodPathAnnotation != null) {
131-
String methodPath = methodPathAnnotation.value();
132-
if (methodPath != null && methodPath.length() > 0) {
133-
request.getResourceRef().setPath(
134-
request.getResourceRef().getPath() + "/" + methodPath);
135-
}
136-
}
167+
if (methodPathAnnotation != null) {
168+
String methodPath = methodPathAnnotation.value();
169+
if (methodPath != null && methodPath.length() > 0) {
170+
request.getResourceRef().setPath(
171+
request.getResourceRef().getPath() + "/" + methodPath);
172+
}
173+
}
137174
}
138175

139176
}

modules/org.restlet.test/src/org/restlet/test/ext/jaxrs/client/JaxRsClientTest.java

+97-86
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
import org.restlet.engine.converter.ConverterHelper;
4646
import org.restlet.ext.jaxrs.JaxRsClientResource;
4747
import org.restlet.ext.xstream.XstreamConverter;
48-
import org.restlet.test.ext.jaxrs.services.point.EchoResource;
49-
import org.restlet.test.ext.jaxrs.services.point.EchoResourceImpl;
48+
import org.restlet.test.ext.jaxrs.services.echo.EchoResource;
49+
import org.restlet.test.ext.jaxrs.services.echo.EchoResourceImpl;
5050
import org.restlet.test.ext.jaxrs.services.tests.JaxRsTestCase;
5151

5252
/**
@@ -56,97 +56,108 @@
5656
* @see <a
5757
* href="https://github.com/restlet/restlet-framework-java/issues/441">Issue
5858
* #441</a>
59-
* @author Shaun Elliot
59+
* @author Shaun Elliott
6060
*/
6161
public class JaxRsClientTest extends JaxRsTestCase {
62-
63-
//TODO - add tests for other param types: QueryParam, MatrixParam, CookieParam, etc.
64-
62+
63+
// TODO - add tests for other param types: QueryParam, MatrixParam,
64+
// CookieParam, etc.
65+
6566
private AtomicBoolean _serverStarted = new AtomicBoolean(false);
67+
private Object lock = new Object();
68+
69+
@Override
70+
protected Application getApplication() {
71+
return new Application() {
72+
@Override
73+
@SuppressWarnings({ "unchecked", "rawtypes" })
74+
public Set<Class<?>> getClasses() {
75+
return (Set) Collections.singleton(EchoResourceImpl.class);
76+
}
77+
};
78+
}
79+
80+
public void testEchoString() throws Exception {
81+
final JaxRsClientTest clientTest = startSocketServerDaemon();
82+
83+
// give the server a chance to come up before using it
84+
while (!_serverStarted.get()) {
85+
Thread.sleep(100);
86+
System.out.println("waiting for the server to start...");
87+
}
88+
89+
EchoResource echoResource = JaxRsClientResource.createJaxRsClient(
90+
"http://localhost:" + clientTest.getServerPort(),
91+
EchoResource.class);
92+
93+
assertEquals("this is a test", echoResource.echo("this is a test"));
94+
95+
synchronized (lock) {
96+
clientTest.stopServer();
97+
_serverStarted.set(false);
98+
}
99+
}
100+
101+
/*
102+
* Shows the problem addressed in:
103+
* https://github.com/restlet/restlet-framework-java/issues/441
104+
*/
105+
public void testEchoPoint() throws Exception {
106+
final JaxRsClientTest clientTest = startSocketServerDaemon();
107+
108+
// give the server a chance to come up before using it
109+
while (!_serverStarted.get()) {
110+
Thread.sleep(100);
111+
System.out.println("waiting for the server to start...");
112+
}
66113

67-
@Override
68-
protected Application getApplication() {
69-
return new Application() {
70-
@Override
71-
@SuppressWarnings({ "unchecked", "rawtypes" })
72-
public Set<Class<?>> getClasses() {
73-
return (Set) Collections.singleton(EchoResourceImpl.class);
74-
}
75-
};
76-
}
77-
78-
public void testEchoString() throws Exception {
79-
final JaxRsClientTest clientTest = startSocketServerDaemon();
80-
81-
// give the server a chance to come up before using it
82-
while(!_serverStarted.get()){
83-
Thread.sleep(100);
84-
}
85-
86-
EchoResource echoResource = JaxRsClientResource.createJaxRsClient(
87-
"http://localhost:" + clientTest.getServerPort(),
88-
EchoResource.class);
89-
90-
assertEquals("this is a test", echoResource.echo("this is a test"));
91-
92-
clientTest.stopServer();
93-
_serverStarted.set(false);
94-
}
95-
96-
/*
97-
* Shows the problem addressed in:
98-
* https://github.com/restlet/restlet-framework-java/issues/441
99-
*/
100-
public void testEchoPoint() throws Exception {
101-
final JaxRsClientTest clientTest = startSocketServerDaemon();
102-
103-
// give the server a chance to come up before using it
104-
while(!_serverStarted.get()){
105-
Thread.sleep(100);
106-
}
107-
108-
109-
EchoResource echoResource = JaxRsClientResource.createJaxRsClient(
110-
"http://localhost:" + clientTest.getServerPort(),
111-
EchoResource.class);
112-
113-
assertEquals(1, echoResource.echoPoint(new Point(1, 2)).x);
114-
115-
clientTest.stopServer();
116-
_serverStarted.set(false);
117-
}
118-
119-
private JaxRsClientTest startSocketServerDaemon()
120-
throws InterruptedException {
121-
122-
//there are a bunch of converters registered in the unit test project, we only want xstream
123-
List<ConverterHelper> registeredConverters = Engine.getInstance().getRegisteredConverters();
124-
for (int i = registeredConverters.size() - 1; i >= 0; i--) {
114+
EchoResource echoResource = JaxRsClientResource.createJaxRsClient(
115+
"http://localhost:" + clientTest.getServerPort(),
116+
EchoResource.class);
117+
118+
assertEquals(1, echoResource.echoPointHeaderParam(new Point(1, 2)).x);
119+
120+
assertEquals(3, echoResource.echoPointQueryParam(new Point(3, 4)).x);
121+
122+
synchronized (lock) {
123+
clientTest.stopServer();
124+
_serverStarted.set(false);
125+
}
126+
}
127+
128+
private JaxRsClientTest startSocketServerDaemon()
129+
throws InterruptedException {
130+
131+
// there are a bunch of converters registered in the unit test project,
132+
// we only want xstream
133+
List<ConverterHelper> registeredConverters = Engine.getInstance()
134+
.getRegisteredConverters();
135+
for (int i = registeredConverters.size() - 1; i >= 0; i--) {
125136
ConverterHelper converterHelper = registeredConverters.get(i);
126-
if(!(converterHelper instanceof XstreamConverter)){
137+
if (!(converterHelper instanceof XstreamConverter)) {
127138
registeredConverters.remove(i);
128139
}
129140
}
130-
131-
final JaxRsClientTest clientTest = new JaxRsClientTest();
132-
setUseTcp(true);
133-
134-
Thread t = new Thread(new Runnable() {
135-
@Override
136-
public void run() {
137-
try {
138-
clientTest.startServer(clientTest.createApplication());
139-
_serverStarted.set(true);
140-
} catch (Exception e) {
141-
throw new RuntimeException(e);
142-
}
143-
}
144-
});
145-
146-
t.setDaemon(true);
147-
t.start();
148-
149-
return clientTest;
150-
}
141+
142+
final JaxRsClientTest clientTest = new JaxRsClientTest();
143+
setUseTcp(true);
144+
145+
Thread t = new Thread(new Runnable() {
146+
@Override
147+
public void run() {
148+
try {
149+
clientTest.startServer(clientTest.createApplication());
150+
_serverStarted.set(true);
151+
} catch (Exception e) {
152+
throw new RuntimeException(e);
153+
}
154+
}
155+
});
156+
157+
t.setDaemon(true);
158+
t.start();
159+
160+
return clientTest;
161+
}
151162

152163
}

0 commit comments

Comments
 (0)