Adding multiple varargs HttpRequestOptions support

This commit is contained in:
Adam Lowe 2012-05-31 10:16:09 +01:00
parent 96773b8a1b
commit 76531d62e0
2 changed files with 62 additions and 55 deletions

View File

@ -472,8 +472,7 @@ public class RestAnnotationProcessor<T> {
}
Payload payload = null;
HttpRequestOptions options = findOptionsIn(method, args);
if (options != null) {
for(HttpRequestOptions options : findOptionsIn(method, args)) {
injector.injectMembers(options);// TODO test case
for (Entry<String, String> header : options.buildRequestHeaders().entries()) {
headers.put(header.getKey(), Strings2.replaceTokens(header.getValue(), tokenValues.entries()));
@ -1051,30 +1050,26 @@ public class RestAnnotationProcessor<T> {
}
//TODO: change to LoadingCache<ClassMethodArgs, HttpRequestOptions and move this logic to the CacheLoader.
private HttpRequestOptions findOptionsIn(Method method, Object... args) throws ExecutionException {
for (int index : methodToIndexesOfOptions.get(method)) {
private Set<HttpRequestOptions> findOptionsIn(Method method, Object... args) throws ExecutionException {
ImmutableSet.Builder<HttpRequestOptions> result = ImmutableSet.builder();
for (int index : methodToIndexesOfOptions.get(method)) {
if (args.length >= index + 1) {// accomodate varargs
if (args[index] instanceof Object[]) {
Object[] options = (Object[]) args[index];
if (options.length == 0) {
} else if (options.length == 1) {
if (options[0] instanceof HttpRequestOptions) {
HttpRequestOptions binder = (HttpRequestOptions) options[0];
injector.injectMembers(binder);
return binder;
}
} else {
if (options[0] instanceof HttpRequestOptions) {
throw new IllegalArgumentException("we currently do not support multiple varargs options in: "
+ method.getName());
for (Object option : (Object[]) args[index]) {
if (option instanceof HttpRequestOptions) {
result.add((HttpRequestOptions) option);
}
}
} else {
return (HttpRequestOptions) args[index];
for (; index < args.length; index++) {
if (args[index] instanceof HttpRequestOptions) {
result.add((HttpRequestOptions) args[index]);
}
}
}
}
}
return null;
return result.build();
}
public Multimap<String, String> buildHeaders(Collection<Entry<String, String>> tokenValues, Method method,

View File

@ -511,6 +511,9 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@POST
public void varargs(HttpRequestOptions... options);
@POST
public void varargsWithReq(String required, HttpRequestOptions... options);
@POST
public void post(HttpRequestOptions options);
@ -530,6 +533,12 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, "", "application/octet-stream", false);
}
private class TestHttpRequestOptions extends BaseHttpRequestOptions {
TestHttpRequestOptions payload(String payload) { this.payload = payload; return this; }
TestHttpRequestOptions headerParams(Multimap<String, String> headers) { this.headers.putAll(headers); return this; }
TestHttpRequestOptions queryParams(Multimap<String, String> params) { this.queryParameters.putAll(params); return this; }
}
public void testHttpRequestOptionsPayloadParam() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPayloadParamVarargs.class.getMethod("post", Payload.class);
@ -541,48 +550,51 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testHttpRequestWithOnlyContentType() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPayloadParamVarargs.class.getMethod("post", HttpRequestOptions.class);
verifyTestPostOptions(method);
}
public void testPayloadParamVarargs() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPayloadParamVarargs.class.getMethod("varargs", Array.newInstance(HttpRequestOptions.class, 0)
.getClass());
verifyTestPostOptions(method);
}
private void verifyTestPostOptions(Method method) throws IOException {
HttpRequest request = factory(TestPayloadParamVarargs.class).createRequest(method, new HttpRequestOptions() {
public Multimap<String, String> buildMatrixParameters() {
return LinkedHashMultimap.create();
}
public String buildPathSuffix() {
return null;
}
public Multimap<String, String> buildQueryParameters() {
return LinkedHashMultimap.create();
}
public Multimap<String, String> buildFormParameters() {
return LinkedHashMultimap.create();
}
public Multimap<String, String> buildRequestHeaders() {
return LinkedHashMultimap.create();
}
public String buildStringPayload() {
return "fooya";
}
});
HttpRequest request = factory(TestPayloadParamVarargs.class).createRequest(method, new TestHttpRequestOptions().payload("fooya"));
assertRequestLineEquals(request, "POST http://localhost:9999 HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, "fooya", "application/unknown", false);
}
public void testHeaderAndQueryVarargs() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPayloadParamVarargs.class.getMethod("varargs", Array.newInstance(HttpRequestOptions.class, 0)
.getClass());
HttpRequest request = factory(TestPayloadParamVarargs.class).createRequest(method,
new TestHttpRequestOptions().payload("fooya"),
new TestHttpRequestOptions().headerParams(ImmutableMultimap.of("X-header-1", "fooya")),
new TestHttpRequestOptions().queryParams(ImmutableMultimap.of("key", "value")));
assertRequestLineEquals(request, "POST http://localhost:9999?key=value HTTP/1.1");
assertNonPayloadHeadersEqual(request, "X-header-1: fooya\n");
assertPayloadEquals(request, "fooya", "application/unknown", false);
}
public void testHeaderAndQueryVarargsPlusReq() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPayloadParamVarargs.class.getMethod("varargsWithReq", String.class, Array.newInstance(HttpRequestOptions.class, 0)
.getClass());
HttpRequest request = factory(TestPayloadParamVarargs.class).createRequest(method, "required param",
new Object[]{ new TestHttpRequestOptions().payload("fooya"),
new TestHttpRequestOptions().headerParams(ImmutableMultimap.of("X-header-1", "fooya")),
new TestHttpRequestOptions().queryParams(ImmutableMultimap.of("key", "value"))});
assertRequestLineEquals(request, "POST http://localhost:9999?key=value HTTP/1.1");
assertNonPayloadHeadersEqual(request, "X-header-1: fooya\n");
assertPayloadEquals(request, "fooya", "application/unknown", false);
}
public void testDuplicateHeaderAndQueryVarargs() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPayloadParamVarargs.class.getMethod("varargs", Array.newInstance(HttpRequestOptions.class, 0)
.getClass());
HttpRequest request = factory(TestPayloadParamVarargs.class).createRequest(method,
new TestHttpRequestOptions().queryParams(ImmutableMultimap.of("key", "value")),
new TestHttpRequestOptions().payload("fooya"),
new TestHttpRequestOptions().headerParams(ImmutableMultimap.of("X-header-1", "fooya")),
new TestHttpRequestOptions().queryParams(ImmutableMultimap.of("key", "anothervalue")),
new TestHttpRequestOptions().headerParams(ImmutableMultimap.of("X-header-1", "fooya again!")),
new TestHttpRequestOptions().payload("last_payload_wins!"));
assertRequestLineEquals(request, "POST http://localhost:9999?key=value&key=anothervalue HTTP/1.1");
assertNonPayloadHeadersEqual(request, "X-header-1: fooya\nX-header-1: fooya again!\n");
assertPayloadEquals(request, "last_payload_wins!", "application/unknown", false);
}
public class TestCustomMethod {
@FOO
public void foo() {