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:com.etest.pdfviewer.ItemAnalysisReportViewer.java

public ItemAnalysisReportViewer(int tqCoverageId) {
    this.tqCoverageId = tqCoverageId;

    setWidth("900px");
    setHeight("600px");
    center();//w  w  w  . j a  va2  s .c  o m

    StreamResource resource = new StreamResource(new ItemAnalysisReportPDF(getTqCoverageId()),
            "ItemAnalysis.pdf");
    resource.setMIMEType("application/pdf");

    VerticalLayout v = new VerticalLayout();
    v.setSizeFull();
    Embedded e = new Embedded();
    e.setSource(resource);
    e.setSizeFull();
    e.setType(Embedded.TYPE_BROWSER);
    v.addComponent(e);

    setContent(v);
}

From source file:com.etest.pdfviewer.MultipleChoiceHelpViewer.java

public MultipleChoiceHelpViewer() {
    setWidth("900px");
    setHeight("600px");
    center();//from w ww. j av  a2 s.  c  o  m

    StreamResource.StreamSource source = new StreamResource.StreamSource() {
        @Override
        public InputStream getStream() {
            try {
                return this.getClass().getResourceAsStream("/files/TestConstRules.pdf");
            } catch (Exception e) {
                e.getMessage();
                return null;
            }
        }
    };

    StreamResource resource = new StreamResource(source, "TestConstRules.pdf");
    resource.setMIMEType("application/pdf");

    VerticalLayout v = new VerticalLayout();
    v.setSizeFull();
    Embedded e = new Embedded();
    e.setSource(resource);
    e.setSizeFull();
    e.setType(Embedded.TYPE_BROWSER);
    v.addComponent(e);

    setContent(v);
}

From source file:com.etest.pdfviewer.SummaryReportViewer.java

public SummaryReportViewer(String summaryType) {
    setCaption(summaryType);/*ww w . j av a2  s.c o m*/
    setWidth("900px");
    setHeight("600px");
    center();

    StreamResource resource; //= new StreamResource(new InventoryCasesReportPDF(), "InventoryOfCases.pdf");   
    if (summaryType.equals("Summary: Case vs Items")) {
        resource = new StreamResource(new InventoryCasesReportPDF(), "InventoryOfCases.pdf");
    } else {
        resource = new StreamResource(new InventoryItemsReportPDF(), "InventoryOfItems.pdf");
    }
    resource.setMIMEType("application/pdf");

    VerticalLayout v = new VerticalLayout();
    v.setSizeFull();
    Embedded e = new Embedded();
    e.setSource(resource);
    e.setSizeFull();
    e.setType(Embedded.TYPE_BROWSER);
    v.addComponent(e);

    setContent(v);
}

From source file:com.etest.pdfviewer.TQCriticalIndexReportViewer.java

public TQCriticalIndexReportViewer(int tqCoverageId) {
    this.tqCoverageId = tqCoverageId;
    setWidth("900px");
    setHeight("600px");
    center();/*from   w  w  w . j  a va2s .  c  om*/

    StreamResource resource = new StreamResource(new TQCriticalIndexValuesReportPDF(getTQCoverageId()),
            "TQCriticalIndexValues.pdf");
    resource.setMIMEType("application/pdf");

    VerticalLayout v = new VerticalLayout();
    v.setSizeFull();
    Embedded e = new Embedded();
    e.setSource(resource);
    e.setSizeFull();
    e.setType(Embedded.TYPE_BROWSER);
    v.addComponent(e);

    setContent(v);
}

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);
                        }//w w  w  .j  av a 2 s .  c o m
                        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.FVBlobDisplay.java

License:Apache License

public void displayPDFFile() {
    BlobResource blobResource = getBlobResource();
    if (blobResource != null) {
        String fileName = blobResource.getFilename();
        if (fileName != null) {
            String mimeType = blobResource.getMIMEType();

            StreamSource streamSource = new StreamSource() {
                private DownloadStream downloadStream = getBlobResource() != null
                        ? getBlobResource().getStream()
                        : null;/*w  w w  .  ja v  a 2  s . com*/

                @Override
                public InputStream getStream() {

                    return downloadStream != null ? downloadStream.getStream() : null;
                }
            };
            String viewButtonCaption = "View "
                    + fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) + " File";
            Button viewDocButton = new Button(viewButtonCaption);
            viewDocButton.setStyleName(Reindeer.BUTTON_LINK);
            if (streamSource != null) {
                StreamResource streamResource = new StreamResource(streamSource, fileName);
                streamResource.setMIMEType(mimeType);

                BrowserWindowOpener opener = new BrowserWindowOpener(streamResource);
                opener.extend(viewDocButton);
                settingsLayout.addComponent(viewDocButton);
                Resource resource = FVIconFactory.getInstance().getFVIcon(FVIconFactory.ICON_NOTE);
                Image image = new Image();
                image.setSource(resource);
                addComponent(image);
            }
        }
    }
}

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

