Example usage for com.vaadin.server DownloadStream setContentType

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

Introduction

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

Prototype

public void setContentType(String contentType) 

Source Link

Document

Sets 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 ww .j a v  a2 s  .  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;
    }/*ww  w  .  j  ava 2  s .  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 && !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;
}

From source file:fr.univlorraine.mondossierweb.utils.MyFileDownloader.java

License:Apache License

@Override
public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path)
        throws IOException {
    BusyIndicatorWindow busyIndicatorWindow = new BusyIndicatorWindow();
    UI ui = UI.getCurrent();/*  w w  w  .  jav a2 s.c o  m*/
    ui.access(() -> ui.addWindow(busyIndicatorWindow));
    try {
        if (!path.matches("dl(/.*)?")) {
            // Ignore if it isn't for us
            return false;
        }

        Resource resource = getFileDownloadResource();

        if (resource instanceof ConnectorResource) {
            DownloadStream stream = ((ConnectorResource) resource).getStream();

            if (stream == null)
                return false;

            if (stream.getParameter("Content-Disposition") == null) {
                // Content-Disposition: attachment generally forces download
                stream.setParameter("Content-Disposition",
                        "attachment; filename=\"" + stream.getFileName() + "\"");
            }

            //Forcer "Ouvrir avec" par dfaut. Permet de proposer "Ouvrir avec" sous Firefox/MacOS. Indisponible sinon
            stream.setParameter("Content-Type", "application/force-download");

            // Content-Type to block eager browser plug-ins from hijacking the
            // file
            if (isOverrideContentType()) {
                stream.setContentType("application/octet-stream;charset=UTF-8");
            }

            if (fileSize > 0) {
                stream.setParameter("Content-Length", "" + fileSize);
            }

            stream.writeResponse(request, response);
            return true;
        } else {
            return false;
        }
    } finally {
        busyIndicatorWindow.close();
    }
}

From source file:org.hip.vif.admin.admin.print.DownloadFile.java

License:Open Source License

@Override
public DownloadStream getStream() {
    try {/*from  www.j a v  a  2s.  c o  m*/
        final DownloadStream out = new DownloadStream(buffer.getInputStream(), getMIMEType(), getFileName());
        out.setCacheTime(0);
        out.setParameter("Content-Disposition", "attachment; filename=\"" + getFileName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        out.setParameter("Content-Length", String.valueOf(buffer.getSize() - buffer.getSpaceLeft())); //$NON-NLS-1$
        out.setContentType("application/octet-stream;charset=UTF-8");
        LOG.debug("Printed discusion group to file \"{}\".", getFileName()); //$NON-NLS-1$
        return out;
    } catch (final Exception exc) {
        LOG.error("Error encountered while printing the discussion groups!", exc); //$NON-NLS-1$
    }
    return null;
}

From source file:org.vaadin.gridfiledownloader.GridFileDownloader.java

License:Apache License

@Override
public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path)
        throws IOException {

    if (!path.matches("dl(/.*)?")) {
        // Ignore if it isn't for us
        return false;
    }/*from  w w  w .  j  a  v  a 2s.c  o  m*/

    boolean markedProcessed = false;
    try {
        if (!waitForRPC()) {
            handleRPCTimeout();
            return false;
        }
        getResource().setFilename(gridStreamResource.getFilename());

        VaadinSession session = getSession();

        session.lock();
        DownloadStream stream;

        try {
            Resource resource = getFileDownloadResource();
            if (!(resource instanceof ConnectorResource)) {
                return false;
            }
            stream = ((ConnectorResource) resource).getStream();

            if (stream.getParameter("Content-Disposition") == null) {
                // Content-Disposition: attachment generally forces download
                stream.setParameter("Content-Disposition",
                        "attachment; filename=\"" + stream.getFileName() + "\"");
            }

            // Content-Type to block eager browser plug-ins from hijacking
            // the file
            if (isOverrideContentType()) {
                stream.setContentType("application/octet-stream;charset=UTF-8");
            }
        } finally {
            try {
                markProcessed();
                markedProcessed = true;
            } finally {
                session.unlock();
            }
        }
        try {
            stream.writeResponse(request, response);
        } catch (Exception e) {
            handleWriteResponseException(e);
        }
        return true;
    } finally {
        // ensure the download request always gets marked processed
        if (!markedProcessed) {
            markProcessed();
        }
    }
}