Example usage for com.vaadin.server DownloadStream DownloadStream

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

Introduction

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

Prototype

public DownloadStream(InputStream stream, String contentType, String fileName) 

Source Link

Document

Creates a new instance of DownloadStream.

Usage

From source file:com.cms.utils.FileDownloader.java

License:Apache License

@Override
public DownloadStream getStream() {
    try {/*www.ja  va2 s  .c o m*/
        final DownloadStream ds = new DownloadStream(new FileInputStream(sourceFile), getMIMEType(),
                fileNameNew);
        ds.setParameter("Content-Length", String.valueOf(sourceFile.length()));

        ds.setCacheTime(cacheTime);
        return ds;
    } catch (final FileNotFoundException e) {
        throw new RuntimeException("File not found: " + sourceFile.getName(), e);
    }
}

From source file:com.esofthead.mycollab.community.ui.chart.JFreeChartWrapper.java

License:Open Source License

@Override
public Resource getSource() {
    if (res == null) {
        StreamSource streamSource = new StreamResource.StreamSource() {
            private ByteArrayInputStream bytestream = null;

            ByteArrayInputStream getByteStream() {
                if (chart != null && bytestream == null) {
                    int width = getGraphWidth();
                    int height = getGraphHeight();

                    if (mode == RenderingMode.SVG) {

                        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                        DocumentBuilder docBuilder = null;
                        try {
                            docBuilder = docBuilderFactory.newDocumentBuilder();
                        } catch (ParserConfigurationException e1) {
                            throw new RuntimeException(e1);
                        }//from  w  w w . j  a  v a 2  s .  com
                        Document document = docBuilder.newDocument();
                        Element svgelem = document.createElement("svg");
                        document.appendChild(svgelem);

                        // Create an instance of the SVG Generator
                        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

                        // draw the chart in the SVG generator
                        chart.draw(svgGenerator, new Rectangle(width, height));
                        Element el = svgGenerator.getRoot();
                        el.setAttributeNS(null, "viewBox", "0 0 " + width + " " + height + "");
                        el.setAttributeNS(null, "style", "width:100%;height:100%;");
                        el.setAttributeNS(null, "preserveAspectRatio", getSvgAspectRatio());

                        // Write svg to buffer
                        ByteArrayOutputStream baoutputStream = new ByteArrayOutputStream();
                        Writer out;
                        try {
                            OutputStream outputStream = gzipEnabled ? new GZIPOutputStream(baoutputStream)
                                    : baoutputStream;
                            out = new OutputStreamWriter(outputStream, "UTF-8");
                            /*
                            * don't use css, FF3 can'd deal with the result
                            * perfectly: wrong font sizes
                            */
                            boolean useCSS = false;
                            svgGenerator.stream(el, out, useCSS, false);
                            outputStream.flush();
                            outputStream.close();
                            bytestream = new ByteArrayInputStream(baoutputStream.toByteArray());
                        } catch (Exception e) {
                            log.error("Error while generating SVG chart", e);
                        }
                    } else {
                        // Draw png to bytestream
                        try {
                            byte[] bytes = ChartUtilities.encodeAsPNG(chart.createBufferedImage(width, height));
                            bytestream = new ByteArrayInputStream(bytes);
                        } catch (Exception e) {
                            log.error("Error while generating PNG chart", e);
                        }

                    }

                } else {
                    bytestream.reset();
                }
                return bytestream;
            }

            @Override
            public InputStream getStream() {
                return getByteStream();
            }
        };

        res = new StreamResource(streamSource, String.format("graph%d", System.currentTimeMillis())) {

            @Override
            public int getBufferSize() {
                if (getStreamSource().getStream() != null) {
                    try {
                        return getStreamSource().getStream().available();
                    } catch (IOException e) {
                        log.warn("Error while get stream info", e);
                        return 0;
                    }
                } else {
                    return 0;
                }
            }

            @Override
            public long getCacheTime() {
                return 0;
            }

            @Override
            public String getFilename() {
                if (mode == RenderingMode.PNG) {
                    return super.getFilename() + ".png";
                } else {
                    return super.getFilename() + (gzipEnabled ? ".svgz" : ".svg");
                }
            }

            @Override
            public DownloadStream getStream() {
                DownloadStream downloadStream = new DownloadStream(getStreamSource().getStream(), getMIMEType(),
                        getFilename());
                if (gzipEnabled && mode == RenderingMode.SVG) {
                    downloadStream.setParameter("Content-Encoding", "gzip");
                }
                return downloadStream;
            }

            @Override
            public String getMIMEType() {
                if (mode == RenderingMode.PNG) {
                    return "image/png";
                } else {
                    return "image/svg+xml";
                }
            }
        };
    }
    return res;
}

