Home / Class/ HttpClientTransport Class — spring-boot Architecture

HttpClientTransport Class — spring-boot Architecture

Architecture documentation for the HttpClientTransport class in HttpClientTransport.java from the spring-boot codebase.

Entity Profile

Relationship Graph

Source Code

buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/transport/HttpClientTransport.java lines 58–307

abstract class HttpClientTransport implements HttpTransport {

	static final String REGISTRY_AUTH_HEADER = "X-Registry-Auth";

	private final HttpClient client;

	private final HttpHost host;

	protected HttpClientTransport(HttpClient client, HttpHost host) {
		Assert.notNull(client, "'client' must not be null");
		Assert.notNull(host, "'host' must not be null");
		this.client = client;
		this.host = host;
	}

	/**
	 * Perform an HTTP GET operation.
	 * @param uri the destination URI
	 * @return the operation response
	 */
	@Override
	public Response get(URI uri) {
		return execute(new HttpGet(uri));
	}

	/**
	 * Perform an HTTP POST operation.
	 * @param uri the destination URI
	 * @return the operation response
	 */
	@Override
	public Response post(URI uri) {
		return execute(new HttpPost(uri));
	}

	/**
	 * Perform an HTTP POST operation.
	 * @param uri the destination URI
	 * @param registryAuth registry authentication credentials
	 * @return the operation response
	 */
	@Override
	public Response post(URI uri, @Nullable String registryAuth) {
		return execute(new HttpPost(uri), registryAuth);
	}

	/**
	 * Perform an HTTP POST operation.
	 * @param uri the destination URI
	 * @param contentType the content type to write
	 * @param writer a content writer
	 * @return the operation response
	 */
	@Override
	public Response post(URI uri, String contentType, IOConsumer<OutputStream> writer) {
		return execute(new HttpPost(uri), contentType, writer);
	}

	/**
	 * Perform an HTTP PUT operation.
	 * @param uri the destination URI
	 * @param contentType the content type to write
	 * @param writer a content writer
	 * @return the operation response
	 */
	@Override
	public Response put(URI uri, String contentType, IOConsumer<OutputStream> writer) {
		return execute(new HttpPut(uri), contentType, writer);
	}

	/**
	 * Perform an HTTP DELETE operation.
	 * @param uri the destination URI
	 * @return the operation response
	 */
	@Override
	public Response delete(URI uri) {
		return execute(new HttpDelete(uri));
	}

	/**
	 * Perform an HTTP HEAD operation.
	 * @param uri the destination URI
	 * @return the operation response
	 */
	@Override
	public Response head(URI uri) {
		return execute(new HttpHead(uri));
	}

	private Response execute(HttpUriRequestBase request, String contentType, IOConsumer<OutputStream> writer) {
		request.setEntity(new WritableHttpEntity(contentType, writer));
		return execute(request);
	}

	private Response execute(HttpUriRequestBase request, @Nullable String registryAuth) {
		if (StringUtils.hasText(registryAuth)) {
			request.setHeader(REGISTRY_AUTH_HEADER, registryAuth);
		}
		return execute(request);
	}

	private Response execute(HttpUriRequest request) {
		try {
			beforeExecute(request);
			ClassicHttpResponse response = this.client.executeOpen(this.host, request, null);
			int statusCode = response.getCode();
			if (statusCode >= 400 && statusCode <= 500) {
				byte[] content = readContent(response);
				response.close();
				Errors errors = (statusCode != 500) ? deserializeErrors(content) : null;
				Message message = deserializeMessage(content);
				throw new DockerEngineException(this.host.toHostString(), request.getUri(), statusCode,
						response.getReasonPhrase(), errors, message, content);
			}
			return new HttpClientResponse(response);
		}
		catch (IOException | URISyntaxException ex) {
			throw new DockerConnectionException(this.host.toHostString(), ex);
		}
	}

	protected void beforeExecute(HttpRequest request) {
	}

	private byte @Nullable [] readContent(ClassicHttpResponse response) throws IOException {
		HttpEntity entity = response.getEntity();
		if (entity == null) {
			return null;
		}
		try (InputStream stream = entity.getContent()) {
			return (stream != null) ? stream.readAllBytes() : null;
		}
	}

	private @Nullable Errors deserializeErrors(byte @Nullable [] content) {
		if (content == null) {
			return null;
		}
		try {
			return SharedJsonMapper.get().readValue(content, Errors.class);
		}
		catch (JacksonException ex) {
			return null;
		}
	}

	private @Nullable Message deserializeMessage(byte @Nullable [] content) {
		if (content == null) {
			return null;
		}
		try {
			Message message = SharedJsonMapper.get().readValue(content, Message.class);
			return (message.getMessage() != null) ? message : null;
		}
		catch (JacksonException ex) {
			return null;
		}
	}

	HttpHost getHost() {
		return this.host;
	}

	/**
	 * {@link HttpEntity} to send {@link Content} content.
	 */
	private static class WritableHttpEntity extends AbstractHttpEntity {

		private final IOConsumer<OutputStream> writer;

		WritableHttpEntity(String contentType, IOConsumer<OutputStream> writer) {
			super(contentType, "UTF-8");
			this.writer = writer;
		}

		@Override
		public boolean isRepeatable() {
			return false;
		}

		@Override
		public long getContentLength() {
			if (this.getContentType() != null && this.getContentType().equals("application/json")) {
				return calculateStringContentLength();
			}
			return -1;
		}

		@Override
		public InputStream getContent() throws UnsupportedOperationException {
			throw new UnsupportedOperationException();
		}

		@Override
		public void writeTo(OutputStream outputStream) throws IOException {
			this.writer.accept(outputStream);
		}

		@Override
		public boolean isStreaming() {
			return true;
		}

		private int calculateStringContentLength() {
			try {
				ByteArrayOutputStream bytes = new ByteArrayOutputStream();
				this.writer.accept(bytes);
				return bytes.toByteArray().length;
			}
			catch (IOException ex) {
				return -1;
			}
		}

		@Override
		public void close() throws IOException {
		}

	}

	/**
	 * An HTTP operation response.
	 */
	private static class HttpClientResponse implements Response {

		private final ClassicHttpResponse response;

		HttpClientResponse(ClassicHttpResponse response) {
			this.response = response;
		}

		@Override
		public InputStream getContent() throws IOException {
			return this.response.getEntity().getContent();
		}

		@Override
		public Header getHeader(String name) {
			return this.response.getFirstHeader(name);
		}

		@Override
		public void close() throws IOException {
			this.response.close();
		}

	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free