title: PrimeFaces p:media not working with StreamedContent in a @ViewScoped bean
body: <p>I have an issue related to Primefaces4 meida type rendering pdf file in browser. I have successfully tried the example in showcase from primefaces website. Now I want to get a new feature, which offers an tree structure with document nodes in the left panel. User could select one document to display it in the center panel. That means it generates pdf media field in backbean once user selects on one document on the tree. </p>
  
  <p>related code is shown below:</p>
  
  <p>backbean:</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class DocumentsBean implements Serializable {
  
  private static final long serialVersionUID = 3560539268513760978L;
  private TreeNode root;
  private String url;
  private TreeNode selectedNode; 
  private StreamedContent media;
  
  public DocumentsBean() {
      root = new DefaultTreeNode("Root");
  }
  
  public TreeNode getRoot() {
      return root;
  }
  
  public TreeNode getSelectedNode() {  
      return selectedNode;  
  }  
  
  public void setSelectedNode(TreeNode selectedNode) {  
      this.selectedNode = selectedNode;  
  }  
  
  public void onNodeSelect(NodeSelectEvent event) {  
      File file = (File) this.selectedNode.getData();
      generatePDF(file);
  }
  
  public String getUrl() {
      return url;
  }
  
  public void setUrl(String url) {
      this.url = url;
  }
  
  public void explore() {
      root = new DefaultTreeNode(new File(this.url), null);
      constructDir(root);
  }
  
  /**
   * construct directory and its sub files.
   * @param parent
   */
  private void constructDir(TreeNode parent) {
      File file = (File) parent.getData();
      File[] files = file.listFiles();
      for (File f: files) {
          if (f.isFile()) {
              new DefaultTreeNode("document", f, parent);
          } else {
              TreeNode subParent = new DefaultTreeNode(f, parent);
              constructDir(subParent);
          }
      }
  
  }
  
  private void generatePDF(File file) {
      PDFGenerator generator = new PDFGenerator(file);
      File pdf = generator.transformToPDF();
  
      if (pdf != null) {
          InputStream stream = null;
          try {
              stream = new FileInputStream(pdf);
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
          media = new DefaultStreamedContent(stream, "application/pdf");
      }
  
  }
  
  public StreamedContent getMedia() {
      return media;
  }
  
  }
  </code></pre>
  
  <p>part of my view:</p>
  
  <pre><code>&lt;p:layoutUnit position="west" size="300" header="Directory Content" resizable="false" collapsible="true"&gt;
              &lt;h:form id="docTree_form"&gt;
                  &lt;p:growl id="messages" showDetail="true" /&gt; 
                  &lt;p:tree id="docTree" value="#{documentsBean.root}" var="node" animate="true" selectionMode="single" selection="#{documentsBean.selectedNode}" dynamic="true" cache="true"&gt;
  
                      &lt;p:ajax event="select" update=":pdf_form:media" listener="#{documentsBean.onNodeSelect}" /&gt;
                      &lt;p:treeNode expandedIcon="ui-icon-folder-open" collapsedIcon="ui-icon-folder-collapsed"&gt;
                          &lt;h:outputText value="#{node.name}" /&gt;
                      &lt;/p:treeNode&gt;
  
                      &lt;p:treeNode type="document" icon="ui-icon-document"&gt;
                          &lt;h:outputText value="#{node.name}" /&gt;
                      &lt;/p:treeNode&gt;
                  &lt;/p:tree&gt;
              &lt;/h:form&gt;
          &lt;/p:layoutUnit&gt;
  
          &lt;p:layoutUnit position="center" header="Center" resizable="true"&gt;
              &lt;h:form id="pdf_form"&gt;
                  &lt;p:media id="media" value="#{documentsBean.media}"     player="pdf" width="100%" height="700px"&gt;  
                      Your browser can't display pdf 
                  &lt;/p:media&gt;
              &lt;/h:form&gt;
          &lt;/p:layoutUnit&gt;
  </code></pre>
  
  <p>When I run this code, there is no error or exception. However, there is no PDF viewer generated in Firefox. Really strange !</p>
  
  <p>a follow up question based on BalusC comments:</p>
  
  <p>I got this exception when my app is running up:</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path     [/DocumentViewer_JSF] threw exception
  java.lang.NullPointerException
  at     org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:114)
  </code></pre>
  
  <p>I found that this line causing this exception:</p>
  
  <pre><code>return new DefaultStreamedContent();
  </code></pre>
  
  <p>If I create a real pdf file, the exception is gone. But I really don't want a pdf file displayed if user has not chosen the file.</p>