do not send payload when method doesn't send payload

This commit is contained in:
adriancole 2013-03-03 17:22:23 -08:00
parent b3b2d4c572
commit 4f2c64681a
3 changed files with 12 additions and 8 deletions

View File

@ -28,6 +28,7 @@ import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jclouds.io.Payload;
import org.jclouds.javax.annotation.Nullable;
@ -36,6 +37,7 @@ import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
@ -47,6 +49,9 @@ import com.google.common.collect.Multimaps;
*/
public class HttpRequest extends HttpMessage {
public static final Set<String> NON_PAYLOAD_METHODS = ImmutableSet
.of("OPTIONS", "GET", "HEAD", "DELETE", "TRACE", "CONNECT");
public static Builder<?> builder() {
return new ConcreteBuilder();
}

View File

@ -63,7 +63,6 @@ import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.CountingOutputStream;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.inject.Inject;
@ -227,11 +226,8 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
}
protected void writeNothing(HttpURLConnection connection) {
connection.setRequestProperty(CONTENT_LENGTH, "0");
// for some reason POST/PUT undoes the content length header above.
if (ImmutableSet.of("POST", "PUT").contains(connection.getRequestMethod())) {
connection.setFixedLengthStreamingMode(0);
connection.setDoOutput(true);
if (!HttpRequest.NON_PAYLOAD_METHODS.contains(connection.getRequestMethod())) {
connection.setRequestProperty(CONTENT_LENGTH, "0");
}
}

View File

@ -200,10 +200,13 @@ public class RestAnnotationProcessor implements Function<Invocation, HttpRequest
throw new NoSuchElementException(format("no endpoint found for %s", invocation));
GeneratedHttpRequest.Builder requestBuilder = GeneratedHttpRequest.builder().invocation(invocation)
.caller(caller);
String requestMethod = null;
if (r != null) {
requestMethod = r.getMethod();
requestBuilder.fromHttpRequest(r);
} else {
requestBuilder.method(tryFindHttpMethod(invocation.getInvokable()).get());
requestMethod = tryFindHttpMethod(invocation.getInvokable()).get();
requestBuilder.method(requestMethod);
}
requestBuilder.filters(getFiltersIfAnnotated(invocation));
@ -286,7 +289,7 @@ public class RestAnnotationProcessor implements Function<Invocation, HttpRequest
payload = new MultipartForm(MultipartForm.BOUNDARY, parts);
} else if (formParams.size() > 0) {
payload = Payloads.newUrlEncodedFormPayload(transformValues(formParams, NullableToStringFunction.INSTANCE));
} else if (headers.containsKey(CONTENT_TYPE)) {
} else if (headers.containsKey(CONTENT_TYPE) && !HttpRequest.NON_PAYLOAD_METHODS.contains(requestMethod)) {
if (payload == null)
payload = Payloads.newByteArrayPayload(new byte[] {});
payload.getContentMetadata().setContentType(get(headers.get(CONTENT_TYPE), 0));