1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
import java .util .List ;
22
22
import java .util .Map ;
23
23
24
- import org .springframework .http .HttpHeaders ;
25
- import org .springframework .http .HttpMethod ;
26
-
27
24
import org .apache .http .HttpEntity ;
28
25
import org .apache .http .HttpEntityEnclosingRequest ;
29
26
import org .apache .http .HttpResponse ;
33
30
import org .apache .http .protocol .HTTP ;
34
31
import org .apache .http .protocol .HttpContext ;
35
32
33
+ import org .springframework .http .HttpHeaders ;
34
+ import org .springframework .http .HttpMethod ;
35
+ import org .springframework .util .StringUtils ;
36
+
36
37
/**
37
38
* {@link org.springframework.http.client.ClientHttpRequest} implementation that uses
38
39
* Apache HttpComponents HttpClient to execute requests.
@@ -53,7 +54,7 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR
53
54
private final HttpContext httpContext ;
54
55
55
56
56
- public HttpComponentsClientHttpRequest (HttpClient httpClient , HttpUriRequest httpRequest , HttpContext httpContext ) {
57
+ HttpComponentsClientHttpRequest (HttpClient httpClient , HttpUriRequest httpRequest , HttpContext httpContext ) {
57
58
this .httpClient = httpClient ;
58
59
this .httpRequest = httpRequest ;
59
60
this .httpContext = httpContext ;
@@ -71,15 +72,8 @@ public URI getURI() {
71
72
72
73
@ Override
73
74
protected ClientHttpResponse executeInternal (HttpHeaders headers , byte [] bufferedOutput ) throws IOException {
74
- for (Map .Entry <String , List <String >> entry : headers .entrySet ()) {
75
- String headerName = entry .getKey ();
76
- if (!headerName .equalsIgnoreCase (HTTP .CONTENT_LEN ) &&
77
- !headerName .equalsIgnoreCase (HTTP .TRANSFER_ENCODING )) {
78
- for (String headerValue : entry .getValue ()) {
79
- this .httpRequest .addHeader (headerName , headerValue );
80
- }
81
- }
82
- }
75
+ addHeaders (this .httpRequest , headers );
76
+
83
77
if (this .httpRequest instanceof HttpEntityEnclosingRequest ) {
84
78
HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest ) this .httpRequest ;
85
79
HttpEntity requestEntity = new ByteArrayEntity (bufferedOutput );
@@ -89,4 +83,26 @@ protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] buffere
89
83
return new HttpComponentsClientHttpResponse (httpResponse );
90
84
}
91
85
86
+
87
+ /**
88
+ * Add the given headers to the given HTTP request.
89
+ * @param httpRequest the request to add the headers to
90
+ * @param headers the headers to add
91
+ */
92
+ static void addHeaders (HttpUriRequest httpRequest , HttpHeaders headers ) {
93
+ for (Map .Entry <String , List <String >> entry : headers .entrySet ()) {
94
+ String headerName = entry .getKey ();
95
+ if ("Cookie" .equalsIgnoreCase (headerName )) { // RFC 6265
96
+ String headerValue = StringUtils .collectionToDelimitedString (entry .getValue (), "; " );
97
+ httpRequest .addHeader (headerName , headerValue );
98
+ }
99
+ else if (!HTTP .CONTENT_LEN .equalsIgnoreCase (headerName ) &&
100
+ !HTTP .TRANSFER_ENCODING .equalsIgnoreCase (headerName )) {
101
+ for (String headerValue : entry .getValue ()) {
102
+ httpRequest .addHeader (headerName , headerValue );
103
+ }
104
+ }
105
+ }
106
+ }
107
+
92
108
}
0 commit comments