Issue 62: always set Content-Length

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1424 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-06-11 23:20:37 +00:00
parent cd1c3ae900
commit e773803ee2
4 changed files with 189 additions and 185 deletions

View File

@ -34,6 +34,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.jclouds.http.HttpConstants;
import org.jclouds.http.HttpFutureCommand;
import org.jclouds.http.HttpFutureCommandClient;
import org.jclouds.http.HttpRequest;
@ -50,8 +51,7 @@ import com.google.inject.Inject;
public class JavaUrlHttpFutureCommandClient extends BaseHttpFutureCommandClient {
@Inject
public JavaUrlHttpFutureCommandClient(URL target)
throws MalformedURLException {
public JavaUrlHttpFutureCommandClient(URL target) throws MalformedURLException {
super(target);
}
@ -66,9 +66,7 @@ public class JavaUrlHttpFutureCommandClient extends BaseHttpFutureCommandClient
}
logger.trace("%1$s - converting request %2$s", target, request);
connection = openJavaConnection(request);
logger
.trace("%1$s - submitting request %2$s", target,
connection);
logger.trace("%1$s - submitting request %2$s", target, connection);
response = getResponse(connection);
logger.trace("%1$s - received response %2$s", target, response);
if (response.getStatusCode() >= 500 && httpRetryHandler.retryRequest(command, response))
@ -86,8 +84,7 @@ public class JavaUrlHttpFutureCommandClient extends BaseHttpFutureCommandClient
}
}
protected HttpResponse getResponse(HttpURLConnection connection)
throws IOException {
protected HttpResponse getResponse(HttpURLConnection connection) throws IOException {
HttpResponse response = new HttpResponse();
InputStream in;
try {
@ -100,16 +97,14 @@ public class JavaUrlHttpFutureCommandClient extends BaseHttpFutureCommandClient
}
response.setStatusCode(connection.getResponseCode());
for (String header : connection.getHeaderFields().keySet()) {
response.getHeaders().putAll(header,
connection.getHeaderFields().get(header));
response.getHeaders().putAll(header, connection.getHeaderFields().get(header));
}
response.setMessage(connection.getResponseMessage());
return response;
}
protected HttpURLConnection openJavaConnection(HttpRequest request)
throws IOException {
protected HttpURLConnection openJavaConnection(HttpRequest request) throws IOException {
URL url = new URL(target, request.getUri());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
@ -130,19 +125,19 @@ public class JavaUrlHttpFutureCommandClient extends BaseHttpFutureCommandClient
} else if (request.getPayload() instanceof InputStream) {
IOUtils.copy((InputStream) request.getPayload(), out);
} else if (request.getPayload() instanceof File) {
IOUtils.copy(new FileInputStream((File) request
.getPayload()), out);
IOUtils.copy(new FileInputStream((File) request.getPayload()), out);
} else if (request.getPayload() instanceof byte[]) {
IOUtils.write((byte[]) request.getPayload(), out);
} else {
throw new UnsupportedOperationException(
"Content not supported "
throw new UnsupportedOperationException("Content not supported "
+ request.getPayload().getClass());
}
} finally {
IOUtils.closeQuietly(out);
}
} else {
connection.setRequestProperty(HttpConstants.CONTENT_LENGTH, "0");
}
return connection;
}

View File

@ -65,13 +65,12 @@ public abstract class BaseJettyTest {
@BeforeTest
@Parameters( { "test-jetty-port" })
public void setUpJetty(@Optional("8123") final int testPort)
throws Exception {
public void setUpJetty(@Optional("8123") final int testPort) throws Exception {
Handler handler = new AbstractHandler() {
public void handle(String target, HttpServletRequest request,
HttpServletResponse response, int dispatch)
throws IOException, ServletException {
HttpServletResponse response, int dispatch) throws IOException, ServletException {
failIfNoContentLength(request, response);
if (request.getHeader("test") != null) {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
@ -95,8 +94,7 @@ public abstract class BaseJettyTest {
properties.put(HttpConstants.PROPERTY_HTTP_PORT, testPort + "");
properties.put(HttpConstants.PROPERTY_HTTP_SECURE, "false");
addConnectionProperties(properties);
final List<HttpRequestFilter> filters = new ArrayList<HttpRequestFilter>(
1);
final List<HttpRequestFilter> filters = new ArrayList<HttpRequestFilter>(1);
filters.add(new HttpRequestFilter() {
public void filter(HttpRequest request) throws HttpException {
if (request.getHeaders().containsKey("filterme")) {
@ -109,8 +107,8 @@ public abstract class BaseJettyTest {
protected void configure() {
Names.bindProperties(binder(), properties);
}
}, new JDKLoggingModule(), new HttpCommandsModule(),
createClientModule(), new AbstractModule() {
}, new JDKLoggingModule(), new HttpCommandsModule(), createClientModule(),
new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<List<HttpRequestFilter>>() {
@ -129,7 +127,6 @@ public abstract class BaseJettyTest {
server.stop();
}
protected abstract void addConnectionProperties(Properties props);
protected abstract Module createClientModule();
@ -142,8 +139,8 @@ public abstract class BaseJettyTest {
* @return
* @throws IOException
*/
protected boolean failOnRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException {
protected boolean failOnRequest(HttpServletRequest request, HttpServletResponse response)
throws IOException {
if (cycle.incrementAndGet() % 10 == 0) {
response.sendError(500);
((Request) request).setHandled(true);
@ -152,4 +149,14 @@ public abstract class BaseJettyTest {
return false;
}
protected boolean failIfNoContentLength(HttpServletRequest request, HttpServletResponse response)
throws IOException {
if (request.getHeader(HttpConstants.CONTENT_LENGTH) == null) {
response.sendError(500);
((Request) request).setHandled(true);
return true;
}
return false;
}
}

View File

@ -179,6 +179,8 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
if (request.getPayload() != null) {
changeRequestContentToBytes(request);
gaeRequest.setPayload((byte[]) request.getPayload());
} else {
gaeRequest.addHeader(new HTTPHeader(HttpConstants.CONTENT_LENGTH, "0"));
}
return gaeRequest;
}

View File

@ -126,7 +126,7 @@ public class URLFetchServiceClientTest {
HttpRequest request = new HttpRequest("GET", "foo");
HTTPRequest gaeRequest = client.convert(request);
assert gaeRequest.getPayload() == null;
assertEquals(gaeRequest.getHeaders().size(), 0);
assertEquals(gaeRequest.getHeaders().size(), 1);//content length
}
@Test