added debug to http executor

This commit is contained in:
Adrian Cole 2011-02-24 21:45:04 -08:00
parent f459100c75
commit 84004b8986
2 changed files with 33 additions and 18 deletions

View File

@ -153,8 +153,8 @@ public abstract class BaseHttpCommandExecutorService<Q> implements HttpCommandEx
"After filtering, the request has niether chunked encoding nor content length: " + request); "After filtering, the request has niether chunked encoding nor content length: " + request);
logger.debug("Sending request %s: %s", request.hashCode(), request.getRequestLine()); logger.debug("Sending request %s: %s", request.hashCode(), request.getRequestLine());
wirePayloadIfEnabled(wire, request); wirePayloadIfEnabled(wire, request);
nativeRequest = convert(request);
utils.logRequest(headerLog, request, ">>"); utils.logRequest(headerLog, request, ">>");
nativeRequest = convert(request);
response = invoke(nativeRequest); response = invoke(nativeRequest);
logger.debug("Receiving response %s: %s", request.hashCode(), response.getStatusLine()); logger.debug("Receiving response %s: %s", request.hashCode(), response.getStatusLine());

View File

@ -19,6 +19,7 @@
package org.jclouds.http.internal; package org.jclouds.http.internal;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.propagate; import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.getLast; import static com.google.common.collect.Iterables.getLast;
@ -66,8 +67,9 @@ import org.jclouds.rest.internal.RestAnnotationProcessor;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.io.CountingOutputStream;
/** /**
* Basic implementation of a {@link HttpCommandExecutorService}. * Basic implementation of a {@link HttpCommandExecutorService}.
@ -86,11 +88,11 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
@Inject @Inject
public JavaUrlHttpCommandExecutorService(HttpUtils utils, public JavaUrlHttpCommandExecutorService(HttpUtils utils,
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor, @Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor,
DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler, DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler,
DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier, DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier,
@Named("untrusted") Supplier<SSLContext> untrustedSSLContextProvider) throws SecurityException, @Named("untrusted") Supplier<SSLContext> untrustedSSLContextProvider) throws SecurityException,
NoSuchFieldException { NoSuchFieldException {
super(utils, ioWorkerExecutor, retryHandler, ioRetryHandler, errorHandler, wire); super(utils, ioWorkerExecutor, retryHandler, ioRetryHandler, errorHandler, wire);
if (utils.getMaxConnections() > 0) if (utils.getMaxConnections() > 0)
System.setProperty("http.maxConnections", String.valueOf(checkNotNull(utils, "utils").getMaxConnections())); System.setProperty("http.maxConnections", String.valueOf(checkNotNull(utils, "utils").getMaxConnections()));
@ -102,6 +104,7 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
@Override @Override
protected HttpResponse invoke(HttpURLConnection connection) throws IOException, InterruptedException { protected HttpResponse invoke(HttpURLConnection connection) throws IOException, InterruptedException {
HttpResponse.Builder builder = HttpResponse.builder();
InputStream in = null; InputStream in = null;
try { try {
in = consumeOnClose(connection.getInputStream()); in = consumeOnClose(connection.getInputStream());
@ -113,19 +116,28 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
assert false : "should have propagated exception"; assert false : "should have propagated exception";
} }
if (connection.getResponseCode() == 204) { int responseCode = connection.getResponseCode();
if (responseCode == 204) {
closeQuietly(in); closeQuietly(in);
in = null; in = null;
} }
Multimap<String, String> headers = LinkedHashMultimap.create(); builder.statusCode(responseCode);
builder.message(connection.getResponseMessage());
Builder<String, String> headerBuilder = ImmutableMultimap.<String, String> builder();
for (String header : connection.getHeaderFields().keySet()) { for (String header : connection.getHeaderFields().keySet()) {
headers.putAll(header, connection.getHeaderFields().get(header)); // HTTP message comes back as a header without a key
if (header != null)
headerBuilder.putAll(header, connection.getHeaderFields().get(header));
} }
ImmutableMultimap<String, String> headers = headerBuilder.build();
Payload payload = in != null ? Payloads.newInputStreamPayload(in) : null; Payload payload = in != null ? Payloads.newInputStreamPayload(in) : null;
if (payload != null) if (payload != null) {
payload.getContentMetadata().setPropertiesFromHttpHeaders(headers); payload.getContentMetadata().setPropertiesFromHttpHeaders(headers);
return new HttpResponse(connection.getResponseCode(), connection.getResponseMessage(), payload, builder.payload(payload);
RestAnnotationProcessor.filterOutContentHeaders(headers)); }
builder.headers(RestAnnotationProcessor.filterOutContentHeaders(headers));
return builder.build();
} }
private InputStream bufferAndCloseStream(InputStream inputStream) throws IOException { private InputStream bufferAndCloseStream(InputStream inputStream) throws IOException {
@ -220,14 +232,17 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
} else { } else {
Long length = checkNotNull(md.getContentLength(), "payload.getContentLength"); Long length = checkNotNull(md.getContentLength(), "payload.getContentLength");
connection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, length.toString()); connection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, length.toString());
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6755625
checkArgument(length < Integer.MAX_VALUE,
"JDK 1.6 does not support >2GB chunks. Use chunked encoding, if possible.");
connection.setFixedLengthStreamingMode(length.intValue()); connection.setFixedLengthStreamingMode(length.intValue());
} }
// writeTo will close the output stream CountingOutputStream out = new CountingOutputStream(connection.getOutputStream());
try { try {
request.getPayload().writeTo(connection.getOutputStream()); request.getPayload().writeTo(out);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new RuntimeException(String.format("error after writing %d/%s bytes to %s", out.getCount(), md
throw e; .getContentLength(), request.getRequestLine()), e);
} }
} else { } else {
connection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, "0"); connection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, "0");