public final class ContentDispositionUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
USER_AGENT_AGNOSTIC_CONTENT_DISPOSITION_FILENAME_ENCODING
The default encoding for content disposition fileName is 'user-agent-agnostic', also see
#encodeContentDispositionFileName(HttpServletRequest, HttpServletResponse, String) |
static String |
USER_AGENT_SPECIFIC_CONTENT_DISPOSITION_FILENAME_ENCODING |
Modifier and Type | Method and Description |
---|---|
static void |
addContentDispositionHeader(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
String fileName,
String contentDispositionFileNameEncoding)
Adds a Content-Disposition header to the given
binaryFileNode . |
static String |
encodeFileName(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
String fileName,
String contentDispositionFileNameEncoding)
When the
fileName consists only of US-ASCII chars, we can safely return the fileName as is. |
static boolean |
isContentDispositionType(String mimeType,
Set<String> contentDispositionContentTypes)
Test if a mime type matches any of the content disposition types.
|
public static final String USER_AGENT_AGNOSTIC_CONTENT_DISPOSITION_FILENAME_ENCODING
#encodeContentDispositionFileName(HttpServletRequest, HttpServletResponse, String)
public static final String USER_AGENT_SPECIFIC_CONTENT_DISPOSITION_FILENAME_ENCODING
public static boolean isContentDispositionType(String mimeType, Set<String> contentDispositionContentTypes)
mimeType
- the mime type to testcontentDispositionContentTypes
- content disposition types.public static void addContentDispositionHeader(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String fileName, String contentDispositionFileNameEncoding)
binaryFileNode
. The HTTP header is only set when the
content type matches one of the configured contentDispositionContentTypes
.
When the Content-Disposition header is set, the filename is retrieved from the binaryFileNode
and added to the header value. The property in which the filename is stored should be configured using
contentDispositionFilenameProperty
.request
- HTTP requestresponse
- HTTP response to set header inresponseContentType
- content type of the binary filebinaryFileNode
- the node representing the binary file that is streamed to the clientjavax.jcr.RepositoryException
- when something goes wrong during repository accesspublic static String encodeFileName(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String fileName, String contentDispositionFileNameEncoding)
When the fileName
consists only of US-ASCII chars, we can safely return the fileName
as is. However, when the fileName
does contains non-ascii-chars there is a problem because of different browsers expect different encoding: there is no uniform version that works for
all browsers. So, we are either stuck to user-agent sniffing to return the correct encoding, or try to return a US-ASCII form as best as we can.
The problem with user-agent sniffing is that in general, when you use reverse proxies, you do not want to cache pages per browser type. If one version is demanded for all different user agents, the best we can do is trying to bring the fileName back to its base form, thus, replacing diacritics by their base form. This will work fine for most Latin alphabets.
However, a language like Chinese won't be applicable for this approach. The only way to have it correct in such languages, is to return a different version for different browsers
To be able to serve both usecases, we make it optional how you'd like your encoding strategy to be. The default strategy, is assuming Latin alphabets and try to get the non-diacritical version of a fileName: The default strategy is thus (browser) user-agent-agnostic.
For languages like Chinese, you can if you do want all browser version to display the correct fileName for the Content-Disposition, tell the binaries servlet to do so with the following init-param. Note that in this case, you might have to account for the user-agent in your reverse proxy setup
<init-param> <param-name>contentDispositionFilenameEncoding</param-name> <param-value>user-agent-specific</param-value> </init-param>
request
- response
- fileName
- the un-encoded filenameCopyright © 2008–2016 Hippo B.V. (http://www.onehippo.com). All rights reserved.