Skip to content

Commit

Permalink
Fix primefaces#10251: FileDownload allow enabling of caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Aug 15, 2023
1 parent 358addb commit 3ce645d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/13_0_0/components/filedownload.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ stream the binary data. FileDownload presents an easier way to do the same.
| value | null | StreamedContent | A streamed content instance.
| contentDisposition | attachment | String | Specifies display mode on non-AJAX downloads.
| monitorKey | null | String | Optional key to support monitoring multiple filedownloads on same page.
| cache | false | Boolean | Controls browser caching mode of the resource.

## Getting started with FileDownload
A user command action is required to trigger the file-download process.
Expand Down Expand Up @@ -73,6 +74,10 @@ On regular (non-AJAX) downloads, by default, content is displayed as an `attachm
another alternative is the `inline` mode, in this case browser will try to open the file internally without a prompt.
Note that content disposition is not part of the http standard, although it is widely implemented.

## Cache
Most of the time you want to leave cache="false" which is the default to not cache the results. However, in the case of PDF Downloads there have
been issues with some browser like Chrome/Edge. See: https://github.com/primefaces/primefaces/issues/10251

## Monitor Status
When fileDownload is used without AJAX, ajaxStatus cannot apply. Still PrimeFaces provides a feature
to monitor file downloads via client side `monitorDownload(startFunction, endFunction)` method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@ public class FileDownloadActionListener implements ActionListener, StateHolder {

private ValueExpression monitorKey;

private ValueExpression cache;

public FileDownloadActionListener() {
ResourceUtils.addComponentResource(FacesContext.getCurrentInstance(), "filedownload/filedownload.js");
}

public FileDownloadActionListener(ValueExpression value, ValueExpression contentDisposition, ValueExpression monitorKey) {
public FileDownloadActionListener(ValueExpression value, ValueExpression contentDisposition, ValueExpression monitorKey, ValueExpression cache) {
this();
this.value = value;
this.contentDisposition = contentDisposition;
this.monitorKey = monitorKey;
this.cache = cache;
}

@Override
Expand Down Expand Up @@ -101,7 +104,11 @@ protected void regularDownload(FacesContext context, StreamedContent content) {
? "/"
: externalContext.getRequestContextPath()); // Always add cookies to context root; see #3108
ResourceUtils.addResponseCookie(context, monitorKeyCookieName, "true", cookieOptions);
ResourceUtils.addNoCacheControl(externalContext);

Boolean cache = contentDisposition != null ? (Boolean) this.cache.getValue(context.getELContext()) : Boolean.FALSE;
if (!cache) {
ResourceUtils.addNoCacheControl(externalContext);
}

if (content.getContentLength() != null) {
// we can't use externalContext.setResponseContentLength as our contentLength is a long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public class FileDownloadTagHandler extends TagHandler {
private final TagAttribute value;
private final TagAttribute contentDisposition;
private final TagAttribute monitorKey;
private final TagAttribute cache;

public FileDownloadTagHandler(TagConfig tagConfig) {
super(tagConfig);
value = getRequiredAttribute("value");
contentDisposition = getAttribute("contentDisposition");
monitorKey = getAttribute("monitorKey");
cache = getAttribute("cache");
}

@Override
Expand All @@ -54,15 +56,19 @@ public void apply(FaceletContext faceletContext, UIComponent parent) throws IOEx
ValueExpression valueVE = value.getValueExpression(faceletContext, Object.class);
ValueExpression contentDispositionVE = null;
ValueExpression monitorKeyVE = null;
ValueExpression cacheVE = null;

if (contentDisposition != null) {
contentDispositionVE = contentDisposition.getValueExpression(faceletContext, String.class);
}
if (monitorKey != null) {
monitorKeyVE = monitorKey.getValueExpression(faceletContext, String.class);
}
if (cache != null) {
cacheVE = cache.getValueExpression(faceletContext, Boolean.class);
}

ActionSource actionSource = (ActionSource) parent;
actionSource.addActionListener(new FileDownloadActionListener(valueVE, contentDispositionVE, monitorKeyVE));
actionSource.addActionListener(new FileDownloadActionListener(valueVE, contentDispositionVE, monitorKeyVE, cacheVE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@
<required>false</required>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>
<![CDATA[Controls browser caching mode of the resource. Default is false.]]>
</description>
<name>cache</name>
<required>false</required>
<type>java.lang.Boolean</type>
</attribute>
</tag>

<tag>
Expand Down

0 comments on commit 3ce645d

Please sign in to comment.