From source file:com.esofthead.mycollab.ui.chart.JFreeChartWrapper.java

License:Open Source License

@Override
public Resource getSource() {
    if (res == null) {
        StreamSource streamSource = new StreamResource.StreamSource() {
            private ByteArrayInputStream bytestream = null;

            ByteArrayInputStream getByteStream() {
                if (chart != null && bytestream == null) {
                    int width = getGraphWidth();
                    int height = getGraphHeight();

                    if (mode == RenderingMode.SVG) {
                        // Create an instance of the SVG Generator
                        SVGGraphics2D svgGenerator = new SVGGraphics2D(width, height);

                        // draw the chart in the SVG generator
                        chart.draw(svgGenerator, new Rectangle(width, height));
                        // create an xml string in svg format from the drawing
                        String drawingSVG = svgGenerator.getSVGElement();
                        return new ByteArrayInputStream(drawingSVG.getBytes(StandardCharsets.UTF_8));
                    } else {
                        // Draw png to bytestream
                        try {
                            byte[] bytes = ChartUtilities.encodeAsPNG(chart.createBufferedImage(width, height));
                            bytestream = new ByteArrayInputStream(bytes);
                        } catch (Exception e) {
                            log.error("Error while generating PNG chart", e);
                        }//www  .j a va2  s .c  o m

                    }

                } else {
                    bytestream.reset();
                }
                return bytestream;
            }

            @Override
            public InputStream getStream() {
                return getByteStream();
            }
        };

        res = new StreamResource(streamSource, String.format("graph%d", System.currentTimeMillis())) {

            @Override
            public int getBufferSize() {
                if (getStreamSource().getStream() != null) {
                    try {
                        return getStreamSource().getStream().available();
                    } catch (IOException e) {
                        log.warn("Error while get stream info", e);
                        return 0;
                    }
                } else {
                    return 0;
                }
            }

            @Override
            public long getCacheTime() {
                return 0;
            }

            @Override
            public String getFilename() {
                if (mode == RenderingMode.PNG) {
                    return super.getFilename() + ".png";
                } else {
                    return super.getFilename() + (gzipEnabled ? ".svgz" : ".svg");
                }
            }

            @Override
            public DownloadStream getStream() {
                DownloadStream downloadStream = new DownloadStream(getStreamSource().getStream(), getMIMEType(),
                        getFilename());
                if (gzipEnabled && mode == RenderingMode.SVG) {
                    downloadStream.setParameter("Content-Encoding", "gzip");
                }
                return downloadStream;
            }

            @Override
            public String getMIMEType() {
                if (mode == RenderingMode.PNG) {
                    return "image/png";
                } else {
                    return "image/svg+xml";
                }
            }
        };
    }
    return res;
}

From source file:com.esofthead.mycollab.vaadin.resources.file.FileStreamDownloadResource.java

License:Open Source License

@Override
public DownloadStream getStream() {
    try {/*from   w  w  w.  j  a  va  2  s  .c  om*/
        String fileName = getFilename();
        fileName = fileName.replaceAll(" ", "_").replaceAll("-", "_");
        final DownloadStream ds = new DownloadStream(new FileInputStream(getSourceFile()), getMIMEType(),
                fileName);
        ds.setParameter("Content-Disposition", "attachment; filename=" + fileName);
        ds.setCacheTime(0);
        return ds;
    } catch (final FileNotFoundException e) {
        LOG.error("Error to create download stream", e);
        return null;
    }
}

From source file:com.foc.msword.WordTemplateFillerResource.java

License:Apache License

@Override
public DownloadStream getStream() {
    DownloadStream downloadStream = null;
    try {/*  ww  w  .  j a va 2s .c o  m*/
        ClassResource resource = null;
        InputStream inputStream = null;
        resource = new ClassResource(tempateFileName);
        inputStream = resource.getStream().getStream();

        ExtendedWordDocument xWord = new ExtendedWordDocument(inputStream);
        if (xWord != null) {
            fill(xWord);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            xWord.write(baos);
            bais = new ByteArrayInputStream(baos.toByteArray());

        }
        xWord.dispose();
    } catch (Exception e) {
        Globals.logException(e);
    }

    if (bais != null) {
        String fileName2 = downloadFileName;
        if (!fileName2.endsWith(".doc") && !fileName2.endsWith(".docx")) {
            fileName2 += ".docx";
        }

        downloadStream = new DownloadStream(bais, "application/x-unknown", fileName2);
        downloadStream.setParameter("Content-Disposition", "attachment; filename=" + fileName2);
        downloadStream.setCacheTime(0);
    }

    return downloadStream;
}

