Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Upgraded to Wicket 1.5 and added anti-cache URL parameter

...

Code Block
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.AbstractAjaxBehavior;
import org.apache.wicket.request.targethandler.resource.ResourceStreamRequestTargetResourceStreamRequestHandler;
import org.apache.wicket.util.resource.IResourceStream;

/**
 * @author Sven Meier
 * @author Ernesto Reinaldo Barreiro (reiern70@gmail.com)
 * @author Jordi Deu-Pons (jordi@jordeu.net)
 */
public abstract class AJAXDownload extends AbstractAjaxBehavior
{
        private boolean addAntiCache;

        public AJAXDownload() {
    	    this(true);
    	}
    	
	public AJAXDownload(boolean addAntiCache) {
	    super();
	    this.addAntiCache = addAntiCache;
	}

	/**
	 * Call this method to initiate the download.
	 */
	public void initiate(AjaxRequestTarget target)
	{
		CharSequenceString url = getCallbackUrl().toString();

                if (addAntiCache) {
		    url = url + (url.contains("?") ? "&" : "?");
		    url = url + "antiCache=" + System.currentTimeMillis();
		}
 
		target.appendJavascriptappendJavaScript("window.location.href='" + url + "'");
	}

	public void onRequest()
	{
		getComponent().getRequestCycle().setRequestTargetscheduleRequestHandlerAfterCurrent(
				new ResourceStreamRequestTarget                new ResourceStreamRequestHandler(getResourceStream(), getFileName()));
	}

	/**
	 * Override this method for a file name which will let the browser prompt with a save/open dialog.
	 * @see ResourceStreamRequestTarget#getFileName()
	 */
	protected String getFileName()
	{
		return null;
	}

	/**
	 * Hook method providing the actual resource stream.
	 */
	protected abstract IResourceStream getResourceStream();
}