All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.http.client.reactive.JettyClientHttpRequest Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2002-2023 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.reactive;

import java.net.HttpCookie;
import java.net.URI;
import java.util.Collection;
import java.util.function.Function;

import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.reactive.client.ContentChunk;
import org.eclipse.jetty.reactive.client.ReactiveRequest;
import org.eclipse.jetty.util.Callback;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoSink;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.PooledDataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;

/**
 * {@link ClientHttpRequest} implementation for the Jetty ReactiveStreams HTTP client.
 *
 * @author Sebastien Deleuze
 * @since 5.1
 * @see Jetty ReactiveStreams HttpClient
 */
class JettyClientHttpRequest extends AbstractClientHttpRequest {

	private final Request jettyRequest;

	private final DataBufferFactory bufferFactory;

	private final ReactiveRequest.Builder builder;


	public JettyClientHttpRequest(Request jettyRequest, DataBufferFactory bufferFactory) {
		this.jettyRequest = jettyRequest;
		this.bufferFactory = bufferFactory;
		this.builder = ReactiveRequest.newBuilder(this.jettyRequest).abortOnCancel(true);
	}


	@Override
	public HttpMethod getMethod() {
		return HttpMethod.valueOf(this.jettyRequest.getMethod());
	}

	@Override
	public URI getURI() {
		return this.jettyRequest.getURI();
	}

	@Override
	public Mono setComplete() {
		return doCommit();
	}

	@Override
	public DataBufferFactory bufferFactory() {
		return this.bufferFactory;
	}

	@Override
	@SuppressWarnings("unchecked")
	public  T getNativeRequest() {
		return (T) this.jettyRequest;
	}

	@Override
	public Mono writeWith(Publisher body) {
		return Mono.create(sink -> {
			ReactiveRequest.Content content = Flux.from(body)
					.map(buffer -> toContentChunk(buffer, sink))
					.as(chunks -> ReactiveRequest.Content.fromPublisher(chunks, getContentType()));
			this.builder.content(content);
			sink.success();
		}).then(doCommit());
	}

	@Override
	public Mono writeAndFlushWith(Publisher> body) {
		return writeWith(Flux.from(body)
				.flatMap(Function.identity())
				.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release));
	}

	private String getContentType() {
		MediaType contentType = getHeaders().getContentType();
		return contentType != null ? contentType.toString() : MediaType.APPLICATION_OCTET_STREAM_VALUE;
	}

	private ContentChunk toContentChunk(DataBuffer buffer, MonoSink sink) {
		return new ContentChunk(buffer.asByteBuffer(), new Callback() {
			@Override
			public void succeeded() {
				DataBufferUtils.release(buffer);
			}
			@Override
			public void failed(Throwable t) {
				DataBufferUtils.release(buffer);
				sink.error(t);
			}
		});
	}

	@Override
	protected void applyCookies() {
		getCookies().values().stream().flatMap(Collection::stream)
				.map(cookie -> new HttpCookie(cookie.getName(), cookie.getValue()))
				.forEach(this.jettyRequest::cookie);
	}

	@Override
	protected void applyHeaders() {
		HttpHeaders headers = getHeaders();
		headers.forEach((key, value) -> value.forEach(v -> this.jettyRequest.header(key, v)));
		if (!headers.containsKey(HttpHeaders.ACCEPT)) {
			this.jettyRequest.header(HttpHeaders.ACCEPT, "*/*");
		}
	}

	@Override
	protected HttpHeaders initReadOnlyHeaders() {
		MultiValueMap headers = (Jetty10HttpFieldsHelper.jetty10Present() ?
				Jetty10HttpFieldsHelper.getHttpHeaders(this.jettyRequest) :
				new JettyHeadersAdapter(this.jettyRequest.getHeaders()));

		return HttpHeaders.readOnlyHttpHeaders(headers);
	}

	public ReactiveRequest toReactiveRequest() {
		return this.builder.build();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy