polishing
This commit is contained in:
parent
c564049b23
commit
f0db3d0992
|
|
@ -34,13 +34,13 @@ import org.springframework.http.HttpMethod;
|
|||
|
||||
/**
|
||||
* {@link org.springframework.http.client.ClientHttpRequest} implementation that uses
|
||||
* Apache HTTPComponents HttpClient to execute requests.
|
||||
* Apache HttpComponents HttpClient to execute requests.
|
||||
*
|
||||
* <p>Created via the {@link HttpComponentsClientHttpRequestFactory}.
|
||||
*
|
||||
* @author Oleg Kalnichevski
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
* @since 3.1
|
||||
* @see HttpComponentsClientHttpRequestFactory#createRequest(URI, HttpMethod)
|
||||
*/
|
||||
final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpRequest {
|
||||
|
|
@ -49,19 +49,22 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR
|
|||
|
||||
private final HttpUriRequest httpRequest;
|
||||
|
||||
|
||||
public HttpComponentsClientHttpRequest(HttpClient httpClient, HttpUriRequest httpRequest) {
|
||||
this.httpClient = httpClient;
|
||||
this.httpRequest = httpRequest;
|
||||
}
|
||||
|
||||
|
||||
public HttpMethod getMethod() {
|
||||
return HttpMethod.valueOf(httpRequest.getMethod());
|
||||
return HttpMethod.valueOf(this.httpRequest.getMethod());
|
||||
}
|
||||
|
||||
public URI getURI() {
|
||||
return httpRequest.getURI();
|
||||
return this.httpRequest.getURI();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
|
||||
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
|
||||
|
|
@ -69,17 +72,17 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR
|
|||
if (!headerName.equalsIgnoreCase(HTTP.CONTENT_LEN) &&
|
||||
!headerName.equalsIgnoreCase(HTTP.TRANSFER_ENCODING)) {
|
||||
for (String headerValue : entry.getValue()) {
|
||||
httpRequest.addHeader(headerName, headerValue);
|
||||
this.httpRequest.addHeader(headerName, headerValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (httpRequest instanceof HttpEntityEnclosingRequest) {
|
||||
HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest;
|
||||
if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
|
||||
HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
|
||||
HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
|
||||
entityEnclosingRequest.setEntity(requestEntity);
|
||||
}
|
||||
HttpResponse httpResponse = httpClient.execute(httpRequest);
|
||||
HttpResponse httpResponse = this.httpClient.execute(this.httpRequest);
|
||||
return new HttpComponentsClientHttpResponse(httpResponse);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,13 +29,13 @@ import org.springframework.http.HttpStatus;
|
|||
|
||||
/**
|
||||
* {@link org.springframework.http.client.ClientHttpResponse} implementation that uses
|
||||
* Apache Http Components HttpClient to execute requests.
|
||||
* Apache HttpComponents HttpClient to execute requests.
|
||||
*
|
||||
* <p>Created via the {@link HttpComponentsClientHttpRequest}.
|
||||
*
|
||||
* @author Oleg Kalnichevski
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
* @since 3.1
|
||||
* @see HttpComponentsClientHttpRequest#execute()
|
||||
*/
|
||||
final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
|
||||
|
|
@ -44,35 +44,37 @@ final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
|
|||
|
||||
private HttpHeaders headers;
|
||||
|
||||
|
||||
public HttpComponentsClientHttpResponse(HttpResponse httpResponse) {
|
||||
this.httpResponse = httpResponse;
|
||||
}
|
||||
|
||||
|
||||
public HttpStatus getStatusCode() throws IOException {
|
||||
return HttpStatus.valueOf(httpResponse.getStatusLine().getStatusCode());
|
||||
return HttpStatus.valueOf(this.httpResponse.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
public String getStatusText() throws IOException {
|
||||
return httpResponse.getStatusLine().getReasonPhrase();
|
||||
return this.httpResponse.getStatusLine().getReasonPhrase();
|
||||
}
|
||||
|
||||
public HttpHeaders getHeaders() {
|
||||
if (headers == null) {
|
||||
headers = new HttpHeaders();
|
||||
for (Header header : httpResponse.getAllHeaders()) {
|
||||
headers.add(header.getName(), header.getValue());
|
||||
if (this.headers == null) {
|
||||
this.headers = new HttpHeaders();
|
||||
for (Header header : this.httpResponse.getAllHeaders()) {
|
||||
this.headers.add(header.getName(), header.getValue());
|
||||
}
|
||||
}
|
||||
return headers;
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
public InputStream getBody() throws IOException {
|
||||
HttpEntity entity = httpResponse.getEntity();
|
||||
HttpEntity entity = this.httpResponse.getEntity();
|
||||
return entity != null ? entity.getContent() : null;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
HttpEntity entity = httpResponse.getEntity();
|
||||
HttpEntity entity = this.httpResponse.getEntity();
|
||||
if (entity != null) {
|
||||
try {
|
||||
// Release underlying connection back to the connection manager
|
||||
|
|
@ -83,4 +85,5 @@ final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue