Example usage for com.vaadin.server StreamResource getStream

List of usage examples for com.vaadin.server StreamResource getStream

Introduction

In this page you can find the example usage for com.vaadin.server StreamResource getStream.

Prototype

@Override
    public DownloadStream getStream() 

Source Link

Usage

From source file:com.hack23.cia.web.impl.ui.application.views.common.pagelinks.impl.ExternalAttachmentDownloadLink.java

License:Apache License

@Override
public void attach() {
    super.attach();

    final StreamResource.StreamSource source = new StreamResource.StreamSource() {

        /**//from   www  .  j a  v a 2s . c  o  m
         *
         */
        private static final long serialVersionUID = 1L;

        public InputStream getStream() {

            try {
                return new URL(fileUrl).openStream();
            } catch (final IOException e) {
                LOGGER.warn("Problem opening url:" + fileUrl, e);
                return new ByteArrayInputStream(new byte[0]);
            }
        }
    };

    final StreamResource resource = new StreamResource(source, fileName);

    resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
    resource.setMIMEType("application/" + fileType);
    resource.setCacheTime(0);

    setResource(resource);
}

From source file:com.jain.addon.component.download.JDownloader.java

License:Apache License

/**
 * Download actual file//from   w  w w  .  jav  a 2  s. c  om
 */
public void download() {
    StreamResource resource = new StreamResource(source, fileName);
    resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
    resource.setMIMEType("application/octet-stream");
    resource.setCacheTime(0);
    FileDownloader downloader = new FileDownloader(resource);
    downloader.extend(ui);
}

From source file:fr.univlorraine.mondossierweb.controllers.NoteController.java

License:Apache License

/**
 * /* ww w .jav  a 2 s  .  c  o m*/
 * @return le fichier pdf du detail des notes.
 */
public com.vaadin.server.Resource exportPdfDetail(Etape etape) {

    String nomFichier = applicationContext.getMessage("pdf.detail.title", null, Locale.getDefault()) + " "
            + MainUI.getCurrent().getEtudiant().getNom().replace('.', ' ').replace(' ', '_') + ".pdf";
    nomFichier = nomFichier.replaceAll(" ", "_");

    StreamResource.StreamSource source = new StreamResource.StreamSource() {
        private static final long serialVersionUID = 1L;

        @Override
        public InputStream getStream() {
            try {
                ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(OUTPUTSTREAM_SIZE);
                PdfWriter docWriter = null;
                Document document = configureDocument(MARGE_PDF);
                docWriter = PdfWriter.getInstance(document, baosPDF);
                docWriter.setEncryption(null, null, PdfWriter.AllowPrinting, PdfWriter.ENCRYPTION_AES_128);
                docWriter.setStrictImageSequence(true);
                if (configController.isInsertionFiligranePdfNotes()) {
                    docWriter.setPageEvent(new Watermark());
                }
                creerPdfDetail(document, MainUI.getCurrent().getEtudiant(), etape);
                docWriter.close();
                baosPDF.close();
                //Creation de l'export
                byte[] bytes = baosPDF.toByteArray();
                return new ByteArrayInputStream(bytes);
            } catch (DocumentException e) {
                LOG.error("Erreur  la gnration du dtail des notes : DocumentException ", e);
                return null;
            } catch (IOException e) {
                LOG.error("Erreur  la gnration du dtail des notes : IOException ", e);
                return null;
            }

        }
    };

    // Cration de la ressource 
    StreamResource resource = new StreamResource(source, nomFichier);
    resource.getStream().setParameter("Content-Disposition", "attachment; filename=" + nomFichier);
    //resource.setMIMEType("application/unknow");
    resource.setMIMEType("application/force-download;charset=UTF-8");
    resource.setCacheTime(0);
    return resource;
}