1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.http.client;
18
19 import java.io.IOException;
20 import java.net.URI;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.http.HttpEntity;
25 import org.apache.http.HttpEntityEnclosingRequest;
26 import org.apache.http.client.methods.CloseableHttpResponse;
27 import org.apache.http.client.methods.HttpUriRequest;
28 import org.apache.http.entity.ByteArrayEntity;
29 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.apache.http.protocol.HTTP;
31 import org.apache.http.protocol.HttpContext;
32
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.HttpMethod;
35 import org.springframework.util.StringUtils;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpRequest {
52
53 private final CloseableHttpClient httpClient;
54
55 private final HttpUriRequest httpRequest;
56
57 private final HttpContext httpContext;
58
59
60 HttpComponentsClientHttpRequest(CloseableHttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) {
61 this.httpClient = httpClient;
62 this.httpRequest = httpRequest;
63 this.httpContext = httpContext;
64 }
65
66
67 @Override
68 public HttpMethod getMethod() {
69 return HttpMethod.valueOf(this.httpRequest.getMethod());
70 }
71
72 @Override
73 public URI getURI() {
74 return this.httpRequest.getURI();
75 }
76
77 HttpContext getHttpContext() {
78 return this.httpContext;
79 }
80
81
82 @Override
83 protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
84 addHeaders(this.httpRequest, headers);
85
86 if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
87 HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
88 HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
89 entityEnclosingRequest.setEntity(requestEntity);
90 }
91 CloseableHttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
92 return new HttpComponentsClientHttpResponse(httpResponse);
93 }
94
95
96
97
98
99
100
101 static void addHeaders(HttpUriRequest httpRequest, HttpHeaders headers) {
102 for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
103 String headerName = entry.getKey();
104 if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) {
105 String headerValue = StringUtils.collectionToDelimitedString(entry.getValue(), "; ");
106 httpRequest.addHeader(headerName, headerValue);
107 }
108 else if (!HTTP.CONTENT_LEN.equalsIgnoreCase(headerName) &&
109 !HTTP.TRANSFER_ENCODING.equalsIgnoreCase(headerName)) {
110 for (String headerValue : entry.getValue()) {
111 httpRequest.addHeader(headerName, headerValue);
112 }
113 }
114 }
115 }
116
117 }