From source file:com.foc.property.FCloudStoragePropertyResource.java

License:Apache License

@Override
public DownloadStream getStream() {
    DownloadStream downloadStream = null;

    if (cloudStorageProperty != null) {
        if (Utils.isStringEmpty(cloudStorageProperty.getKey())) {
            Globals.logString("DOWNLOAD: newBlobResource 1 Key Empty, generating it");
            cloudStorageProperty.generateKey();
        }// w ww. j  ava 2s .  c  om

        Globals.logString("DOWNLOAD: newBlobResource 1bis Key=" + cloudStorageProperty.getKey());

        if (cloudStorageProperty.getDirectory() == null) {
            Globals.logString("DOWNLOAD: newBlobResource 2 Directory null computing it");
            cloudStorageProperty.setDirectory(Globals.getApp().getCloudStorageDirectory(), false);
        }

        Globals.logString("DOWNLOAD: newBlobResource 2bis Key=" + cloudStorageProperty.getDirectory());

        InputStream is = (InputStream) cloudStorageProperty.getObject();

        if (is == null)
            Globals.logString("DOWNLOAD: newBlobResource 3 inputStream is null");
        else
            Globals.logString("DOWNLOAD: newBlobResource 3bis inputStream is Good");

        Globals.logString("DOWNLOAD: newBlobResource 4 FileName : " + cloudStorageProperty.getFileName());

        if (is != null) {
            downloadStream = new DownloadStream(is, "application/x-unknown",
                    cloudStorageProperty.getFileName());
            downloadStream.setParameter("Content-Disposition", "attachment; filename="
                    + DownloadStream.getContentDispositionFilename(cloudStorageProperty.getFileName()));
            downloadStream.setCacheTime(0);
        }
    }

    return downloadStream;
}

From source file:com.foc.vaadin.gui.components.BlobResource.java

License:Apache License

@Override
public DownloadStream getStream() {
    DownloadStream downloadStream = null;

    if (is != null) {
        downloadStream = new DownloadStream(is, "application/x-unknown", fileName);
        downloadStream.setParameter("Content-Disposition", "attachment; filename=" + fileName);
        downloadStream.setCacheTime(0);/*from  w  w  w . j  ava  2 s  .c om*/
    }

    return downloadStream;
}

From source file:com.foc.vaadin.gui.components.chart.JFreeChartWrapper.java

License:Apache License

