package org.springframework.http.server.reactive;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
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.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
private enum State {NEW, COMMITTING, COMMIT_ACTION_FAILED, COMMITTED}
private final DataBufferFactory dataBufferFactory;
@Nullable
private Integer statusCode;
private final HttpHeaders ;
private final MultiValueMap<String, ResponseCookie> cookies;
private final AtomicReference<State> state = new AtomicReference<>(State.NEW);
private final List<Supplier<? extends Mono<Void>>> commitActions = new ArrayList<>(4);
@Nullable
private HttpHeaders ;
public AbstractServerHttpResponse(DataBufferFactory dataBufferFactory) {
this(dataBufferFactory, new HttpHeaders());
}
public (DataBufferFactory dataBufferFactory, HttpHeaders headers) {
Assert.notNull(dataBufferFactory, "DataBufferFactory must not be null");
Assert.notNull(headers, "HttpHeaders must not be null");
this.dataBufferFactory = dataBufferFactory;
this.headers = headers;
this.cookies = new LinkedMultiValueMap<>();
}
@Override
public final DataBufferFactory bufferFactory() {
return this.dataBufferFactory;
}
@Override
public boolean setStatusCode(@Nullable HttpStatus status) {
if (this.state.get() == State.COMMITTED) {
return false;
}
else {
this.statusCode = (status != null ? status.value() : null);
return true;
}
}
@Override
@Nullable
public HttpStatus getStatusCode() {
return (this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null);
}
@Override
public boolean setRawStatusCode(@Nullable Integer statusCode) {
if (this.state.get() == State.COMMITTED) {
return false;
}
else {
this.statusCode = statusCode;
return true;
}
}
@Override
@Nullable
public Integer getRawStatusCode() {
return this.statusCode;
}
@Deprecated
public void setStatusCodeValue(@Nullable Integer statusCode) {
if (this.state.get() != State.COMMITTED) {
this.statusCode = statusCode;
}
}
@Nullable
@Deprecated
public Integer getStatusCodeValue() {
return this.statusCode;
}
@Override
public HttpHeaders () {
if (this.readOnlyHeaders != null) {
return this.readOnlyHeaders;
}
else if (this.state.get() == State.COMMITTED) {
this.readOnlyHeaders = HttpHeaders.readOnlyHttpHeaders(this.headers);
return this.readOnlyHeaders;
}
else {
return this.headers;
}
}
@Override
public MultiValueMap<String, ResponseCookie> getCookies() {
return (this.state.get() == State.COMMITTED ?
CollectionUtils.unmodifiableMultiValueMap(this.cookies) : this.cookies);
}
@Override
public void addCookie(ResponseCookie cookie) {
Assert.notNull(cookie, "ResponseCookie must not be null");
if (this.state.get() == State.COMMITTED) {
throw new IllegalStateException("Can't add the cookie " + cookie +
"because the HTTP response has already been committed");
}
else {
getCookies().add(cookie.getName(), cookie);
}
}
public abstract <T> T getNativeResponse();
@Override
public void beforeCommit(Supplier<? extends Mono<Void>> action) {
this.commitActions.add(action);
}
@Override
public boolean isCommitted() {
State state = this.state.get();
return (state != State.NEW && state != State.COMMIT_ACTION_FAILED);
}
@Override
@SuppressWarnings("unchecked")
public final Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (body instanceof Mono) {
return ((Mono<? extends DataBuffer>) body)
.flatMap(buffer -> {
touchDataBuffer(buffer);
AtomicBoolean subscribed = new AtomicBoolean();
return doCommit(
() -> {
try {
return writeWithInternal(Mono.fromCallable(() -> buffer)
.doOnSubscribe(s -> subscribed.set(true))
.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release));
}
catch (Throwable ex) {
return Mono.error(ex);
}
})
.doOnError(ex -> DataBufferUtils.release(buffer))
.doOnCancel(() -> {
if (!subscribed.get()) {
DataBufferUtils.release(buffer);
}
});
})
.doOnError(t -> getHeaders().clearContentHeaders())
.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
}
else {
return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeWithInternal(inner)))
.doOnError(t -> getHeaders().clearContentHeaders());
}
}
@Override
public final Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeAndFlushWithInternal(inner)))
.doOnError(t -> getHeaders().clearContentHeaders());
}
@Override
public Mono<Void> setComplete() {
return !isCommitted() ? doCommit(null) : Mono.empty();
}
protected Mono<Void> doCommit() {
return doCommit(null);
}
protected Mono<Void> doCommit(@Nullable Supplier<? extends Mono<Void>> writeAction) {
Flux<Void> allActions = Flux.empty();
if (this.state.compareAndSet(State.NEW, State.COMMITTING)) {
if (!this.commitActions.isEmpty()) {
allActions = Flux.concat(Flux.fromIterable(this.commitActions).map(Supplier::get))
.doOnError(ex -> {
if (this.state.compareAndSet(State.COMMITTING, State.COMMIT_ACTION_FAILED)) {
getHeaders().clearContentHeaders();
}
});
}
}
else if (this.state.compareAndSet(State.COMMIT_ACTION_FAILED, State.COMMITTING)) {
}
else {
return Mono.empty();
}
allActions = allActions.concatWith(Mono.fromRunnable(() -> {
applyStatusCode();
applyHeaders();
applyCookies();
this.state.set(State.COMMITTED);
}));
if (writeAction != null) {
allActions = allActions.concatWith(writeAction.get());
}
return allActions.then();
}
protected abstract Mono<Void> writeWithInternal(Publisher<? extends DataBuffer> body);
protected abstract Mono<Void> writeAndFlushWithInternal(Publisher<? extends Publisher<? extends DataBuffer>> body);
protected abstract void applyStatusCode();
protected abstract void ();
protected abstract void applyCookies();
protected void touchDataBuffer(DataBuffer buffer) {
}
}