Class PortletAsyncContextWrapper
- java.lang.Object
-
- jakarta.portlet.filter.PortletAsyncContextWrapper
-
- All Implemented Interfaces:
PortletAsyncContext
public class PortletAsyncContextWrapper extends Object implements PortletAsyncContext
ThePortletAsyncContextWrapperprovides a convenient implementation of thePortletAsyncContextinterface that can be subclassed by developers. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped object.- Since:
- 3.0
-
-
Constructor Summary
Constructors Constructor Description PortletAsyncContextWrapper(PortletAsyncContext wrapped)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(PortletAsyncListener listener)Registers the givenPortletAsyncListenerwith the most recent asynchronous cycle that was started by a call to one of theResourceRequest.startAsync()methods.voidaddListener(PortletAsyncListener listener, ResourceRequest request, ResourceResponse response)Registers the givenPortletAsyncListenerwith the most recent asynchronous cycle that was started by a call to one of theResourceRequest.startAsync()methods.voidcomplete()Completes thePortletAsynchronousoperation and closes the response associated with thisPortletAsyncContextobject.<T extends PortletAsyncListener>
TcreatePortletAsyncListener(Class<T> cls)Instantiates the givenPortletAsyncListenerclass.voiddispatch()Dispatches the request and response objects of thisPortletAsyncContextto the portlet container.voiddispatch(String path)Dispatches the request and response objects of thisPortletAsyncContextto the given path within thePortletContext.ResourceRequestgetResourceRequest()Gets the request that was used to initialize thisPortletAsyncContextby callingResourceRequest.startAsync()orResourceRequest.startAsync(ServletRequest, ServletResponse).ResourceResponsegetResourceResponse()Gets the response that was used to initialize thisPortletAsyncContextby callingResourceRequest.startAsync()orResourceRequest.startAsync(ServletRequest, ServletResponse).longgetTimeout()Gets the timeout (in milliseconds) for this PortletAsyncContext.PortletAsyncContextgetWrapped()booleanhasOriginalRequestAndResponse()Checks if thisPortletAsyncContextwas initialized with the original or application-wrapped request and response objects.voidsetTimeout(long time)Sets the timeout (in milliseconds) for thisPortletAsyncContext.voidsetWrapped(PortletAsyncContext wrapped)voidstart(Runnable run)Causes the container to dispatch a thread, possibly from a managed thread pool, to run the specifiedRunnable.
-
-
-
Constructor Detail
-
PortletAsyncContextWrapper
public PortletAsyncContextWrapper(PortletAsyncContext wrapped)
-
-
Method Detail
-
getWrapped
public PortletAsyncContext getWrapped()
- Returns:
- the wrapped object
-
setWrapped
public void setWrapped(PortletAsyncContext wrapped)
- Parameters:
wrapped- the wrapped object to set. May not be null.
-
addListener
public void addListener(PortletAsyncListener listener) throws IllegalStateException
Description copied from interface:PortletAsyncContextRegisters the givenPortletAsyncListenerwith the most recent asynchronous cycle that was started by a call to one of theResourceRequest.startAsync()methods.The given
PortletAsyncListenerwill receive anPortletAsyncEventwhen the asynchronous cycle completes successfully, times out, results in an error, or a new asynchronous cycle is being initiated via one of theResourceRequest.startAsync()methods.PortletAsyncListenerinstances will be notified in the order in which they were added.When one of the listener methods is invoked, the
PortletAsyncListenergetSuppliedRequest()andgetSuppliedResponse()methods of thePortletAsyncEventobject will returnnull.- Specified by:
addListenerin interfacePortletAsyncContext- Parameters:
listener- the listener to be added- Throws:
IllegalStateException- if this method is called after the container-initiated dispatch, during which one of theResourceRequest.startAsync()methods was called, has returned to the container- See Also:
PortletAsyncListener,PortletAsyncEvent
-
addListener
public void addListener(PortletAsyncListener listener, ResourceRequest request, ResourceResponse response) throws IllegalStateException
Description copied from interface:PortletAsyncContextRegisters the givenPortletAsyncListenerwith the most recent asynchronous cycle that was started by a call to one of theResourceRequest.startAsync()methods.The given
PortletAsyncListenerwill receive anPortletAsyncEventwhen the asynchronous cycle completes successfully, times out, results in an error, or a new asynchronous cycle is being initiated via one of theResourceRequest.startAsync()methods.PortletAsyncListenerinstances will be notified in the order in which they were added.The given
ResourceRequestandResourceResponseobjects will be made available to the givenPortletAsyncListenervia thegetSuppliedRequestandgetSuppliedResponsemethods, respectively, of thePortletAsyncEventdelivered to it. These objects should not be read from or written to at the time thePortletAsyncEventis delivered, because additional wrapping may have occurred since the givenPortletAsyncListenerwas registered. However, they may be used to release any resources associated with them.- Specified by:
addListenerin interfacePortletAsyncContext- Parameters:
listener- the listener to be addedrequest- the request object to be included in thePortletAsyncEventresponse- the response object to be included in thePortletAsyncEvent- Throws:
IllegalStateException- if this method is called after the container-initiated dispatch, during which one of theResourceRequest.startAsync()methods was called, has returned to the container- See Also:
PortletAsyncListener,PortletAsyncEvent
-
complete
public void complete() throws IllegalStateExceptionDescription copied from interface:PortletAsyncContextCompletes thePortletAsynchronousoperation and closes the response associated with thisPortletAsyncContextobject.The
onCompletemethod of any registered listeners that were registered with thePortletAsyncContextobject for this asynchronous cycle will be invoked.It is legal to call this method any time after a call to
ResourceRequest#startAsync()orResourceRequest#startAsync(ResourceRequest, ResourceResponse), and before a call to one of the dispatch methods of this class. If this method is called before the container-initiated dispatch that calledstartAsynchas returned to the container, then the call will not take effect (and any invocations ofPortletAsyncListener#onComplete(PortletAsyncEvent)will be delayed) until after the container-initiated dispatch has returned to the container.- Specified by:
completein interfacePortletAsyncContext- Throws:
IllegalStateException- if this method is called before an asynchronous processing cycle was started, after it has already been completed, or after a call to one of thedispatch()methods
-
createPortletAsyncListener
public <T extends PortletAsyncListener> T createPortletAsyncListener(Class<T> cls) throws PortletException
Description copied from interface:PortletAsyncContextInstantiates the givenPortletAsyncListenerclass.The returned
PortletAsyncListenerinstance may be further customized before it is registered with thisPortletAsyncContextvia a call to one of the addListener methods.The given
PortletAsyncListenerclass must define a zero argument constructor, which is used to instantiate it.This method supports resource injection if contextual support is available, and if the given class represents a Managed Bean. See the Java EE platform and JSR 299 specifications for additional details about Managed Beans and resource injection.
- Specified by:
createPortletAsyncListenerin interfacePortletAsyncContext- Type Parameters:
T- the type of the async listener, which must be a subtype ofPortletAsyncListener- Parameters:
cls- the class to be instantiated- Returns:
- the instantiated class
- Throws:
PortletException- if the given class cannot be instantiated
-
dispatch
public void dispatch() throws IllegalStateExceptionDescription copied from interface:PortletAsyncContextDispatches the request and response objects of thisPortletAsyncContextto the portlet container.The portlet container will dispatch the the request and response to the portlet resource method in which the first asynchronous processing cycle was initiated.
This method returns immediately after passing the request and response objects to a container managed thread which will perform the dispatch operation. If this method is called before the container-initiated dispatch that called startAsync has returned to the container, the dispatch operation will be delayed until after the container-initiated dispatch has returned to the container.
The dispatcher type of the request is set to
DispatcherType.ASYNC. Unlike forward dispatches, the response buffer and headers will not be reset, and it is legal to dispatch even if the response has already been committed.Control over the request and response is delegated to the dispatch target, and the response will be closed when the dispatch target has completed execution, unless
ResourceRequest#startAsync()orResourceRequest#startAsync(ResourceRequest, ResourceResponse)are called.Any errors or exceptions that may occur during the execution of this method must be caught and handled by the container as follows:
-
Invoke the
onErrormethod of allPortletAsyncListenerinstances registered with thisPortletAsyncContextand make the caughtThrowableavailable viaPortletAsyncEvent#getThrowable. -
If none of the listeners called
complete()or any of thedispatch()methods, set the status code equal toHttpResourceResponse.SC_INTERNAL_SERVER_ERROR, make theThrowableleading to the error available as the value of theRequestDispatcher.ERROR_EXCEPTIONrequest attribute, and perform an error dispatch to generate a platform-specific error message. -
Complete the asynchronous processing cycle by calling
complete().
There can be at most one asynchronous dispatch operation per asynchronous cycle, which is started by a call to one of the
ResourceRequest#startAsyncmethods. Any attempt to perform an additional asynchronous dispatch operation within the same asynchronous cycle will result in anIllegalStateException. When the container performs the dispatch operation,startAsync()may be invoked on the dispatched request to initiate a new asynchronous processing cycle, after which any of the dispatch methods or thecomplete()method may be called.- Specified by:
dispatchin interfacePortletAsyncContext- Throws:
IllegalStateException- if called before an asynchronous processing cycle has been started, if one of the dispatch methods has been called but thestartAsyncmethod has not been called during the resulting dispatch, or ifcomplete()was called
-
Invoke the
-
dispatch
public void dispatch(String path) throws IllegalStateException
Description copied from interface:PortletAsyncContextDispatches the request and response objects of thisPortletAsyncContextto the given path within thePortletContext.The path parameter is interpreted in the same way as in
PortletContext#getRequestDispatcher(String)scoped to the current portlet context.There can be at most one asynchronous dispatch operation per asynchronous cycle, which is started by a call to one of the
ResourceRequest#startAsyncmethods. Any attempt to perform an additional asynchronous dispatch operation within the same asynchronous cycle will result in anIllegalStateException. When the container performs the dispatch operation,startAsync()may be invoked on the dispatched request to initiate a new asynchronous processing cycle, after which any of the dispatch methods or thecomplete()method may be called.See
PortletAsyncContext.dispatch()for additional details, including error handling.- Specified by:
dispatchin interfacePortletAsyncContext- Parameters:
path- the path of the dispatch target, scoped to the PortletContext from which this PortletAsyncContext was initialized- Throws:
IllegalStateException- if called before an asynchronous processing cycle has been started, if one of the dispatch methods has been called but thestartAsyncmethod has not been called during the resulting dispatch, or ifcomplete()was called
-
getResourceRequest
public ResourceRequest getResourceRequest() throws IllegalStateException
Description copied from interface:PortletAsyncContextGets the request that was used to initialize thisPortletAsyncContextby callingResourceRequest.startAsync()orResourceRequest.startAsync(ServletRequest, ServletResponse).- Specified by:
getResourceRequestin interfacePortletAsyncContext- Returns:
- The resource request object
- Throws:
IllegalStateException- if one of the dispatch methods has been called but thestartAsyncmethod has not been called during the resulting dispatch, or ifcomplete()was called
-
getResourceResponse
public ResourceResponse getResourceResponse() throws IllegalStateException
Description copied from interface:PortletAsyncContextGets the response that was used to initialize thisPortletAsyncContextby callingResourceRequest.startAsync()orResourceRequest.startAsync(ServletRequest, ServletResponse).- Specified by:
getResourceResponsein interfacePortletAsyncContext- Returns:
- The resource response object
- Throws:
IllegalStateException- if one of the dispatch methods has been called but thestartAsyncmethod has not been called during the resulting dispatch, or ifcomplete()was called
-
getTimeout
public long getTimeout()
Description copied from interface:PortletAsyncContextGets the timeout (in milliseconds) for this PortletAsyncContext.This method returns the container's default timeout for asynchronous operations, or the timeout value passed to the most recent invocation of
setTimeout(long).A timeout value of zero or less indicates no timeout.
- Specified by:
getTimeoutin interfacePortletAsyncContext- Returns:
- the timeout in milliseconds
-
hasOriginalRequestAndResponse
public boolean hasOriginalRequestAndResponse()
Description copied from interface:PortletAsyncContextChecks if thisPortletAsyncContextwas initialized with the original or application-wrapped request and response objects.This information may be used by filters invoked in the outbound direction, after a request was put into asynchronous mode, to determine whether any request and/or response wrappers that they added during their inbound invocation need to be preserved for the duration of the asynchronous operation, or may be released.
- Specified by:
hasOriginalRequestAndResponsein interfacePortletAsyncContext- Returns:
trueif thisPortletAsyncContextwas initialized with the original request and response objects by callingResourceRequest.startAsync(), or if it was initialized by callingResourceRequest.startAsync(ResourceRequest, ResourceResponse), and neither theResourceRequestnorResourceResponsearguments carried any application-provided wrappers;falseotherwise
-
setTimeout
public void setTimeout(long time)
Description copied from interface:PortletAsyncContextSets the timeout (in milliseconds) for thisPortletAsyncContext.The timeout applies to this
PortletAsyncContextonce the container-initiated dispatch during which one of theResourceRequest.startAsync()methods was called has returned to the container.The timeout will expire if neither the
complete()method nor any of the dispatch methods are called. A timeout value of zero or less indicates no timeout.If
setTimeout(long)is not called, then the container's default timeout, which is available via a call togetTimeout(), will apply.The default value is 30000 ms.
- Specified by:
setTimeoutin interfacePortletAsyncContext- Parameters:
time- the timeout in milliseconds
-
start
public void start(Runnable run) throws IllegalStateException
Description copied from interface:PortletAsyncContextCauses the container to dispatch a thread, possibly from a managed thread pool, to run the specifiedRunnable. The container may propagate appropriate contextual information to theRunnable.- Specified by:
startin interfacePortletAsyncContext- Parameters:
run- the asynchronous handler- Throws:
IllegalStateException- if this method is called before an asynchronous processing cycle was started, after it has already been completed, or after a call to one of thedispatch()methods
-
-