Issue 76: add support for @QueryParam

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1926 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-09-25 17:22:34 +00:00
parent 4137c3ef85
commit 98b759f51a
2 changed files with 38 additions and 0 deletions

View File

@ -45,6 +45,7 @@ import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.ws.rs.HeaderParam; import javax.ws.rs.HeaderParam;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilder;
@ -93,6 +94,7 @@ public class JaxrsAnnotationProcessor {
private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToEntityAnnotation = createMethodToIndexOfParamToAnnotation(EntityParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToEntityAnnotation = createMethodToIndexOfParamToAnnotation(EntityParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHeaderParamAnnotations = createMethodToIndexOfParamToAnnotation(HeaderParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHeaderParamAnnotations = createMethodToIndexOfParamToAnnotation(HeaderParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHostPrefixParamAnnotations = createMethodToIndexOfParamToAnnotation(HostPrefixParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHostPrefixParamAnnotations = createMethodToIndexOfParamToAnnotation(HostPrefixParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToQueryParamAnnotations = createMethodToIndexOfParamToAnnotation(QueryParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPathParamAnnotations = createMethodToIndexOfParamToAnnotation(PathParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPathParamAnnotations = createMethodToIndexOfParamToAnnotation(PathParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPostParamAnnotations = createMethodToIndexOfParamToAnnotation(MapEntityParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPostParamAnnotations = createMethodToIndexOfParamToAnnotation(MapEntityParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToParamParserAnnotations = createMethodToIndexOfParamToAnnotation(ParamParser.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToParamParserAnnotations = createMethodToIndexOfParamToAnnotation(ParamParser.class);
@ -202,6 +204,7 @@ public class JaxrsAnnotationProcessor {
methodToIndexOfParamToEntityAnnotation.get(method).get(index); methodToIndexOfParamToEntityAnnotation.get(method).get(index);
methodToIndexOfParamToHeaderParamAnnotations.get(method).get(index); methodToIndexOfParamToHeaderParamAnnotations.get(method).get(index);
methodToIndexOfParamToHostPrefixParamAnnotations.get(method).get(index); methodToIndexOfParamToHostPrefixParamAnnotations.get(method).get(index);
methodToindexOfParamToQueryParamAnnotations.get(method).get(index);
methodToindexOfParamToPathParamAnnotations.get(method).get(index); methodToindexOfParamToPathParamAnnotations.get(method).get(index);
methodToindexOfParamToPostParamAnnotations.get(method).get(index); methodToindexOfParamToPostParamAnnotations.get(method).get(index);
methodToindexOfParamToParamParserAnnotations.get(method).get(index); methodToindexOfParamToParamParserAnnotations.get(method).get(index);
@ -282,6 +285,10 @@ public class JaxrsAnnotationProcessor {
addQuery(builder, query); addQuery(builder, query);
} }
for (Entry<String, String> query : getQueryParamKeyValues(method, args).entrySet()) {
builder.queryParam(query.getKey(), query.getValue());
}
Multimap<String, String> headers = buildHeaders(method, args); Multimap<String, String> headers = buildHeaders(method, args);
HttpRequestOptions options = findOptionsIn(method, args); HttpRequestOptions options = findOptionsIn(method, args);
@ -646,6 +653,21 @@ public class JaxrsAnnotationProcessor {
return pathParamValues; return pathParamValues;
} }
private Map<String, String> getQueryParamKeyValues(Method method, Object[] args) {
Map<String, String> queryParamValues = Maps.newHashMap();
queryParamValues.putAll(constants);
Map<Integer, Set<Annotation>> indexToQueryParam = methodToindexOfParamToQueryParamAnnotations
.get(method);
for (Entry<Integer, Set<Annotation>> entry : indexToQueryParam.entrySet()) {
for (Annotation key : entry.getValue()) {
String paramKey = ((QueryParam) key).value();
String paramValue = args[entry.getKey()].toString();
queryParamValues.put(paramKey, paramValue);
}
}
return queryParamValues;
}
private Map<String, String> buildPostParams(Method method, Object[] args) { private Map<String, String> buildPostParams(Method method, Object[] args) {
Map<String, String> postParams = Maps.newHashMap(); Map<String, String> postParams = Maps.newHashMap();
Map<Integer, Set<Annotation>> indexToPathParam = methodToindexOfParamToPostParamAnnotations Map<Integer, Set<Annotation>> indexToPathParam = methodToindexOfParamToPostParamAnnotations

View File

@ -48,6 +48,7 @@ import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import org.jclouds.concurrent.WithinThreadExecutorService; import org.jclouds.concurrent.WithinThreadExecutorService;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
@ -115,6 +116,11 @@ public class JaxrsAnnotationProcessorTest {
@QueryParams(keys = { "foo", "fooble" }, values = { "bar", "baz" }) @QueryParams(keys = { "foo", "fooble" }, values = { "bar", "baz" })
public void foo2() { public void foo2() {
} }
@FOO
@QueryParams(keys = { "foo", "fooble" }, values = { "bar", "baz" })
public void foo3(@QueryParam("robbie") String robbie) {
}
} }
public void testQuery() throws SecurityException, NoSuchMethodException { public void testQuery() throws SecurityException, NoSuchMethodException {
@ -138,6 +144,16 @@ public class JaxrsAnnotationProcessorTest {
assertEquals(httpMethod.getMethod(), "FOO"); assertEquals(httpMethod.getMethod(), "FOO");
} }
public void testQuery3() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("foo3", String.class);
HttpRequest httpMethod = factory.create(TestQuery.class).createRequest(method,
new Object[] {"wonder"});
assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "");
assertEquals(httpMethod.getEndpoint().getQuery(),
"x-ms-version=2009-07-17&foo=bar&fooble=baz&robbie=wonder");
assertEquals(httpMethod.getMethod(), "FOO");
}
@Endpoint(Localhost.class) @Endpoint(Localhost.class)
public class TestCustomMethod { public class TestCustomMethod {
@FOO @FOO