From 210c77ddb0771ef55cd1d0bede6319ec970a98d9 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 13 Jul 2011 23:03:35 +0000 Subject: [PATCH] SimpleClientHttpRequest uses fixed-length streaming mode (always sets content-length header); partial backport from 3.1 M2 --- .../http/client/AbstractClientHttpRequest.java | 10 +++++++--- .../http/client/SimpleClientHttpRequest.java | 9 +++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java index 60afb7d4b9d..361e0cc87e4 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,7 +39,7 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest { public final HttpHeaders getHeaders() { - return executed ? HttpHeaders.readOnlyHttpHeaders(headers) : this.headers; + return (this.executed ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers); } public final OutputStream getBody() throws IOException { @@ -49,7 +49,11 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest { public final ClientHttpResponse execute() throws IOException { checkExecuted(); - ClientHttpResponse result = executeInternal(this.headers, this.bufferedOutput.toByteArray()); + byte[] bytes = this.bufferedOutput.toByteArray(); + if (this.headers.getContentLength() == -1) { + this.headers.setContentLength(bytes.length); + } + ClientHttpResponse result = executeInternal(this.headers, bytes); this.executed = true; return result; } diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java index 5efc1cc6e23..7fec5eb36b3 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,10 +66,15 @@ final class SimpleClientHttpRequest extends AbstractClientHttpRequest { this.connection.addRequestProperty(headerName, headerValue); } } + + if (this.connection.getDoOutput()) { + this.connection.setFixedLengthStreamingMode(bufferedOutput.length); + } this.connection.connect(); - if (bufferedOutput.length > 0) { + if (this.connection.getDoOutput()) { FileCopyUtils.copy(bufferedOutput, this.connection.getOutputStream()); } + return new SimpleClientHttpResponse(this.connection); }