License:Apache License

public boolean refreshImageFromProperty() {
    BufferedImage photo = getBufferedImage();
    boolean error = photo == null;

    if (!error) {
        originalHeight = photo.getHeight();
        originalWidth = photo.getWidth();
        setAttributes(getAttributes());/*from www  .j  a  v a  2 s  .  c  o m*/
        StreamResource.StreamSource streamSource = new StreamResource.StreamSource() {

            public InputStream getStream() {
                try {
                    /* Write the image to a buffer. */
                    ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();
                    ImageIO.write(getBufferedImage()/*Globals.getApp().getCompany().getLogo()*/, "png",
                            imagebuffer);
                    /* Return a stream from the buffer. */
                    return new ByteArrayInputStream(imagebuffer.toByteArray());
                } catch (IOException e) {
                    Globals.logException(e);
                    return null;
                }
            }

        };
        UI application = FocWebApplication.getInstanceForThread();
        if (application != null) {
            StreamResource streamResource = new StreamResource(streamSource, "");
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String timestamp = df.format(new Date(System.currentTimeMillis()));
            streamResource.setFilename("FileName-" + timestamp + ".jpg");

            if (embedded != null) {
                embedded.setSource(streamResource);
            }
        }
    }
    return error;
}

From source file:com.foc.vaadin.gui.mswordGenerator.FocXmlMSWordParser.java

License:Apache License

public StreamResource getStreamResource() {
    //      StreamSource source = new WordDocumentStreamSource("C://Users//user//Desktop//POI Word Doc Sample Table 1.docx");
    StreamSource source = new CreateWordDoc(getWordOutputStream());
    StreamResource resource = new StreamResource(source, "wordfile");

    resource.setMIMEType("application/msword");
    return resource;
}

From source file:com.foc.vaadin.gui.pdfGenerator.FocXmlPDFParser.java

License:Apache License

public StreamResource getStreamResource() {
    StreamSource source = new CreatePDF(getPDFOutoutStream());

    String filename = "pdf" + ASCII.generateRandomString(10);
    StreamResource resource = new StreamResource(source, filename);

    resource.setMIMEType("application/pdf");
    //      resource.getStream().setParameter("Content-Disposition", "attachment; filename=" + filename);
    return resource;
}

From source file:com.foc.web.modules.business.PrnLayout_BrowserWindowOpenerStreamResource.java

License:Apache License

@Override
public DownloadStream getStream() {
    beforeGetStream();//from  w  w  w  .  ja va 2s.  c o  m
    DownloadStream downloadStream = null;
    if (getPrintingAction() != null && getPrintingAction().getLauncher() != null) {
        if (prnLayout != null) {

            //            if(this.prnLayout_Table.getPrintLogoCheckBox() != null){
            //              boolean withLogo = this.prnLayout_Table.getPrintLogoCheckBox().getValue();
            //              this.prnLayout_Table.getPrintingAction().getLauncher().setWithLogo(withLogo);
            //            }

            getPrintingAction().getLauncher().setWithLogo(isWithLogo());
            getPrintingAction().setLaunchedAutomatically(false);

            byte[] bytes = null;
            if (outputFormat == DOC) {
                if (outputFileNameWithoutExtension != null)
                    setFilename(outputFileNameWithoutExtension + ".docx");
                bytes = getPrintingAction().getLauncher().printWordDocument(prnLayout);
                if (bytes != null)
                    setMIMEType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            } else if (outputFormat == RTF) {
                if (outputFileNameWithoutExtension != null)
                    setFilename(outputFileNameWithoutExtension + ".rtf");
                bytes = getPrintingAction().getLauncher().printRTFDocument(prnLayout);
                if (bytes != null)
                    setMIMEType("application/rtf");
            } else {
                if (outputFileNameWithoutExtension != null)
                    setFilename(outputFileNameWithoutExtension + ".pdf");
                bytes = getPrintingAction().getLauncher().web_FillReport(prnLayout, prnLayout.getFileName());
                if (bytes != null)
                    setMIMEType("application/pdf");
            }
            if (bytes != null) {
                setStreamSource(new FStreamSource(bytes));
            }
            downloadStream = super.getStream();
        }
    } else {
        StreamSource source = new StreamSource() {
            @Override
            public InputStream getStream() {
                try {
                    return new ByteArrayInputStream(getErrorMessageAsHTML().getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }
        };
        // second, create a StreamResource and pass the previous StreamResource:
        StreamResource resource = new StreamResource(source, "file.html");
        resource.setMIMEType("text/html; charset=utf-8");
        downloadStream = ((ConnectorResource) resource).getStream();
    }
    return downloadStream;
}