1

I tried to apply the a4j:mediaOutput to print a previously saved jpeg file. It fails with a bean-scope-error. I did some tests along the process cycle.

  1. The file is stored in the database.
  2. I can read from the database and print the image to the filesystem using FileOutputStream. JPEG looks good.
  3. However, the paint method is never triggered. The breakpoint at System.out is never reached. This is strange because the backing bean invokes a getter method, which I use to set the value attribute of the a4j:mediaOutput. Thus, sequentially somewhere between the value and createContent attributes' processing is the error.

Here is the method of my @ViewScoped backing bean.

    public void paintHeaderImage(OutputStream stream, Object object)
                throws IOException {
            UploadedImage ui = pub.getUploadedImages().iterator().next();
            stream.write(ui.getData());     
            stream.close();
    }

The JSF/Richfaces snippet.

<a4j:mediaOutput fileName="header.jpg" element="img" createContent="#{pubController.paintHeaderImage}" value="#{pubController.headerImage.pid}" mimeType="image/jpeg" style="width:100px; height:100px;"  />

Below is the error. Apparently it happens after the RENDER_RESPONSE (see first line of log)

18:12:22,477 INFO  [org.exadel.helper] (http--0.0.0.0-8080-1) AFTER RENDER_RESPONSE 6
18:12:22,636 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/ccms].[Faces Servlet]] (http--0.0.0.0-8080-1) Servlet.service() for servlet Faces Servlet threw exception: java.lang.NullPointerException
    at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:563) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager.getBeanFromScope(BeanManager.java:240) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:242) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72) [jbossweb-7.0.13.Final.jar:]
    at org.apache.el.parser.AstValue.getTarget(AstValue.java:94) [jbossweb-7.0.13.Final.jar:]
    at org.apache.el.parser.AstValue.invoke(AstValue.java:244) [jbossweb-7.0.13.Final.jar:]
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at org.richfaces.resource.MediaOutputResource.encode(MediaOutputResource.java:62) [richfaces-components-ui-4.3.3.Final.jar:4.3.3.Final]
    at org.richfaces.resource.UserResourceWrapperImpl.encode(UserResourceWrapperImpl.java:188) [richfaces-core-impl-4.3.3.Final.jar:4.3.3.Final]
    at org.richfaces.resource.ResourceHandlerImpl.handleResourceRequest(ResourceHandlerImpl.java:229) [richfaces-core-impl-4.3.3.Final.jar:4.3.3.Final]
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:591) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_17]

The img tag is created, but is utterly long. The browser cannot retrieve it as if the resource is not available.

<img style="width:100px; height:100px;" src="/ccms/rfRes/org.richfaces.resource.MediaOutputResource.jsf?do=eAGFUj1oFEEY!XJwoCFFjD8gEricGhB0lmuS4gxIxJiDiylO!EmwmNv93NtldmcyM3u3GBUbG7EQbAMWtlpZCWlshbMIWqW0EAstxMbY-M0mGtkm2wws773vfe97r75BNdNwdLUd8z5ngqchW-7G6Nvmsw-3Xoybc6ICkCsAqBoNR!ZR81IK5Omwph992tj5XoGRFaj2uciQ0BZGo4SH6MUKQ0XEaUfM2V3uo2G-TJRMMbWsY7nFRSkC1B3eR337!Zu55xvDpQpU2nDYF9yYazxBCxOFP8!58zpWR2nYbMOoIU5QaFg4sYuIpNdBHXER3eNdgc1cufEXaCQzWbpnwNkQaA1Dwa7zcAltTwZXcqXRmEimtKz7RsZocw0nd60TtIzjEz!PzH9esy4hyvAfbl!p5eMnnR8rWxeLDF0MUocs7kpj2ABF4ObfpLcs3J1893FneqxGobbhUEBeQ8rJwqlix9xD4ZUpTXLQKKlnNhJuxILUA64DCq3M2n57!Pfw6eam87cGDylRDXWnwhX3e-jIZUorUaL1i2-Lxv0pF1BBG5yFqdPrKutelqnVUgjUTPEotYvI6bYt14UHAOWm7V3y9daNL18n16!-bVrFwrGiaJFky5lVmSUg8sTCePH7v5YqlQ!uwKpHl5OZpnJ5gygI6bQe7Y4LmRAs79lE1C41ZmfOz8zWfBKy6GxS!-bqB5qu59TlXrEFi1X4B2oTIgI_" id="j_idt18:pubFormE:j_idt63">
feder
  • 1,705
  • 5
  • 23
  • 36
  • 1
    I don't do RichFaces, but under the covers this problem has undoubtedly the same grounds as this one with PrimeFaces `` due to the way how HTML and HTTP works: http://stackoverflow.com/questions/8207325/display-image-from-database-with-pgraphicimage/12452144#12452144 Surely the `` also generates `` which in turn lets the webbrowser fire a brand new and entirely separate HTTP request in order to download the content. – BalusC Sep 02 '13 at 16:46

1 Answers1

1

Ok. Got it. I changed the backing bean to session scoped. Now it works.

For a smooth solution one needs to extract the image fetching and streaming to an exra backing bean with a broader scope. As recommended by @BalusC:

  • you pass the reference key to the value attribute of a4j:mediaOutput.
  • the createContent points then to the method in your @SessionScoped or @ApplicationScoped backing bean.
  • the method fetches the value and retrieves the image from your persistence source (DB, filesystem, cloud, you name it) and returns the stream.

Here again, his Link. Display dynamic image from database with p:graphicImage and StreamedContent

Community
  • 1
  • 1
feder
  • 1,705
  • 5
  • 23
  • 36