3 Replies Latest reply on Apr 26, 2010 6:25 PM by Dmitry Shultz

    Seam PDF byte data Problems

    Charles Akalugwu Newbie

      Hi Everyone,


      I am going through a very grave problem. One of the requirements of my project states that upon successful registration of a new user, 5 different pdfs have to be generated (associated with the just-registered user) and saved in a database table. Right now in my code, upon successful persisting of the user entity, I raise an asynchronous seam event to perform the generation of the pdfs. Of course I supply the id of the newly persisted user instance to the asynchronous call to ensure that the generated pdfs are appropriately associated to that user. I have followed all the instructions outlined on this page, http://seamframework.org/Community/PDFdocumentStore , yet I get a NullPointerException in the getBytes method. My code is listed below:



      @Name("createFreeBusinessCards")
      public class CreateFreeBusinessCards implements Serializable {
      
              @Logger
              private Log log;
              
              @In(create=true)
              Renderer renderer;
              
              @In(create=true)
              BusinessCodeGenerator businessCodeGenerator;
              
              @In(create=true)
              IBusinessCardDAO businessCardDAO;
              
              @In(create=true)
              IUserDAO userDAO;
              
              private static final int COUNT = 5;
              
              private User user;
              
              
              public User getUser() {
                      return user;
              }
      
              public void setUser(User user) {
                      this.user = user;
              }
      
      
              @Create
              public void init(){
                      log.info( "Bean Initialized" );
              }
      
              @Observer(Constants.CREATE_FREE_BUSINESS_CARDS_EVENT)
              public void onNewUserCreated( Long userId ){
                      
                      setUser( userDAO.findById( userId ) );
                      
                      BusinessCard card;
                      
                      try {
                              
                                      for( int i = 0; i < COUNT; i++ ){
                                              card = new BusinessCard();
                                              card.setCreated( new Date() );
                                              card.setOwner( user );
                                              card.setCode( businessCodeGenerator.generateCode() );
                                              card.setContent( createPDFData( ) );
                                              
                                              businessCardDAO.persist( card );
                                      }
                             
                             log.info( "business cards were successfully generated and associated with the user's account");
                         } 
                         catch (Exception e) {
                                 log.error( "There was an error generating the business cards [Error Message: "+e.getMessage()+"]" );
                                e.printStackTrace();
                         }
              }
              
              
              private byte[] createPDFData( ){
      
                      EmptyFacesContext emptyFacesContext = new EmptyFacesContext();
                      log.info("value of emptyfaces context = "+ emptyFacesContext);
                      
                      byte[] bytes = null;
      
                      try {
                              Renderer render = Renderer.instance(); 
                              log.info("Generating final pdf from #0", "/ui/pdf/cards.xhtml");
                              render.render( "/ui/pdf/cards.xhtml" );
                              DocumentStore doc = DocumentStore.instance();
      
                              if (doc != null) {
                                      DocumentData data = doc.getDocumentData("1");
                                      ByteArrayDocumentData byteData = null;
                                      if (data instanceof ByteArrayDocumentData) {
                                              byteData = (ByteArrayDocumentData) data;
                                      } else {
                                              throw new IllegalArgumentException("Couldnt get the bytes from the pdf document, unkown class " + data.getClass().getName());
                                      }
                                      bytes = byteData.getData();
                              }
      
                      } catch (Exception ex) {
                              log.error("Error when trying to get the content of the pdf in bytes with the message #0", ex.getMessage());
                              ex.printStackTrace();
                      } finally {
                              emptyFacesContext.restore();
                      }
      
                      return bytes;
      
              }
              
      }
      
      
      16:06:15,373 ERROR [STDERR] java.lang.IllegalArgumentException: URI scheme is not "file"
      16:06:15,373 ERROR [STDERR]     at java.io.File.<init>(Unknown Source)
      16:06:15,373 ERROR [STDERR]     at org.jboss.seam.mock.MockServletContext.<init>(MockServletContext.java:59)
      16:06:15,373 ERROR [STDERR]     at org.jboss.seam.mock.MockExternalContext.<init>(MockExternalContext.java:50)
      16:06:15,373 ERROR [STDERR]     at com.pickupphoto.web.util.EmptyFacesContext.<init>(EmptyFacesContext.java:13)
      16:06:15,373 ERROR [STDERR]     at com.pickupphoto.web.handlers.CreateFreeBusinessCards.createPDFData(CreateFreeBusinessCards.java:95)
      16:06:15,374 ERROR [STDERR]     at com.pickupphoto.web.handlers.CreateFreeBusinessCards.onNewUserCreated(CreateFreeBusinessCards.java:79)
      16:06:15,374 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      16:06:15,374 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      16:06:15,374 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      16:06:15,374 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Unknown Source)
      16:06:15,374 ERROR [STDERR]     at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
      16:06:15,374 ERROR [STDERR]     at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
      16:06:15,374 ERROR [STDERR]     at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
      16:06:15,374 ERROR [STDERR]     at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
      16:06:15,374 ERROR [STDERR]     at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      16:06:15,374 ERROR [STDERR]     at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
      16:06:15,374 ERROR [STDERR]     at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      16:06:15,374 ERROR [STDERR]     at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
      16:06:15,374 ERROR [STDERR]     at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      16:06:15,375 ERROR [STDERR]     at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
      16:06:15,375 ERROR [STDERR]     at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
      16:06:15,375 ERROR [STDERR]     at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
      16:06:15,375 ERROR [STDERR]     at com.charles.web.handlers.CreateFreeBusinessCards_$$_javassist_seam_13.onNewUserCreated(CreateFreeBusinessCards_$$_javassist_seam_13.java)
      16:06:15,375 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      16:06:15,375 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      16:06:15,375 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      16:06:15,375 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Unknown Source)
      16:06:15,375 ERROR [STDERR]     at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
      16:06:15,375 ERROR [STDERR]     at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
      16:06:15,375 ERROR [STDERR]     at org.jboss.seam.Component.callComponentMethod(Component.java:2253)
      16:06:15,375 ERROR [STDERR]     at org.jboss.seam.core.Events.raiseEvent(Events.java:85)
      16:06:15,375 ERROR [STDERR]     at org.jboss.seam.async.AsynchronousEvent$1.process(AsynchronousEvent.java:33)
      16:06:15,375 ERROR [STDERR]     at org.jboss.seam.async.Asynchronous$ContextualAsynchronousRequest.run(Asynchronous.java:80)
      16:06:15,376 ERROR [STDERR]     at org.jboss.seam.async.AsynchronousEvent.execute(AsynchronousEvent.java:27)
      16:06:15,376 ERROR [STDERR]     at org.jboss.seam.async.ThreadPoolDispatcher$RunnableAsynchronous.run(ThreadPoolDispatcher.java:142)
      16:06:15,376 ERROR [STDERR]     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
      16:06:15,376 ERROR [STDERR]     at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
      16:06:15,376 ERROR [STDERR]     at java.util.concurrent.FutureTask.run(Unknown Source)
      16:06:15,376 ERROR [STDERR]     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
      16:06:15,376 ERROR [STDERR]     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
      16:06:15,376 ERROR [STDERR]     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
      16:06:15,376 ERROR [STDERR]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      16:06:15,376 ERROR [STDERR]     at java.lang.Thread.run(Unknown Source)
      




      I am using JBOSS 5.1, Seam 2.2 and Richfaces 3.3.3 Final. Thanks a lot for your help.


      Sincerely
      Charles.

        • 2. Re: Seam PDF byte data Problems
          Charles Akalugwu Newbie

          Hi! Thanks for your response. I followed the instructions on that thread, but I still get a null pointer exception in the call to:




          File webxml = new File(((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("/WEB-INF/web.xml"));
          






          The exception stacktrace is shown below:





          11:22:47,704 ERROR [STDERR] java.lang.NullPointerException
          11:22:47,705 ERROR [STDERR]      at com.pickupphoto.web.util.DibadeMockServletContext.<init>(DibadeMockServletContext.java:47)
          11:22:47,705 ERROR [STDERR]      at com.pickupphoto.web.util.EmptyFacesContext.<init>(EmptyFacesContext.java:13)
          11:22:47,705 ERROR [STDERR]      at com.pickupphoto.web.handlers.CreateFreeBusinessCards.createPDFData(CreateFreeBusinessCards.java:95)
          11:22:47,705 ERROR [STDERR]      at com.pickupphoto.web.handlers.CreateFreeBusinessCards.onNewUserCreated(CreateFreeBusinessCards.java:79)
          11:22:47,705 ERROR [STDERR]      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          11:22:47,705 ERROR [STDERR]      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          11:22:47,705 ERROR [STDERR]      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          11:22:47,705 ERROR [STDERR]      at java.lang.reflect.Method.invoke(Unknown Source)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
          11:22:47,706 ERROR [STDERR]      at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
          11:22:47,706 ERROR [STDERR]      at com.pickupphoto.web.handlers.CreateFreeBusinessCards_$$_javassist_seam_10.onNewUserCreated(CreateFreeBusinessCards_$$_javassist_seam_10.java)
          11:22:47,707 ERROR [STDERR]      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          11:22:47,707 ERROR [STDERR]      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          11:22:47,707 ERROR [STDERR]      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          11:22:47,707 ERROR [STDERR]      at java.lang.reflect.Method.invoke(Unknown Source)
          11:22:47,707 ERROR [STDERR]      at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
          11:22:47,707 ERROR [STDERR]      at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
          11:22:47,707 ERROR [STDERR]      at org.jboss.seam.Component.callComponentMethod(Component.java:2253)
          11:22:47,707 ERROR [STDERR]      at org.jboss.seam.core.Events.raiseEvent(Events.java:85)
          11:22:47,707 ERROR [STDERR]      at org.jboss.seam.async.AsynchronousEvent$1.process(AsynchronousEvent.java:33)
          11:22:47,707 ERROR [STDERR]      at org.jboss.seam.async.Asynchronous$ContextualAsynchronousRequest.run(Asynchronous.java:80)
          11:22:47,707 ERROR [STDERR]      at org.jboss.seam.async.AsynchronousEvent.execute(AsynchronousEvent.java:27)
          11:22:47,707 ERROR [STDERR]      at org.jboss.seam.async.ThreadPoolDispatcher$RunnableAsynchronous.run(ThreadPoolDispatcher.java:142)
          11:22:47,707 ERROR [STDERR]      at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
          11:22:47,708 ERROR [STDERR]      at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
          11:22:47,708 ERROR [STDERR]      at java.util.concurrent.FutureTask.run(Unknown Source)
          11:22:47,708 ERROR [STDERR]      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
          11:22:47,708 ERROR [STDERR]      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
          11:22:47,708 ERROR [STDERR]      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
          11:22:47,708 ERROR [STDERR]      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
          11:22:47,708 ERROR [STDERR]      at java.lang.Thread.run(Unknown Source)
          




          any ideas what the problem is? seems i'm getting null as my ServletContext.



          • 3. Re: Seam PDF byte data Problems
            Dmitry Shultz Newbie

            Hi,


            you are calling it from the concurrent to the request thread, so there is no ServletContext context...