HttpResponse now requires the corresponding request's URL in its constructor, so the response's S3Object key name can be reconstituted. A better approach may be to associate the entire HttpRequest object with the related HttpResponse?

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1620 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
jamurty 2009-07-13 19:56:45 +00:00
parent d8557f6d89
commit f9cd7e19d9
9 changed files with 47 additions and 19 deletions

View File

@ -57,8 +57,12 @@ public class ParseMetadataFromHeaders implements Function<HttpResponse, S3Object
* {@link org.jclouds.aws.s3.domain.S3Object.Metadata} object.
*/
public Metadata apply(HttpResponse from) {
S3Object.Metadata to = new S3Object.Metadata("TODO");
String objectKey = from.getRequestURL().getPath();
if (objectKey.startsWith("/")) {
// Trim initial slash from object key name.
objectKey = objectKey.substring(1);
}
S3Object.Metadata to = new S3Object.Metadata(objectKey);
addAllHeadersTo(from, to);
addUserMetadataTo(from, to);

View File

@ -31,6 +31,9 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
@ -411,7 +414,14 @@ public class StubS3Connection implements S3Connection {
}
private void throwResponseException(int code) throws ExecutionException {
HttpResponse response = new HttpResponse();
HttpResponse response = null;
try {
response = new HttpResponse(new URL("file:///unused")); // TODO: Get real object URL?
} catch (MalformedURLException e) {
// This shouldn't ever happen.
e.printStackTrace();
assert false;
}
response.setStatusCode(code);
throw new ExecutionException(new HttpResponseException(createNiceMock(HttpCommand.class),
response));

View File

@ -24,6 +24,7 @@
package org.jclouds.http;
import java.io.InputStream;
import java.net.URL;
/**
@ -32,9 +33,14 @@ import java.io.InputStream;
* @author Adrian Cole
*/
public class HttpResponse extends HttpMessage {
private URL requestURL;
private int statusCode;
private String message;
private InputStream content;
public HttpResponse(URL requestURL) {
this.requestURL = requestURL;
}
@Override
public String toString() {
@ -71,4 +77,9 @@ public class HttpResponse extends HttpMessage {
public void setContent(InputStream content) {
this.content = content;
}
public URL getRequestURL() {
return this.requestURL;
}
}

View File

@ -90,7 +90,7 @@ public class JavaUrlHttpCommandExecutorService extends
protected HttpResponse invoke(HttpURLConnection connection) throws IOException {
logger.trace("%s - submitting request %s; %s", connection.getURL().getHost(), connection
.getURL(), connection.getHeaderFields().toString());
HttpResponse response = new HttpResponse();
HttpResponse response = new HttpResponse(connection.getURL());
InputStream in;
try {
in = connection.getInputStream();

View File

@ -29,6 +29,7 @@ import static org.testng.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -100,7 +101,7 @@ public class BackoffLimitedRetryHandlerTest {
void testClosesInputStream() throws InterruptedException, IOException {
HttpCommand command = createCommand();
HttpResponse response = new HttpResponse();
HttpResponse response = new HttpResponse(new URL("file:///unused"));
InputStream inputStream = new InputStream() {
boolean isOpen = true;
@ -149,9 +150,9 @@ public class BackoffLimitedRetryHandlerTest {
}
@Test
void testIncrementsFailureCount() throws InterruptedException {
void testIncrementsFailureCount() throws InterruptedException, IOException {
HttpCommand command = createCommand();
HttpResponse response = new HttpResponse();
HttpResponse response = new HttpResponse(new URL("file:///unused"));
handler.shouldRetryRequest(command, response);
assertEquals(command.getFailureCount(), 1);
@ -164,9 +165,9 @@ public class BackoffLimitedRetryHandlerTest {
}
@Test
void testDisallowsExcessiveRetries() throws InterruptedException {
void testDisallowsExcessiveRetries() throws InterruptedException, IOException {
HttpCommand command = createCommand();
HttpResponse response = new HttpResponse();
HttpResponse response = new HttpResponse(new URL("file:///unused"));
assertEquals(handler.shouldRetryRequest(command, response), true); // Failure 1

View File

@ -106,8 +106,8 @@ public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorServic
}
@VisibleForTesting
protected HttpResponse convert(HTTPResponse gaeResponse) {
HttpResponse response = new HttpResponse();
protected HttpResponse convert(URL requestURL, HTTPResponse gaeResponse) {
HttpResponse response = new HttpResponse(requestURL);
response.setStatusCode(gaeResponse.getResponseCode());
for (HTTPHeader header : gaeResponse.getHeaders()) {
response.getHeaders().put(header.getName(), header.getValue());
@ -174,7 +174,7 @@ public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorServic
HTTPResponse response = urlFetchService.fetch(request);
logger.info("%1$s - received response code %2$s, headers: %3$s", request.getURL().getHost(),
response.getResponseCode(), headersAsString(response.getHeaders()));
return convert(response);
return convert(request.getURL(), response);
}
String headersAsString(List<HTTPHeader> headers) {

View File

@ -34,6 +34,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -79,7 +80,7 @@ public class GaeHttpCommandExecutorServiceTest {
}
@Test
void testConvertWithHeaders() {
void testConvertWithHeaders() throws IOException {
HTTPResponse gaeResponse = createMock(HTTPResponse.class);
expect(gaeResponse.getResponseCode()).andReturn(200);
List<HTTPHeader> headers = new ArrayList<HTTPHeader>();
@ -87,7 +88,7 @@ public class GaeHttpCommandExecutorServiceTest {
expect(gaeResponse.getHeaders()).andReturn(headers);
expect(gaeResponse.getContent()).andReturn(null).atLeastOnce();
replay(gaeResponse);
HttpResponse response = client.convert(gaeResponse);
HttpResponse response = client.convert(new URL("file:///unused"), gaeResponse);
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getContent(), null);
assertEquals(response.getHeaders().size(), 1);
@ -103,7 +104,7 @@ public class GaeHttpCommandExecutorServiceTest {
expect(gaeResponse.getHeaders()).andReturn(headers);
expect(gaeResponse.getContent()).andReturn("hello".getBytes()).atLeastOnce();
replay(gaeResponse);
HttpResponse response = client.convert(gaeResponse);
HttpResponse response = client.convert(new URL("file:///unused"), gaeResponse);
assertEquals(response.getStatusCode(), 200);
assertEquals(IOUtils.toString(response.getContent()), "hello");
assertEquals(response.getHeaders().size(), 1);

View File

@ -118,8 +118,8 @@ public class NioHttpCommandExecutionHandler implements NHttpRequestExecutionHand
try {
HttpCommandRendezvous<?> rendezvous = handle.getCommandRendezvous();
HttpCommand command = rendezvous.getCommand();
org.jclouds.http.HttpResponse response = NioHttpUtils
.convertToJavaCloudsResponse(apacheResponse);
org.jclouds.http.HttpResponse response = NioHttpUtils.convertToJavaCloudsResponse(
command.getRequest().getEndpoint().toURL(), apacheResponse);
int statusCode = response.getStatusCode();
// TODO determine how to get the original request here so we don't need to build each
// time

View File

@ -27,6 +27,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import javax.ws.rs.core.HttpHeaders;
@ -102,9 +103,9 @@ public class NioHttpUtils {
}
}
public static HttpResponse convertToJavaCloudsResponse(
public static HttpResponse convertToJavaCloudsResponse(URL requestURL,
org.apache.http.HttpResponse apacheResponse) throws IOException {
HttpResponse response = new HttpResponse();
HttpResponse response = new HttpResponse(requestURL);
if (apacheResponse.getEntity() != null) {
response.setContent(apacheResponse.getEntity().getContent());
}