2 Replies Latest reply on Jun 15, 2010 7:08 AM by Ilya Shaikovsky

    How preview html pages using richfaces?

    Elena Veretilo Newbie

      Hi guys!

       

      I need your help once more!

       

      I have the task: I need to preview the html pages in .xhtml page. My html pages use flash charts, so I need to be able to show flash with other content. Also this previews must be smaller than the original page.

      I try to do it with <a4j:mediaOutput/>: I could see the text, but no flash and image content. Could somebody say - where is the problem? And maybe I need to do it in another way?

       

      Here is some code:

       

      package no.sfront.clientadmin;
      
      /**
      * @author Elena Veretilo
      *
      */
      
      import java.io.IOException;
      import java.io.OutputStream;
      import java.util.ArrayList;
      
      import javax.faces.context.FacesContext;
      
      import org.richfaces.event.UploadEvent;
      import org.richfaces.model.UploadItem;
      
      public class HtmlFileUpload
      {
       private ArrayList<HtmlFile> files = new ArrayList<HtmlFile>();
       private int uploadsAvailable = 1;
       private boolean autoUpload = true;
       private boolean useFlash = true;
      
       
       public static HtmlFileUpload getInstance() {
       FacesContext facesContext = FacesContext.getCurrentInstance();
      
       return (HtmlFileUpload) facesContext.getApplication().getELResolver()
       .getValue(facesContext.getELContext(), null, "htmlFileUpload");
       }
      
       public HtmlFileUpload() {
       }
      
       public void paint(OutputStream stream, Object object) throws IOException {
       stream.write(getFiles().get((Integer)object).getData());
       }
       
       public void listener(UploadEvent event) throws Exception{
       UploadItem item = event.getUploadItem();
       HtmlFile file = new HtmlFile();
       byte[] array = item.getData();
       file.setLength(array.length);
       file.setName(item.getFileName());
       file.setData(item.getData());
       files.add(file);
       uploadsAvailable--;
       }  
      
       public String clearUploadData() {
       files.clear();
       setUploadsAvailable(1);
       return null;
       }
       
       public int getSize() {
       if (getFiles().size() > 0)
       return getFiles().size();
       else 
       return 0;
       }
       
       public long getTimeStamp(){
       return System.currentTimeMillis();
       }
       
       public ArrayList<HtmlFile> getFiles() {
       return files;
       }
      
       public void setFiles(ArrayList<HtmlFile> files) { 
       this.files = files;
       }
      
       public int getUploadsAvailable() {
       return uploadsAvailable;
       }
      
       public void setUploadsAvailable(int uploadsAvailable) {
       this.uploadsAvailable = uploadsAvailable;
       }
      
       public boolean isAutoUpload() {
       return autoUpload;
       }
      
       public void setAutoUpload(boolean autoUpload) {
       this.autoUpload = autoUpload;
       }
      
       public boolean isUseFlash() {
       return useFlash;
       }
      
       public void setUseFlash(boolean useFlash) {
       this.useFlash = useFlash;
       }
      
      }
      
      
      

       

       

       

          public void setName(String name) {
              Name = name;
              int extDot = name.lastIndexOf('.');
              if(extDot > 0){
                  String extension = name.substring(extDot +1);
                  if("html".equals(extension)){ 
                      mime = "text/html";//"multipart/related";//"text/html";
                  } else if("mht".equals(extension)){
                      mime = "text/html";//"multipart/related";//"text/html";
                  } else if("xhtml".equals(extension)){
                      mime = "application/xhtml+xml";
                  } else {
                      mime = "application/x-shockwave-flash";
                  }
              }
          } 
      

       

       

       

      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                      xmlns:ui="http://java.sun.com/jsf/facelets"
                      xmlns:a4j="http://richfaces.org/a4j"
                      xmlns:rich="http://richfaces.org/rich"
                      xmlns:h="http://java.sun.com/jsf/html"
                      xmlns:f="http://java.sun.com/jsf/core"
                      xmlns:fc="http://www.fusioncharts.com"
                      xmlns:t="http://myfaces.apache.org/tomahawk"
                      template="template.xhtml">
           <ui:define name="title">Client Admin Page</ui:define>
           <ui:define name="content">
           
                <style>
                     .top {
                         vertical-align: top;
                         
                     }
                     .info {
                         height: 100%;
                         overflow: auto;
                     }
                </style>
      
               <h:form>
                   <h:panelGrid columns="1" rows="2" columnClasses="top,top">
                       <rich:fileUpload fileUploadListener="#{htmlFileUpload.listener}"
                           maxFilesQuantity="#{htmlFileUpload.uploadsAvailable}"
                           id="upload"
                           immediateUpload="#{htmlFileUpload.autoUpload}"
                           acceptedTypes="html,xhtml,mht" allowFlash="#{htmlFileUpload.useFlash}"
                           listHeight="70px">
                           <a4j:support event="onuploadcomplete" reRender="info" />
                           <a4j:support event="onclear" reRender="upload,info" action="#{htmlFileUpload.clearUploadData}"/>
                       </rich:fileUpload>
                       <h:panelGroup id="info">
                           <rich:panel bodyClass="info">
                               <f:facet name="header">
                                   <h:outputText value="Uploaded Files Info" />
                               </f:facet>
                               <h:outputText value="No files currently uploaded"
                                   rendered="#{htmlFileUpload.size==0}" />
                               <rich:dataGrid columns="1" value="#{htmlFileUpload.files}"
                                   var="file" rowKeyVar="row">
                                   <rich:panel>
                                       <h:panelGrid columns="1">
                                           <a4j:mediaOutput element="object" mimeType="#{file.mime}"
                                               createContent="#{htmlFileUpload.paint}" 
                                               value="#{row}" cacheable="true">
                                               <f:param value="#{htmlFileUpload.timeStamp}" name="time"/>  
                                           </a4j:mediaOutput>
                                       </h:panelGrid>
                                   </rich:panel>
                               </rich:dataGrid>
                           </rich:panel>
                           <rich:spacer height="2"/>
                           <br/>
                           <a4j:commandButton action="#{htmlFileUpload.clearUploadData}"
                               reRender="info, upload" value="Clear Uploaded Data"
                               rendered="#{htmlFileUpload.size>0}" />
                       </h:panelGroup>
                   </h:panelGrid>
               </h:form>
               
           
           </ui:define>
           <ui:define name="return" />
      </ui:composition>
      

       

       

      When I use mime type "text/html" - I could see only text and for other content (images, swf ) I get next exceptions:

       

      org.ajax4jsf.resource.ResourceNotFoundException: Resource not registered : org.ajax4jsf.resource.UserResource/c/n/-1082243251/FusionCharts/AngularGauge.swf

       

      javax.faces.FacesException: Error decode resource data
      at org.ajax4jsf.resource.ResourceBuilderImpl.decrypt(ResourceBuilderImpl.java:627)

       

      SEVERE: Servlet.service() for servlet Faces Servlet threw exception
      org.ajax4jsf.resource.ResourceNotFoundException: Resource not registered : org.ajax4jsf.resource.UserResource/c/n/-1082243251/css/stylesheet.css

       

       

      Message was edited by: Elena Veretilo