Example usage for com.vaadin.server DownloadStream setParameter

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

Introduction

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

Prototype

public void setParameter(String name, String value) 

Source Link

Document

Sets a parameter for download stream.

Usage

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 av a2  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;
}

From source file:fi.semantum.strategia.custom.OnDemandFileDownloader.java

License:Open Source License

public OnDemandFileDownloader(OnDemandStreamSource onDemandStreamSource) {
    super(new StreamResource(onDemandStreamSource, "") {
        private static final long serialVersionUID = -7386418918429322891L;

        // Inject the content length
        @Override//from ww  w. jav a  2  s . c  o  m
        public DownloadStream getStream() {
            DownloadStream ds = super.getStream();
            long size = ((OnDemandStreamSource) getStreamSource()).getFileSize();
            if (size > 0) {
                ds.setParameter("Content-Length", String.valueOf(size));
            }
            return ds;
        }
    });
    this.onDemandStreamSource = onDemandStreamSource;
}

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();/*from  www  .  j ava  2 s  . co  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:info.magnolia.ui.framework.util.TempFileStreamResource.java

License:Open Source License

@Override
public DownloadStream getStream() {
    final DownloadStream stream = super.getStream();
    if (!StringUtils.isBlank(getFilename())) {
        stream.setParameter("Content-Disposition", "attachment;filename=\"" + getFilename() + "\"");
    }//  w w w . jav  a  2 s  .c o m
    return stream;
}

From source file:nz.co.senanque.workflowui.AttachmentsPopup.java

License:Apache License

private void makeStream(final Attachment attachment) {
    final FileResource stream = new FileResource(new File("")) {
        private static final long serialVersionUID = 1L;

        @Override//  w ww . j  a v  a 2s  . com
        public DownloadStream getStream() {
            ByteArrayInputStream in = new ByteArrayInputStream(attachment.getBody());
            DownloadStream ds = new DownloadStream(in, attachment.getMIMEType(), attachment.getFileName());
            // Need a file download POPUP
            ds.setParameter("Content-Disposition", "attachment; filename=" + attachment.getFileName());
            return ds;
        }
    };
    stream.setCacheTime(0);
    Page.getCurrent().open(stream, "_blank", true);
    //        panel.requestRepaint();
}

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

License:Open Source License

@Override
public DownloadStream getStream() {
    try {//from  w w  w  .  j  a  v a2 s . 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.hip.vif.web.util.DownloadFileResouce.java

License:Open Source License

@Override
public DownloadStream getStream() {
    try {/*from   w w  w . j a  v  a2s  .  c  o m*/
        final DownloadStream out = new DownloadStream(new FileInputStream(download.getFile()),
                download.getMIMEType(), download.getFileName());
        out.setCacheTime(0);
        out.setParameter("Content-Disposition", "attachment; filename=\"" + download.getFileName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        LOG.debug("Downloaded file \"{}\"", download.getFileName()); //$NON-NLS-1$
        return out;
    } catch (final FileNotFoundException exc) {
        LOG.error("Couldn't find download file \"{}\"!", download.getFileName(), 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   ww w.  j  a va  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();
        }
    }
}