diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java index 751dfa9d5c..1c07868ef6 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java @@ -32,6 +32,7 @@ import org.apache.http.protocol.HttpContext; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.util.StringUtils; /** * {@link org.springframework.http.client.ClientHttpRequest} implementation that uses @@ -41,6 +42,7 @@ import org.springframework.http.HttpMethod; * * @author Oleg Kalnichevski * @author Arjen Poutsma + * @author Juergen Hoeller * @since 3.1 * @see HttpComponentsClientHttpRequestFactory#createRequest(URI, HttpMethod) */ @@ -97,8 +99,12 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR static void addHeaders(HttpUriRequest httpRequest, HttpHeaders headers) { for (Map.Entry> entry : headers.entrySet()) { String headerName = entry.getKey(); - if (!headerName.equalsIgnoreCase(HTTP.CONTENT_LEN) && - !headerName.equalsIgnoreCase(HTTP.TRANSFER_ENCODING)) { + if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265 + String headerValue = StringUtils.collectionToDelimitedString(entry.getValue(), "; "); + httpRequest.addHeader(headerName, headerValue); + } + else if (!HTTP.CONTENT_LEN.equalsIgnoreCase(headerName) && + !HTTP.TRANSFER_ENCODING.equalsIgnoreCase(headerName)) { for (String headerValue : entry.getValue()) { httpRequest.addHeader(headerName, headerValue); } diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java index 7b5304a8a6..e9594e2864 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java @@ -20,8 +20,6 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; -import java.util.List; -import java.util.Map; import java.util.concurrent.Callable; import org.springframework.core.task.AsyncListenableTaskExecutor; @@ -32,7 +30,7 @@ import org.springframework.util.concurrent.ListenableFuture; /** * {@link org.springframework.http.client.ClientHttpRequest} implementation that uses - * standard J2SE facilities to execute buffered requests. Created via the + * standard JDK facilities to execute buffered requests. Created via the * {@link org.springframework.http.client.SimpleClientHttpRequestFactory}. * * @author Arjen Poutsma @@ -79,12 +77,7 @@ final class SimpleBufferingAsyncClientHttpRequest extends AbstractBufferingAsync return this.taskExecutor.submitListenable(new Callable() { @Override public ClientHttpResponse call() throws Exception { - for (Map.Entry> entry : headers.entrySet()) { - String headerName = entry.getKey(); - for (String headerValue : entry.getValue()) { - connection.addRequestProperty(headerName, headerValue); - } - } + SimpleBufferingClientHttpRequest.addHeaders(connection, headers); if (connection.getDoOutput() && outputStreaming) { connection.setFixedLengthStreamingMode(bufferedOutput.length); } diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java index 9c2d19b3cb..6f7531255d 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -26,12 +26,14 @@ import java.util.Map; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.util.FileCopyUtils; +import org.springframework.util.StringUtils; /** - * {@link ClientHttpRequest} implementation that uses standard J2SE facilities to execute buffered requests. - * Created via the {@link SimpleClientHttpRequestFactory}. + * {@link ClientHttpRequest} implementation that uses standard JDK facilities to + * execute buffered requests. Created via the {@link SimpleClientHttpRequestFactory}. * * @author Arjen Poutsma + * @author Juergen Hoeller * @since 3.0 * @see SimpleClientHttpRequestFactory#createRequest(java.net.URI, HttpMethod) */ @@ -65,12 +67,7 @@ final class SimpleBufferingClientHttpRequest extends AbstractBufferingClientHttp @Override protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { - for (Map.Entry> entry : headers.entrySet()) { - String headerName = entry.getKey(); - for (String headerValue : entry.getValue()) { - this.connection.addRequestProperty(headerName, headerValue); - } - } + addHeaders(this.connection, headers); if (this.connection.getDoOutput() && this.outputStreaming) { this.connection.setFixedLengthStreamingMode(bufferedOutput.length); @@ -83,4 +80,25 @@ final class SimpleBufferingClientHttpRequest extends AbstractBufferingClientHttp return new SimpleClientHttpResponse(this.connection); } + + /** + * Add the given headers to the given HTTP connection. + * @param connection the connection to add the headers to + * @param headers the headers to add + */ + static void addHeaders(HttpURLConnection connection, HttpHeaders headers) { + for (Map.Entry> entry : headers.entrySet()) { + String headerName = entry.getKey(); + if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265 + String headerValue = StringUtils.collectionToDelimitedString(entry.getValue(), "; "); + connection.setRequestProperty(headerName, headerValue); + } + else { + for (String headerValue : entry.getValue()) { + connection.addRequestProperty(headerName, headerValue); + } + } + } + } + } diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java index 34ba8911f2..cf6f59690b 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java @@ -21,8 +21,6 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; -import java.util.List; -import java.util.Map; import java.util.concurrent.Callable; import org.springframework.core.task.AsyncListenableTaskExecutor; @@ -91,22 +89,13 @@ final class SimpleStreamingAsyncClientHttpRequest extends AbstractAsyncClientHtt this.connection.setChunkedStreamingMode(this.chunkSize); } } - writeHeaders(headers); + SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers); this.connection.connect(); this.body = this.connection.getOutputStream(); } return StreamUtils.nonClosing(this.body); } - private void writeHeaders(HttpHeaders headers) { - for (Map.Entry> entry : headers.entrySet()) { - String headerName = entry.getKey(); - for (String headerValue : entry.getValue()) { - this.connection.addRequestProperty(headerName, headerValue); - } - } - } - @Override protected ListenableFuture executeInternal(final HttpHeaders headers) throws IOException { return this.taskExecutor.submitListenable(new Callable() { @@ -117,7 +106,7 @@ final class SimpleStreamingAsyncClientHttpRequest extends AbstractAsyncClientHtt body.close(); } else { - writeHeaders(headers); + SimpleBufferingClientHttpRequest.addHeaders(connection, headers); connection.connect(); } } diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java index 4df9392cd3..504f567a61 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java @@ -21,16 +21,14 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; -import java.util.List; -import java.util.Map; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.util.StreamUtils; /** - * {@link ClientHttpRequest} implementation that uses standard J2SE facilities to execute streaming requests. - * Created via the {@link SimpleClientHttpRequestFactory}. + * {@link ClientHttpRequest} implementation that uses standard JDK facilities to + * execute streaming requests. Created via the {@link SimpleClientHttpRequestFactory}. * * @author Arjen Poutsma * @since 3.0 @@ -80,22 +78,13 @@ final class SimpleStreamingClientHttpRequest extends AbstractClientHttpRequest { this.connection.setChunkedStreamingMode(this.chunkSize); } } - writeHeaders(headers); + SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers); this.connection.connect(); this.body = this.connection.getOutputStream(); } return StreamUtils.nonClosing(this.body); } - private void writeHeaders(HttpHeaders headers) { - for (Map.Entry> 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 { @@ -103,7 +92,7 @@ final class SimpleStreamingClientHttpRequest extends AbstractClientHttpRequest { this.body.close(); } else { - writeHeaders(headers); + SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers); this.connection.connect(); } }