Example usage for com.vaadin.server DownloadStream writeResponse

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

Introduction

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

Prototype

public void writeResponse(VaadinRequest request, VaadinResponse response) throws IOException 

Source Link

Document

Writes this download stream to a Vaadin response.

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.ja v  a  2s.  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  ava  2s .com*/

    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.  ja va 2s .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.FileDownloaderExtension.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  w  w w. j  a v a2  s  . c o  m*/
public boolean handleConnectorRequest(final VaadinRequest inRequest, final VaadinResponse inResponse,
        final String inPath) throws IOException {
    if (!inPath.matches("dl(/.*)?")) {
        // Ignore if it isn't for us
        return false;
    }

    final VaadinSession lSession = getSession();
    lSession.lock();

    DownloadStream lDownloadStream = null;
    try {
        final Collection<GroupWrapper> lSelected = (Collection<GroupWrapper>) groupSelect.getValue();
        Collection<GroupExtent> lGroups = new ArrayList<GroupExtent>();
        for (final GroupWrapper lGroup : lSelected) {
            final Long lGroupID = lGroup.getGroupID();
            if (lGroupID.equals(0l)) {
                lGroups = printTask.getAllGroups();
                break;
            } else {
                lGroups.add(new GroupExtent(lGroup.getGroupID()));
            }
        }

        final DownloadFile lDownloadFile = new DownloadFile(lGroups, VaadinSession.getCurrent().getLocale());
        lDownloadStream = lDownloadFile.getStream();
    } catch (final Exception exc) {
        LOG.error("Error encountered while printing the discussion groups!", exc); //$NON-NLS-1$
    } finally {
        lSession.unlock();
    }
    if (lDownloadStream != null) {
        lDownloadStream.writeResponse(inRequest, inResponse);
    }
    groupSelect.clear();
    return true;
}

From source file:org.lucidj.vaadinui.GlobalResourceHandlerEx.java

License:Apache License

@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
        throws IOException {
    // We simply filter the resources we want to serve directly
    if (path_to_resource.containsKey(request.getPathInfo())) {
        DownloadStream stream = path_to_resource.get(request.getPathInfo()).getStream();

        if (stream != null) {
            stream.writeResponse(request, response);
            return (true);
        }/*w w  w  .  j a va2s  .  c o m*/
        // Fall back if not found here
    }
    return (super.handleRequest(session, request, response));
}

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;
    }//w ww  . ja v  a2s  .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();
        }
    }
}