Merge pull request #1078 from rackspace/jclouds-1077-queryparam-iterable

Added support for Iterable collections to QueryParam.
This commit is contained in:
Adrian Cole 2012-12-20 10:01:03 -08:00
commit 27dbd46409
3 changed files with 67 additions and 3 deletions

View File

@ -137,7 +137,7 @@ public class NodeApiExpectTest extends BaseCloudLoadBalancerApiExpectTest<CloudL
}
public void testRemoveNodesFromLoadBalancer() {
URI endpoint = URI.create("https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/123123/loadbalancers/2000/nodes?id=%5B410%2C%20411%5D");
URI endpoint = URI.create("https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/123123/loadbalancers/2000/nodes?id=410&id=411");
NodeApi api = requestsSendResponses(
rackspaceAuthWithUsernameAndApiKey,
responseWithAccess,

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.rest.internal;
import static com.google.common.base.Functions.toStringFunction;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Collections2.filter;
@ -1317,10 +1318,17 @@ public class RestAnnotationProcessor<T> {
Set<Annotation> extractors = indexToParamExtractor.getUnchecked(entry.getKey());
String paramKey = ((QueryParam) key).value();
Optional<?> paramValue = getParamValue(method, args, extractors, entry, paramKey);
if (paramValue.isPresent())
if (paramValue.isPresent()) {
if (paramValue.get() instanceof Iterable) {
Iterable<String> iterableStrings = transform(Iterable.class.cast(paramValue.get()), toStringFunction());
queryParamValues.putAll(paramKey, iterableStrings);
}
else {
queryParamValues.put(paramKey, paramValue.get().toString());
}
}
}
}
if (method.isAnnotationPresent(QueryParam.class) && method.isAnnotationPresent(ParamParser.class)) {
String paramKey = method.getAnnotation(QueryParam.class).value();

View File

@ -41,6 +41,7 @@ import java.net.URI;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.NoSuchElementException;
@ -151,6 +152,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Multimap;
import com.google.common.io.Files;
import com.google.common.util.concurrent.Futures;
@ -507,6 +509,11 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
@QueryParams(keys = { "foo", "fooble" }, values = { "bar", "baz" })
public void foo3Nullable(@Nullable @QueryParam("robbie") String robbie) {
}
@FOO
@Path("/")
public void queryParamIterable(@Nullable @QueryParam("foo") Iterable<String> bars) {
}
}
public void testUnEncodeQuery() {
@ -565,6 +572,55 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
assertEquals(request.getMethod(), "FOO");
}
public void testQueryParamIterableOneString() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("queryParamIterable", Iterable.class);
Set<String> bars = ImmutableSortedSet.<String> of("1");
HttpRequest request = factory(TestPath.class).createRequest(method, new Object[] { bars });
assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/");
assertEquals(request.getEndpoint().getQuery(), "foo=1");
assertEquals(request.getMethod(), "FOO");
}
public void testQueryParamIterableString() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("queryParamIterable", Iterable.class);
Set<String> bars = ImmutableSortedSet.<String> of("1", "2", "3");
HttpRequest request = factory(TestPath.class).createRequest(method, new Object[] { bars });
assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/");
assertEquals(request.getEndpoint().getQuery(), "foo=1&foo=2&foo=3");
assertEquals(request.getMethod(), "FOO");
}
public void testQueryParamIterableInteger() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("queryParamIterable", Iterable.class);
Set<Integer> bars = ImmutableSortedSet.<Integer> of(1, 2, 3);
HttpRequest request = factory(TestPath.class).createRequest(method, new Object[] { bars });
assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/");
assertEquals(request.getEndpoint().getQuery(), "foo=1&foo=2&foo=3");
assertEquals(request.getMethod(), "FOO");
}
public void testQueryParamIterableEmpty() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("queryParamIterable", Iterable.class);
Set<String> bars = Collections.emptySet();
HttpRequest request = factory(TestPath.class).createRequest(method, new Object[] { bars });
assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/");
assertEquals(request.getEndpoint().getQuery(), null);
assertEquals(request.getMethod(), "FOO");
}
public void testQueryParamIterableNull() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("queryParamIterable", Iterable.class);
HttpRequest request = factory(TestPath.class).createRequest(method, (Iterable<String>) null);
assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/");
assertEquals(request.getEndpoint().getQuery(), null);
assertEquals(request.getMethod(), "FOO");
}
public interface TestPayloadParamVarargs {
@POST
public void varargs(HttpRequestOptions... options);