@Override
public Resource getSource() {
    if (res == null) {
        StreamSource streamSource = new StreamResource.StreamSource() {
            private ByteArrayInputStream bytestream = null;

            ByteArrayInputStream getByteStream() {
                if (chart != null && bytestream == null) {
                    int widht = getGraphWidth();
                    int height = getGraphHeight();

                    if (mode == RenderingMode.SVG) {

                        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                        DocumentBuilder docBuilder = null;
                        try {
                            docBuilder = docBuilderFactory.newDocumentBuilder();
                        } catch (ParserConfigurationException e1) {
                            throw new RuntimeException(e1);
                        }//from www .  j  ava2s .com
                        Document document = docBuilder.newDocument();
                        Element svgelem = document.createElement("svg");
                        document.appendChild(svgelem);

                        // Create an instance of the SVG Generator
                        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

                        // draw the chart in the SVG generator
                        chart.draw(svgGenerator, new Rectangle(widht, height));
                        Element el = svgGenerator.getRoot();
                        el.setAttributeNS(null, "viewBox", "0 0 " + widht + " " + height + "");
                        el.setAttributeNS(null, "style", "width:100%;height:100%;");
                        el.setAttributeNS(null, "preserveAspectRatio", getSvgAspectRatio());

                        // Write svg to buffer
                        ByteArrayOutputStream baoutputStream = new ByteArrayOutputStream();
                        Writer out;
                        try {
                            OutputStream outputStream = gzipEnabled ? new GZIPOutputStream(baoutputStream)
                                    : baoutputStream;
                            out = new OutputStreamWriter(outputStream, "UTF-8");
                            /*
                             * don't use css, FF3 can'd deal with the result
                             * perfectly: wrong font sizes
                             */
                            boolean useCSS = false;
                            svgGenerator.stream(el, out, useCSS, false);
                            outputStream.flush();
                            outputStream.close();
                            bytestream = new ByteArrayInputStream(baoutputStream.toByteArray());
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (SVGGraphics2DIOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    } else {
                        // Draw png to bytestream
                        try {
                            byte[] bytes = ChartUtilities.encodeAsPNG(chart.createBufferedImage(widht, height));
                            bytestream = new ByteArrayInputStream(bytes);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }

                } else {
                    bytestream.reset();
                }
                return bytestream;
            }

            @Override
            public InputStream getStream() {
                return getByteStream();
            }
        };

        res = new StreamResource(streamSource, String.format("graph%d", System.currentTimeMillis())) {

            @Override
            public int getBufferSize() {
                if (getStreamSource().getStream() != null) {
                    try {
                        return getStreamSource().getStream().available();
                    } catch (IOException e) {
                        return 0;
                    }
                } else {
                    return 0;
                }
            }

            @Override
            public long getCacheTime() {
                return 0;
            }

            @Override
            public String getFilename() {
                if (mode == RenderingMode.PNG) {
                    return super.getFilename() + ".png";
                } else {
                    return super.getFilename() + (gzipEnabled ? ".svgz" : ".svg");
                }
            }

            @Override
            public DownloadStream getStream() {
                DownloadStream downloadStream = new DownloadStream(getStreamSource().getStream(), getMIMEType(),
                        getFilename());
                if (gzipEnabled && mode == RenderingMode.SVG) {
                    downloadStream.setParameter("Content-Encoding", "gzip");
                }
                return downloadStream;
            }

            @Override
            public String getMIMEType() {
                if (mode == RenderingMode.PNG) {
                    return "image/png";
                } else {
                    return "image/svg+xml";
                }
            }
        };
    }
    return res;
}

From source file:com.foc.vaadin.gui.components.chart.JFreeChartWrapper_Modified.java

License:Apache License

@Override
public Resource getSource() {
    if (res == null) {
        res = new ConnectorResource() {
            private ByteArrayInputStream bytestream = null;

            ByteArrayInputStream getByteStream() {
                if (chart != null && bytestream == null) {
                    int widht = getGraphWidth();
                    int height = getGraphHeight();

                    if (mode == RenderingMode.SVG) {
                        //VAADIN 7
                        /*
                        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                              .newInstance();
                        DocumentBuilder docBuilder = null;
                        try {//from  w  w  w.  j av  a 2 s  . c  o m
                           docBuilder = docBuilderFactory
                                 .newDocumentBuilder();
                        } catch (ParserConfigurationException e1) {
                           throw new RuntimeException(e1);
                        }
                        Document document = docBuilder.newDocument();
                        Element svgelem = document.createElement("svg");
                        document.appendChild(svgelem);
                                
                        // Create an instance of the SVG Generator
                                
                        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
                                
                        // draw the chart in the SVG generator
                        chart.draw(svgGenerator, new Rectangle(widht, height));
                        Element el = svgGenerator.getRoot();
                        el.setAttributeNS(null, "viewBox", "0 0 " + widht + " " + height + "");
                        el.setAttributeNS(null, "style", "width:100%;height:100%;");
                        el.setAttributeNS(null, "preserveAspectRatio", getSvgAspectRatio());
                                
                        // Write svg to buffer
                        ByteArrayOutputStream baoutputStream = new ByteArrayOutputStream();
                        Writer out;
                        try {
                           OutputStream outputStream = gzipEnabled ? new GZIPOutputStream(
                                 baoutputStream) : baoutputStream;
                           out = new OutputStreamWriter(outputStream,
                                 "UTF-8");
                                
                        //                         * don't use css, FF3 can'd deal with the result
                        //                         * perfectly: wrong font sizes
                           boolean useCSS = false;
                           svgGenerator.stream(el, out, useCSS, false);
                           outputStream.flush();
                           outputStream.close();
                           bytestream = new ByteArrayInputStream(
                                 baoutputStream.toByteArray());
                        } catch (Exception e) {
                           e.printStackTrace();
                        }
                        */
                    } else {
                        // Draw png to bytestream
                        try {
                            byte[] bytes = ChartUtilities.encodeAsPNG(chart.createBufferedImage(widht, height));
                            bytestream = new ByteArrayInputStream(bytes);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                } else {
                    bytestream.reset();
                }
                return bytestream;
            }

            @Override
            public String getMIMEType() {
                if (mode == RenderingMode.PNG) {
                    return "image/png";
                } else {
                    return "image/svg+xml";
                }
            }

            @Override
            public DownloadStream getStream() {
                DownloadStream downloadStream = new DownloadStream(getByteStream(), getMIMEType(),
                        getFilename());
                if (gzipEnabled && mode == RenderingMode.SVG) {
                    downloadStream.setParameter("Content-Encoding", "gzip");
                }
                return downloadStream;
            }

            @Override
            public String getFilename() {
                if (mode == RenderingMode.PNG) {
                    return "graph.png";
                } else {
                    return gzipEnabled ? "graph.svgz" : "graph.svg";
                }
            }
        };
    }
    return res;
}

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//from  w w  w  .  j  a va 2s  .  c  o m
        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();
}