Class AjaxDownloadBehavior

  • All Implemented Interfaces:
    Serializable, org.apache.wicket.IComponentAwareEventSink, org.apache.wicket.IRequestListener, org.apache.wicket.markup.html.IComponentAwareHeaderContributor, org.apache.wicket.util.io.IClusterable

    public class AjaxDownloadBehavior
    extends org.apache.wicket.ajax.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) or AbstractResource.ResourceResponse.setFileName(String)

    Author:
    svenmeier, Martin Grigorov, Maxim Solodovnik
    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from class org.apache.wicket.ajax.AbstractDefaultAjaxBehavior

        INDICATOR
    • Constructor Summary

      Constructors 
      Constructor Description
      AjaxDownloadBehavior​(org.apache.wicket.request.resource.IResource resource)
      Download of a IResource.
      AjaxDownloadBehavior​(org.apache.wicket.request.resource.ResourceReference reference)
      Download of a ResourceReference.
      AjaxDownloadBehavior​(org.apache.wicket.request.resource.ResourceReference reference, org.apache.wicket.request.mapper.parameter.PageParameters resourceParameters)
      Download of a ResourceReference.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      AjaxDownloadBehavior.Location getLocation()  
      org.apache.wicket.util.cookies.CookieDefaults.SameSite getSameSite()  
      void initiate​(org.apache.wicket.core.request.handler.IPartialPageRequestHandler handler)
      Call this method to initiate the download.
      void initiate​(org.apache.wicket.core.request.handler.IPartialPageRequestHandler handler, org.apache.wicket.request.mapper.parameter.PageParameters resourceParameters)
      Call this method to initiate the download.
      static void markCompleted​(org.apache.wicket.request.resource.IResource.Attributes attributes)
      Mark a resource as complete.
      protected void onBeforeDownload​(org.apache.wicket.core.request.handler.IPartialPageRequestHandler handler)  
      protected void onBind()  
      protected void onDownloadCompleted​(org.apache.wicket.ajax.AjaxRequestTarget target)
      A callback executed when the download of the resource finished successfully or with a failure.
      protected void onDownloadFailed​(org.apache.wicket.ajax.AjaxRequestTarget target)
      A callback executed when the download of the resource failed for some reason, e.g.
      protected void onDownloadSuccess​(org.apache.wicket.ajax.AjaxRequestTarget target)
      A callback executed when the download of the resource finished successfully.
      protected void onUnbind()  
      void renderHead​(org.apache.wicket.Component component, org.apache.wicket.markup.head.IHeaderResponse response)  
      protected void respond​(org.apache.wicket.ajax.AjaxRequestTarget target)  
      AjaxDownloadBehavior setLocation​(AjaxDownloadBehavior.Location location)  
      void setSameSite​(org.apache.wicket.util.cookies.CookieDefaults.SameSite sameSite)
      Setter for the same CookieDefaults.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 interface org.apache.wicket.IRequestListener

        rendersPage
    • Constructor Detail

      • AjaxDownloadBehavior

        public AjaxDownloadBehavior​(org.apache.wicket.request.resource.IResource resource)
        Download of a IResource.
        Parameters:
        resource - resource to download
      • AjaxDownloadBehavior

        public AjaxDownloadBehavior​(org.apache.wicket.request.resource.ResourceReference reference,
                                    org.apache.wicket.request.mapper.parameter.PageParameters resourceParameters)
        Download of a ResourceReference.

        The IResource returned by ResourceReference.getResource() must call markCompleted(Attributes) when responding, otherwise the callback onDownloadSuccess(AjaxRequestTarget) will not work.

        Parameters:
        reference - reference to resource to download
        resourceParameters - parameters for the resource
    • Method Detail

      • onBind

        protected void onBind()
        Overrides:
        onBind in class org.apache.wicket.ajax.AbstractDefaultAjaxBehavior
      • onUnbind

        protected void onUnbind()
        Overrides:
        onUnbind in class org.apache.wicket.behavior.AbstractAjaxBehavior
      • initiate

        public void initiate​(org.apache.wicket.core.request.handler.IPartialPageRequestHandler handler,
                             org.apache.wicket.request.mapper.parameter.PageParameters resourceParameters)
        Call this method to initiate the download. You can use the resourceParameters to dynamically pass information to the IResource in order to generate contents.
        Parameters:
        handler - the initiating RequestHandler
        resourceParameters - Some PageParameters that might be used by the resource in order to generate content
      • initiate

        public void initiate​(org.apache.wicket.core.request.handler.IPartialPageRequestHandler handler)
        Call this method to initiate the download.
        Parameters:
        handler - the initiating RequestHandler
      • onBeforeDownload

        protected void onBeforeDownload​(org.apache.wicket.core.request.handler.IPartialPageRequestHandler handler)
      • onDownloadSuccess

        protected void onDownloadSuccess​(org.apache.wicket.ajax.AjaxRequestTarget target)
        A callback executed when the download of the resource finished successfully.
        Parameters:
        target - The Ajax request handler
      • onDownloadFailed

        protected void onDownloadFailed​(org.apache.wicket.ajax.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​(org.apache.wicket.ajax.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​(org.apache.wicket.Component component,
                               org.apache.wicket.markup.head.IHeaderResponse response)
        Specified by:
        renderHead in interface org.apache.wicket.markup.html.IComponentAwareHeaderContributor
        Overrides:
        renderHead in class org.apache.wicket.ajax.AbstractDefaultAjaxBehavior
      • respond

        protected void respond​(org.apache.wicket.ajax.AjaxRequestTarget target)
        Specified by:
        respond in class org.apache.wicket.ajax.AbstractDefaultAjaxBehavior
      • markCompleted

        public static void markCompleted​(org.apache.wicket.request.resource.IResource.Attributes attributes)
        Mark a resource as complete.

        Has to be called from IResource.respond(Attributes) when downloaded via AjaxDownloadBehavior(IResource).

        Parameters:
        attributes - resource attributes
      • getSameSite

        public org.apache.wicket.util.cookies.CookieDefaults.SameSite getSameSite()
        Returns:
        The CookieDefaults.SameSite attribute to be used for the complete download.
      • setSameSite

        public void setSameSite​(org.apache.wicket.util.cookies.CookieDefaults.SameSite sameSite)
        Setter for the same CookieDefaults.SameSite
        Parameters:
        sameSite - The non-null sameSite attribute