Class AjaxDownloadBehavior
- java.lang.Object
-
- org.apache.wicket.behavior.Behavior
-
- org.apache.wicket.behavior.AbstractAjaxBehavior
-
- org.apache.wicket.ajax.AbstractDefaultAjaxBehavior
-
- org.apache.wicket.extensions.ajax.AjaxDownloadBehavior
-
- All Implemented Interfaces:
Serializable
,IComponentAwareEventSink
,IRequestListener
,IComponentAwareHeaderContributor
,IClusterable
public class AjaxDownloadBehavior extends AbstractDefaultAjaxBehavior
Download resources via Ajax.Usage:
final AjaxDownloadBehavior download = new AjaxDownloadBehavior(resource); add(download); add(new AjaxButton("download") { @Override protected void onSubmit(IPartialPageRequestHandler handler, Form<?> form) { download.initiate(handler); } });
To set the name of the downloaded resource make use of
ResourceStreamResource.setFileName(String)
orAbstractResource.ResourceResponse.setFileName(String)
- Author:
- svenmeier, Martin Grigorov, Maxim Solodovnik
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AjaxDownloadBehavior.Location
-
Field Summary
-
Fields inherited from class org.apache.wicket.ajax.AbstractDefaultAjaxBehavior
INDICATOR
-
-
Constructor Summary
Constructors Constructor Description AjaxDownloadBehavior(IResource resource)
Download of aIResource
.AjaxDownloadBehavior(ResourceReference reference)
Download of aResourceReference
.AjaxDownloadBehavior(ResourceReference reference, PageParameters resourceParameters)
Download of aResourceReference
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AjaxDownloadBehavior.Location
getLocation()
CookieDefaults.SameSite
getSameSite()
void
initiate(IPartialPageRequestHandler handler)
Call this method to initiate the download.void
initiate(IPartialPageRequestHandler handler, PageParameters resourceParameters)
Call this method to initiate the download.static void
markCompleted(IResource.Attributes attributes)
Mark a resource as complete.protected void
onBeforeDownload(IPartialPageRequestHandler handler)
protected void
onBind()
Subclasses should call super.onBind()protected void
onDownloadCompleted(AjaxRequestTarget target)
A callback executed when the download of the resource finished successfully or with a failure.protected void
onDownloadFailed(AjaxRequestTarget target)
A callback executed when the download of the resource failed for some reason, e.g.protected void
onDownloadSuccess(AjaxRequestTarget target)
A callback executed when the download of the resource finished successfully.protected void
onUnbind()
Called when the behavior is removed from its component.void
renderHead(Component component, IHeaderResponse response)
Render to the web response whatever the component wants to contribute to the head section.protected void
respond(AjaxRequestTarget target)
AjaxDownloadBehavior
setLocation(AjaxDownloadBehavior.Location location)
void
setSameSite(CookieDefaults.SameSite sameSite)
Setter for the sameCookieDefaults.SameSite
-
Methods inherited from class org.apache.wicket.ajax.AbstractDefaultAjaxBehavior
findIndicatorId, getAttributes, getCallbackFunction, getCallbackFunctionBody, getCallbackScript, getCallbackScript, onMethodMismatch, onRequest, postprocessConfiguration, renderAjaxAttributes, renderAjaxAttributes, updateAjaxAttributes
-
Methods inherited from class org.apache.wicket.behavior.AbstractAjaxBehavior
afterRender, bind, getCallbackUrl, getComponent, onComponentRendered, onComponentTag, onComponentTag, unbind
-
Methods inherited from class org.apache.wicket.behavior.Behavior
beforeRender, canCallListener, detach, getStatelessHint, isEnabled, isTemporary, onAttribute, onConfigure, onEvent, onException, onRemove, onTag
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.wicket.IRequestListener
rendersPage
-
-
-
-
Constructor Detail
-
AjaxDownloadBehavior
public AjaxDownloadBehavior(IResource resource)
Download of aIResource
.- Parameters:
resource
- resource to download
-
AjaxDownloadBehavior
public AjaxDownloadBehavior(ResourceReference reference)
Download of aResourceReference
.The
IResource
returned byResourceReference.getResource()
must callmarkCompleted(Attributes)
when responding, otherwise the callbackonDownloadSuccess(AjaxRequestTarget)
will not work.- Parameters:
reference
- reference to resource to download
-
AjaxDownloadBehavior
public AjaxDownloadBehavior(ResourceReference reference, PageParameters resourceParameters)
Download of aResourceReference
.The
IResource
returned byResourceReference.getResource()
must callmarkCompleted(Attributes)
when responding, otherwise the callbackonDownloadSuccess(AjaxRequestTarget)
will not work.- Parameters:
reference
- reference to resource to downloadresourceParameters
- parameters for the resource
-
-
Method Detail
-
onBind
protected void onBind()
Description copied from class:AbstractDefaultAjaxBehavior
Subclasses should call super.onBind()- Overrides:
onBind
in classAbstractDefaultAjaxBehavior
- See Also:
AbstractAjaxBehavior.onBind()
-
onUnbind
protected void onUnbind()
Description copied from class:AbstractAjaxBehavior
Called when the behavior is removed from its component. The bound host component is still available through AbstractAjaxBehavior.getComponent(). The relation to it will be removed right after the finish of the execution of this method.- Overrides:
onUnbind
in classAbstractAjaxBehavior
-
initiate
public void initiate(IPartialPageRequestHandler handler, PageParameters resourceParameters)
Call this method to initiate the download. You can use theresourceParameters
to dynamically pass information to theIResource
in order to generate contents.- Parameters:
handler
- the initiating RequestHandlerresourceParameters
- Some PageParameters that might be used by the resource in order to generate content
-
initiate
public void initiate(IPartialPageRequestHandler handler)
Call this method to initiate the download.- Parameters:
handler
- the initiating RequestHandler
-
onBeforeDownload
protected void onBeforeDownload(IPartialPageRequestHandler handler)
-
onDownloadSuccess
protected void onDownloadSuccess(AjaxRequestTarget target)
A callback executed when the download of the resource finished successfully.- Parameters:
target
- The Ajax request handler
-
onDownloadFailed
protected void onDownloadFailed(AjaxRequestTarget target)
A callback executed when the download of the resource failed for some reason, e.g. an error at the server side.Since the HTTP status code of the download is not available to Wicket, any HTML in the resource response will be interpreted as a failure HTTP status message. Thus is it not possible to download HTML resources via
AjaxDownloadBehavior
.- Parameters:
target
- The Ajax request handler
-
onDownloadCompleted
protected void onDownloadCompleted(AjaxRequestTarget target)
A callback executed when the download of the resource finished successfully or with a failure.- Parameters:
target
- The Ajax request handler
-
renderHead
public void renderHead(Component component, IHeaderResponse response)
Description copied from class:Behavior
Render to the web response whatever the component wants to contribute to the head section.- Specified by:
renderHead
in interfaceIComponentAwareHeaderContributor
- Overrides:
renderHead
in classAbstractDefaultAjaxBehavior
- Parameters:
component
- component which is contributing to the response. This parameter is here to give the component as the context for component-awares implementing this interfaceresponse
- Response object- See Also:
Behavior.renderHead(Component, org.apache.wicket.markup.head.IHeaderResponse)
-
respond
protected void respond(AjaxRequestTarget target)
- Specified by:
respond
in classAbstractDefaultAjaxBehavior
- Parameters:
target
- The AJAX target
-
getLocation
public AjaxDownloadBehavior.Location getLocation()
-
setLocation
public AjaxDownloadBehavior setLocation(AjaxDownloadBehavior.Location location)
-
markCompleted
public static void markCompleted(IResource.Attributes attributes)
Mark a resource as complete.Has to be called from
IResource.respond(Attributes)
when downloaded viaAjaxDownloadBehavior(IResource)
.- Parameters:
attributes
- resource attributes
-
getSameSite
public CookieDefaults.SameSite getSameSite()
- Returns:
- The
CookieDefaults.SameSite
attribute to be used for the complete download.
-
setSameSite
public void setSameSite(CookieDefaults.SameSite sameSite)
Setter for the sameCookieDefaults.SameSite
- Parameters:
sameSite
- The non-null sameSite attribute
-
-