|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 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.io.InputStream; |
| 21 | +import java.io.OutputStream; |
| 22 | +import java.net.URI; |
| 23 | + |
| 24 | +import org.apache.http.Header; |
| 25 | +import org.apache.http.HttpEntity; |
| 26 | +import org.apache.http.HttpEntityEnclosingRequest; |
| 27 | +import org.apache.http.HttpResponse; |
| 28 | +import org.apache.http.client.HttpClient; |
| 29 | +import org.apache.http.client.methods.HttpUriRequest; |
| 30 | +import org.apache.http.message.BasicHeader; |
| 31 | +import org.apache.http.protocol.HttpContext; |
| 32 | + |
| 33 | +import org.springframework.http.HttpHeaders; |
| 34 | +import org.springframework.http.HttpMethod; |
| 35 | +import org.springframework.http.MediaType; |
| 36 | +import org.springframework.http.StreamingHttpOutputMessage; |
| 37 | + |
| 38 | +/** |
| 39 | + * {@link ClientHttpRequest} implementation that uses Apache HttpComponents HttpClient to |
| 40 | + * execute requests. |
| 41 | + * |
| 42 | + * <p>Created via the {@link org.springframework.http.client.HttpComponentsClientHttpRequestFactory}. |
| 43 | + * |
| 44 | + * @author Arjen Poutsma |
| 45 | + * @see org.springframework.http.client.HttpComponentsClientHttpRequestFactory#createRequest(java.net.URI, |
| 46 | + * org.springframework.http.HttpMethod) |
| 47 | + * @since 4.0 |
| 48 | + */ |
| 49 | +final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpRequest |
| 50 | + implements StreamingHttpOutputMessage { |
| 51 | + |
| 52 | + private final HttpClient httpClient; |
| 53 | + |
| 54 | + private final HttpUriRequest httpRequest; |
| 55 | + |
| 56 | + private final HttpContext httpContext; |
| 57 | + |
| 58 | + private Body body; |
| 59 | + |
| 60 | + public HttpComponentsStreamingClientHttpRequest(HttpClient httpClient, |
| 61 | + HttpUriRequest httpRequest, HttpContext httpContext) { |
| 62 | + this.httpClient = httpClient; |
| 63 | + this.httpRequest = httpRequest; |
| 64 | + this.httpContext = httpContext; |
| 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 | + @Override |
| 78 | + public void setBody(Body body) { |
| 79 | + assertNotExecuted(); |
| 80 | + this.body = body; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException { |
| 85 | + throw new UnsupportedOperationException( |
| 86 | + "getBody not supported when bufferRequestBody is false"); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException { |
| 91 | + HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers); |
| 92 | + |
| 93 | + if (this.httpRequest instanceof HttpEntityEnclosingRequest && body != null) { |
| 94 | + HttpEntityEnclosingRequest entityEnclosingRequest = |
| 95 | + (HttpEntityEnclosingRequest) this.httpRequest; |
| 96 | + |
| 97 | + HttpEntity requestEntity = new StreamingHttpEntity(getHeaders(), body); |
| 98 | + entityEnclosingRequest.setEntity(requestEntity); |
| 99 | + } |
| 100 | + HttpResponse httpResponse = |
| 101 | + this.httpClient.execute(this.httpRequest, this.httpContext); |
| 102 | + return new HttpComponentsClientHttpResponse(httpResponse); |
| 103 | + } |
| 104 | + |
| 105 | + private static class StreamingHttpEntity implements HttpEntity { |
| 106 | + |
| 107 | + private final HttpHeaders headers; |
| 108 | + |
| 109 | + private final StreamingHttpOutputMessage.Body body; |
| 110 | + |
| 111 | + private StreamingHttpEntity(HttpHeaders headers, |
| 112 | + StreamingHttpOutputMessage.Body body) { |
| 113 | + this.headers = headers; |
| 114 | + this.body = body; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean isRepeatable() { |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean isChunked() { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public long getContentLength() { |
| 129 | + return headers.getContentLength(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Header getContentType() { |
| 134 | + MediaType contentType = headers.getContentType(); |
| 135 | + return contentType != null ? |
| 136 | + new BasicHeader("Content-Type", contentType.toString()) : null; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Header getContentEncoding() { |
| 141 | + String contentEncoding = headers.getFirst("Content-Encoding"); |
| 142 | + return contentEncoding != null ? |
| 143 | + new BasicHeader("Content-Encoding", contentEncoding) : null; |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public InputStream getContent() throws IOException, IllegalStateException { |
| 149 | + throw new IllegalStateException(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void writeTo(OutputStream outputStream) throws IOException { |
| 154 | + body.writeTo(outputStream); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public boolean isStreaming() { |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void consumeContent() throws IOException { |
| 164 | + throw new UnsupportedOperationException(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments