Class ResponseWriter
- java.lang.Object
-
- org.glassfish.jersey.servlet.internal.ResponseWriter
-
- All Implemented Interfaces:
ContainerResponseWriter
public class ResponseWriter extends java.lang.Object implements ContainerResponseWriter
An internal implementation ofContainerResponseWriter
for Servlet containers. The writer depends on providedAsyncContextDelegate
to support async functionality.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ResponseWriter.NonCloseableOutputStreamWrapper
-
Nested classes/interfaces inherited from interface org.glassfish.jersey.server.spi.ContainerResponseWriter
ContainerResponseWriter.TimeoutHandler
-
-
Field Summary
Fields Modifier and Type Field Description private AsyncContextDelegate
asyncExt
private boolean
configSetStatusOverSendError
Cached value of configuration propertyServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR
.private static java.util.logging.Logger
LOGGER
private JerseyRequestTimeoutHandler
requestTimeoutHandler
private javax.servlet.http.HttpServletResponse
response
private java.util.concurrent.CompletableFuture<ContainerResponse>
responseContext
private boolean
useSetStatusOn404
Cached value of configuration propertyServletProperties.FILTER_FORWARD_ON_404
.
-
Constructor Summary
Constructors Constructor Description ResponseWriter(boolean useSetStatusOn404, boolean configSetStatusOverSendError, javax.servlet.http.HttpServletResponse response, AsyncContextDelegate asyncExt, java.util.concurrent.ScheduledExecutorService timeoutTaskExecutor)
Creates a new instance to write a single Jersey response.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
callSendError()
According to configuration and response processing status it callsHttpServletResponse.sendError(int, String)
over commonHttpServletResponse.setStatus(int)
.void
commit()
Commit the response & close the container response writer.boolean
enableResponseBuffering()
Returntrue
if the entity buffering should be enabled in Jersey.void
failure(java.lang.Throwable error)
Propagate an unhandled error to the I/O container.ContainerResponse
getResponseContext()
int
getResponseStatus()
Provides response status captured whenwriteResponseStatusAndHeaders(long, org.glassfish.jersey.server.ContainerResponse)
has been invoked.boolean
responseContextResolved()
private void
rethrow(java.lang.Throwable error)
Rethrow the original exception as required by JAX-RS, 3.3.4void
setSuspendTimeout(long timeOut, java.util.concurrent.TimeUnit timeUnit)
Set the suspend timeout.boolean
suspend(long timeOut, java.util.concurrent.TimeUnit timeUnit, ContainerResponseWriter.TimeoutHandler timeoutHandler)
Suspend the request/response processing.java.io.OutputStream
writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext)
Write the status and headers of the response and return an output stream for the web application to write the entity of the response.
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
response
private final javax.servlet.http.HttpServletResponse response
-
useSetStatusOn404
private final boolean useSetStatusOn404
Cached value of configuration propertyServletProperties.FILTER_FORWARD_ON_404
.
-
configSetStatusOverSendError
private final boolean configSetStatusOverSendError
Cached value of configuration propertyServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR
. Iftrue
methodHttpServletResponse.setStatus(int)
is used overHttpServletResponse.sendError(int, java.lang.String)
.
-
responseContext
private final java.util.concurrent.CompletableFuture<ContainerResponse> responseContext
-
asyncExt
private final AsyncContextDelegate asyncExt
-
requestTimeoutHandler
private final JerseyRequestTimeoutHandler requestTimeoutHandler
-
-
Constructor Detail
-
ResponseWriter
public ResponseWriter(boolean useSetStatusOn404, boolean configSetStatusOverSendError, javax.servlet.http.HttpServletResponse response, AsyncContextDelegate asyncExt, java.util.concurrent.ScheduledExecutorService timeoutTaskExecutor)
Creates a new instance to write a single Jersey response.- Parameters:
useSetStatusOn404
- true if status should be written explicitly when 404 is returnedconfigSetStatusOverSendError
- iftrue
methodHttpServletResponse.setStatus(int)
is used overHttpServletResponse.sendError(int, java.lang.String)
response
- original HttpResponseRequestasyncExt
- delegate to use for async features implementationtimeoutTaskExecutor
- Jersey runtime executor used for background execution of timeout handling tasks.
-
-
Method Detail
-
suspend
public boolean suspend(long timeOut, java.util.concurrent.TimeUnit timeUnit, ContainerResponseWriter.TimeoutHandler timeoutHandler)
Description copied from interface:ContainerResponseWriter
Suspend the request/response processing. The method returnstrue
to indicate the response writer was suspended successfully. In case the provider has already been suspended earlier, the method returnsfalse
.I/O container must not automatically
commit
the response writer when the processing on the I/O container thread is finished and the thread is released. Instead, the Jersey runtime will make sure to manually close the container response writer instance by explicitly calling theContainerResponseWriter.commit()
orContainerResponseWriter.failure(Throwable)
method at some later point in time.Once suspended, the specified suspend timeout can be further updated using
ContainerResponseWriter.setSuspendTimeout(long, java.util.concurrent.TimeUnit)
method.- Specified by:
suspend
in interfaceContainerResponseWriter
- Parameters:
timeOut
- time-out value. Value less or equal to 0, indicates that the processing is suspended indefinitely.timeUnit
- time-out time unit.timeoutHandler
- time-out handler to process a time-out event if it occurs.- Returns:
true
if the suspend operation completed successfully,false
otherwise.- See Also:
ContainerResponseWriter.setSuspendTimeout(long, TimeUnit)
,ContainerResponseWriter.commit()
-
setSuspendTimeout
public void setSuspendTimeout(long timeOut, java.util.concurrent.TimeUnit timeUnit) throws java.lang.IllegalStateException
Description copied from interface:ContainerResponseWriter
Set the suspend timeout. Once the container response writer is suspended, the suspend timeout value can be further updated by the method.- Specified by:
setSuspendTimeout
in interfaceContainerResponseWriter
- Parameters:
timeOut
- time-out value. Value less or equal to 0, indicates that the processing is suspended indefinitely.timeUnit
- time-out time unit.- Throws:
java.lang.IllegalStateException
- in case the response writer has not been suspended yet.
-
writeResponseStatusAndHeaders
public java.io.OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext) throws ContainerException
Description copied from interface:ContainerResponseWriter
Write the status and headers of the response and return an output stream for the web application to write the entity of the response.If the response content length is declared to be greater or equal to 0, it means that the content length in bytes of the entity to be written is known, otherwise -1. I/O containers may use this value to determine whether the
"Content-Length"
header can be set or utilize chunked transfer encoding.- Specified by:
writeResponseStatusAndHeaders
in interfaceContainerResponseWriter
- Parameters:
contentLength
- greater or equal to 0 if the content length in bytes of the entity to be written is known, otherwise -1. Containers may use this value to determine whether the"Content-Length"
header can be set or utilize chunked transfer encoding.responseContext
- the JAX-RS response to be written. The status and headers are obtained from the response.- Returns:
- the output stream to write the entity (if any).
- Throws:
ContainerException
- if an error occurred when writing out the status and headers or obtaining the output stream.
-
commit
public void commit()
Description copied from interface:ContainerResponseWriter
Commit the response & close the container response writer. Indicates to the I/O container that request has been fully processed and response has been fully written. This signals the I/O container to finish the request/response processing, clean up any state, flush any streams, release resources etc.- Specified by:
commit
in interfaceContainerResponseWriter
- See Also:
ContainerResponseWriter.suspend(long, TimeUnit, TimeoutHandler)
,ContainerResponseWriter.failure(Throwable)
-
callSendError
private void callSendError()
According to configuration and response processing status it callsHttpServletResponse.sendError(int, String)
over commonHttpServletResponse.setStatus(int)
.
-
failure
public void failure(java.lang.Throwable error)
Description copied from interface:ContainerResponseWriter
Propagate an unhandled error to the I/O container. Indicates to the I/O container that the request processing has finished with an error that could not be processed by the Jersey runtime. The I/O container is expected to process the exception in a container-specific way. This method also signals the I/O container to finish the request/response processing, clean up any state, flush any streams, release resources etc.- Specified by:
failure
in interfaceContainerResponseWriter
- Parameters:
error
- unhandled request processing error.- See Also:
ContainerResponseWriter.suspend(long, TimeUnit, TimeoutHandler)
,ContainerResponseWriter.commit()
-
enableResponseBuffering
public boolean enableResponseBuffering()
Description copied from interface:ContainerResponseWriter
Returntrue
if the entity buffering should be enabled in Jersey. If enabled, the outbound entity is buffered by Jersey runtime up to a configured amount of bytes prior to being written to the output stream to determine its size that may be used to set the value of HTTP "Content-Length" header.Containers that provide it's own solution for determining the message payload size may decide to return
false
to prevent Jersey from buffering message entities unnecessarily.- Specified by:
enableResponseBuffering
in interfaceContainerResponseWriter
- Returns:
true
to enable entity buffering to be done by Jersey runtime,false
otherwise.
-
rethrow
private void rethrow(java.lang.Throwable error)
Rethrow the original exception as required by JAX-RS, 3.3.4- Parameters:
error
- throwable to be re-thrown
-
getResponseStatus
public int getResponseStatus()
Provides response status captured whenwriteResponseStatusAndHeaders(long, org.glassfish.jersey.server.ContainerResponse)
has been invoked. The method will block if the write method has not been called yet.- Returns:
- response status
-
responseContextResolved
public boolean responseContextResolved()
-
getResponseContext
public ContainerResponse getResponseContext()
-
-