Class AbstractServerHttpResponse
- java.lang.Object
- org.springframework.http.server.reactive.AbstractServerHttpResponse
- All Implemented Interfaces:
HttpMessage,ReactiveHttpOutputMessage,ServerHttpResponse
- Direct Known Subclasses:
AbstractListenerServerHttpResponse,MockServerHttpResponse
public abstract class AbstractServerHttpResponse extends Object implements ServerHttpResponse
Base class forServerHttpResponseimplementations.- Since:
- 5.0
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze, Brian Clozel
Constructor Summary
Constructors Constructor Description AbstractServerHttpResponse(DataBufferFactory dataBufferFactory)AbstractServerHttpResponse(DataBufferFactory dataBufferFactory, HttpHeaders headers)
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddCookie(ResponseCookie cookie)Add the givenResponseCookie.protected abstract voidapplyCookies()Add cookies fromgetHeaders()to the underlying response.protected abstract voidapplyHeaders()Invoked when the response is getting committed allowing sub-classes to make apply header values to the underlying response.protected abstract voidapplyStatusCode()Write the status code to the underlying response.voidbeforeCommit(Supplier<? extends reactor.core.publisher.Mono<Void>> action)Register an action to apply just before the HttpOutputMessage is committed.DataBufferFactorybufferFactory()Return aDataBufferFactorythat can be used to create the body.protected reactor.core.publisher.Mono<Void>doCommit()A variant ofdoCommit(Supplier)for a response without no body.protected reactor.core.publisher.Mono<Void>doCommit(Supplier<? extends reactor.core.publisher.Mono<Void>> writeAction)ApplybeforeCommitactions, apply the response status and headers/cookies, and write the response body.MultiValueMap<String,ResponseCookie>getCookies()Return a mutable map with the cookies to send to the server.HttpHeadersgetHeaders()Return the headers of this message.abstract <T> TgetNativeResponse()Return the underlying server response.IntegergetRawStatusCode()Return the status code that has been set, or otherwise fall back on the status of the response from the underlying server.HttpStatusgetStatusCode()Return the status code that has been set, or otherwise fall back on the status of the response from the underlying server.IntegergetStatusCodeValue()Deprecated.as of 5.2.4 in favor ofServerHttpResponse.getRawStatusCode().booleanisCommitted()Whether the HttpOutputMessage is committed.reactor.core.publisher.Mono<Void>setComplete()Indicate that message handling is complete, allowing for any cleanup or end-of-processing tasks to be performed such as applying header changes made viaHttpMessage.getHeaders()to the underlying HTTP message (if not applied already).booleansetRawStatusCode(Integer statusCode)Set the HTTP status code to the given value (potentially non-standard and not resolvable through theHttpStatusenum) as an integer.booleansetStatusCode(HttpStatus status)Set the HTTP status code of the response.voidsetStatusCodeValue(Integer statusCode)Deprecated.as of 5.2.4 in favor ofServerHttpResponse.setRawStatusCode(Integer).reactor.core.publisher.Mono<Void>writeAndFlushWith(org.reactivestreams.Publisher<? extends org.reactivestreams.Publisher<? extends DataBuffer>> body)Use the givenPublisherofPublishersto write the body of the HttpOutputMessage to the underlying HTTP layer, flushing after eachPublisher<DataBuffer>.protected abstract reactor.core.publisher.Mono<Void>writeAndFlushWithInternal(org.reactivestreams.Publisher<? extends org.reactivestreams.Publisher<? extends DataBuffer>> body)Write to the underlying the response, and flush after eachPublisher<DataBuffer>.reactor.core.publisher.Mono<Void>writeWith(org.reactivestreams.Publisher<? extends DataBuffer> body)Use the givenPublisherto write the body of the message to the underlying HTTP layer.protected abstract reactor.core.publisher.Mono<Void>writeWithInternal(org.reactivestreams.Publisher<? extends DataBuffer> body)Write to the underlying the response.
Constructor Detail
AbstractServerHttpResponse
public AbstractServerHttpResponse(DataBufferFactory dataBufferFactory)
AbstractServerHttpResponse
public AbstractServerHttpResponse(DataBufferFactory dataBufferFactory, HttpHeaders headers)
Method Detail
bufferFactory
public final DataBufferFactory bufferFactory()
Description copied from interface:ReactiveHttpOutputMessageReturn aDataBufferFactorythat can be used to create the body.- Specified by:
bufferFactoryin interfaceReactiveHttpOutputMessage- Returns:
- a buffer factory
- See Also:
ReactiveHttpOutputMessage.writeWith(Publisher)
setStatusCode
public boolean setStatusCode(@Nullable HttpStatus status)
Description copied from interface:ServerHttpResponseSet the HTTP status code of the response.- Specified by:
setStatusCodein interfaceServerHttpResponse- Parameters:
status- the HTTP status as anHttpStatusenum value- Returns:
falseif the status code change wasn't processed because the HTTP response is committed,trueif successfully set.
getStatusCode
@Nullable public HttpStatus getStatusCode()
Description copied from interface:ServerHttpResponseReturn the status code that has been set, or otherwise fall back on the status of the response from the underlying server. The return value may benullif the status code value is outside theHttpStatusenum range, or if there is no default value from the underlying server.- Specified by:
getStatusCodein interfaceServerHttpResponse
setRawStatusCode
public boolean setRawStatusCode(@Nullable Integer statusCode)
Description copied from interface:ServerHttpResponseSet the HTTP status code to the given value (potentially non-standard and not resolvable through theHttpStatusenum) as an integer.- Specified by:
setRawStatusCodein interfaceServerHttpResponse- Parameters:
statusCode- the status code value- Returns:
falseif the status code change wasn't processed because the HTTP response is committed,trueif successfully set.
getRawStatusCode
@Nullable public Integer getRawStatusCode()
Description copied from interface:ServerHttpResponseReturn the status code that has been set, or otherwise fall back on the status of the response from the underlying server. The return value may benullif there is no default value from the underlying server.- Specified by:
getRawStatusCodein interfaceServerHttpResponse
setStatusCodeValue
@Deprecated public void setStatusCodeValue(@Nullable Integer statusCode)
Deprecated.as of 5.2.4 in favor ofServerHttpResponse.setRawStatusCode(Integer).Set the HTTP status code of the response.- Parameters:
statusCode- the HTTP status as an integer value- Since:
- 5.0.1
getStatusCodeValue
@Nullable @Deprecated public Integer getStatusCodeValue()
Deprecated.as of 5.2.4 in favor ofServerHttpResponse.getRawStatusCode().Return the HTTP status code of the response.- Returns:
- the HTTP status as an integer value
- Since:
- 5.0.1
getHeaders
public HttpHeaders getHeaders()
Description copied from interface:HttpMessageReturn the headers of this message.- Specified by:
getHeadersin interfaceHttpMessage- Returns:
- a corresponding HttpHeaders object (never
null)
getCookies
public MultiValueMap<String,ResponseCookie> getCookies()
Description copied from interface:ServerHttpResponseReturn a mutable map with the cookies to send to the server.- Specified by:
getCookiesin interfaceServerHttpResponse
addCookie
public void addCookie(ResponseCookie cookie)
Description copied from interface:ServerHttpResponseAdd the givenResponseCookie.- Specified by:
addCookiein interfaceServerHttpResponse- Parameters:
cookie- the cookie to add
getNativeResponse
public abstract <T> T getNativeResponse()
Return the underlying server response.Note: This is exposed mainly for internal framework use such as WebSocket upgrades in the spring-webflux module.
beforeCommit
public void beforeCommit(Supplier<? extends reactor.core.publisher.Mono<Void>> action)
Description copied from interface:ReactiveHttpOutputMessageRegister an action to apply just before the HttpOutputMessage is committed.Note: the supplied action must be properly deferred, e.g. via
Mono.defer(java.util.function.Supplier<? extends reactor.core.publisher.Mono<? extends T>>)orMono.fromRunnable(java.lang.Runnable), to ensure it's executed in the right order, relative to other actions.- Specified by:
beforeCommitin interfaceReactiveHttpOutputMessage- Parameters:
action- the action to apply
isCommitted
public boolean isCommitted()
Description copied from interface:ReactiveHttpOutputMessageWhether the HttpOutputMessage is committed.- Specified by:
isCommittedin interfaceReactiveHttpOutputMessage
writeWith
public final reactor.core.publisher.Mono<Void> writeWith(org.reactivestreams.Publisher<? extends DataBuffer> body)
Description copied from interface:ReactiveHttpOutputMessageUse the givenPublisherto write the body of the message to the underlying HTTP layer.- Specified by:
writeWithin interfaceReactiveHttpOutputMessage- Parameters:
body- the body content publisher- Returns:
- a
Monothat indicates completion or error
writeAndFlushWith
public final reactor.core.publisher.Mono<Void> writeAndFlushWith(org.reactivestreams.Publisher<? extends org.reactivestreams.Publisher<? extends DataBuffer>> body)
Description copied from interface:ReactiveHttpOutputMessageUse the givenPublisherofPublishersto write the body of the HttpOutputMessage to the underlying HTTP layer, flushing after eachPublisher<DataBuffer>.- Specified by:
writeAndFlushWithin interfaceReactiveHttpOutputMessage- Parameters:
body- the body content publisher- Returns:
- a
Monothat indicates completion or error
setComplete
public reactor.core.publisher.Mono<Void> setComplete()
Description copied from interface:ReactiveHttpOutputMessageIndicate that message handling is complete, allowing for any cleanup or end-of-processing tasks to be performed such as applying header changes made viaHttpMessage.getHeaders()to the underlying HTTP message (if not applied already).This method should be automatically invoked at the end of message processing so typically applications should not have to invoke it. If invoked multiple times it should have no side effects.
- Specified by:
setCompletein interfaceReactiveHttpOutputMessage- Returns:
- a
Monothat indicates completion or error
doCommit
protected reactor.core.publisher.Mono<Void> doCommit()
A variant ofdoCommit(Supplier)for a response without no body.- Returns:
- a completion publisher
doCommit
protected reactor.core.publisher.Mono<Void> doCommit(@Nullable Supplier<? extends reactor.core.publisher.Mono<Void>> writeAction)
ApplybeforeCommitactions, apply the response status and headers/cookies, and write the response body.- Parameters:
writeAction- the action to write the response body (may benull)- Returns:
- a completion publisher
writeWithInternal
protected abstract reactor.core.publisher.Mono<Void> writeWithInternal(org.reactivestreams.Publisher<? extends DataBuffer> body)
Write to the underlying the response.- Parameters:
body- the publisher to write with
writeAndFlushWithInternal
protected abstract reactor.core.publisher.Mono<Void> writeAndFlushWithInternal(org.reactivestreams.Publisher<? extends org.reactivestreams.Publisher<? extends DataBuffer>> body)
Write to the underlying the response, and flush after eachPublisher<DataBuffer>.- Parameters:
body- the publisher to write and flush with
applyStatusCode
protected abstract void applyStatusCode()
Write the status code to the underlying response. This method is called once only.
applyHeaders
protected abstract void applyHeaders()
Invoked when the response is getting committed allowing sub-classes to make apply header values to the underlying response.Note that most sub-classes use an
HttpHeadersinstance that wraps an adapter to the native response headers such that changes are propagated to the underlying response on the go. That means this callback is typically not used other than for specialized updates such as setting the contentType or characterEncoding fields in a Servlet response.
applyCookies
protected abstract void applyCookies()
Add cookies fromgetHeaders()to the underlying response. This method is called once only.