package org.apache.wicket.examples.ajax.builtin;
import java.util.concurrent.TimeUnit;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.core.request.handler.IPartialPageRequestHandler;
import org.apache.wicket.extensions.ajax.AjaxDownloadBehavior;
import org.apache.wicket.extensions.ajax.AjaxDownloadBehavior.Location;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException;
import org.apache.wicket.request.resource.ContentDisposition;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.request.resource.ResourceStreamResource;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.StringResourceStream;
import org.apache.wicket.util.time.Duration;
@author
public class AjaxDownloadPage extends BasePage
{
private static final long serialVersionUID = 1L;
private WebMarkupContainer downloadingContainer;
public AjaxDownloadPage()
{
downloadingContainer = new WebMarkupContainer("downloading");
downloadingContainer.setOutputMarkupPlaceholderTag(true);
downloadingContainer.setVisible(false);
add(downloadingContainer);
initDownload();
initDownloadInIframe();
initDownloadInNewWindow();
initDownloadInSameWindow();
initDownloadReference();
}
@Override
protected void onConfigure()
{
super.onConfigure();
downloadingContainer.setVisible(false);
}
private void initDownload()
{
IResource resource = new ExampleResource("downloaded via ajax")
.setContentDisposition(ContentDisposition.ATTACHMENT);
final AjaxDownloadBehavior download = new AjaxDownloadBehavior(resource) {
private static final long serialVersionUID = 1L;
@Override
protected void onBeforeDownload(IPartialPageRequestHandler handler)
{
downloadingContainer.setVisible(true);
handler.add(downloadingContainer);
}
@Override
protected void onDownloadSuccess(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
}
@Override
protected void onDownloadFailed(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
target.appendJavaScript("alert('Download failed');");
}
};
add(download);
add(new AjaxLink<Void>("download")
{
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target)
{
download.initiate(target);
}
});
}
private void initDownloadInIframe()
{
IResource resource = new ExampleResource("downloaded via ajax in iframe")
.setContentDisposition(ContentDisposition.ATTACHMENT);
final AjaxDownloadBehavior download = new AjaxDownloadBehavior(resource)
{
private static final long serialVersionUID = 1L;
@Override
protected void onBeforeDownload(IPartialPageRequestHandler handler)
{
downloadingContainer.setVisible(true);
handler.add(downloadingContainer);
}
@Override
protected void onDownloadSuccess(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
}
@Override
protected void onDownloadFailed(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
target.appendJavaScript("alert('Download failed');");
}
};
download.setLocation(Location.IFrame);
add(download);
add(new AjaxLink<Void>("downloadIframe")
{
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target)
{
download.initiate(target);
}
});
}
private void initDownloadReference()
{
ResourceReference reference = new ResourceReference("referenceToResource")
{
private static final long serialVersionUID = 1L;
@Override
public IResource getResource()
{
return new StaticResource();
}
};
final AjaxDownloadBehavior download = new AjaxDownloadBehavior(reference)
{
private static final long serialVersionUID = 1L;
@Override
protected void onBeforeDownload(IPartialPageRequestHandler handler)
{
downloadingContainer.setVisible(true);
handler.add(downloadingContainer);
}
@Override
protected void onDownloadSuccess(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
}
@Override
protected void onDownloadFailed(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
target.appendJavaScript("alert('Download failed');");
}
};
add(download);
add(new AjaxLink<Void>("downloadReference")
{
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target)
{
download.initiate(target);
}
});
}
private void initDownloadInNewWindow()
{
IResource resource = new ExampleResource("downloaded via ajax in a new browser window")
.setContentDisposition(ContentDisposition.INLINE);
final AjaxDownloadBehavior download = new AjaxDownloadBehavior(resource)
{
private static final long serialVersionUID = 1L;
@Override
protected void onBeforeDownload(IPartialPageRequestHandler handler)
{
downloadingContainer.setVisible(true);
handler.add(downloadingContainer);
}
@Override
protected void onDownloadSuccess(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
}
@Override
protected void onDownloadFailed(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
target.appendJavaScript("alert('Download failed');");
}
@Override
protected void onDownloadCompleted(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
}
};
download.setLocation(AjaxDownloadBehavior.Location.NewWindow);
add(download);
add(new AjaxLink<Void>("downloadInNewWindow")
{
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target)
{
download.initiate(target);
}
});
}
private void initDownloadInSameWindow()
{
IResource resource = new ExampleResource("downloaded via ajax in same browser window")
.setContentDisposition(ContentDisposition.ATTACHMENT);
final AjaxDownloadBehavior download = new AjaxDownloadBehavior(resource)
{
private static final long serialVersionUID = 1L;
@Override
protected void onBeforeDownload(IPartialPageRequestHandler handler)
{
downloadingContainer.setVisible(true);
handler.add(downloadingContainer);
}
@Override
protected void onDownloadSuccess(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
}
@Override
protected void onDownloadFailed(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
target.appendJavaScript("alert('Download failed');");
}
@Override
protected void onDownloadCompleted(AjaxRequestTarget target)
{
downloadingContainer.setVisible(false);
target.add(downloadingContainer);
}
};
download.setLocation(AjaxDownloadBehavior.Location.SameWindow);
add(download);
add(new AjaxLink<Void>("downloadInSameWindow")
{
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target)
{
download.initiate(target);
}
});
}
public static class StaticResource extends ResourceStreamResource
{
private static final long serialVersionUID = 1L;
StaticResource() {
setFileName("File-from-ResourceReference");
setContentDisposition(ContentDisposition.ATTACHMENT);
setCacheDuration(Duration.NONE);
}
@Override
public void respond(Attributes attributes)
{
AjaxDownloadBehavior.markCompleted(attributes);
super.respond(attributes);
}
@Override
protected IResourceStream getResourceStream(Attributes attributes)
{
try
{
TimeUnit.MILLISECONDS.sleep(5000);
}
catch (InterruptedException e)
{
}
return new StringResourceStream("downloaded via ajax with resource reference");
}
}
private class ExampleResource extends ResourceStreamResource
{
private static final long serialVersionUID = 1L;
private String content;
private int count = 0;
public ExampleResource(String content)
{
this.content = content;
setFileName("File-from-IResource.txt");
setCacheDuration(Duration.NONE);
}
@Override
protected IResourceStream getResourceStream(Attributes attributes) {
try
{
TimeUnit.MILLISECONDS.sleep(3000);
}
catch (InterruptedException e)
{
}
count++;
if (count == 3) {
count = 0;
throw new AbortWithHttpErrorCodeException(400);
}
return new StringResourceStream(content);
};
}
}