This is an automated email from the ASF dual-hosted git repository.
reiern70 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git
The following commit(s) were added to refs/heads/master by this push:
new f23136b [WICKET-6949] add initiate method that receive parameters:
this allows to initiate "dynamic" downloads
f23136b is described below
commit f23136bd6c6825d05b84fda7af72b4cc9773fbb0
Author: reiern70 <[email protected]>
AuthorDate: Wed Jan 26 20:30:31 2022 -0500
[WICKET-6949] add initiate method that receive parameters: this allows to
initiate "dynamic" downloads
---
.../examples/ajax/builtin/AjaxApplication.java | 2 +
.../examples/ajax/builtin/AjaxDownloadPage.html | 14 +++
.../examples/ajax/builtin/AjaxDownloadPage.java | 129 ++++++++++++++++++++-
.../extensions/ajax/AjaxDownloadBehavior.java | 15 +++
4 files changed, 156 insertions(+), 4 deletions(-)
diff --git
a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxApplication.java
b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxApplication.java
index b60617b..cbeed2d 100644
---
a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxApplication.java
+++
b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxApplication.java
@@ -68,6 +68,8 @@ public class AjaxApplication extends WicketExampleApplication
mountPage("world-clock", WorldClockPage.class);
mountPage("upload", FileUploadPage.class);
mountPage("download", AjaxDownloadPage.class);
+
+ mountResource("dynamic-text-file",
AjaxDownloadPage.DynamicTextFileResource.instance);
}
/**
diff --git
a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxDownloadPage.html
b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxDownloadPage.html
index 0ac79df..5e30257 100644
---
a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxDownloadPage.html
+++
b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxDownloadPage.html
@@ -17,6 +17,20 @@
</ul>
<p>
+ <form wicket:id="form">
+ <p>
+ Type something bellow:
+ </p>
+ <p>
+ <textarea wicket:id="text" rows="5"
cols="40"></textarea>
+ </p>
+ <p>
+ <a wicket:id="downloadDynamicContents">then click here
in order to generate and download a file based on the text above</a>.
+ </p>
+ </form>
+</p>
+
+<p>
Alternatively a <a wicket:id="downloadReference">resource reference</a>
can be used too.
</p>
diff --git
a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxDownloadPage.java
b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxDownloadPage.java
index 0b1cccc..211d9a6 100644
---
a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxDownloadPage.java
+++
b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxDownloadPage.java
@@ -20,12 +20,20 @@ import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
+import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
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.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.TextArea;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
import org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.apache.wicket.request.resource.AbstractResource;
import org.apache.wicket.request.resource.ContentDisposition;
+import org.apache.wicket.request.resource.DynamicImageResource;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.request.resource.ResourceStreamResource;
@@ -40,7 +48,9 @@ import org.apache.wicket.util.resource.StringResourceStream;
public class AjaxDownloadPage extends BasePage
{
private static final long serialVersionUID = 1L;
- private WebMarkupContainer downloadingContainer;
+ private final WebMarkupContainer downloadingContainer;
+
+ private IModel<String> text;
/**
* Constructor
@@ -60,6 +70,8 @@ public class AjaxDownloadPage extends BasePage
initDownloadInSameWindow();
+ initDynamicDownload();
+
initDownloadReference();
}
@@ -326,6 +338,62 @@ public class AjaxDownloadPage extends BasePage
});
}
+ private void initDynamicDownload()
+ {
+ final AjaxDownloadBehavior download = new
AjaxDownloadBehavior(DynamicTextFileResource.instance)
+ {
+ 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);
+ }
+ };
+ add(download);
+ download.setLocation(Location.Blob);
+ text = Model.of("");
+ Form<Void> form = new Form<>("form");
+ add(form);
+ final TextArea<String> stringTextArea =new TextArea<>("text",
text);
+ stringTextArea.setOutputMarkupId(true);
+ form.add(stringTextArea);
+ form.add(new AjaxSubmitLink("downloadDynamicContents")
+ {
+ @Override
+ protected void onSubmit(AjaxRequestTarget target)
+ {
+ download.initiate(target,
DynamicTextFileResource.encodeText(text.getObject()));
+ text.setObject("");
+ target.add(stringTextArea);
+ }
+ });
+ }
+
public static class StaticResource extends ResourceStreamResource
{
private static final long serialVersionUID = 1L;
@@ -360,7 +428,7 @@ public class AjaxDownloadPage extends BasePage
}
}
- private class ExampleResource extends ResourceStreamResource
+ private static class ExampleResource extends ResourceStreamResource
{
private static final long serialVersionUID = 1L;
@@ -368,6 +436,12 @@ public class AjaxDownloadPage extends BasePage
private int count = 0;
+ public ExampleResource()
+ {
+ setFileName("Dynamic-File-from-IResource.txt");
+ setCacheDuration(Duration.ZERO);
+ }
+
public ExampleResource(String content)
{
this.content = content;
@@ -393,8 +467,55 @@ public class AjaxDownloadPage extends BasePage
throw new AbortWithHttpErrorCodeException(400);
}
- return new StringResourceStream(content);
- };
+ return new StringResourceStream(getContent(attributes));
+ }
+
+ protected String getContent(Attributes attributes) {
+ return content;
+ }
+
+ }
+
+ public static class DynamicTextFileResource extends ResourceReference {
+
+ static final String FILE_CONTENTS = "fileContents";
+ public static DynamicTextFileResource instance = new
DynamicTextFileResource();
+
+ public DynamicTextFileResource() {
+ super(AjaxDownloadPage.class,
"DynamicTextFileResource");
+ }
+
+ public static PageParameters encodeText(String text) {
+ PageParameters parameters = new PageParameters();
+ parameters.add(FILE_CONTENTS, text);
+ return parameters;
+ }
+
+ @Override
+ public IResource getResource() {
+ return new ExampleResource() {
+ @Override
+ protected String getContent(Attributes
attributes) {
+ String licence = "/*\n" +
+ " * Licensed to the
Apache Software Foundation (ASF) under one or more\n" +
+ " * contributor license
agreements. See the NOTICE file distributed with\n" +
+ " * this work for
additional information regarding copyright ownership.\n" +
+ " * The ASF licenses
this file to You under the Apache License, Version 2.0\n" +
+ " * (the \"License\");
you may not use this file except in compliance with\n" +
+ " * the License. You
may obtain a copy of the License at\n" +
+ " *\n" +
+ " *
http://www.apache.org/licenses/LICENSE-2.0\n" +
+ " *\n" +
+ " * Unless required by
applicable law or agreed to in writing, software\n" +
+ " * distributed under
the License is distributed on an \"AS IS\" BASIS,\n" +
+ " * WITHOUT WARRANTIES
OR CONDITIONS OF ANY KIND, either express or implied.\n" +
+ " * See the License for
the specific language governing permissions and\n" +
+ " * limitations under
the License.\n" +
+ " */ \n\n\n";
+ return licence +
attributes.getParameters().get(FILE_CONTENTS).toString("");
+ }
+ };
+ }
}
}
diff --git
a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxDownloadBehavior.java
b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxDownloadBehavior.java
index 8743253..5557f6b 100644
---
a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxDownloadBehavior.java
+++
b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxDownloadBehavior.java
@@ -197,6 +197,21 @@ public class AjaxDownloadBehavior extends
AbstractDefaultAjaxBehavior
}
/**
+ * Call this method to initiate the download. You can use the {@link
#resourceParameters} to dynamically pass
+ * information to the {@link
org.apache.wicket.request.resource.IResource} in order to generate contents.
+ *
+ * @param handler
+ * the initiating RequestHandler
+ * @param resourceParameters
+ * Some PageParameters that might be used by the resource in
order to generate content
+ */
+ public void initiate(IPartialPageRequestHandler handler, PageParameters
resourceParameters)
+ {
+ this.resourceParameters = resourceParameters;
+ initiate(handler);
+ }
+
+ /**
* Call this method to initiate the download.
*
* @param handler