fixed issue where body of a Payload arg wasn't being added to the http request

This commit is contained in:
Adrian Cole 2010-12-04 23:45:03 +00:00
parent 6e975662c1
commit 1063924f6a
4 changed files with 226 additions and 211 deletions

View File

@ -472,8 +472,9 @@ public class RestAnnotationProcessor<T> {
} else if (formParams.size() > 0) { } else if (formParams.size() > 0) {
payload = Payloads.newUrlEncodedFormPayload(formParams, skips); payload = Payloads.newUrlEncodedFormPayload(formParams, skips);
} else if (headers.containsKey(CONTENT_TYPE)) { } else if (headers.containsKey(CONTENT_TYPE)) {
payload = Payloads.newByteArrayPayload(new byte[]{}); if (payload == null)
payload.getContentMetadata().setContentType(Iterables.get(headers.get(CONTENT_TYPE),0)); payload = Payloads.newByteArrayPayload(new byte[] {});
payload.getContentMetadata().setContentType(Iterables.get(headers.get(CONTENT_TYPE), 0));
} }
if (payload != null) { if (payload != null) {
request.setPayload(payload); request.setPayload(payload);
@ -633,8 +634,8 @@ public class RestAnnotationProcessor<T> {
int index = map.keySet().iterator().next(); int index = map.keySet().iterator().next();
try { try {
URI returnVal = parser.apply(args[index]); URI returnVal = parser.apply(args[index]);
checkArgument(returnVal != null, String.format("endpoint for [%s] not configured for %s", args[index], checkArgument(returnVal != null,
method)); String.format("endpoint for [%s] not configured for %s", args[index], 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);
@ -651,8 +652,8 @@ public class RestAnnotationProcessor<T> {
}); });
try { try {
URI returnVal = parser.apply(argsToParse); URI returnVal = parser.apply(argsToParse);
checkArgument(returnVal != null, String.format("endpoint for [%s] not configured for %s", argsToParse, checkArgument(returnVal != null,
method)); String.format("endpoint for [%s] not configured for %s", argsToParse, method));
return returnVal; return returnVal;
} catch (NullPointerException e) { } catch (NullPointerException e) {
throw new IllegalArgumentException(String.format("argument at indexes %s on method %s", map.keySet(), throw new IllegalArgumentException(String.format("argument at indexes %s on method %s", map.keySet(),

View File

@ -104,6 +104,7 @@ import org.jclouds.http.options.GetOptions;
import org.jclouds.http.options.HttpRequestOptions; import org.jclouds.http.options.HttpRequestOptions;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.io.PayloadEnclosing; import org.jclouds.io.PayloadEnclosing;
import org.jclouds.io.Payloads;
import org.jclouds.logging.config.NullLoggingModule; import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.BaseRestClientTest; import org.jclouds.rest.BaseRestClientTest;
import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.ConfiguresRestClient;
@ -278,9 +279,9 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
private Injector injectorForClient() { private Injector injectorForClient() {
RestContextSpec<Caller, AsyncCaller> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", null, RestContextSpec<Caller, AsyncCaller> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo",
Caller.class, AsyncCaller.class, ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), null, Caller.class, AsyncCaller.class,
new CallerCalleeModule())); ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), new CallerCalleeModule()));
return createContextBuilder(contextSpec).buildInjector(); return createContextBuilder(contextSpec).buildInjector();
@ -295,7 +296,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
}; };
@Target( { ElementType.METHOD }) @Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@javax.ws.rs.HttpMethod("FOO") @javax.ws.rs.HttpMethod("FOO")
public @interface FOO { public @interface FOO {
@ -378,9 +379,14 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Path("") @Path("")
@Produces(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_OCTET_STREAM)
public void post(); public void post();
@POST
@Path("")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public void post(Payload payload);
} }
public void testHttpRequestOptionsPayloadParam() throws SecurityException, NoSuchMethodException, IOException { public void testHttpRequestOptionsNoPayloadParam() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPayloadParamVarargs.class.getMethod("post"); Method method = TestPayloadParamVarargs.class.getMethod("post");
HttpRequest request = factory(TestQuery.class).createRequest(method); HttpRequest request = factory(TestQuery.class).createRequest(method);
assertRequestLineEquals(request, "POST http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); assertRequestLineEquals(request, "POST http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1");
@ -388,6 +394,14 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertPayloadEquals(request, "", "application/octet-stream", false); assertPayloadEquals(request, "", "application/octet-stream", false);
} }
public void testHttpRequestOptionsPayloadParam() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPayloadParamVarargs.class.getMethod("post", Payload.class);
HttpRequest request = factory(TestQuery.class).createRequest(method, Payloads.newStringPayload("foo"));
assertRequestLineEquals(request, "POST http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, "foo", "application/octet-stream", false);
}
public void testHttpRequestWithOnlyContentType() throws SecurityException, NoSuchMethodException, IOException { public void testHttpRequestWithOnlyContentType() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPayloadParamVarargs.class.getMethod("post", HttpRequestOptions.class); Method method = TestPayloadParamVarargs.class.getMethod("post", HttpRequestOptions.class);
verifyTestPostOptions(method); verifyTestPostOptions(method);
@ -761,7 +775,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<? extends Set<String>> testUnwrap4(); ListenableFuture<? extends Set<String>> testUnwrap4();
@Target( { ElementType.METHOD }) @Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@HttpMethod("ROWDY") @HttpMethod("ROWDY")
public @interface ROWDY { public @interface ROWDY {
@ -833,8 +847,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
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\"}"))),
"foo", "bar")); ImmutableMap.of("foo", "bar"));
} }
@ -849,8 +863,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
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\"}"))),
"foo", "bar")); ImmutableMap.of("foo", "bar"));
} }
@ -865,8 +879,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
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\"}"))),
"foo", "bar")); ImmutableMap.of("foo", "bar"));
} }
@ -1211,8 +1225,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Test @Test
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)
new Object[] { "robot", new TestReplaceQueryOptions() }).getEndpoint().getQuery(); .createRequest(oneQuery, new Object[] { "robot", new TestReplaceQueryOptions() }).getEndpoint().getQuery();
assertEquals(query, "x-amz-copy-source=/robot"); assertEquals(query, "x-amz-copy-source=/robot");
} }
@ -1308,8 +1322,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
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)
new Object[] { "robot", "eggs" }).getEndpoint().getQuery(); .createRequest(twoQuerysOutOfOrder, new Object[] { "robot", "eggs" }).getEndpoint().getQuery();
assertEquals(query, "x-amz-copy-source=/eggs/robot"); assertEquals(query, "x-amz-copy-source=/eggs/robot");
} }
@ -1323,8 +1337,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
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)
new Object[] { "robot", new TestReplaceMatrixOptions() }).getEndpoint().getPath(); .createRequest(oneMatrix, new Object[] { "robot", new TestReplaceMatrixOptions() }).getEndpoint().getPath();
assertEquals(path, "/;x-amz-copy-source=/robot"); assertEquals(path, "/;x-amz-copy-source=/robot");
} }
@ -1407,8 +1421,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
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)
new Object[] { "robot", "eggs" }).getEndpoint().getPath(); .createRequest(twoMatrixsOutOfOrder, new Object[] { "robot", "eggs" }).getEndpoint().getPath();
assertEquals(path, "/;x-amz-copy-source=/eggs/robot"); assertEquals(path, "/;x-amz-copy-source=/eggs/robot");
} }
@ -1620,8 +1634,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void oneTransformerWithContext() throws SecurityException, NoSuchMethodException { public void oneTransformerWithContext() throws SecurityException, NoSuchMethodException {
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",
.create("http://localhost"), TestTransformers.class, method); URI.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);
@ -1688,8 +1702,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertEquals(request.getMethod(), HttpMethod.GET); assertEquals(request.getMethod(), HttpMethod.GET);
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),
.rfc822DateFormat(date))); Collections.singletonList(dateService.rfc822DateFormat(date)));
} }
public void testCreateGetOptionsThatProducesHeaders() throws SecurityException, NoSuchMethodException { public void testCreateGetOptionsThatProducesHeaders() throws SecurityException, NoSuchMethodException {
@ -1702,8 +1716,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertEquals(request.getMethod(), HttpMethod.GET); assertEquals(request.getMethod(), HttpMethod.GET);
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),
.rfc822DateFormat(date))); Collections.singletonList(dateService.rfc822DateFormat(date)));
} }
public class PrefixOptions extends BaseHttpRequestOptions { public class PrefixOptions extends BaseHttpRequestOptions {
@ -2094,13 +2108,12 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@BeforeClass @BeforeClass
void setupFactory() { void setupFactory() {
RestContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", null, RestContextSpec<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,
new AbstractModule() { ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), 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"));
} }
})); }));

