public abstract class AbstractServerHttpResponse extends java.lang.Object implements ServerHttpResponse
ServerHttpResponse
implementations.Modifier and Type | Class and Description |
---|---|
private static class |
AbstractServerHttpResponse.State
COMMITTING -> COMMITTED is the period after doCommit is called but before
the response status and headers have been applied to the underlying
response during which time pre-commit actions can still make changes to
the response status and headers.
|
Modifier and Type | Field and Description |
---|---|
private java.util.List<java.util.function.Supplier<? extends <any>>> |
commitActions |
private MultiValueMap<java.lang.String,ResponseCookie> |
cookies |
private DataBufferFactory |
dataBufferFactory |
private HttpHeaders |
headers |
private Log |
logger |
private java.util.concurrent.atomic.AtomicReference<AbstractServerHttpResponse.State> |
state |
private HttpStatus |
statusCode |
private java.util.function.Function<java.lang.String,java.lang.String> |
urlEncoder |
Constructor and Description |
---|
AbstractServerHttpResponse(DataBufferFactory dataBufferFactory) |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
applyCookies()
Implement this method to add cookies from
getHeaders() to the
underlying response. |
protected abstract void |
applyHeaders()
Implement this method to apply header changes from
getHeaders()
to the underlying response. |
protected abstract void |
applyStatusCode()
Implement this method to write the status code to the underlying response.
|
void |
beforeCommit(java.util.function.Supplier<? extends <any>> action)
Register an action to apply just before the HttpOutputMessage is committed.
|
DataBufferFactory |
bufferFactory()
Return a
DataBufferFactory that can be used to create the body. |
protected <any> |
doCommit()
A variant of
doCommit(Supplier) for a response without no body. |
protected <any> |
doCommit(java.util.function.Supplier<? extends <any>> writeAction)
Apply
beforeCommit actions, apply the
response status and headers/cookies, and write the response body. |
java.lang.String |
encodeUrl(java.lang.String url)
A mechanism for URL rewriting that applications and libraries such as
HTML template libraries to use consistently for all URLs emitted by
the application.
|
MultiValueMap<java.lang.String,ResponseCookie> |
getCookies()
Return a mutable map with the cookies to send to the server.
|
HttpHeaders |
getHeaders()
Return the headers of this message.
|
HttpStatus |
getStatusCode()
Return the HTTP status code or
null if not set. |
boolean |
isCommitted()
Whether the HttpOutputMessage is committed.
|
void |
registerUrlEncoder(java.util.function.Function<java.lang.String,java.lang.String> encoder)
Register a URL rewriting function for use with
ServerHttpResponse.encodeUrl(java.lang.String) . |
<any> |
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 via
HttpMessage.getHeaders() to the underlying HTTP message (if not
applied already). |
boolean |
setStatusCode(HttpStatus statusCode)
Set the HTTP status code of the response.
|
<any> |
writeAndFlushWith(<any> body)
Use the given
Publisher of Publishers to write the body
of the HttpOutputMessage to the underlying HTTP layer, flushing after
each Publisher<DataBuffer> . |
protected abstract <any> |
writeAndFlushWithInternal(<any> body)
Implement this method to write to the underlying the response, and flush after
each
Publisher<DataBuffer> . |
<any> |
writeWith(<any> body)
Use the given
Publisher to write the body of the message to the
underlying HTTP layer. |
protected abstract <any> |
writeWithInternal(<any> body)
Implement this method to write to the underlying the response.
|
private final Log logger
private final DataBufferFactory dataBufferFactory
private HttpStatus statusCode
private final HttpHeaders headers
private final MultiValueMap<java.lang.String,ResponseCookie> cookies
private java.util.function.Function<java.lang.String,java.lang.String> urlEncoder
private final java.util.concurrent.atomic.AtomicReference<AbstractServerHttpResponse.State> state
private final java.util.List<java.util.function.Supplier<? extends <any>>> commitActions
public AbstractServerHttpResponse(DataBufferFactory dataBufferFactory)
public final DataBufferFactory bufferFactory()
ReactiveHttpOutputMessage
DataBufferFactory
that can be used to create the body.bufferFactory
in interface ReactiveHttpOutputMessage
#writeWith(Publisher)
public boolean setStatusCode(HttpStatus statusCode)
ServerHttpResponse
setStatusCode
in interface ServerHttpResponse
statusCode
- the HTTP status as an HttpStatus
enum valuefalse
if the status code has not been set because the HTTP response
is already committed, true
if it has been set correctly.public HttpStatus getStatusCode()
ServerHttpResponse
null
if not set.getStatusCode
in interface ServerHttpResponse
public HttpHeaders getHeaders()
HttpMessage
getHeaders
in interface HttpMessage
null
)public MultiValueMap<java.lang.String,ResponseCookie> getCookies()
ServerHttpResponse
getCookies
in interface ServerHttpResponse
public java.lang.String encodeUrl(java.lang.String url)
ServerHttpResponse
ServerHttpResponse.registerUrlEncoder(java.util.function.Function<java.lang.String, java.lang.String>)
that can insert an id for authentication,
a nonce for CSRF protection, a version for a static resource, etc.encodeUrl
in interface ServerHttpResponse
url
- the URL to encodepublic void registerUrlEncoder(java.util.function.Function<java.lang.String,java.lang.String> encoder)
ServerHttpResponse
ServerHttpResponse.encodeUrl(java.lang.String)
.
The function must return an encoded URL or the same URL.registerUrlEncoder
in interface ServerHttpResponse
encoder
- a URL encoding function to usepublic void beforeCommit(java.util.function.Supplier<? extends <any>> action)
ReactiveHttpOutputMessage
beforeCommit
in interface ReactiveHttpOutputMessage
action
- the action to applypublic boolean isCommitted()
ReactiveHttpOutputMessage
isCommitted
in interface ReactiveHttpOutputMessage
public final <any> writeWith(<any> body)
ReactiveHttpOutputMessage
Publisher
to write the body of the message to the
underlying HTTP layer.writeWith
in interface ReactiveHttpOutputMessage
body
- the body content publisherMono
that indicates completion or errorpublic final <any> writeAndFlushWith(<any> body)
ReactiveHttpOutputMessage
Publisher
of Publishers
to write the body
of the HttpOutputMessage to the underlying HTTP layer, flushing after
each Publisher<DataBuffer>
.writeAndFlushWith
in interface ReactiveHttpOutputMessage
body
- the body content publisherMono
that indicates completion or errorpublic <any> setComplete()
ReactiveHttpOutputMessage
HttpMessage.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.
setComplete
in interface ReactiveHttpOutputMessage
Mono
that indicates completion or errorprotected <any> doCommit()
doCommit(Supplier)
for a response without no body.protected <any> doCommit(java.util.function.Supplier<? extends <any>> writeAction)
beforeCommit
actions, apply the
response status and headers/cookies, and write the response body.writeAction
- the action to write the response body or null
protected abstract <any> writeWithInternal(<any> body)
body
- the publisher to write withprotected abstract <any> writeAndFlushWithInternal(<any> body)
Publisher<DataBuffer>
.body
- the publisher to write and flush withprotected abstract void applyStatusCode()
protected abstract void applyHeaders()
getHeaders()
to the underlying response. This method is called once only.protected abstract void applyCookies()
getHeaders()
to the
underlying response. This method is called once only.