title: Unable to show PDF in p:media generated from streamed content in Primefaces
body: <p>I'm trying to show inline PDF which is opened in new browser window. I have following scenario:  </p>
  
  <ol>
  <li>In some ActionListen which is called by ajax I generate PDF content, put data in session, and send Javascript to be executed (<code>window.open</code> to open new page to show PDF)</li>
  <li><p>On opened page I just have <code>p:media</code> tag inside <code>h:body</code> with value pointing to <code>StreamedContent</code>:</p>
  
  <p></p></li>
  </ol>
  
  <p>Now, on that page my PDF is not generated. In log I can see these two lines:</p>
  
  <pre><code>org.primefaces.application.PrimeResourceHandler handleResourceRequest
  SEVERE: Error in streaming dynamic resource. Expression cannot be null
  </code></pre>
  
  <p>I started to debug and find out a few things.  </p>
  
  <p>First, I added breakpoint to <code>@PostConstruct</code> method of my <code>RequestScoped</code> bean. What is interesting is that breakpoint is reached twice, and to my big surprise after that PDF is shown perfectly?!  </p>
  
  <p>After some debugging through <code>PrimeResourceHandler</code> I figure out that in some cases <code>ValueExpression</code> is not calculated, in fact it throws <code>NullPointerException</code>, and again while debugging I saw that two requests are sent, and second request fails because <code>dynamicContentId</code> is removed in first request, and second call to <code>handleResourceRequest</code> doesn't have sense.  </p>
  
  <p>Through Firebug I can see two requests, first which is good with PDF data, and second which is also with content-type application/pdf but empty, with size 0.  </p>
  
  <p>xhtml page:</p>
  
  <pre><code>&lt;html&gt;
    &lt;h:head&gt;&lt;/h:head&gt;
    &lt;h:body&gt;
      &lt;p:media value="#{reportBean.streamedContent}" player="pdf" width="500" height="500"/&gt;
    &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>backing bean:</p>
  
  <pre><code>@RequestScoped
  public class StampaListeBackingBean implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      private StreamedContent streamedContent;
  
      @PostConstruct
      public void init() {
          Map&lt;String, Object&gt; session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
          byte[] b = (byte[]) session.get("reportBytes");
          if (b != null) {
              streamedContent = new DefaultStreamedContent(new ByteArrayInputStream(b), "application/pdf");
          }
      }
  
      public StreamedContent getStreamedContent() {
          if (FacesContext.getCurrentInstance().getRenderResponse()) {
              return new DefaultStreamedContent();
          } else {
              return streamedContent;
          }
  }
  
      public void setStreamedContent(StreamedContent streamedContent) {
          this.streamedContent = streamedContent;
      }
  }
  </code></pre>
  
  <p>I need to understand why two requests are sent on page with <code>p:media</code> tag, and to figure out how to make this work. Backing bean is request scoped, it creates <code>StreamedContent</code> in <code>@PostConstruct</code> method, and has getter and setter for that field. Primefaces version is 3.4.2, with Mojarra 2.1.14.</p>
  
  <p><strong>ADDED:</strong></p>
  
  <p>It is easy to reproduce my problem. If code in <code>init</code> method is replaced with following:</p>
  
  <pre><code>FileInputStream fis = new FileInputStream(new File("C:\\samplexxx.pdf"));
  streamedContent = new DefaultStreamedContent(fis, "application/pdf");
  </code></pre>
  
  <p>problem can be reproduced.</p>