Permalink
Browse files

Add Servlets#formatContentDispositionHeader()

  • Loading branch information...
1 parent c62dd9f commit ffb2b32e733ec01aa4f3813005db5bb484f88f32 @BalusC BalusC committed Dec 15, 2016
@@ -12,8 +12,8 @@
*/
package org.omnifaces.servlet;
+import static org.omnifaces.util.Servlets.formatContentDispositionHeader;
import static org.omnifaces.util.Utils.coalesce;
-import static org.omnifaces.util.Utils.encodeURI;
import static org.omnifaces.util.Utils.encodeURL;
import static org.omnifaces.util.Utils.startsWithOneOf;
import static org.omnifaces.util.Utils.stream;
@@ -121,7 +121,6 @@
private static final long ONE_SECOND_IN_MILLIS = TimeUnit.SECONDS.toMillis(1);
private static final String ETAG = "W/\"%s-%s\"";
private static final Pattern RANGE_PATTERN = Pattern.compile("^bytes=[0-9]*-[0-9]*(,[0-9]*-[0-9]*)*$");
- private static final String CONTENT_DISPOSITION_HEADER = "%s;filename=\"%2$s\"; filename*=UTF-8''%2$s";
private static final String MULTIPART_BOUNDARY = UUID.randomUUID().toString();
// Actions --------------------------------------------------------------------------------------------------------
@@ -368,9 +367,9 @@ else if (end == -1 || end > length - 1) {
*/
private String setContentHeaders(HttpServletRequest request, HttpServletResponse response, Resource resource, List<Range> ranges) {
String contentType = getContentType(request, resource.file);
- String disposition = isAttachment(request, contentType) ? "attachment" : "inline";
- String filename = encodeURI(getAttachmentName(request, resource.file));
- response.setHeader("Content-Disposition", String.format(CONTENT_DISPOSITION_HEADER, disposition, filename));
+ String filename = getAttachmentName(request, resource.file);
+ boolean attachment = isAttachment(request, contentType);
+ response.setHeader("Content-Disposition", formatContentDispositionHeader(filename, attachment));
response.setHeader("Accept-Ranges", "bytes");
if (ranges.size() == 1) {
@@ -17,10 +17,10 @@
import static javax.servlet.http.HttpServletResponse.SC_MOVED_PERMANENTLY;
import static org.omnifaces.util.Reflection.instance;
import static org.omnifaces.util.Reflection.toClassOrNull;
+import static org.omnifaces.util.Servlets.formatContentDispositionHeader;
import static org.omnifaces.util.Servlets.prepareRedirectURL;
import static org.omnifaces.util.Servlets.toQueryString;
import static org.omnifaces.util.Utils.coalesce;
-import static org.omnifaces.util.Utils.encodeURI;
import static org.omnifaces.util.Utils.encodeURL;
import static org.omnifaces.util.Utils.isAnyEmpty;
import static org.omnifaces.util.Utils.isEmpty;
@@ -130,7 +130,6 @@
private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
private static final int DEFAULT_SENDFILE_BUFFER_SIZE = 10240;
- private static final String SENDFILE_HEADER = "%s;filename=\"%2$s\"; filename*=UTF-8''%2$s";
private static final String ERROR_NO_VIEW = "There is no view.";
private static final String[] FACELET_CONTEXT_KEYS = {
FaceletContext.FACELET_CONTEXT_KEY, // Compiletime constant, may fail when compiled against EE6 and run on EE7.
@@ -1670,8 +1669,7 @@ public static void sendFile(FacesContext context, String filename, boolean attac
// Prepare the response and set the necessary headers.
externalContext.setResponseBufferSize(DEFAULT_SENDFILE_BUFFER_SIZE);
externalContext.setResponseContentType(getMimeType(context, filename));
- externalContext.setResponseHeader("Content-Disposition", String.format(SENDFILE_HEADER,
- (attachment ? "attachment" : "inline"), encodeURI(filename)));
+ externalContext.setResponseHeader("Content-Disposition", formatContentDispositionHeader(filename, attachment));
// Not exactly mandatory, but this fixes at least a MSIE quirk: http://support.microsoft.com/kb/316431
if (((HttpServletRequest) externalContext.getRequest()).isSecure()) {
@@ -24,6 +24,7 @@
import static org.omnifaces.util.JNDI.lookup;
import static org.omnifaces.util.Utils.coalesce;
import static org.omnifaces.util.Utils.decodeURL;
+import static org.omnifaces.util.Utils.encodeURI;
import static org.omnifaces.util.Utils.encodeURL;
import static org.omnifaces.util.Utils.isEmpty;
import static org.omnifaces.util.Utils.startsWithOneOf;
@@ -88,6 +89,7 @@
// Constants ------------------------------------------------------------------------------------------------------
+ private static final String CONTENT_DISPOSITION_HEADER = "%s;filename=\"%2$s\"; filename*=UTF-8''%2$s";
private static final Set<String> FACES_AJAX_HEADERS = unmodifiableSet("partial/ajax", "partial/process");
private static final String FACES_AJAX_REDIRECT_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<partial-response><redirect url=\"%s\"></redirect></partial-response>";
@@ -473,6 +475,17 @@ public static void setNoCacheHeaders(HttpServletResponse response) {
response.setHeader("Pragma", "no-cache"); // Backwards compatibility for HTTP 1.0.
}
+ /**
+ * <p>Format an UTF-8 compatible content disposition header for the given filename and whether it's an attachment.
+ * @param filename The filename to appear in "Save As" dialogue.
+ * @param attachment Whether the content should be provided as an attachment or inline.
+ * @return An UTF-8 compatible content disposition header.
+ * @since 2.6
+ */
+ public static String formatContentDispositionHeader(String filename, boolean attachment) {
+ return String.format(CONTENT_DISPOSITION_HEADER, (attachment ? "attachment" : "inline"), encodeURI(filename));
+ }
+
// Cookies --------------------------------------------------------------------------------------------------------
/**

0 comments on commit ffb2b32

Please sign in to comment.