Search in sources :

Example 1 with UserAgentType

use of org.codelibs.fess.helper.UserAgentHelper.UserAgentType in project fess by codelibs.

the class ViewHelper method writeFileName.

protected void writeFileName(final StreamResponse response, final ResponseData responseData) {
    final UserAgentHelper userAgentHelper = ComponentUtil.getUserAgentHelper();
    final UserAgentType userAgentType = userAgentHelper.getUserAgentType();
    String charset = responseData.getCharSet();
    if (charset == null) {
        charset = Constants.UTF_8;
    }
    final String name;
    final String url = responseData.getUrl();
    final int pos = url.lastIndexOf('/');
    try {
        if (pos >= 0 && pos + 1 < url.length()) {
            name = URLDecoder.decode(url.substring(pos + 1), charset);
        } else {
            name = URLDecoder.decode(url, charset);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("userAgentType: " + userAgentType + ", charset: " + charset + ", name: " + name);
        }
        switch(userAgentType) {
            case IE:
                response.header("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(name, Constants.UTF_8) + "\"");
                break;
            case OPERA:
                response.header("Content-Disposition", "attachment; filename*=utf-8'ja'" + URLEncoder.encode(name, Constants.UTF_8));
                break;
            case SAFARI:
                response.header("Content-Disposition", "attachment; filename=\"" + name + "\"");
                break;
            case CHROME:
            case FIREFOX:
            case OTHER:
            default:
                response.header("Content-Disposition", "attachment; filename=\"=?utf-8?B?" + Base64Util.encode(name.getBytes(Constants.UTF_8)) + "?=\"");
                break;
        }
    } catch (final Exception e) {
        logger.warn("Failed to write a filename: " + responseData, e);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) FessSystemException(org.codelibs.fess.exception.FessSystemException) ClientAbortException(org.apache.catalina.connector.ClientAbortException) IOException(java.io.IOException) UserAgentType(org.codelibs.fess.helper.UserAgentHelper.UserAgentType)

Example 2 with UserAgentType

use of org.codelibs.fess.helper.UserAgentHelper.UserAgentType in project fess by codelibs.

the class ViewHelper method updateFileProtocol.

protected String updateFileProtocol(String url) {
    final int pos = url.indexOf(':', 5);
    final boolean isLocalFile = pos > 0 && pos < 12;
    final UserAgentType ua = ComponentUtil.getUserAgentHelper().getUserAgentType();
    final DynamicProperties systemProperties = ComponentUtil.getSystemProperties();
    switch(ua) {
        case IE:
            if (isLocalFile) {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.winlocal.ie", "file://"));
            } else {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.ie", "file://"));
            }
            break;
        case FIREFOX:
            if (isLocalFile) {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.winlocal.firefox", "file://"));
            } else {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.firefox", "file://///"));
            }
            break;
        case CHROME:
            if (isLocalFile) {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.winlocal.chrome", "file://"));
            } else {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.chrome", "file://"));
            }
            break;
        case SAFARI:
            if (isLocalFile) {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.winlocal.safari", "file://"));
            } else {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.safari", "file:////"));
            }
            break;
        case OPERA:
            if (isLocalFile) {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.winlocal.opera", "file://"));
            } else {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.opera", "file://"));
            }
            break;
        default:
            if (isLocalFile) {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.winlocal.other", "file://"));
            } else {
                url = url.replaceFirst("file:/+", systemProperties.getProperty("file.protocol.other", "file://"));
            }
            break;
    }
    return url;
}
Also used : DynamicProperties(org.codelibs.core.misc.DynamicProperties) UserAgentType(org.codelibs.fess.helper.UserAgentHelper.UserAgentType)

Aggregations

UserAgentType (org.codelibs.fess.helper.UserAgentHelper.UserAgentType)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ClientAbortException (org.apache.catalina.connector.ClientAbortException)1 DynamicProperties (org.codelibs.core.misc.DynamicProperties)1 FessSystemException (org.codelibs.fess.exception.FessSystemException)1