Browse Source

SPR-8809 - RestTemplate headers not sent when bufferRequestBody is false

pull/7/head
Arjen Poutsma 13 years ago
parent
commit
53cb529162
  1. 5
      org.springframework.web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java
  2. 44
      org.springframework.web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java
  3. 24
      org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
  4. 10
      org.springframework.web/src/test/java/org/springframework/http/client/StreamingSimpleHttpRequestFactoryTests.java

5
org.springframework.web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java

@ -85,8 +85,9 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest { @@ -85,8 +85,9 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
delegate.getHeaders().putAll(request.getHeaders());
FileCopyUtils.copy(body, delegate.getBody());
if (body.length > 0) {
FileCopyUtils.copy(body, delegate.getBody());
}
return delegate.execute();
}
}

44
org.springframework.web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java

@ -73,30 +73,38 @@ final class SimpleStreamingClientHttpRequest extends AbstractClientHttpRequest { @@ -73,30 +73,38 @@ final class SimpleStreamingClientHttpRequest extends AbstractClientHttpRequest {
else {
this.connection.setChunkedStreamingMode(this.chunkSize);
}
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
for (String headerValue : entry.getValue()) {
this.connection.addRequestProperty(headerName, headerValue);
}
}
this.connection.connect();
writeHeaders(headers);
this.connection.connect();
this.body = this.connection.getOutputStream();
}
return new NonClosingOutputStream(this.body);
}
@Override
private void writeHeaders(HttpHeaders headers) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
for (String headerValue : entry.getValue()) {
this.connection.addRequestProperty(headerName, headerValue);
}
}
}
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
try {
if (this.body != null) {
this.body.close();
}
}
catch (IOException ex) {
// ignore
}
return new SimpleClientHttpResponse(this.connection);
}
try {
if (this.body != null) {
this.body.close();
}
else {
writeHeaders(headers);
this.connection.connect();
}
}
catch (IOException ex) {
// ignore
}
return new SimpleClientHttpResponse(this.connection);
}
private static class NonClosingOutputStream extends FilterOutputStream {

24
org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java

@ -30,6 +30,10 @@ import javax.servlet.http.HttpServlet; @@ -30,6 +30,10 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.util.FileCopyUtils;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
@ -38,11 +42,8 @@ import org.mortbay.jetty.Server; @@ -38,11 +42,8 @@ import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.util.FileCopyUtils;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public abstract class AbstractHttpRequestFactoryTestCase {
@ -233,16 +234,9 @@ public abstract class AbstractHttpRequestFactoryTestCase { @@ -233,16 +234,9 @@ public abstract class AbstractHttpRequestFactoryTestCase {
private static class EchoServlet extends HttpServlet {
@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
echo(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
echo(request, response);
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
echo(req, resp);
}
private void echo(HttpServletRequest request, HttpServletResponse response) throws IOException {

10
org.springframework.web/src/test/java/org/springframework/http/client/StreamingSimpleHttpRequestFactoryTests.java

@ -22,15 +22,15 @@ import java.net.URI; @@ -22,15 +22,15 @@ import java.net.URI;
import java.util.Collections;
import java.util.Random;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
@ -58,7 +58,7 @@ public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestF @@ -58,7 +58,7 @@ public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestF
ClientHttpResponse response = null;
try {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.GET);
response = request.execute();
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
HttpHeaders responseHeaders = response.getHeaders();

Loading…
Cancel
Save