View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * {@link org.springframework.http.client.ClientHttpRequest} implementation that uses
39   * Apache HttpComponents HttpClient to execute requests.
40   *
41   * <p>Created via the {@link HttpComponentsClientHttpRequestFactory}.
42   *
43   * <p><b>NOTE:</b> Requires Apache HttpComponents 4.3 or higher, as of Spring 4.0.
44   *
45   * @author Oleg Kalnichevski
46   * @author Arjen Poutsma
47   * @author Juergen Hoeller
48   * @since 3.1
49   * @see HttpComponentsClientHttpRequestFactory#createRequest(URI, HttpMethod)
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  	 * Add the given headers to the given HTTP request.
98  	 * @param httpRequest the request to add the headers to
99  	 * @param headers the headers to add
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)) {  // RFC 6265
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 }