added endpoint parser that can collaborate with multiple parameters

This commit is contained in:
Adrian Cole 2010-08-09 01:26:59 -07:00
parent 10c2a0e36b
commit 1ec0c40c95
2 changed files with 282 additions and 209 deletions

View File

@ -159,7 +159,7 @@ public class RestAnnotationProcessor<T> {
static final Map<MethodKey, Method> delegationMap = newHashMap(); static final Map<MethodKey, Method> delegationMap = newHashMap();
static Map<Method, Map<Integer, Set<Annotation>>> createMethodToIndexOfParamToAnnotation( static Map<Method, Map<Integer, Set<Annotation>>> createMethodToIndexOfParamToAnnotation(
final Class<? extends Annotation> annotation) { final Class<? extends Annotation> annotation) {
return new MapMaker().makeComputingMap(new Function<Method, Map<Integer, Set<Annotation>>>() { return new MapMaker().makeComputingMap(new Function<Method, Map<Integer, Set<Annotation>>>() {
public Map<Integer, Set<Annotation>> apply(final Method method) { public Map<Integer, Set<Annotation>> apply(final Method method) {
return new MapMaker().makeComputingMap(new GetAnnotationsForMethodParameterIndex(method, annotation)); return new MapMaker().makeComputingMap(new GetAnnotationsForMethodParameterIndex(method, annotation));
@ -193,7 +193,7 @@ public class RestAnnotationProcessor<T> {
} }
private static final Class<? extends HttpRequestOptions[]> optionsVarArgsClass = new HttpRequestOptions[] {} private static final Class<? extends HttpRequestOptions[]> optionsVarArgsClass = new HttpRequestOptions[] {}
.getClass(); .getClass();
private static final Function<? super Entry<String, String>, ? extends Part> ENTRY_TO_PART = new Function<Entry<String, String>, Part>() { private static final Function<? super Entry<String, String>, ? extends Part> ENTRY_TO_PART = new Function<Entry<String, String>, Part>() {
@ -205,17 +205,17 @@ public class RestAnnotationProcessor<T> {
}; };
private final Map<Method, Set<Integer>> methodToIndexesOfOptions = new MapMaker() private final Map<Method, Set<Integer>> methodToIndexesOfOptions = new MapMaker()
.makeComputingMap(new Function<Method, Set<Integer>>() { .makeComputingMap(new Function<Method, Set<Integer>>() {
public Set<Integer> apply(final Method method) { public Set<Integer> apply(final Method method) {
Set<Integer> toReturn = newHashSet(); Set<Integer> toReturn = newHashSet();
for (int index = 0; index < method.getParameterTypes().length; index++) { for (int index = 0; index < method.getParameterTypes().length; index++) {
Class<?> type = method.getParameterTypes()[index]; Class<?> type = method.getParameterTypes()[index];
if (HttpRequestOptions.class.isAssignableFrom(type) || optionsVarArgsClass.isAssignableFrom(type)) if (HttpRequestOptions.class.isAssignableFrom(type) || optionsVarArgsClass.isAssignableFrom(type))
toReturn.add(index); toReturn.add(index);
}
return toReturn;
} }
}); return toReturn;
}
});
private final ParseSax.Factory parserFactory; private final ParseSax.Factory parserFactory;
private final HttpUtils utils; private final HttpUtils utils;
@ -233,7 +233,7 @@ public class RestAnnotationProcessor<T> {
@VisibleForTesting @VisibleForTesting
public static Function<HttpResponse, ?> createResponseParser(ParseSax.Factory parserFactory, Injector injector, public static Function<HttpResponse, ?> createResponseParser(ParseSax.Factory parserFactory, Injector injector,
Method method, HttpRequest request) { Method method, HttpRequest request) {
Function<HttpResponse, ?> transformer; Function<HttpResponse, ?> transformer;
Class<? extends HandlerWithResult<?>> handler = getSaxResponseParserClassOrNull(method); Class<? extends HandlerWithResult<?>> handler = getSaxResponseParserClassOrNull(method);
if (handler != null) { if (handler != null) {
@ -254,7 +254,7 @@ public class RestAnnotationProcessor<T> {
@VisibleForTesting @VisibleForTesting
public static Function<Exception, ?> createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation( public static Function<Exception, ?> createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(
Injector injector, Method method) { Injector injector, Method method) {
ExceptionParser annotation = method.getAnnotation(ExceptionParser.class); ExceptionParser annotation = method.getAnnotation(ExceptionParser.class);
if (annotation != null) { if (annotation != null) {
return injector.getInstance(annotation.value()); return injector.getInstance(annotation.value());
@ -265,7 +265,7 @@ public class RestAnnotationProcessor<T> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Inject @Inject
public RestAnnotationProcessor(Injector injector, ParseSax.Factory parserFactory, HttpUtils utils, public RestAnnotationProcessor(Injector injector, ParseSax.Factory parserFactory, HttpUtils utils,
TypeLiteral<T> typeLiteral) { TypeLiteral<T> typeLiteral) {
this.declaring = (Class<T>) typeLiteral.getRawType(); this.declaring = (Class<T>) typeLiteral.getRawType();
this.injector = injector; this.injector = injector;
this.parserFactory = parserFactory; this.parserFactory = parserFactory;
@ -380,7 +380,7 @@ public class RestAnnotationProcessor<T> {
public GeneratedHttpRequest<T> createRequest(Method method, Object... args) { public GeneratedHttpRequest<T> createRequest(Method method, Object... args) {
inputParamValidator.validateMethodParametersOrThrow(method, args); inputParamValidator.validateMethodParametersOrThrow(method, args);
ClassMethodArgs cma = logger.isTraceEnabled() ? new ClassMethodArgs(method.getDeclaringClass(), method, args) ClassMethodArgs cma = logger.isTraceEnabled() ? new ClassMethodArgs(method.getDeclaringClass(), method, args)
: null; : null;
URI endpoint = callerEndpoint; URI endpoint = callerEndpoint;
try { try {
@ -453,7 +453,7 @@ public class RestAnnotationProcessor<T> {
} }
GeneratedHttpRequest<T> request = new GeneratedHttpRequest<T>(httpMethod, endpoint, skips, declaring, method, GeneratedHttpRequest<T> request = new GeneratedHttpRequest<T>(httpMethod, endpoint, skips, declaring, method,
args); args);
addHostHeaderIfAnnotatedWithVirtualHost(headers, request.getEndpoint().getHost(), method); addHostHeaderIfAnnotatedWithVirtualHost(headers, request.getEndpoint().getHost(), method);
addFiltersIfAnnotated(method, request); addFiltersIfAnnotated(method, request);
@ -483,7 +483,7 @@ public class RestAnnotationProcessor<T> {
builder.path(clazz); builder.path(clazz);
builder.path(method); builder.path(method);
return builder.buildFromEncodedMap(convertUnsafe(encodeValues(getPathParamKeyValues(method, args), skips))) return builder.buildFromEncodedMap(convertUnsafe(encodeValues(getPathParamKeyValues(method, args), skips)))
.getPath(); .getPath();
} }
private Multimap<String, String> addPathAndGetTokens(Class<?> clazz, Method method, Object[] args, UriBuilder builder) { private Multimap<String, String> addPathAndGetTokens(Class<?> clazz, Method method, Object[] args, UriBuilder builder) {
@ -498,14 +498,14 @@ public class RestAnnotationProcessor<T> {
} }
public static URI replaceQuery(Provider<UriBuilder> uriBuilderProvider, URI in, String newQuery, public static URI replaceQuery(Provider<UriBuilder> uriBuilderProvider, URI in, String newQuery,
@Nullable Comparator<Entry<String, String>> sorter, char... skips) { @Nullable Comparator<Entry<String, String>> sorter, char... skips) {
UriBuilder builder = uriBuilderProvider.get().uri(in); UriBuilder builder = uriBuilderProvider.get().uri(in);
builder.replaceQuery(makeQueryLine(parseQueryToMap(newQuery), sorter, skips)); builder.replaceQuery(makeQueryLine(parseQueryToMap(newQuery), sorter, skips));
return builder.build(); return builder.build();
} }
private void addMatrixParams(UriBuilder builder, Collection<Entry<String, String>> tokenValues, Method method, private void addMatrixParams(UriBuilder builder, Collection<Entry<String, String>> tokenValues, Method method,
Object... args) { Object... args) {
if (declaring.isAnnotationPresent(MatrixParams.class)) { if (declaring.isAnnotationPresent(MatrixParams.class)) {
MatrixParams matrix = declaring.getAnnotation(MatrixParams.class); MatrixParams matrix = declaring.getAnnotation(MatrixParams.class);
addMatrix(builder, matrix, tokenValues); addMatrix(builder, matrix, tokenValues);
@ -522,7 +522,7 @@ public class RestAnnotationProcessor<T> {
} }
private Multimap<String, String> addFormParams(Collection<Entry<String, String>> tokenValues, Method method, private Multimap<String, String> addFormParams(Collection<Entry<String, String>> tokenValues, Method method,
Object... args) { Object... args) {
Multimap<String, String> formMap = LinkedListMultimap.create(); Multimap<String, String> formMap = LinkedListMultimap.create();
if (declaring.isAnnotationPresent(FormParams.class)) { if (declaring.isAnnotationPresent(FormParams.class)) {
FormParams form = declaring.getAnnotation(FormParams.class); FormParams form = declaring.getAnnotation(FormParams.class);
@ -541,7 +541,7 @@ public class RestAnnotationProcessor<T> {
} }
private Multimap<String, String> addQueryParams(Collection<Entry<String, String>> tokenValues, Method method, private Multimap<String, String> addQueryParams(Collection<Entry<String, String>> tokenValues, Method method,
Object... args) { Object... args) {
Multimap<String, String> queryMap = LinkedListMultimap.create(); Multimap<String, String> queryMap = LinkedListMultimap.create();
if (declaring.isAnnotationPresent(QueryParams.class)) { if (declaring.isAnnotationPresent(QueryParams.class)) {
QueryParams query = declaring.getAnnotation(QueryParams.class); QueryParams query = declaring.getAnnotation(QueryParams.class);
@ -560,7 +560,7 @@ public class RestAnnotationProcessor<T> {
} }
private void addForm(Multimap<String, String> formParams, FormParams form, private void addForm(Multimap<String, String> formParams, FormParams form,
Collection<Entry<String, String>> tokenValues) { Collection<Entry<String, String>> tokenValues) {
for (int i = 0; i < form.keys().length; i++) { for (int i = 0; i < form.keys().length; i++) {
if (form.values()[i].equals(FormParams.NULL)) { if (form.values()[i].equals(FormParams.NULL)) {
formParams.removeAll(form.keys()[i]); formParams.removeAll(form.keys()[i]);
@ -572,7 +572,7 @@ public class RestAnnotationProcessor<T> {
} }
private void addQuery(Multimap<String, String> queryParams, QueryParams query, private void addQuery(Multimap<String, String> queryParams, QueryParams query,
Collection<Entry<String, String>> tokenValues) { Collection<Entry<String, String>> tokenValues) {
for (int i = 0; i < query.keys().length; i++) { for (int i = 0; i < query.keys().length; i++) {
if (query.values()[i].equals(QueryParams.NULL)) { if (query.values()[i].equals(QueryParams.NULL)) {
queryParams.removeAll(query.keys()[i]); queryParams.removeAll(query.keys()[i]);
@ -613,20 +613,41 @@ public class RestAnnotationProcessor<T> {
} }
@VisibleForTesting @VisibleForTesting
public static URI getEndpointInParametersOrNull(Method method, Object[] args, Injector injector) { public static URI getEndpointInParametersOrNull(Method method, final Object[] args, Injector injector) {
Map<Integer, Set<Annotation>> map = RestAnnotationProcessor.indexWithOnlyOneAnnotation(method, "@EndpointParam", Map<Integer, Set<Annotation>> map = indexWithAtLeastOneAnnotation(method,
RestAnnotationProcessor.methodToIndexOfParamToEndpointParamAnnotations); methodToIndexOfParamToEndpointParamAnnotations);
if (map.size() == 1 && args.length > 0) { if (map.size() >= 1 && args.length > 0) {
EndpointParam annotation = (EndpointParam) map.values().iterator().next().iterator().next(); EndpointParam firstAnnotation = (EndpointParam) Iterables.get(Iterables.get(map.values(), 0), 0);
int index = map.keySet().iterator().next(); Function<Object, URI> parser = injector.getInstance(firstAnnotation.parser());
Function<Object, URI> parser = injector.getInstance(annotation.parser());
try { if (map.size() == 1) {
URI returnVal = parser.apply(args[index]); int index = map.keySet().iterator().next();
checkArgument(returnVal != null, String.format("endpoint for [%s] not configured for %s", args[index], try {
URI returnVal = parser.apply(args[index]);
checkArgument(returnVal != null, String.format("endpoint for [%s] not configured for %s", args[index],
method)); method));
return returnVal; return returnVal;
} catch (NullPointerException e) { } catch (NullPointerException e) {
throw new IllegalArgumentException(String.format("argument at index %d on method %s", index, method), e); throw new IllegalArgumentException(String.format("argument at index %d on method %s", index, method), e);
}
} else {
Iterable<Object> argsToParse = Iterables.transform(map.keySet(), new Function<Integer, Object>() {
@Override
public Object apply(Integer from) {
return args[from];
}
});
try {
URI returnVal = parser.apply(argsToParse);
checkArgument(returnVal != null, String.format("endpoint for [%s] not configured for %s", argsToParse,
method));
return returnVal;
} catch (NullPointerException e) {
throw new IllegalArgumentException(String.format("argument at indexes %s on method %s", map.keySet(),
method), e);
}
} }
} }
return null; return null;
@ -666,13 +687,13 @@ public class RestAnnotationProcessor<T> {
ResponseParser annotation = method.getAnnotation(ResponseParser.class); ResponseParser annotation = method.getAnnotation(ResponseParser.class);
if (annotation == null) { if (annotation == null) {
if (method.getReturnType().equals(void.class) if (method.getReturnType().equals(void.class)
|| TypeLiteral.get(method.getGenericReturnType()).equals(futureVoidLiteral)) { || TypeLiteral.get(method.getGenericReturnType()).equals(futureVoidLiteral)) {
return Key.get(ReleasePayloadAndReturn.class); return Key.get(ReleasePayloadAndReturn.class);
} else if (method.getReturnType().equals(boolean.class) || method.getReturnType().equals(Boolean.class) } else if (method.getReturnType().equals(boolean.class) || method.getReturnType().equals(Boolean.class)
|| TypeLiteral.get(method.getGenericReturnType()).equals(futureBooleanLiteral)) { || TypeLiteral.get(method.getGenericReturnType()).equals(futureBooleanLiteral)) {
return Key.get(ReturnTrueIf2xx.class); return Key.get(ReturnTrueIf2xx.class);
} else if (method.getReturnType().equals(InputStream.class) } else if (method.getReturnType().equals(InputStream.class)
|| TypeLiteral.get(method.getGenericReturnType()).equals(futureInputStreamLiteral)) { || TypeLiteral.get(method.getGenericReturnType()).equals(futureInputStreamLiteral)) {
return Key.get(ReturnInputStream.class); return Key.get(ReturnInputStream.class);
} else if (getAcceptHeadersOrNull(method).contains(MediaType.APPLICATION_JSON)) { } else if (getAcceptHeadersOrNull(method).contains(MediaType.APPLICATION_JSON)) {
Type returnVal; Type returnVal;
@ -693,10 +714,10 @@ public class RestAnnotationProcessor<T> {
parserType = Types.newParameterizedType(ParseJson.class, returnVal); parserType = Types.newParameterizedType(ParseJson.class, returnVal);
return (Key<? extends Function<HttpResponse, ?>>) Key.get(parserType); return (Key<? extends Function<HttpResponse, ?>>) Key.get(parserType);
} else if (method.getReturnType().equals(String.class) } else if (method.getReturnType().equals(String.class)
|| TypeLiteral.get(method.getGenericReturnType()).equals(futureStringLiteral)) { || TypeLiteral.get(method.getGenericReturnType()).equals(futureStringLiteral)) {
return Key.get(ReturnStringIf2xx.class); return Key.get(ReturnStringIf2xx.class);
} else if (method.getReturnType().equals(URI.class) } else if (method.getReturnType().equals(URI.class)
|| TypeLiteral.get(method.getGenericReturnType()).equals(futureURILiteral)) { || TypeLiteral.get(method.getGenericReturnType()).equals(futureURILiteral)) {
return Key.get(ParseURIFromListOrLocationHeaderIf20x.class); return Key.get(ParseURIFromListOrLocationHeaderIf20x.class);
} else { } else {
throw new IllegalStateException("You must specify a ResponseParser annotation on: " + method.toString()); throw new IllegalStateException("You must specify a ResponseParser annotation on: " + method.toString());
@ -728,7 +749,7 @@ public class RestAnnotationProcessor<T> {
} else { } else {
if (postBinders[0] instanceof org.jclouds.rest.MapBinder) { if (postBinders[0] instanceof org.jclouds.rest.MapBinder) {
throw new IllegalArgumentException("we currently do not support multiple varargs postBinders in: " throw new IllegalArgumentException("we currently do not support multiple varargs postBinders in: "
+ method.getName()); + method.getName());
} }
} }
} else if (arg instanceof org.jclouds.rest.MapBinder) { } else if (arg instanceof org.jclouds.rest.MapBinder) {
@ -765,8 +786,8 @@ public class RestAnnotationProcessor<T> {
Set<String> requests = IsHttpMethod.getHttpMethods(method); Set<String> requests = IsHttpMethod.getHttpMethods(method);
if (requests == null || requests.size() != 1) { if (requests == null || requests.size() != 1) {
throw new IllegalStateException( throw new IllegalStateException(
"You must use at least one, but no more than one http method or pathparam annotation on: " "You must use at least one, but no more than one http method or pathparam annotation on: "
+ method.toString()); + method.toString());
} }
return requests.iterator().next(); return requests.iterator().next();
} }
@ -789,12 +810,12 @@ public class RestAnnotationProcessor<T> {
mapBinder.bindToRequest(request, mapParams); mapBinder.bindToRequest(request, mapParams);
} else { } else {
OUTER: for (Entry<Integer, Set<Annotation>> entry : filterValues( OUTER: for (Entry<Integer, Set<Annotation>> entry : filterValues(
methodToIndexOfParamToDecoratorParamAnnotation.get(request.getJavaMethod()), methodToIndexOfParamToDecoratorParamAnnotation.get(request.getJavaMethod()),
new Predicate<Set<Annotation>>() { new Predicate<Set<Annotation>>() {
public boolean apply(Set<Annotation> input) { public boolean apply(Set<Annotation> input) {
return input.size() >= 1; return input.size() >= 1;
} }
}).entrySet()) { }).entrySet()) {
boolean shouldBreak = false; boolean shouldBreak = false;
BinderParam payloadAnnotation = (BinderParam) entry.getValue().iterator().next(); BinderParam payloadAnnotation = (BinderParam) entry.getValue().iterator().next();
Binder binder = injector.getInstance(payloadAnnotation.value()); Binder binder = injector.getInstance(payloadAnnotation.value());
@ -830,22 +851,27 @@ public class RestAnnotationProcessor<T> {
} }
public static Map<Integer, Set<Annotation>> indexWithOnlyOneAnnotation(Method method, String description, public static Map<Integer, Set<Annotation>> indexWithOnlyOneAnnotation(Method method, String description,
Map<Method, Map<Integer, Set<Annotation>>> toRefine) { Map<Method, Map<Integer, Set<Annotation>>> toRefine) {
Map<Integer, Set<Annotation>> indexToPayloadAnnotation = filterValues(toRefine.get(method), Map<Integer, Set<Annotation>> indexToPayloadAnnotation = indexWithAtLeastOneAnnotation(method, toRefine);
new Predicate<Set<Annotation>>() {
public boolean apply(Set<Annotation> input) {
return input.size() == 1;
}
});
if (indexToPayloadAnnotation.size() > 1) { if (indexToPayloadAnnotation.size() > 1) {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"You must not specify more than one %s annotation on: %s; found %s", description, method.toString(), "You must not specify more than one %s annotation on: %s; found %s", description, method.toString(),
indexToPayloadAnnotation)); indexToPayloadAnnotation));
} }
return indexToPayloadAnnotation; return indexToPayloadAnnotation;
} }
private static Map<Integer, Set<Annotation>> indexWithAtLeastOneAnnotation(Method method,
Map<Method, Map<Integer, Set<Annotation>>> toRefine) {
Map<Integer, Set<Annotation>> indexToPayloadAnnotation = filterValues(toRefine.get(method),
new Predicate<Set<Annotation>>() {
public boolean apply(Set<Annotation> input) {
return input.size() == 1;
}
});
return indexToPayloadAnnotation;
}
private HttpRequestOptions findOptionsIn(Method method, Object... args) { private HttpRequestOptions findOptionsIn(Method method, Object... args) {
for (int index : methodToIndexesOfOptions.get(method)) { for (int index : methodToIndexesOfOptions.get(method)) {
if (args.length >= index + 1) {// accomodate varargs if (args.length >= index + 1) {// accomodate varargs
@ -861,7 +887,7 @@ public class RestAnnotationProcessor<T> {
} else { } else {
if (options[0] instanceof HttpRequestOptions) { if (options[0] instanceof HttpRequestOptions) {
throw new IllegalArgumentException("we currently do not support multiple varargs options in: " throw new IllegalArgumentException("we currently do not support multiple varargs options in: "
+ method.getName()); + method.getName());
} }
} }
} else { } else {
@ -873,7 +899,7 @@ public class RestAnnotationProcessor<T> {
} }
public Multimap<String, String> buildHeaders(Collection<Entry<String, String>> tokenValues, Method method, public Multimap<String, String> buildHeaders(Collection<Entry<String, String>> tokenValues, Method method,
final Object... args) { final Object... args) {
Multimap<String, String> headers = LinkedHashMultimap.create(); Multimap<String, String> headers = LinkedHashMultimap.create();
addHeaderIfAnnotationPresentOnMethod(headers, method, tokenValues); addHeaderIfAnnotationPresentOnMethod(headers, method, tokenValues);
Map<Integer, Set<Annotation>> indexToHeaderParam = methodToIndexOfParamToHeaderParamAnnotations.get(method); Map<Integer, Set<Annotation>> indexToHeaderParam = methodToIndexOfParamToHeaderParamAnnotations.get(method);
@ -920,7 +946,7 @@ public class RestAnnotationProcessor<T> {
} }
public void addHeaderIfAnnotationPresentOnMethod(Multimap<String, String> headers, Method method, public void addHeaderIfAnnotationPresentOnMethod(Multimap<String, String> headers, Method method,
Collection<Entry<String, String>> tokenValues) { Collection<Entry<String, String>> tokenValues) {
if (declaring.isAnnotationPresent(Headers.class)) { if (declaring.isAnnotationPresent(Headers.class)) {
Headers header = declaring.getAnnotation(Headers.class); Headers header = declaring.getAnnotation(Headers.class);
addHeader(headers, header, tokenValues); addHeader(headers, header, tokenValues);
@ -932,7 +958,7 @@ public class RestAnnotationProcessor<T> {
} }
private void addHeader(Multimap<String, String> headers, Headers header, private void addHeader(Multimap<String, String> headers, Headers header,
Collection<Entry<String, String>> tokenValues) { Collection<Entry<String, String>> tokenValues) {
for (int i = 0; i < header.keys().length; i++) { for (int i = 0; i < header.keys().length; i++) {
String value = header.values()[i]; String value = header.values()[i];
value = replaceTokens(value, tokenValues); value = replaceTokens(value, tokenValues);

View File

@ -60,6 +60,7 @@ import javax.annotation.Nullable;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Provider; import javax.inject.Provider;
import javax.inject.Qualifier; import javax.inject.Qualifier;
import javax.inject.Singleton;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam; import javax.ws.rs.FormParam;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -138,6 +139,7 @@ import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -213,7 +215,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testDelegateAsync() throws SecurityException, NoSuchMethodException, InterruptedException, public void testDelegateAsync() throws SecurityException, NoSuchMethodException, InterruptedException,
ExecutionException { ExecutionException {
Injector child = injectorForClient(); Injector child = injectorForClient();
TransformingHttpCommandExecutorService mock = child.getInstance(TransformingHttpCommandExecutorService.class); TransformingHttpCommandExecutorService mock = child.getInstance(TransformingHttpCommandExecutorService.class);
@ -228,9 +230,9 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
AsyncCaller caller = child.getInstance(AsyncCaller.class); AsyncCaller caller = child.getInstance(AsyncCaller.class);
expect(mock.submit(requestLineEquals("GET http://localhost:9999/goo/client/foo HTTP/1.1"), eq(function))) expect(mock.submit(requestLineEquals("GET http://localhost:9999/goo/client/foo HTTP/1.1"), eq(function)))
.andReturn(createNiceMock(ListenableFuture.class)).atLeastOnce(); .andReturn(createNiceMock(ListenableFuture.class)).atLeastOnce();
expect(mock.submit(requestLineEquals("GET http://localhost:9999/client/foo HTTP/1.1"), eq(function))).andReturn( expect(mock.submit(requestLineEquals("GET http://localhost:9999/client/foo HTTP/1.1"), eq(function))).andReturn(
createNiceMock(ListenableFuture.class)).atLeastOnce(); createNiceMock(ListenableFuture.class)).atLeastOnce();
replay(mock); replay(mock);
caller.getCallee("goo").onePath("foo"); caller.getCallee("goo").onePath("foo");
@ -260,7 +262,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
} }
public void testDelegateWithOverridingEndpoint() throws SecurityException, NoSuchMethodException, public void testDelegateWithOverridingEndpoint() throws SecurityException, NoSuchMethodException,
InterruptedException, ExecutionException { InterruptedException, ExecutionException {
Injector child = injectorForClient(); Injector child = injectorForClient();
TransformingHttpCommandExecutorService mock = child.getInstance(TransformingHttpCommandExecutorService.class); TransformingHttpCommandExecutorService mock = child.getInstance(TransformingHttpCommandExecutorService.class);
@ -275,10 +277,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
Caller caller = child.getInstance(Caller.class); Caller caller = child.getInstance(Caller.class);
expect(mock.submit(requestLineEquals("GET http://localhost:1111/goo/client/foo HTTP/1.1"), eq(function))) expect(mock.submit(requestLineEquals("GET http://localhost:1111/goo/client/foo HTTP/1.1"), eq(function)))
.andReturn(Futures.<Void> immediateFuture(null)).atLeastOnce(); .andReturn(Futures.<Void> immediateFuture(null)).atLeastOnce();
expect(mock.submit(requestLineEquals("GET http://localhost:1111/client/foo HTTP/1.1"), eq(function))).andReturn( expect(mock.submit(requestLineEquals("GET http://localhost:1111/client/foo HTTP/1.1"), eq(function))).andReturn(
Futures.<Void> immediateFuture(null)).atLeastOnce(); Futures.<Void> immediateFuture(null)).atLeastOnce();
replay(mock); replay(mock);
caller.getCallee("goo").onePath("foo"); caller.getCallee("goo").onePath("foo");
@ -291,8 +293,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
private Injector injectorForClient() { private Injector injectorForClient() {
ContextSpec<Caller, AsyncCaller> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", null, ContextSpec<Caller, AsyncCaller> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", null,
Caller.class, AsyncCaller.class, ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), Caller.class, AsyncCaller.class, ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(),
new CallerCalleeModule())); new CallerCalleeModule()));
return createContextBuilder(contextSpec).buildInjector(); return createContextBuilder(contextSpec).buildInjector();
@ -311,7 +313,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testDelegateWithPathOnClass() throws SecurityException, NoSuchMethodException, InterruptedException, public void testDelegateWithPathOnClass() throws SecurityException, NoSuchMethodException, InterruptedException,
ExecutionException { ExecutionException {
Injector child = injectorForClient(); Injector child = injectorForClient();
TransformingHttpCommandExecutorService mock = child.getInstance(TransformingHttpCommandExecutorService.class); TransformingHttpCommandExecutorService mock = child.getInstance(TransformingHttpCommandExecutorService.class);
@ -328,10 +330,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
CallerWithPathOnClass caller = factory.create(CallerWithPathOnClass.class); CallerWithPathOnClass caller = factory.create(CallerWithPathOnClass.class);
expect(mock.submit(requestLineEquals("GET http://localhost:9999/caller/goo/client/foo HTTP/1.1"), eq(function))) expect(mock.submit(requestLineEquals("GET http://localhost:9999/caller/goo/client/foo HTTP/1.1"), eq(function)))
.andReturn(createNiceMock(ListenableFuture.class)).atLeastOnce(); .andReturn(createNiceMock(ListenableFuture.class)).atLeastOnce();
expect(mock.submit(requestLineEquals("GET http://localhost:9999/caller/client/foo HTTP/1.1"), eq(function))) expect(mock.submit(requestLineEquals("GET http://localhost:9999/caller/client/foo HTTP/1.1"), eq(function)))
.andReturn(createNiceMock(ListenableFuture.class)).atLeastOnce(); .andReturn(createNiceMock(ListenableFuture.class)).atLeastOnce();
replay(mock); replay(mock);
caller.getCallee("goo").onePath("foo"); caller.getCallee("goo").onePath("foo");
caller.getCallee().onePath("foo"); caller.getCallee().onePath("foo");
@ -380,10 +382,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testUnEncodeQuery() { public void testUnEncodeQuery() {
URI expects = URI URI expects = URI
.create("http://services.nirvanix.com/ws/Metadata/SetMetadata.ashx?output=json&path=adriancole-compute.testObjectOperations&metadata=chef:sushi&metadata=foo:bar&sessionToken=775ef26e-0740-4707-ad92-afe9814bc436"); .create("http://services.nirvanix.com/ws/Metadata/SetMetadata.ashx?output=json&path=adriancole-compute.testObjectOperations&metadata=chef:sushi&metadata=foo:bar&sessionToken=775ef26e-0740-4707-ad92-afe9814bc436");
URI start = URI URI start = URI
.create("http://services.nirvanix.com/ws/Metadata/SetMetadata.ashx?output=json&path=adriancole-compute.testObjectOperations&metadata=chef%3Asushi&metadata=foo%3Abar&sessionToken=775ef26e-0740-4707-ad92-afe9814bc436"); .create("http://services.nirvanix.com/ws/Metadata/SetMetadata.ashx?output=json&path=adriancole-compute.testObjectOperations&metadata=chef%3Asushi&metadata=foo%3Abar&sessionToken=775ef26e-0740-4707-ad92-afe9814bc436");
URI value = RestAnnotationProcessor.replaceQuery(uriBuilderProvider, start, start.getQuery(), null, '/', ':'); URI value = RestAnnotationProcessor.replaceQuery(uriBuilderProvider, start, start.getQuery(), null, '/', ':');
assertEquals(value, expects); assertEquals(value, expects);
} }
@ -431,7 +433,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testPayloadParamVarargs() throws SecurityException, NoSuchMethodException, IOException { public void testPayloadParamVarargs() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPayloadParamVarargs.class.getMethod("varargs", Array.newInstance(HttpRequestOptions.class, 0) Method method = TestPayloadParamVarargs.class.getMethod("varargs", Array.newInstance(HttpRequestOptions.class, 0)
.getClass()); .getClass());
verifyTestPostOptions(method); verifyTestPostOptions(method);
} }
@ -524,7 +526,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testOverriddenEndpointParameter() throws SecurityException, NoSuchMethodException { public void testOverriddenEndpointParameter() throws SecurityException, NoSuchMethodException {
Method method = TestOverriddenEndpoint.class.getMethod("foo", URI.class); Method method = TestOverriddenEndpoint.class.getMethod("foo", URI.class);
HttpRequest request = factory(TestOverriddenEndpoint.class).createRequest(method, HttpRequest request = factory(TestOverriddenEndpoint.class).createRequest(method,
new Object[] { URI.create("http://wowsa:8001") }); new Object[] { URI.create("http://wowsa:8001") });
assertEquals(request.getEndpoint().getHost(), "wowsa"); assertEquals(request.getEndpoint().getHost(), "wowsa");
assertEquals(request.getEndpoint().getPort(), 8001); assertEquals(request.getEndpoint().getPort(), 8001);
assertEquals(request.getEndpoint().getPath(), ""); assertEquals(request.getEndpoint().getPath(), "");
@ -615,44 +617,44 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@POST @POST
void withParamFileBinaryPart(@FormParam("name") String name, void withParamFileBinaryPart(@FormParam("name") String name,
@PartParam(name = "file", contentType = MediaType.APPLICATION_OCTET_STREAM) File path); @PartParam(name = "file", contentType = MediaType.APPLICATION_OCTET_STREAM) File path);
@POST @POST
void withParamByteArrayBinaryPart( void withParamByteArrayBinaryPart(
@FormParam("name") String name, @FormParam("name") String name,
@PartParam(name = "file", contentType = MediaType.APPLICATION_OCTET_STREAM, filename = "{name}.tar.gz") byte[] content); @PartParam(name = "file", contentType = MediaType.APPLICATION_OCTET_STREAM, filename = "{name}.tar.gz") byte[] content);
} }
public void testMultipartWithStringPart() throws SecurityException, NoSuchMethodException, IOException { public void testMultipartWithStringPart() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestMultipartForm.class.getMethod("withStringPart", String.class); Method method = TestMultipartForm.class.getMethod("withStringPart", String.class);
GeneratedHttpRequest<TestMultipartForm> httpRequest = factory(TestMultipartForm.class).createRequest(method, GeneratedHttpRequest<TestMultipartForm> httpRequest = factory(TestMultipartForm.class).createRequest(method,
"foobledata"); "foobledata");
assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest,// assertPayloadEquals(httpRequest,//
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"fooble\"\r\n" + // "Content-Disposition: form-data; name=\"fooble\"\r\n" + //
"\r\n" + // "\r\n" + //
"foobledata\r\n" + // "foobledata\r\n" + //
"----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false);
} }
public void testMultipartWithParamStringPart() throws SecurityException, NoSuchMethodException, IOException { public void testMultipartWithParamStringPart() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestMultipartForm.class.getMethod("withParamStringPart", String.class, String.class); Method method = TestMultipartForm.class.getMethod("withParamStringPart", String.class, String.class);
GeneratedHttpRequest<TestMultipartForm> httpRequest = factory(TestMultipartForm.class).createRequest(method, GeneratedHttpRequest<TestMultipartForm> httpRequest = factory(TestMultipartForm.class).createRequest(method,
"name", "foobledata"); "name", "foobledata");
assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest,// assertPayloadEquals(httpRequest,//
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"name\"\r\n" + // "Content-Disposition: form-data; name=\"name\"\r\n" + //
"\r\n" + // "\r\n" + //
"name\r\n" + // / "name\r\n" + // /
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"file\"\r\n" + // "Content-Disposition: form-data; name=\"file\"\r\n" + //
"\r\n" + // "\r\n" + //
"foobledata\r\n" + // "foobledata\r\n" + //
"----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false);
} }
public void testMultipartWithParamFilePart() throws SecurityException, NoSuchMethodException, IOException { public void testMultipartWithParamFilePart() throws SecurityException, NoSuchMethodException, IOException {
@ -662,38 +664,38 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
file.deleteOnExit(); file.deleteOnExit();
GeneratedHttpRequest<TestMultipartForm> httpRequest = factory(TestMultipartForm.class).createRequest(method, GeneratedHttpRequest<TestMultipartForm> httpRequest = factory(TestMultipartForm.class).createRequest(method,
"name", file); "name", file);
assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest,// assertPayloadEquals(httpRequest,//
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"name\"\r\n" + // "Content-Disposition: form-data; name=\"name\"\r\n" + //
"\r\n" + // "\r\n" + //
"name\r\n" + // / "name\r\n" + // /
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + // "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + //
"\r\n" + // "\r\n" + //
"foobledata\r\n" + // "foobledata\r\n" + //
"----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false);
} }
public void testMultipartWithParamByteArrayPart() throws SecurityException, NoSuchMethodException, IOException { public void testMultipartWithParamByteArrayPart() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestMultipartForm.class.getMethod("withParamByteArrayBinaryPart", String.class, byte[].class); Method method = TestMultipartForm.class.getMethod("withParamByteArrayBinaryPart", String.class, byte[].class);
GeneratedHttpRequest<TestMultipartForm> httpRequest = factory(TestMultipartForm.class).createRequest(method, GeneratedHttpRequest<TestMultipartForm> httpRequest = factory(TestMultipartForm.class).createRequest(method,
"name", "goo".getBytes()); "name", "goo".getBytes());
assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest,// assertPayloadEquals(httpRequest,//
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"name\"\r\n" + // "Content-Disposition: form-data; name=\"name\"\r\n" + //
"\r\n" + // "\r\n" + //
"name\r\n" + // / "name\r\n" + // /
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"file\"; filename=\"name.tar.gz\"\r\n" + // "Content-Disposition: form-data; name=\"file\"; filename=\"name.tar.gz\"\r\n" + //
"Content-Type: application/octet-stream\r\n" + // "Content-Type: application/octet-stream\r\n" + //
"\r\n" + // "\r\n" + //
"goo\r\n" + // "goo\r\n" + //
"----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false);
}; };
public void testMultipartWithParamFileBinaryPart() throws SecurityException, NoSuchMethodException, IOException { public void testMultipartWithParamFileBinaryPart() throws SecurityException, NoSuchMethodException, IOException {
@ -703,20 +705,20 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
file.deleteOnExit(); file.deleteOnExit();
GeneratedHttpRequest<TestMultipartForm> httpRequest = factory(TestMultipartForm.class).createRequest(method, GeneratedHttpRequest<TestMultipartForm> httpRequest = factory(TestMultipartForm.class).createRequest(method,
"name", file); "name", file);
assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest,// assertPayloadEquals(httpRequest,//
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"name\"\r\n" + // "Content-Disposition: form-data; name=\"name\"\r\n" + //
"\r\n" + // "\r\n" + //
"name\r\n" + // / "name\r\n" + // /
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + // "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + //
"Content-Type: application/octet-stream\r\n" + // "Content-Type: application/octet-stream\r\n" + //
"\r\n" + // "\r\n" + //
"'(2\r\n" + // "'(2\r\n" + //
"----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false);
} }
public interface TestPut { public interface TestPut {
@ -804,7 +806,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
// now test that it works! // now test that it works!
Function<HttpResponse, Wrapper> parser = (Function<HttpResponse, Wrapper>) RestAnnotationProcessor Function<HttpResponse, Wrapper> parser = (Function<HttpResponse, Wrapper>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))).foo, "bar"); assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))).foo, "bar");
@ -819,10 +821,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
// now test that it works! // now test that it works!
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), ImmutableMap.of( assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), ImmutableMap.of(
"foo", "bar")); "foo", "bar"));
} }
@ -835,10 +837,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
// now test that it works! // now test that it works!
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), ImmutableMap.of( assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), ImmutableMap.of(
"foo", "bar")); "foo", "bar"));
} }
@ -851,10 +853,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
// now test that it works! // now test that it works!
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), ImmutableMap.of( assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), ImmutableMap.of(
"foo", "bar")); "foo", "bar"));
} }
@ -867,7 +869,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
// now test that it works! // now test that it works!
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), "bar"); assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), "bar");
@ -882,7 +884,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
// now test that it works! // now test that it works!
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), "bar"); assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), "bar");
@ -897,10 +899,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
// now test that it works! // now test that it works!
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{\"runit\":[\"0.7.0\",\"0.7.1\"]}"))), assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{\"runit\":[\"0.7.0\",\"0.7.1\"]}"))),
ImmutableSet.of("0.7.0", "0.7.1")); ImmutableSet.of("0.7.0", "0.7.1"));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -912,10 +914,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
// now test that it works! // now test that it works!
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{\"runit\":[\"0.7.0\",\"0.7.1\"]}"))), assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{\"runit\":[\"0.7.0\",\"0.7.1\"]}"))),
ImmutableSet.of("0.7.0", "0.7.1")); ImmutableSet.of("0.7.0", "0.7.1"));
} }
static class TestRequestFilter1 implements HttpRequestFilter { static class TestRequestFilter1 implements HttpRequestFilter {
@ -1000,7 +1002,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testConstantPathParam() throws SecurityException, NoSuchMethodException, IOException { public void testConstantPathParam() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestConstantPathParam.class.getMethod("twoPaths", String.class, String.class); Method method = TestConstantPathParam.class.getMethod("twoPaths", String.class, String.class);
HttpRequest request = factory(TestConstantPathParam.class).createRequest(method, HttpRequest request = factory(TestConstantPathParam.class).createRequest(method,
new Object[] { "1", "localhost" }); new Object[] { "1", "localhost" });
assertRequestLineEquals(request, "GET http://localhost:9999/v1/ralphie/1/localhost HTTP/1.1"); assertRequestLineEquals(request, "GET http://localhost:9999/v1/ralphie/1/localhost HTTP/1.1");
assertNonPayloadHeadersEqual(request, ""); assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, null, null, false); assertPayloadEquals(request, null, null, false);
@ -1133,7 +1135,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildTwoHeader() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildTwoHeader() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneHeader = TestHeader.class.getMethod("twoHeader", String.class); Method oneHeader = TestHeader.class.getMethod("twoHeader", String.class);
Multimap<String, String> headers = factory(TestHeader.class).createRequest(oneHeader, new Object[] { "robot" }) Multimap<String, String> headers = factory(TestHeader.class).createRequest(oneHeader, new Object[] { "robot" })
.getHeaders(); .getHeaders();
assertEquals(headers.size(), 2); assertEquals(headers.size(), 2);
assertEquals(headers.get("slash"), Collections.singletonList("/robot")); assertEquals(headers.get("slash"), Collections.singletonList("/robot"));
assertEquals(headers.get("hyphen"), Collections.singletonList("-robot")); assertEquals(headers.get("hyphen"), Collections.singletonList("-robot"));
@ -1150,7 +1152,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildOneClassHeader() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildOneClassHeader() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneHeader = TestClassHeader.class.getMethod("oneHeader", String.class); Method oneHeader = TestClassHeader.class.getMethod("oneHeader", String.class);
Multimap<String, String> headers = factory(TestClassHeader.class).createRequest(oneHeader, Multimap<String, String> headers = factory(TestClassHeader.class).createRequest(oneHeader,
new Object[] { "robot" }).getHeaders(); new Object[] { "robot" }).getHeaders();
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot")); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot"));
} }
@ -1159,7 +1161,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildOneHeader() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildOneHeader() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneHeader = TestHeader.class.getMethod("oneHeader", String.class); Method oneHeader = TestHeader.class.getMethod("oneHeader", String.class);
Multimap<String, String> headers = factory(TestHeader.class).createRequest(oneHeader, new Object[] { "robot" }) Multimap<String, String> headers = factory(TestHeader.class).createRequest(oneHeader, new Object[] { "robot" })
.getHeaders(); .getHeaders();
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot")); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot"));
} }
@ -1168,17 +1170,17 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildTwoHeaders() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildTwoHeaders() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method twoHeaders = TestHeader.class.getMethod("twoHeaders", String.class, String.class); Method twoHeaders = TestHeader.class.getMethod("twoHeaders", String.class, String.class);
Multimap<String, String> headers = factory(TestHeader.class).createRequest(twoHeaders, Multimap<String, String> headers = factory(TestHeader.class).createRequest(twoHeaders,
new Object[] { "robot", "eggs" }).getHeaders(); new Object[] { "robot", "eggs" }).getHeaders();
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot/eggs")); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot/eggs"));
} }
@Test @Test
public void testBuildTwoHeadersOutOfOrder() throws SecurityException, NoSuchMethodException, public void testBuildTwoHeadersOutOfOrder() throws SecurityException, NoSuchMethodException,
UnsupportedEncodingException { UnsupportedEncodingException {
Method twoHeadersOutOfOrder = TestHeader.class.getMethod("twoHeadersOutOfOrder", String.class, String.class); Method twoHeadersOutOfOrder = TestHeader.class.getMethod("twoHeadersOutOfOrder", String.class, String.class);
Multimap<String, String> headers = factory(TestHeader.class).createRequest(twoHeadersOutOfOrder, Multimap<String, String> headers = factory(TestHeader.class).createRequest(twoHeadersOutOfOrder,
new Object[] { "robot", "eggs" }).getHeaders(); new Object[] { "robot", "eggs" }).getHeaders();
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/eggs/robot")); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/eggs/robot"));
} }
@ -1193,7 +1195,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testQueryInOptions() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testQueryInOptions() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneQuery = TestQueryReplace.class.getMethod("queryInOptions", String.class, TestReplaceQueryOptions.class); Method oneQuery = TestQueryReplace.class.getMethod("queryInOptions", String.class, TestReplaceQueryOptions.class);
String query = factory(TestQueryReplace.class).createRequest(oneQuery, String query = factory(TestQueryReplace.class).createRequest(oneQuery,
new Object[] { "robot", new TestReplaceQueryOptions() }).getEndpoint().getQuery(); new Object[] { "robot", new TestReplaceQueryOptions() }).getEndpoint().getQuery();
assertEquals(query, "x-amz-copy-source=/robot"); assertEquals(query, "x-amz-copy-source=/robot");
} }
@ -1201,13 +1203,13 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@POST @POST
@Path("objects/{id}/action/{action}") @Path("objects/{id}/action/{action}")
ListenableFuture<String> action(@PathParam("id") String id, @PathParam("action") String action, ListenableFuture<String> action(@PathParam("id") String id, @PathParam("action") String action,
@BinderParam(BindMapToMatrixParams.class) Map<String, String> options); @BinderParam(BindMapToMatrixParams.class) Map<String, String> options);
} }
public void testTestMapMatrixParams() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testTestMapMatrixParams() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method method = TestMapMatrixParams.class.getMethod("action", String.class, String.class, Map.class); Method method = TestMapMatrixParams.class.getMethod("action", String.class, String.class, Map.class);
HttpRequest request = factory(TestMapMatrixParams.class).createRequest(method, HttpRequest request = factory(TestMapMatrixParams.class).createRequest(method,
new Object[] { "robot", "kill", ImmutableMap.of("death", "slow") }); new Object[] { "robot", "kill", ImmutableMap.of("death", "slow") });
assertRequestLineEquals(request, "POST http://localhost:9999/objects/robot/action/kill;death=slow HTTP/1.1"); assertRequestLineEquals(request, "POST http://localhost:9999/objects/robot/action/kill;death=slow HTTP/1.1");
assertEquals(request.getHeaders().size(), 0); assertEquals(request.getHeaders().size(), 0);
} }
@ -1244,7 +1246,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildTwoQuery() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildTwoQuery() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneQuery = TestQueryReplace.class.getMethod("twoQuery", String.class); Method oneQuery = TestQueryReplace.class.getMethod("twoQuery", String.class);
String query = factory(TestQueryReplace.class).createRequest(oneQuery, new Object[] { "robot" }).getEndpoint() String query = factory(TestQueryReplace.class).createRequest(oneQuery, new Object[] { "robot" }).getEndpoint()
.getQuery(); .getQuery();
assertEquals(query, "slash=/robot&hyphen=-robot"); assertEquals(query, "slash=/robot&hyphen=-robot");
} }
@ -1259,7 +1261,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildOneClassQuery() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildOneClassQuery() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneQuery = TestClassQuery.class.getMethod("oneQuery", String.class); Method oneQuery = TestClassQuery.class.getMethod("oneQuery", String.class);
String query = factory(TestClassQuery.class).createRequest(oneQuery, new Object[] { "robot" }).getEndpoint() String query = factory(TestClassQuery.class).createRequest(oneQuery, new Object[] { "robot" }).getEndpoint()
.getQuery(); .getQuery();
assertEquals(query, "x-amz-copy-source=/robot"); assertEquals(query, "x-amz-copy-source=/robot");
} }
@ -1267,7 +1269,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildOneQuery() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildOneQuery() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneQuery = TestQueryReplace.class.getMethod("oneQuery", String.class); Method oneQuery = TestQueryReplace.class.getMethod("oneQuery", String.class);
String query = factory(TestQueryReplace.class).createRequest(oneQuery, new Object[] { "robot" }).getEndpoint() String query = factory(TestQueryReplace.class).createRequest(oneQuery, new Object[] { "robot" }).getEndpoint()
.getQuery(); .getQuery();
assertEquals(query, "x-amz-copy-source=/robot"); assertEquals(query, "x-amz-copy-source=/robot");
} }
@ -1275,16 +1277,16 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildTwoQuerys() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildTwoQuerys() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method twoQuerys = TestQueryReplace.class.getMethod("twoQuerys", String.class, String.class); Method twoQuerys = TestQueryReplace.class.getMethod("twoQuerys", String.class, String.class);
String query = factory(TestQueryReplace.class).createRequest(twoQuerys, new Object[] { "robot", "eggs" }) String query = factory(TestQueryReplace.class).createRequest(twoQuerys, new Object[] { "robot", "eggs" })
.getEndpoint().getQuery(); .getEndpoint().getQuery();
assertEquals(query, "x-amz-copy-source=/robot/eggs"); assertEquals(query, "x-amz-copy-source=/robot/eggs");
} }
@Test @Test
public void testBuildTwoQuerysOutOfOrder() throws SecurityException, NoSuchMethodException, public void testBuildTwoQuerysOutOfOrder() throws SecurityException, NoSuchMethodException,
UnsupportedEncodingException { UnsupportedEncodingException {
Method twoQuerysOutOfOrder = TestQueryReplace.class.getMethod("twoQuerysOutOfOrder", String.class, String.class); Method twoQuerysOutOfOrder = TestQueryReplace.class.getMethod("twoQuerysOutOfOrder", String.class, String.class);
String query = factory(TestQueryReplace.class).createRequest(twoQuerysOutOfOrder, String query = factory(TestQueryReplace.class).createRequest(twoQuerysOutOfOrder,
new Object[] { "robot", "eggs" }).getEndpoint().getQuery(); new Object[] { "robot", "eggs" }).getEndpoint().getQuery();
assertEquals(query, "x-amz-copy-source=/eggs/robot"); assertEquals(query, "x-amz-copy-source=/eggs/robot");
} }
@ -1297,9 +1299,9 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Test @Test
public void testMatrixInOptions() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testMatrixInOptions() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneMatrix = TestMatrixReplace.class.getMethod("matrixInOptions", String.class, Method oneMatrix = TestMatrixReplace.class.getMethod("matrixInOptions", String.class,
TestReplaceMatrixOptions.class); TestReplaceMatrixOptions.class);
String path = factory(TestMatrixReplace.class).createRequest(oneMatrix, String path = factory(TestMatrixReplace.class).createRequest(oneMatrix,
new Object[] { "robot", new TestReplaceMatrixOptions() }).getEndpoint().getPath(); new Object[] { "robot", new TestReplaceMatrixOptions() }).getEndpoint().getPath();
assertEquals(path, "/;x-amz-copy-source=/robot"); assertEquals(path, "/;x-amz-copy-source=/robot");
} }
@ -1335,7 +1337,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildTwoMatrix() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildTwoMatrix() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneMatrix = TestMatrixReplace.class.getMethod("twoMatrix", String.class); Method oneMatrix = TestMatrixReplace.class.getMethod("twoMatrix", String.class);
String path = factory(TestMatrixReplace.class).createRequest(oneMatrix, new Object[] { "robot" }).getEndpoint() String path = factory(TestMatrixReplace.class).createRequest(oneMatrix, new Object[] { "robot" }).getEndpoint()
.getPath(); .getPath();
assertEquals(path, "/;slash=/robot;hyphen=-robot"); assertEquals(path, "/;slash=/robot;hyphen=-robot");
} }
@ -1351,7 +1353,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildOneClassMatrix() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildOneClassMatrix() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneMatrix = TestClassMatrix.class.getMethod("oneMatrix", String.class); Method oneMatrix = TestClassMatrix.class.getMethod("oneMatrix", String.class);
String path = factory(TestClassMatrix.class).createRequest(oneMatrix, new Object[] { "robot" }).getEndpoint() String path = factory(TestClassMatrix.class).createRequest(oneMatrix, new Object[] { "robot" }).getEndpoint()
.getPath(); .getPath();
assertEquals(path, "/;x-amz-copy-source=/robot"); assertEquals(path, "/;x-amz-copy-source=/robot");
} }
@ -1359,7 +1361,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildOneMatrix() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildOneMatrix() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneMatrix = TestMatrixReplace.class.getMethod("oneMatrix", String.class); Method oneMatrix = TestMatrixReplace.class.getMethod("oneMatrix", String.class);
String path = factory(TestMatrixReplace.class).createRequest(oneMatrix, new Object[] { "robot" }).getEndpoint() String path = factory(TestMatrixReplace.class).createRequest(oneMatrix, new Object[] { "robot" }).getEndpoint()
.getPath(); .getPath();
assertEquals(path, "/;x-amz-copy-source=/robot"); assertEquals(path, "/;x-amz-copy-source=/robot");
} }
@ -1367,17 +1369,17 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildTwoMatrixs() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildTwoMatrixs() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method twoMatrixs = TestMatrixReplace.class.getMethod("twoMatrixs", String.class, String.class); Method twoMatrixs = TestMatrixReplace.class.getMethod("twoMatrixs", String.class, String.class);
String path = factory(TestMatrixReplace.class).createRequest(twoMatrixs, new Object[] { "robot", "eggs" }) String path = factory(TestMatrixReplace.class).createRequest(twoMatrixs, new Object[] { "robot", "eggs" })
.getEndpoint().getPath(); .getEndpoint().getPath();
assertEquals(path, "/;x-amz-copy-source=/robot/eggs"); assertEquals(path, "/;x-amz-copy-source=/robot/eggs");
} }
@Test @Test
public void testBuildTwoMatrixsOutOfOrder() throws SecurityException, NoSuchMethodException, public void testBuildTwoMatrixsOutOfOrder() throws SecurityException, NoSuchMethodException,
UnsupportedEncodingException { UnsupportedEncodingException {
Method twoMatrixsOutOfOrder = TestMatrixReplace.class.getMethod("twoMatrixsOutOfOrder", String.class, Method twoMatrixsOutOfOrder = TestMatrixReplace.class.getMethod("twoMatrixsOutOfOrder", String.class,
String.class); String.class);
String path = factory(TestMatrixReplace.class).createRequest(twoMatrixsOutOfOrder, String path = factory(TestMatrixReplace.class).createRequest(twoMatrixsOutOfOrder,
new Object[] { "robot", "eggs" }).getEndpoint().getPath(); new Object[] { "robot", "eggs" }).getEndpoint().getPath();
assertEquals(path, "/;x-amz-copy-source=/eggs/robot"); assertEquals(path, "/;x-amz-copy-source=/eggs/robot");
} }
@ -1419,7 +1421,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testPutPayloadEnclosing() throws SecurityException, NoSuchMethodException, IOException { public void testPutPayloadEnclosing() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestTransformers.class.getMethod("put", PayloadEnclosing.class); Method method = TestTransformers.class.getMethod("put", PayloadEnclosing.class);
HttpRequest request = factory(TestQuery.class).createRequest(method, HttpRequest request = factory(TestQuery.class).createRequest(method,
new PayloadEnclosingImpl(newStringPayload("whoops"))); new PayloadEnclosingImpl(newStringPayload("whoops")));
assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1");
assertNonPayloadHeadersEqual(request, ""); assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, "whoops", "application/unknown", false); assertPayloadEquals(request, "whoops", "application/unknown", false);
@ -1437,7 +1439,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
} }
public void testPutInputStreamPayloadEnclosingGenerateMD5() throws SecurityException, NoSuchMethodException, public void testPutInputStreamPayloadEnclosingGenerateMD5() throws SecurityException, NoSuchMethodException,
IOException { IOException {
Method method = TestTransformers.class.getMethod("put", PayloadEnclosing.class); Method method = TestTransformers.class.getMethod("put", PayloadEnclosing.class);
PayloadEnclosing payloadEnclosing = new PayloadEnclosingImpl(newInputStreamPayload(toInputStream("whoops"))); PayloadEnclosing payloadEnclosing = new PayloadEnclosingImpl(newInputStreamPayload(toInputStream("whoops")));
@ -1466,7 +1468,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
} }
public void testPutPayloadWithGeneratedMD5AndNoContentType() throws SecurityException, NoSuchMethodException, public void testPutPayloadWithGeneratedMD5AndNoContentType() throws SecurityException, NoSuchMethodException,
IOException { IOException {
Payload payload = newStringPayload("whoops"); Payload payload = newStringPayload("whoops");
calculateMD5(payload, crypto.md5()); calculateMD5(payload, crypto.md5());
Method method = TestTransformers.class.getMethod("put", Payload.class); Method method = TestTransformers.class.getMethod("put", Payload.class);
@ -1487,7 +1489,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
} }
public void testPutInputStreamPayloadWithMD5() throws NoSuchAlgorithmException, IOException, SecurityException, public void testPutInputStreamPayloadWithMD5() throws NoSuchAlgorithmException, IOException, SecurityException,
NoSuchMethodException { NoSuchMethodException {
Payload payload = newStringPayload("whoops"); Payload payload = newStringPayload("whoops");
payload.setContentLength((long) "whoops".length()); payload.setContentLength((long) "whoops".length());
calculateMD5(payload, crypto.md5()); calculateMD5(payload, crypto.md5());
@ -1512,9 +1514,9 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> Class<? extends Function<HttpResponse, ?>> unwrap(RestAnnotationProcessor<T> processor, public static <T> Class<? extends Function<HttpResponse, ?>> unwrap(RestAnnotationProcessor<T> processor,
Method method) { Method method) {
return (Class<? extends Function<HttpResponse, ?>>) RestAnnotationProcessor.getParserOrThrowException(method) return (Class<? extends Function<HttpResponse, ?>>) RestAnnotationProcessor.getParserOrThrowException(method)
.getTypeLiteral().getRawType(); .getTypeLiteral().getRawType();
} }
public void testURI() throws SecurityException, NoSuchMethodException { public void testURI() throws SecurityException, NoSuchMethodException {
@ -1551,7 +1553,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
RestAnnotationProcessor<TestTransformers> processor = factory(TestTransformers.class); RestAnnotationProcessor<TestTransformers> processor = factory(TestTransformers.class);
Method method = TestTransformers.class.getMethod("oneTransformerWithContext"); Method method = TestTransformers.class.getMethod("oneTransformerWithContext");
GeneratedHttpRequest<TestTransformers> request = new GeneratedHttpRequest<TestTransformers>("GET", URI GeneratedHttpRequest<TestTransformers> request = new GeneratedHttpRequest<TestTransformers>("GET", URI
.create("http://localhost"), TestTransformers.class, method); .create("http://localhost"), TestTransformers.class, method);
Function<HttpResponse, ?> transformer = processor.createResponseParser(method, request); Function<HttpResponse, ?> transformer = processor.createResponseParser(method, request);
assertEquals(transformer.getClass(), ReturnStringIf200Context.class); assertEquals(transformer.getClass(), ReturnStringIf200Context.class);
assertEquals(((ReturnStringIf200Context) transformer).request, request); assertEquals(((ReturnStringIf200Context) transformer).request, request);
@ -1592,7 +1594,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@PUT @PUT
@Path("/{id}") @Path("/{id}")
ListenableFuture<String> put(@PathParam("id") @ParamParser(FirstCharacter.class) String id, ListenableFuture<String> put(@PathParam("id") @ParamParser(FirstCharacter.class) String id,
@BinderParam(BindToStringPayload.class) String payload); @BinderParam(BindToStringPayload.class) String payload);
@PUT @PUT
@Path("/{id}") @Path("/{id}")
@ -1604,7 +1606,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Headers(keys = "foo", values = "--{id}--") @Headers(keys = "foo", values = "--{id}--")
@ResponseParser(ReturnTrueIf2xx.class) @ResponseParser(ReturnTrueIf2xx.class)
ListenableFuture<String> putHeader(@PathParam("id") String id, ListenableFuture<String> putHeader(@PathParam("id") String id,
@BinderParam(BindToStringPayload.class) String payload); @BinderParam(BindToStringPayload.class) String payload);
} }
public void testCreateGetVarArgOptionsThatProducesHeaders() throws SecurityException, NoSuchMethodException { public void testCreateGetVarArgOptionsThatProducesHeaders() throws SecurityException, NoSuchMethodException {
@ -1619,7 +1621,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertEquals(request.getHeaders().size(), 2); assertEquals(request.getHeaders().size(), 2);
assertEquals(request.getHeaders().get(HttpHeaders.HOST), Collections.singletonList("localhost")); assertEquals(request.getHeaders().get(HttpHeaders.HOST), Collections.singletonList("localhost"));
assertEquals(request.getHeaders().get(HttpHeaders.IF_MODIFIED_SINCE), Collections.singletonList(dateService assertEquals(request.getHeaders().get(HttpHeaders.IF_MODIFIED_SINCE), Collections.singletonList(dateService
.rfc822DateFormat(date))); .rfc822DateFormat(date)));
} }
public void testCreateGetOptionsThatProducesHeaders() throws SecurityException, NoSuchMethodException { public void testCreateGetOptionsThatProducesHeaders() throws SecurityException, NoSuchMethodException {
@ -1633,7 +1635,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertEquals(request.getHeaders().size(), 2); assertEquals(request.getHeaders().size(), 2);
assertEquals(request.getHeaders().get(HttpHeaders.HOST), Collections.singletonList("localhost")); assertEquals(request.getHeaders().get(HttpHeaders.HOST), Collections.singletonList("localhost"));
assertEquals(request.getHeaders().get(HttpHeaders.IF_MODIFIED_SINCE), Collections.singletonList(dateService assertEquals(request.getHeaders().get(HttpHeaders.IF_MODIFIED_SINCE), Collections.singletonList(dateService
.rfc822DateFormat(date))); .rfc822DateFormat(date)));
} }
public class PrefixOptions extends BaseHttpRequestOptions { public class PrefixOptions extends BaseHttpRequestOptions {
@ -1696,7 +1698,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Test(dataProvider = "strings") @Test(dataProvider = "strings")
public void testCreateGetRequest(String key) throws SecurityException, NoSuchMethodException, public void testCreateGetRequest(String key) throws SecurityException, NoSuchMethodException,
UnsupportedEncodingException { UnsupportedEncodingException {
Method method = TestRequest.class.getMethod("get", String.class, String.class); Method method = TestRequest.class.getMethod("get", String.class, String.class);
HttpRequest request = factory(TestRequest.class).createRequest(method, new Object[] { key, "localhost" }); HttpRequest request = factory(TestRequest.class).createRequest(method, new Object[] { key, "localhost" });
assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getHost(), "localhost");
@ -1739,7 +1741,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testVirtualHostMethod() throws SecurityException, NoSuchMethodException { public void testVirtualHostMethod() throws SecurityException, NoSuchMethodException {
Method method = TestVirtualHostMethod.class.getMethod("get", String.class, String.class); Method method = TestVirtualHostMethod.class.getMethod("get", String.class, String.class);
HttpRequest request = factory(TestVirtualHostMethod.class).createRequest(method, HttpRequest request = factory(TestVirtualHostMethod.class).createRequest(method,
new Object[] { "1", "localhost" }); new Object[] { "1", "localhost" });
assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/1"); assertEquals(request.getEndpoint().getPath(), "/1");
assertEquals(request.getMethod(), HttpMethod.GET); assertEquals(request.getMethod(), HttpMethod.GET);
@ -1786,29 +1788,25 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
factory(TestVirtualHost.class).createRequest(method, "1", ""); factory(TestVirtualHost.class).createRequest(method, "1", "");
} }
public class TestHeaders { public interface TestHeaders {
@GET @GET
public void oneHeader(@HeaderParam("header") String header) { void oneHeader(@HeaderParam("header") String header);
}
@GET @GET
public void oneIntHeader(@HeaderParam("header") int header) { void oneIntHeader(@HeaderParam("header") int header);
}
@GET @GET
public void twoDifferentHeaders(@HeaderParam("header1") String header1, @HeaderParam("header2") String header2) { void twoDifferentHeaders(@HeaderParam("header1") String header1, @HeaderParam("header2") String header2);
}
@GET @GET
public void twoSameHeaders(@HeaderParam("header") String header1, @HeaderParam("header") String header2) { void twoSameHeaders(@HeaderParam("header") String header1, @HeaderParam("header") String header2);
}
} }
@Test @Test
public void testOneHeader() throws SecurityException, NoSuchMethodException { public void testOneHeader() throws SecurityException, NoSuchMethodException {
Method method = TestHeaders.class.getMethod("oneHeader", String.class); Method method = TestHeaders.class.getMethod("oneHeader", String.class);
Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders( Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders(
ImmutableMultimap.<String, String> of().entries(), method, "robot"); ImmutableMultimap.<String, String> of().entries(), method, "robot");
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("header"), Collections.singletonList("robot")); assertEquals(headers.get("header"), Collections.singletonList("robot"));
} }
@ -1817,7 +1815,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testOneIntHeader() throws SecurityException, NoSuchMethodException { public void testOneIntHeader() throws SecurityException, NoSuchMethodException {
Method method = TestHeaders.class.getMethod("oneIntHeader", int.class); Method method = TestHeaders.class.getMethod("oneIntHeader", int.class);
Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders( Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders(
ImmutableMultimap.<String, String> of().entries(), method, 1); ImmutableMultimap.<String, String> of().entries(), method, 1);
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("header"), Collections.singletonList("1")); assertEquals(headers.get("header"), Collections.singletonList("1"));
} }
@ -1826,7 +1824,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testTwoDifferentHeaders() throws SecurityException, NoSuchMethodException { public void testTwoDifferentHeaders() throws SecurityException, NoSuchMethodException {
Method method = TestHeaders.class.getMethod("twoDifferentHeaders", String.class, String.class); Method method = TestHeaders.class.getMethod("twoDifferentHeaders", String.class, String.class);
Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders( Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders(
ImmutableMultimap.<String, String> of().entries(), method, "robot", "egg"); ImmutableMultimap.<String, String> of().entries(), method, "robot", "egg");
assertEquals(headers.size(), 2); assertEquals(headers.size(), 2);
assertEquals(headers.get("header1"), Collections.singletonList("robot")); assertEquals(headers.get("header1"), Collections.singletonList("robot"));
assertEquals(headers.get("header2"), Collections.singletonList("egg")); assertEquals(headers.get("header2"), Collections.singletonList("egg"));
@ -1836,13 +1834,63 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testTwoSameHeaders() throws SecurityException, NoSuchMethodException { public void testTwoSameHeaders() throws SecurityException, NoSuchMethodException {
Method method = TestHeaders.class.getMethod("twoSameHeaders", String.class, String.class); Method method = TestHeaders.class.getMethod("twoSameHeaders", String.class, String.class);
Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders( Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders(
ImmutableMultimap.<String, String> of().entries(), method, "robot", "egg"); ImmutableMultimap.<String, String> of().entries(), method, "robot", "egg");
assertEquals(headers.size(), 2); assertEquals(headers.size(), 2);
Collection<String> values = headers.get("header"); Collection<String> values = headers.get("header");
assert values.contains("robot"); assert values.contains("robot");
assert values.contains("egg"); assert values.contains("egg");
} }
public interface TestEndpointParams {
@GET
void oneEndpointParam(@EndpointParam(parser = ConvertToURI.class) String EndpointParam);
@Singleton
public static class ConvertToURI implements Function<Object, URI> {
@Override
public URI apply(Object from) {
return URI.create(from.toString());
}
}
@GET
void twoEndpointParams(@EndpointParam(parser = ConvertTwoToURI.class) String EndpointParam1,
@EndpointParam(parser = ConvertTwoToURI.class) String EndpointParam2);
@Singleton
public static class ConvertTwoToURI implements Function<Object, URI> {
@SuppressWarnings("unchecked")
@Override
public URI apply(Object from) {
return URI.create(Joiner.on('/').join((Iterable<Object>) from));
}
}
}
@SuppressWarnings("static-access")
@Test
public void testOneEndpointParam() throws SecurityException, NoSuchMethodException {
Method method = TestEndpointParams.class.getMethod("oneEndpointParam", String.class);
URI uri = factory(TestEndpointParams.class).getEndpointInParametersOrNull(method, new Object[] { "robot" },
injector);
assertEquals(uri, URI.create("robot"));
}
@SuppressWarnings("static-access")
@Test
public void testTwoDifferentEndpointParams() throws SecurityException, NoSuchMethodException {
Method method = TestEndpointParams.class.getMethod("twoEndpointParams", String.class, String.class);
URI uri = factory(TestEndpointParams.class).getEndpointInParametersOrNull(method,
new Object[] { "robot", "egg" }, injector);
assertEquals(uri, URI.create("robot/egg"));
}
public interface TestPayload { public interface TestPayload {
@PUT @PUT
public void put(@BinderParam(BindToStringPayload.class) String content); public void put(@BinderParam(BindToStringPayload.class) String content);
@ -1850,11 +1898,11 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@PUT @PUT
@Path("{foo}") @Path("{foo}")
public ListenableFuture<Void> putWithPath(@PathParam("foo") String path, public ListenableFuture<Void> putWithPath(@PathParam("foo") String path,
@BinderParam(BindToStringPayload.class) String content); @BinderParam(BindToStringPayload.class) String content);
@PUT @PUT
public void twoEntities(@BinderParam(BindToStringPayload.class) String payload1, public void twoEntities(@BinderParam(BindToStringPayload.class) String payload1,
@BinderParam(BindToStringPayload.class) String payload2); @BinderParam(BindToStringPayload.class) String payload2);
} }
@Test @Test
@ -1946,23 +1994,23 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildTwoForms() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildTwoForms() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method twoForms = TestFormReplace.class.getMethod("twoForms", String.class, String.class); Method twoForms = TestFormReplace.class.getMethod("twoForms", String.class, String.class);
Object form = factory(TestFormReplace.class).createRequest(twoForms, "robot", "eggs").getPayload() Object form = factory(TestFormReplace.class).createRequest(twoForms, "robot", "eggs").getPayload()
.getRawContent(); .getRawContent();
assertEquals(form, "x-amz-copy-source=/robot/eggs"); assertEquals(form, "x-amz-copy-source=/robot/eggs");
} }
@Test @Test
public void testBuildTwoFormsOutOfOrder() throws SecurityException, NoSuchMethodException, public void testBuildTwoFormsOutOfOrder() throws SecurityException, NoSuchMethodException,
UnsupportedEncodingException { UnsupportedEncodingException {
Method twoFormsOutOfOrder = TestFormReplace.class.getMethod("twoFormsOutOfOrder", String.class, String.class); Method twoFormsOutOfOrder = TestFormReplace.class.getMethod("twoFormsOutOfOrder", String.class, String.class);
Object form = factory(TestFormReplace.class).createRequest(twoFormsOutOfOrder, "robot", "eggs").getPayload() Object form = factory(TestFormReplace.class).createRequest(twoFormsOutOfOrder, "robot", "eggs").getPayload()
.getRawContent(); .getRawContent();
assertEquals(form, "x-amz-copy-source=/eggs/robot"); assertEquals(form, "x-amz-copy-source=/eggs/robot");
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> RestAnnotationProcessor<T> factory(Class<T> clazz) { private <T> RestAnnotationProcessor<T> factory(Class<T> clazz) {
return ((RestAnnotationProcessor<T>) injector.getInstance(Key.get(newParameterizedType( return ((RestAnnotationProcessor<T>) injector.getInstance(Key.get(newParameterizedType(
RestAnnotationProcessor.class, clazz)))); RestAnnotationProcessor.class, clazz))));
} }
DateService dateService = new SimpleDateFormatDateService(); DateService dateService = new SimpleDateFormatDateService();
@ -1970,16 +2018,15 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@BeforeClass @BeforeClass
void setupFactory() { void setupFactory() {
ContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", null, ContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", null,
String.class, Integer.class, ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), String.class, Integer.class, ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(),
new AbstractModule() { new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
bind(URI.class).annotatedWith(Localhost2.class).toInstance( bind(URI.class).annotatedWith(Localhost2.class).toInstance(URI.create("http://localhost:1111"));
URI.create("http://localhost:1111")); }
}
})); }));
injector = createContextBuilder(contextSpec).buildInjector(); injector = createContextBuilder(contextSpec).buildInjector();
parserFactory = injector.getInstance(ParseSax.Factory.class); parserFactory = injector.getInstance(ParseSax.Factory.class);