spring HttpComponentsStreamingClientHttpRequest 源码
spring HttpComponentsStreamingClientHttpRequest 代码
文件路径:/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java
/*
 * Copyright 2002-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.http.client;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.lang.Nullable;
/**
 * {@link ClientHttpRequest} implementation based on
 * Apache HttpComponents HttpClient in streaming mode.
 *
 * <p>Created via the {@link HttpComponentsClientHttpRequestFactory}.
 *
 * @author Arjen Poutsma
 * @since 4.0
 * @see HttpComponentsClientHttpRequestFactory#createRequest(java.net.URI, org.springframework.http.HttpMethod)
 */
final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpRequest
		implements StreamingHttpOutputMessage {
	private final HttpClient httpClient;
	private final HttpUriRequest httpRequest;
	private final HttpContext httpContext;
	@Nullable
	private Body body;
	HttpComponentsStreamingClientHttpRequest(HttpClient client, HttpUriRequest request, HttpContext context) {
		this.httpClient = client;
		this.httpRequest = request;
		this.httpContext = context;
	}
	@Override
	public HttpMethod getMethod() {
		return HttpMethod.valueOf(this.httpRequest.getMethod());
	}
	@Override
	@Deprecated
	public String getMethodValue() {
		return this.httpRequest.getMethod();
	}
	@Override
	public URI getURI() {
		return this.httpRequest.getURI();
	}
	@Override
	public void setBody(Body body) {
		assertNotExecuted();
		this.body = body;
	}
	@Override
	protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
		throw new UnsupportedOperationException("getBody not supported");
	}
	@Override
	protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
		HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);
		if (this.httpRequest instanceof HttpEntityEnclosingRequest entityEnclosingRequest && this.body != null) {
			HttpEntity requestEntity = new StreamingHttpEntity(getHeaders(), this.body);
			entityEnclosingRequest.setEntity(requestEntity);
		}
		HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
		return new HttpComponentsClientHttpResponse(httpResponse);
	}
	private static class StreamingHttpEntity implements HttpEntity {
		private final HttpHeaders headers;
		private final StreamingHttpOutputMessage.Body body;
		public StreamingHttpEntity(HttpHeaders headers, StreamingHttpOutputMessage.Body body) {
			this.headers = headers;
			this.body = body;
		}
		@Override
		public boolean isRepeatable() {
			return false;
		}
		@Override
		public boolean isChunked() {
			return false;
		}
		@Override
		public long getContentLength() {
			return this.headers.getContentLength();
		}
		@Override
		@Nullable
		public Header getContentType() {
			MediaType contentType = this.headers.getContentType();
			return (contentType != null ? new BasicHeader("Content-Type", contentType.toString()) : null);
		}
		@Override
		@Nullable
		public Header getContentEncoding() {
			String contentEncoding = this.headers.getFirst("Content-Encoding");
			return (contentEncoding != null ? new BasicHeader("Content-Encoding", contentEncoding) : null);
		}
		@Override
		public InputStream getContent() throws IOException, IllegalStateException {
			throw new IllegalStateException("No content available");
		}
		@Override
		public void writeTo(OutputStream outputStream) throws IOException {
			this.body.writeTo(outputStream);
		}
		@Override
		public boolean isStreaming() {
			return true;
		}
		@Override
		@Deprecated
		public void consumeContent() throws IOException {
			throw new UnsupportedOperationException();
		}
	}
}
相关信息
相关文章
spring AbstractBufferingClientHttpRequest 源码
spring AbstractClientHttpRequest 源码
spring AbstractClientHttpRequestFactoryWrapper 源码
spring AbstractClientHttpResponse 源码
spring BufferingClientHttpRequestFactory 源码
spring BufferingClientHttpRequestWrapper 源码
spring BufferingClientHttpResponseWrapper 源码
                        
                            0
                        
                        
                             赞
                        
                    
                    
                热门推荐
- 
                        2、 - 优质文章
- 
                        3、 gate.io
- 
                        8、 openharmony
- 
                        9、 golang