Author: mgrigorov
Date: Wed Oct 26 07:20:55 2011
New Revision: 1189051
URL: http://svn.apache.org/viewvc?rev=1189051&view=rev
Log:
WICKET-4171 Add additional constructor to DownloadLink that accepts the
filename in a Model
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/link/DownloadLink.java
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/link/DownloadLink.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/link/DownloadLink.java?rev=1189051&r1=1189050&r2=1189051&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/link/DownloadLink.java
(original)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/link/DownloadLink.java
Wed Oct 26 07:20:55 2011
@@ -25,6 +25,7 @@ import org.apache.wicket.request.UrlEnco
import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler;
import org.apache.wicket.request.resource.ContentDisposition;
import org.apache.wicket.util.file.Files;
+import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.resource.FileResourceStream;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.string.Strings;
@@ -33,25 +34,26 @@ import org.apache.wicket.util.string.Str
* A link that streams a file to the client. When clicked this link will
prompt the save as dialog
* in the browser.
*
- * NOTICE that this link will block the pagemap. That means only one link from
the pagemap can be
- * downloaded at a time, and also while the download happens no pages from
this pagemap can be
- * accessed. If you need to stream multiple files concurrently without
blocking then you should use
+ * NOTICE that this link will lock the page. That means only one link from the
page can be
+ * downloaded at a time, and also while the download happens the page cannot
be accessed by other
+ * threads. If you need to stream multiple files concurrently without blocking
then you should use
* shared resources or a non-wicket servlet.
*
* @author Igor Vaynberg (ivaynberg)
*/
public class DownloadLink extends Link<File>
{
- /**
- *
- */
private static final long serialVersionUID = 1L;
/**
- * File name to stream
+ * The file name that will be used in the response headers.<br/>
+ * Optional. If omitted the name of the provided file will be used.
*/
- private String fileName;
+ private IModel<String> fileNameModel;
+ /**
+ * A flag indicating whether the file should be deleted after download.
+ */
private boolean deleteAfter;
@@ -65,12 +67,7 @@ public class DownloadLink extends Link<F
*/
public DownloadLink(String id, File file)
{
- super(id);
- if (file == null)
- {
- throw new IllegalArgumentException("file cannot be
null");
- }
- setDefaultModel(new Model<File>(file));
+ this(id, new Model<File>(Args.notNull(file, "file")));
}
/**
@@ -83,7 +80,7 @@ public class DownloadLink extends Link<F
*/
public DownloadLink(String id, IModel<File> model)
{
- super(id, model);
+ this(id, model, (IModel<String>)null);
}
/**
@@ -98,11 +95,9 @@ public class DownloadLink extends Link<F
*/
public DownloadLink(String id, IModel<File> model, String fileName)
{
- super(id, model);
- this.fileName = fileName;
+ this(id, model, Model.of(fileName));
}
-
/**
* Constructor
*
@@ -115,24 +110,25 @@ public class DownloadLink extends Link<F
*/
public DownloadLink(String id, File file, String fileName)
{
- super(id);
- if (file == null)
- {
- throw new IllegalArgumentException("file cannot be
null");
- }
- if (Strings.isEmpty(fileName))
- {
- throw new IllegalArgumentException("fileName cannot be
an empty string");
- }
- setDefaultModel(new Model<File>(file));
- this.fileName = fileName;
+ this(id, Model.of(Args.notNull(file, "file")),
Model.of(fileName));
}
-
/**
+ * Constructor. File name used will be the result of
<code>file.getName()</code>
*
- * @see org.apache.wicket.markup.html.link.Link#onClick()
+ * @param id
+ * component id
+ * @param fileModel
+ * model that contains the file object
+ * @param fileNameModel
+ * model that provides the file name to use in the response
headers
*/
+ public DownloadLink(String id, IModel<File> fileModel, IModel<String>
fileNameModel)
+ {
+ super(id, fileModel);
+ this.fileNameModel = fileNameModel;
+ }
+
@Override
public void onClick()
{
@@ -142,9 +138,14 @@ public class DownloadLink extends Link<F
throw new IllegalStateException(getClass().getName() +
" failed to retrieve a File object from model");
}
- final String fn = UrlEncoder.QUERY_INSTANCE.encode(
- (fileName != null) ? fileName : file.getName(),
getRequest().getCharset());
+ String fileName = fileNameModel != null ?
fileNameModel.getObject() : null;
+ if (Strings.isEmpty(fileName))
+ {
+ fileName = file.getName();
+ }
+
+ fileName = UrlEncoder.QUERY_INSTANCE.encode(fileName,
getRequest().getCharset());
IResourceStream resourceStream = new FileResourceStream(
new org.apache.wicket.util.file.File(file));
@@ -161,7 +162,7 @@ public class DownloadLink extends Link<F
Files.remove(file);
}
}
-
}.setFileName(fn).setContentDisposition(ContentDisposition.ATTACHMENT));
+
}.setFileName(fileName).setContentDisposition(ContentDisposition.ATTACHMENT));
}
/**
@@ -173,7 +174,7 @@ public class DownloadLink extends Link<F
*
* @param deleteAfter
* true to delete file after download succeeds
- * @return component
+ * @return this component
*/
public final DownloadLink setDeleteAfterDownload(boolean deleteAfter)
{