Example usage for com.vaadin.server DownloadStream getContentType

List of usage examples for com.vaadin.server DownloadStream getContentType

Introduction

In this page you can find the example usage for com.vaadin.server DownloadStream getContentType.

Prototype

public String getContentType() 

Source Link

Document

Gets stream content type.

Usage

From source file:com.haulmont.cuba.web.toolkit.ui.CubaFileDownloader.java

License:Apache License

@Override
public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path)
        throws IOException {
    if (path == null) {
        return false;
    }//from   w  w w. j  a  v  a2s  .c o  m

    String targetResourceKey;
    DownloadStream stream;

    VaadinSession session = getSession();
    session.lock();
    try {
        String[] parts = path.split("/", 2);
        targetResourceKey = parts[0];
        if (targetResourceKey.isEmpty()) {
            return false;
        }

        Resource resource = getResource(targetResourceKey);
        if (resource == null) {
            return false;
        }

        boolean isViewDocumentRequest = targetResourceKey.startsWith(VIEW_RESOURCE_PREFIX);

        stream = ((ConnectorResource) resource).getStream();

        String contentDisposition = stream.getParameter(DownloadStream.CONTENT_DISPOSITION);
        if (contentDisposition == null) {
            // Content-Disposition: attachment generally forces download
            contentDisposition = (isViewDocumentRequest ? "inline" : "attachment") + "; "
                    + DownloadStream.getContentDispositionFilename(stream.getFileName());
        }

        stream.setParameter(DownloadStream.CONTENT_DISPOSITION, contentDisposition);

        // Content-Type to block eager browser plug-ins from hijacking the file
        if (isOverrideContentType() && !isViewDocumentRequest) {
            stream.setContentType("application/octet-stream;charset=UTF-8");
        } else {
            if (StringUtils.isNotEmpty(stream.getContentType())) {
                stream.setContentType(stream.getContentType() + ";charset=UTF-8\"");
            } else {
                stream.setContentType(";charset=UTF-8\"");
            }
        }
    } finally {
        session.unlock();
    }

    stream.writeResponse(request, response);
    return true;
}

From source file:com.haulmont.cuba.web.widgets.CubaFileDownloader.java

License:Apache License

@Override
public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path)
        throws IOException {
    if (path == null) {
        return false;
    }//from  w  w  w . j a  va 2  s .c  om

    String targetResourceKey;
    DownloadStream stream;

    VaadinSession session = getSession();
    session.lock();
    try {
        String[] parts = path.split("/", 2);
        targetResourceKey = parts[0];
        if (targetResourceKey.isEmpty()) {
            return false;
        }

        Resource resource = getResource(targetResourceKey);
        if (resource == null) {
            return false;
        }

        boolean isViewDocumentRequest = targetResourceKey.startsWith(VIEW_RESOURCE_PREFIX);

        stream = ((ConnectorResource) resource).getStream();

        String contentDisposition = stream.getParameter(DownloadStream.CONTENT_DISPOSITION);
        if (contentDisposition == null) {
            // Content-Disposition: attachment generally forces download
            contentDisposition = (isViewDocumentRequest ? "inline" : "attachment") + "; "
                    + DownloadStream.getContentDispositionFilename(stream.getFileName());
        }

        stream.setParameter(DownloadStream.CONTENT_DISPOSITION, contentDisposition);

        // Content-Type to block eager browser plug-ins from hijacking the file
        if (isOverrideContentType() && !isViewDocumentRequest && !isSafariOrIOS()) {
            stream.setContentType("application/octet-stream;charset=UTF-8");
        } else {
            if (StringUtils.isNotEmpty(stream.getContentType())) {
                stream.setContentType(stream.getContentType() + ";charset=UTF-8\"");
            } else {
                stream.setContentType(";charset=UTF-8\"");
            }
        }
    } finally {
        session.unlock();
    }

    stream.writeResponse(request, response);
    return true;
}