View File

@ -300,7 +300,7 @@ public class ElasticStackAsyncClientTest extends RestClientTest<ElasticStackAsyn
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/drives/100/write HTTP/1.1"); assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/drives/100/write HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n"); assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, "", MediaType.APPLICATION_OCTET_STREAM, false); assertPayloadEquals(httpRequest, "foo", MediaType.APPLICATION_OCTET_STREAM, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class); assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null); assertSaxResponseParserClassEquals(method, null);
@ -316,7 +316,7 @@ public class ElasticStackAsyncClientTest extends RestClientTest<ElasticStackAsyn
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/drives/100/write/2048 HTTP/1.1"); assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/drives/100/write/2048 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n"); assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, "", MediaType.APPLICATION_OCTET_STREAM, false); assertPayloadEquals(httpRequest, "foo", MediaType.APPLICATION_OCTET_STREAM, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class); assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null); assertSaxResponseParserClassEquals(method, null);

View File

@ -76,7 +76,8 @@ public class ElasticStackClientLiveTest extends
// TODO block until complete // TODO block until complete
System.err.println("state " + client.getDriveInfo(info2.getUuid())); System.err.println("state " + client.getDriveInfo(info2.getUuid()));
assertEquals(Utils.toStringAndClose(client.readDrive(info2.getUuid()).getInput()), "foo"); assertEquals(Utils.toStringAndClose(client.readDrive(info2.getUuid(),
ReadDriveOptions.Builder.offset(0).size(3)).getInput()), "foo");
} finally { } finally {
client.destroyDrive(info2.getUuid()); client.destroyDrive(info2.getUuid());
} }