polishing

This commit is contained in:
Juergen Hoeller 2011-08-15 20:57:59 +00:00
parent c564049b23
commit f0db3d0992
2 changed files with 26 additions and 20 deletions

View File

@ -34,13 +34,13 @@ import org.springframework.http.HttpMethod;
/** /**
* {@link org.springframework.http.client.ClientHttpRequest} implementation that uses * {@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}. * <p>Created via the {@link HttpComponentsClientHttpRequestFactory}.
* *
* @author Oleg Kalnichevski * @author Oleg Kalnichevski
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0 * @since 3.1
* @see HttpComponentsClientHttpRequestFactory#createRequest(URI, HttpMethod) * @see HttpComponentsClientHttpRequestFactory#createRequest(URI, HttpMethod)
*/ */
final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpRequest { final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpRequest {
@ -49,19 +49,22 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR
private final HttpUriRequest httpRequest; private final HttpUriRequest httpRequest;
public HttpComponentsClientHttpRequest(HttpClient httpClient, HttpUriRequest httpRequest) { public HttpComponentsClientHttpRequest(HttpClient httpClient, HttpUriRequest httpRequest) {
this.httpClient = httpClient; this.httpClient = httpClient;
this.httpRequest = httpRequest; this.httpRequest = httpRequest;
} }
public HttpMethod getMethod() { public HttpMethod getMethod() {
return HttpMethod.valueOf(httpRequest.getMethod()); return HttpMethod.valueOf(this.httpRequest.getMethod());
} }
public URI getURI() { public URI getURI() {
return httpRequest.getURI(); return this.httpRequest.getURI();
} }
@Override @Override
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) { for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
@ -69,17 +72,17 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR
if (!headerName.equalsIgnoreCase(HTTP.CONTENT_LEN) && if (!headerName.equalsIgnoreCase(HTTP.CONTENT_LEN) &&
!headerName.equalsIgnoreCase(HTTP.TRANSFER_ENCODING)) { !headerName.equalsIgnoreCase(HTTP.TRANSFER_ENCODING)) {
for (String headerValue : entry.getValue()) { for (String headerValue : entry.getValue()) {
httpRequest.addHeader(headerName, headerValue); this.httpRequest.addHeader(headerName, headerValue);
} }
} }
} }
if (httpRequest instanceof HttpEntityEnclosingRequest) { if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest; HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput); HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
entityEnclosingRequest.setEntity(requestEntity); entityEnclosingRequest.setEntity(requestEntity);
} }
HttpResponse httpResponse = httpClient.execute(httpRequest); HttpResponse httpResponse = this.httpClient.execute(this.httpRequest);
return new HttpComponentsClientHttpResponse(httpResponse); return new HttpComponentsClientHttpResponse(httpResponse);
} }
} }

View File

@ -29,13 +29,13 @@ import org.springframework.http.HttpStatus;
/** /**
* {@link org.springframework.http.client.ClientHttpResponse} implementation that uses * {@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}. * <p>Created via the {@link HttpComponentsClientHttpRequest}.
* *
* @author Oleg Kalnichevski * @author Oleg Kalnichevski
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0 * @since 3.1
* @see HttpComponentsClientHttpRequest#execute() * @see HttpComponentsClientHttpRequest#execute()
*/ */
final class HttpComponentsClientHttpResponse implements ClientHttpResponse { final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
@ -44,35 +44,37 @@ final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
private HttpHeaders headers; private HttpHeaders headers;
public HttpComponentsClientHttpResponse(HttpResponse httpResponse) { public HttpComponentsClientHttpResponse(HttpResponse httpResponse) {
this.httpResponse = httpResponse; this.httpResponse = httpResponse;
} }
public HttpStatus getStatusCode() throws IOException { public HttpStatus getStatusCode() throws IOException {
return HttpStatus.valueOf(httpResponse.getStatusLine().getStatusCode()); return HttpStatus.valueOf(this.httpResponse.getStatusLine().getStatusCode());
} }
public String getStatusText() throws IOException { public String getStatusText() throws IOException {
return httpResponse.getStatusLine().getReasonPhrase(); return this.httpResponse.getStatusLine().getReasonPhrase();
} }
public HttpHeaders getHeaders() { public HttpHeaders getHeaders() {
if (headers == null) { if (this.headers == null) {
headers = new HttpHeaders(); this.headers = new HttpHeaders();
for (Header header : httpResponse.getAllHeaders()) { for (Header header : this.httpResponse.getAllHeaders()) {
headers.add(header.getName(), header.getValue()); this.headers.add(header.getName(), header.getValue());
} }
} }
return headers; return this.headers;
} }
public InputStream getBody() throws IOException { public InputStream getBody() throws IOException {
HttpEntity entity = httpResponse.getEntity(); HttpEntity entity = this.httpResponse.getEntity();
return entity != null ? entity.getContent() : null; return entity != null ? entity.getContent() : null;
} }
public void close() { public void close() {
HttpEntity entity = httpResponse.getEntity(); HttpEntity entity = this.httpResponse.getEntity();
if (entity != null) { if (entity != null) {
try { try {
// Release underlying connection back to the connection manager // Release underlying connection back to the connection manager
@ -83,4 +85,5 @@ final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
} }
} }
} }
} }