Search code examples
google-chromejsf-2primefacesgraphicimage

p:graphicImage disappears after a while in google chrome


I use p:graphic image component of prime faces in jsf 2.0 project with pf 3.3. I implemented it as BalusC explained in severeal questions like this. It orks fine on internet explorer and firefox but in google chrome the image disappears leaving a broken image link behind.

In internet explorer; when i save as the image it's named untitled.png and it's ok.

In internet explorer; when i save as the image it's named dynamiccontent.xhtml.png and it's ok.

But in google chrome its named dynamiccontent.xhtml and even if i change the extension the image is not visible.

I create the image with java graphics library as

BufferedImage bufferedimage = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedimage.createGraphics();
....
g2d.dispose();

ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedimage, "png", os);

return os;

And my xhtml is like:

<p:graphicImage value="#{imagesBean.image}">
    <f:param name="lilw" value="#{reportsLoadSheetBean.loadedIndexLandingWeight}" />
    <f:param name="litow" value="#{reportsLoadSheetBean.loadedIndexTakeOffWeight}" />
    <f:param name="lizfw" value="#{reportsLoadSheetBean.loadedIndexZeroFuelWeight}" />
    <f:param name="tl" value="#{reportsLoadSheetBean.topLimit}" />
    <f:param name="zfwf" value="#{reportsLoadSheetBean.zeroFuelWeightForward}" />
    <f:param name="zfwa" value="#{reportsLoadSheetBean.zeroFuelWeightAfter}" />
    <f:param name="towf" value="#{reportsLoadSheetBean.takeOffWeightForward}" />
    <f:param name="towa" value="#{reportsLoadSheetBean.takeOffWeightAfter}" />
    <f:param name="lwf" value="#{reportsLoadSheetBean.landingWeightForward}" />
    <f:param name="lwa" value="#{reportsLoadSheetBean.landingWeightAfter}" />
    <f:param name="width" value="600" />
    <f:param name="height" value="200" />
</p:graphicImage>

and lastly the Application scoped bean that gets the image and sends it to the interface:

public StreamedContent getImage() throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();

    if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
        // So, we're rendering the view. Return a stub StreamedContent so
        // that it will generate right URL.
        return new DefaultStreamedContent();
    } else {
        String lilw = context.getExternalContext().getRequestParameterMap().get("lilw");
        String litow = context.getExternalContext().getRequestParameterMap().get("litow");
        String lizfw = context.getExternalContext().getRequestParameterMap().get("lizfw");
        String tl = context.getExternalContext().getRequestParameterMap().get("tl");
        String zfwf = context.getExternalContext().getRequestParameterMap().get("zfwf");
        String zfwa = context.getExternalContext().getRequestParameterMap().get("zfwa");
        String towf = context.getExternalContext().getRequestParameterMap().get("towf");
        String towa = context.getExternalContext().getRequestParameterMap().get("towa");
        String lwf = context.getExternalContext().getRequestParameterMap().get("lwf");
        String lwa = context.getExternalContext().getRequestParameterMap().get("lwa");

        List<Number> zeroFuelWeightIntervals = new ArrayList<Number>();
        List<Number> takeOffWeightIntervals = new ArrayList<Number>();
        List<Number> landingWeightIntervals = new ArrayList<Number>();

        zeroFuelWeightIntervals.add(Double.valueOf(zfwf));
        zeroFuelWeightIntervals.add(Double.valueOf(zfwa));
        zeroFuelWeightIntervals.add(Double.valueOf(tl));

        takeOffWeightIntervals.add(Double.valueOf(towf));
        takeOffWeightIntervals.add(Double.valueOf(towa));
        takeOffWeightIntervals.add(Double.valueOf(tl));

        landingWeightIntervals.add(Double.valueOf(lwf));
        landingWeightIntervals.add(Double.valueOf(lwa));
        landingWeightIntervals.add(Double.valueOf(tl));

        int width = Integer.parseInt(context.getExternalContext().getRequestParameterMap().get("width"));
        int height = Integer.parseInt(context.getExternalContext().getRequestParameterMap().get("height"));

        IdealIntervalPointDiagram diagram = new IdealIntervalPointDiagram(
                "ZFL", Double.valueOf(lizfw), zeroFuelWeightIntervals, 
                "TOW", Double.valueOf(litow), takeOffWeightIntervals, 
                "LAW", Double.valueOf(lilw), landingWeightIntervals,
                width, height);

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            os = diagram.paint();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");
    }

May it be a bug about primefaces?


Solution

  • I found the problem. It's about the google chrome checking the content-length header strictly in the response including the image and because the server compresses the image; the actual content size and the size written on the header does not match. Other mainstream browser ignore this mismatching but google chrome not. so i called

    FacesContext.getCurrentInstance().getExternalContext().setResponseContentLength(os.size());
    

    before returning the image and now it also works fine in google chrome.