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

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

There is a newer version: 6.1.6
Show newest version
/*
 * Copyright 2002-2018 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
 *
 *      http://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.util.List;

import org.eclipse.jetty.reactive.client.ReactiveResponse;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * {@link ClientHttpResponse} implementation for the Jetty ReactiveStreams HTTP client.
 *
 * @author Sebastien Deleuze
 * @since 5.1
 * @see Jetty ReactiveStreams HttpClient
 */
class JettyClientHttpResponse implements ClientHttpResponse {

	private final ReactiveResponse reactiveResponse;

	private final Flux content;


	public JettyClientHttpResponse(ReactiveResponse reactiveResponse, Publisher content) {
		this.reactiveResponse = reactiveResponse;
		this.content = Flux.from(content);
	}


	@Override
	public HttpStatus getStatusCode() {
		return HttpStatus.valueOf(getRawStatusCode());
	}

	@Override
	public int getRawStatusCode() {
		return this.reactiveResponse.getStatus();
	}

	@Override
	public MultiValueMap getCookies() {
		MultiValueMap result = new LinkedMultiValueMap<>();
		List cookieHeader = getHeaders().get(HttpHeaders.SET_COOKIE);
		if (cookieHeader != null) {
			cookieHeader.forEach(header -> {
				HttpCookie.parse(header)
						.forEach(cookie -> result.add(cookie.getName(),
								ResponseCookie.from(cookie.getName(), cookie.getValue())
						.domain(cookie.getDomain())
						.path(cookie.getPath())
						.maxAge(cookie.getMaxAge())
						.secure(cookie.getSecure())
						.httpOnly(cookie.isHttpOnly())
						.build()));
			});
		}
		return CollectionUtils.unmodifiableMultiValueMap(result);
	}

	@Override
	public Flux getBody() {
		return this.content;
	}

	@Override
	public HttpHeaders getHeaders() {
		HttpHeaders headers = new HttpHeaders();
		this.reactiveResponse.getHeaders().stream()
				.forEach(field -> headers.add(field.getName(), field.getValue()));
		return headers;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy