Example usage for com.vaadin.server StreamResource StreamResource

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

Introduction

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

Prototype

public StreamResource(StreamSource streamSource, String filename) 

Source Link

Document

Creates a new stream resource for downloading from stream.

Usage

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

License:Apache License

/**
 * FileDownloader extension that adds a download behaviour to the given
 * column of the Grid. Note that if the order or count of the columns or
 * selection mode change you need to call
 * {@link #recalculateDownloadColumn()} explicitly.
 *
 * @param grid//from   w  ww  . jav  a  2  s  . com
 * @param downloadPropertyId
 * @param gridStreamResource
 */
public GridFileDownloader(Grid grid, Object downloadPropertyId, GridStreamResource gridStreamResource) {
    super(new StreamResource(gridStreamResource, ""));
    assert gridStreamResource != null : "The given on-demand stream resource may never be null!";
    assert grid != null : "The given grid may never be null!";

    this.gridStreamResource = gridStreamResource;
    registerRpc(rpc);
    extend(grid);
    if (downloadPropertyId == null) {
        addDownloadColumn();
    } else {
        setDownloadColumn(downloadPropertyId);
    }
    grid.setCellStyleGenerator(new Grid.CellStyleGenerator() {

        @Override
        public String getStyle(Grid.CellReference cellReference) {
            if (GridFileDownloader.this.downloadPropertyId.equals(cellReference.getPropertyId())) {
                return "gridfiledownloader-downloadcolumn";
            }
            return null;
        }
    });
}

From source file:org.vaadin.presentation.views.NewsView.java

private Resource getRes(TagUser customer) {
    StreamResource.StreamSource ss = new StreamResource.StreamSource() {

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

        @Override
        public InputStream getStream() {
            return new ByteArrayInputStream(customer.getPhoto());
        }
    };

    Resource res = new StreamResource(ss, "image" + UUID.randomUUID().toString());
    return res;
}

From source file:pdf.CoverLetterUtility.java

License:Apache License

public StreamResource getStreamResource() {
    return new StreamResource(new StreamResource.StreamSource() {
        @Override// w  w  w. j av a 2s  . c  o m
        public InputStream getStream() {
            // Here we return the pdf contents as a byte-array
            return new ByteArrayInputStream(baos.toByteArray());
        }
    }, "coverLetter_" + userId + "_" + language + ".pdf");
}

From source file:pdf.PdfUtility.java

public StreamResource getStreamResource() {
    return new StreamResource(new StreamResource.StreamSource() {
        @Override//  w  ww . j  ava  2  s .co m
        public InputStream getStream() {
            // Here we return the pdf contents as a byte-array
            return new ByteArrayInputStream(baos.toByteArray());
        }
    }, filename);
}

From source file:pl.exsio.ca.module.terrain.evidence.FilesDataTable.java

License:Open Source License

protected void addDownloadButtonColumn() {
    dataComponent.addGeneratedColumn("", new Table.ColumnGenerator() {

        @Override/*from  ww w  . j  av  a2s .  c  o m*/
        public Component generateCell(final Table source, final Object itemId, final Object columnId) {

            final Button download = new Button("", FontAwesome.DOWNLOAD);
            final TerrainFile terrainFile = ((EntityItem<TerrainFile>) dataComponent.getItem(itemId))
                    .getEntity();
            Resource res = new StreamResource(new StreamSource() {

                @Override
                public InputStream getStream() {
                    return new ByteArrayInputStream(terrainFile.getData());
                }
            }, terrainFile.getName());
            FileDownloader fd = new FileDownloader(res);
            fd.extend(download);
            download.setImmediate(true);
            download.setStyleName("link");
            return download;
        }
    });
}

From source file:probe.com.view.body.quantdatasetsoverview.diseasegroupsfilters.ComparisonsSelectionOverviewBubbleChart.java

private StreamResource createResource() {
    return new StreamResource(new StreamSource() {
        @Override//  w  w w  .ja  v a  2s .c o m
        @SuppressWarnings("CallToPrintStackTrace")
        public InputStream getStream() {
            try {
                byte[] pdfFile = CSFPR_Handler.exportBubbleChartAsPdf(chart,
                        "bubblechart_comparisons_selection.pdf", "Overview",
                        (int) chartLayoutContainer.getWidth(), (int) chartLayoutContainer.getHeight());
                return new ByteArrayInputStream(pdfFile);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

        }
    }, "bubblechart_comparisons_selection.pdf");
}

From source file:probe.com.view.body.quantdatasetsoverview.diseasegroupsfilters.interactivepiechartfilters.StudiesPieChartFiltersContainerLayout.java

private StreamResource createResource(final CSFPRHandler handler) {
    return new StreamResource(new StreamResource.StreamSource() {
        @Override//from www .j av a  2 s .  c o  m
        @SuppressWarnings("CallToPrintStackTrace")
        public InputStream getStream() {

            //                BufferedImage bi = chart.createBufferedImage(width, height, chartRenderingInfo);
            try {

                byte[] pdfFile = handler.exportStudiesInformationPieCharts(chartSet, "piechart_filters.pdf",
                        "Datasets");
                return new ByteArrayInputStream(pdfFile);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

        }
    }, "piechart_filters.pdf");
}

From source file:roart.client.MyVaadinUI.java

void addListTable(VerticalLayout ts, List<ResultItem> strarr) {
    if (strarr.size() <= 1) {
        return;/*  w  w w  .  j a va  2  s.c  om*/
    }

    SearchDisplay display = SearchService.getSearchDisplay(getCurrent());
    boolean dodownload = (boolean) getSession().getAttribute("download");

    Table table = new Table("Table");
    table.setWidth("90%");
    int columns = strarr.get(0).get().size();
    for (int i = 0; i < strarr.size(); i++) {
        if (strarr.get(i).get().size() != columns) {
            System.out.println("column differs " + columns + " found " + strarr.get(i).get().size());
            break;
        }
    }
    for (int i = 0; i < columns; i++) {
        if (display.highlightmlt && i == IndexFiles.HIGHLIGHTMLTCOLUMN
                && strarr.get(0).get().get(IndexFiles.HIGHLIGHTMLTCOLUMN).equals("Highlight and similar")) {
            table.addContainerProperty(strarr.get(0).get().get(i), Button.class, null);
            continue;
        }
        table.addContainerProperty(strarr.get(0).get().get(i), String.class, null);
    }
    if (dodownload) {
        if (columns > IndexFiles.FILENAMECOLUMN
                && strarr.get(0).get().get(IndexFiles.FILENAMECOLUMN).equals("Filename")) {
            table.addGeneratedColumn("Download", new ColumnGenerator() {
                @Override
                public Object generateCell(Table source, Object itemId, Object columnId) {
                    String nodename = (String) source.getItem(itemId).getItemProperty("Node").getValue();
                    String filename = (String) source.getItem(itemId).getItemProperty("Filename").getValue();
                    if (nodename != null && !nodename.equals(ControlService.nodename)) {
                        return null;
                    }
                    if (filename == null) {
                        return null;
                    }
                    if (false) {
                        return null;
                    }
                    final FileObject fo = FileSystemDao.get(filename);
                    if (filename.startsWith(FileSystemDao.FILE) || filename.startsWith(FileSystemDao.HDFS)) {
                        filename = filename.substring(5);
                    }
                    int i = filename.lastIndexOf("/");
                    String fn = filename.substring(i + 1);
                    StreamResource resource = new StreamResource(new StreamSource() {
                        @Override
                        public InputStream getStream() {
                            try {
                                return FileSystemDao.getInputStream(fo);
                            } catch (Exception e) {
                                log.error(Constants.EXCEPTION, e);
                                return null;
                            }
                        }
                    }, fn);
                    //FileResource resource = new FileResource(new File(filename));
                    Button button = new Button("Download");
                    button.setStyleName(BaseTheme.BUTTON_LINK);
                    FileDownloader downloader = new FileDownloader(resource);
                    downloader.extend(button);
                    return button;
                }
            });
        }
    }
    for (int i = 1; i < strarr.size(); i++) {
        ResultItem str = strarr.get(i);
        if (display.highlightmlt && columns > IndexFiles.HIGHLIGHTMLTCOLUMN
                && strarr.get(0).get().get(IndexFiles.HIGHLIGHTMLTCOLUMN).equals("Highlight and similar")) {
            String text = (String) str.get().get(IndexFiles.HIGHLIGHTMLTCOLUMN);
            if (text != null) {
                String md5 = (String) str.get().get(1);
                str.get().set(IndexFiles.HIGHLIGHTMLTCOLUMN, getSimilar(text, md5));
            }
        }
        table.addItem(str.getarr(), i);
    }
    //table.setPageLength(table.size());
    ts.addComponent(table);
}

From source file:sk.stefan.mvps.model.serviceImpl.DocumentServiceImpl.java

@Override
public Resource getDocumentResource(Document document) {
    StreamResource.StreamSource source;
    StreamResource resource;// ww w.j  a  v a  2s.  c  o  m

    source = (StreamResource.StreamSource) () -> {
        InputStream inputStream = new ByteArrayInputStream(document.getDocument());
        return inputStream;
    };

    resource = new StreamResource(source, document.getFile_name());
    return resource;
}

From source file:ui.helper.ImageDropBox.java

License:Apache License

private void showFile(final String name, final String type, final ByteArrayOutputStream bas) {
    // resource for serving the file contents
    final StreamResource.StreamSource streamSource = new StreamResource.StreamSource() {
        @Override/*from w  w  w .j  av  a  2 s .c  o  m*/
        public InputStream getStream() {
            if (bas != null) {
                final byte[] byteArray = bas.toByteArray();
                return new ByteArrayInputStream(byteArray);
            }
            return null;
        }
    };
    final StreamResource resource = new StreamResource(streamSource, name);

    // show the file contents - images only for now
    final Embedded embedded = new Embedded("", resource);
    showComponent(embedded, name);
}