org.hippoecm.hst.servlet.utils
Class ContentDispositionUtils

java.lang.Object
  extended by org.hippoecm.hst.servlet.utils.ContentDispositionUtils

public final class ContentDispositionUtils
extends Object

Utility class for setting content disposition headers on responses.


Field Summary
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
           
 
Method Summary
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

USER_AGENT_AGNOSTIC_CONTENT_DISPOSITION_FILENAME_ENCODING

public static final 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)

See Also:
Constant Field Values

USER_AGENT_SPECIFIC_CONTENT_DISPOSITION_FILENAME_ENCODING

public static final String USER_AGENT_SPECIFIC_CONTENT_DISPOSITION_FILENAME_ENCODING
See Also:
Constant Field Values
Method Detail

isContentDispositionType

public static boolean isContentDispositionType(String mimeType,
                                               Set<String> contentDispositionContentTypes)
Test if a mime type matches any of the content disposition types.

The content disposition types can have a '*' as wildcards.

Parameters:
mimeType - the mime type to test
contentDispositionContentTypes - content disposition types.
Returns:
true is the mime type matches any of the content disposition types.

addContentDispositionHeader

public 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. 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.

Parameters:
request - HTTP request
response - HTTP response to set header in
responseContentType - content type of the binary file
binaryFileNode - the node representing the binary file that is streamed to the client
Throws:
javax.jcr.RepositoryException - when something goes wrong during repository access

encodeFileName

public 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>
 

Parameters:
request -
response -
fileName - the un-encoded filename
Returns:


Copyright © 2008-2012 Hippo. All Rights Reserved.