[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/upload ...

Christian Bauer christian at hibernate.org
Tue Dec 18 23:29:30 EST 2007


  User: cbauer  
  Date: 07/12/18 23:29:30

  Added:       examples/wiki/src/main/org/jboss/seam/wiki/core/upload   
                        Uploader.java UploadTypes.java UploadType.java
  Log:
  Major rewrite of the most of the application
  
  Revision  Changes    Path
  1.1      date: 2007/12/19 04:29:30;  author: cbauer;  state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/upload/Uploader.java
  
  Index: Uploader.java
  ===================================================================
  package org.jboss.seam.wiki.core.upload;
  
  import net.sf.jmimemagic.Magic;
  import org.jboss.seam.ScopeType;
  import org.jboss.seam.annotations.In;
  import org.jboss.seam.annotations.Logger;
  import org.jboss.seam.annotations.Name;
  import org.jboss.seam.annotations.Scope;
  import org.jboss.seam.faces.FacesMessages;
  import org.jboss.seam.log.Log;
  import org.jboss.seam.wiki.core.model.WikiUpload;
  import org.jboss.seam.wiki.core.upload.handler.UploadHandler;
  
  import javax.faces.application.FacesMessage;
  import java.util.Map;
  import java.io.Serializable;
  
  /**
   * A handy conversation-scoped component we can use to bind a typical upload form to.
   * <p>
   * Call the <tt>uploadNewInstance()</tt> action and then access <tt>getUpload()</tt> in the same
   * conversation. Uses upload handlers to instantiate and fill the data into the right
   * <tt>WikiUpload</tt> subclass.
   * <p>
   * Call the <tt>uploadUpdateInstance(id)</tt> action to retrieve and fill the data into
   * an existing <tt>WikiUpload</tt> subclass instance.
   *
   * @author Christian Bauer
   */
  @Name("uploader")
  @Scope(ScopeType.CONVERSATION)
  public class Uploader implements Serializable {
  
      @Logger
      Log log;
  
      @In
      private FacesMessages facesMessages;
  
      @In
      Map<String, UploadType> uploadTypes;
  
      UploadHandler handler;
  
      WikiUpload upload;
  
      private String filename;
      private String contentType;
      private byte[] data;
      Long parentDirectoryId;
  
      public UploadHandler getUploadHandler() {
          return handler;
      }
  
      public WikiUpload getUpload() {
          return upload;
      }
  
      public Long getParentDirectoryId() {
          return parentDirectoryId;
      }
  
      public void setParentDirectoryId(Long parentDirectoryId) {
          this.parentDirectoryId = parentDirectoryId;
      }
  
      public String getFilename() {
          return filename;
      }
  
      public void setFilename(String filename) {
          this.filename = filename;
      }
  
      public String getContentType() {
          return contentType;
      }
  
      public void setContentType(String contentType) {
          this.contentType = contentType;
      }
  
      public byte[] getData() {
          return data;
      }
  
      public void setData(byte[] data) {
          this.data = data;
      }
  
      /**
       * Marshall form data into a new <tt>WikiUpload</tt> instance, pick
       * the right handler automatically based on the data (or browser, if magic fails) content type.
       *
       * @return String outcome of action, null or if successful, the simple classname of the created entity.
       */
      public String uploadNewInstance() {
          log.debug("uploading new instance");
          if (!validateData()) return null;
          resolveContentType();
          UploadType uploadType = uploadTypes.get(contentType);
          if (uploadType == null) {
              uploadType = uploadTypes.get(UploadTypes.GENERIC_UPLOAD_TYPE);
          }
          upload = uploadType.getUploadHandler().handleUpload(this);
          handler = uploadType.getUploadHandler();
          log.debug("uploaded: " + upload);
          return upload.getClass().getSimpleName();
      }
  
      /**
       * Marshall form data into existing <tt>WikiUpload</tt> instance, pick
       * the right handler automatically based on (possibly new) content type.
       *
       * @param instance The instance to be updated
       * @return String outcome of action, null or if successful, the simple classname of the updated entity.
       */
      public String uploadUpdateInstance(WikiUpload instance) {
          return uploadUpdateInstance(instance, false);
      }
  
      /**
       * Marshall form data into existing <tt>WikiUpload</tt> instance.
       * <p>
       * Optionally you can enforce that the same upload handler class should be used
       * as has been used for the existing <tt>WikiUpload</tt>. For example, if the
       * existing entity is a <tt>WikiUploadImage</tt>, its upload handler would be
       * <tt>WikiUploadImageHandler</tt>, based on the content type we stored along with
       * the entity. If the newly uploaded data/content type does not produce the same
       * handler class, a JSF message will be queued, <tt>getUpdate()</tt> will return null,
       * and no data will be marshalled.
       *
       * @param instance The instance to be updated
       * @param forceSameHandler Force the same handler
       * @return String outcome of action, null or if successful, the simple classname of the updated entity.
       */
      public String uploadUpdateInstance(WikiUpload instance, boolean forceSameHandler) {
          log.debug("uploading and updating existing instance: " + instance);
          this.upload = instance;
          if (!validateData()) return null;
          resolveContentType();
  
          UploadType newUploadType = uploadTypes.get(contentType);
          UploadHandler newupUploadHandler =
                  newUploadType != null
                  ? newUploadType.getUploadHandler()
                  : uploadTypes.get(UploadTypes.GENERIC_UPLOAD_TYPE).getUploadHandler();
  
          if (forceSameHandler) {
              log.debug("using same upload handler as for the original, based on content type: " + instance.getContentType());
              UploadHandler previousUploadHandler;
              UploadType previousUploadType = uploadTypes.get(instance.getContentType());
              previousUploadHandler =
                      previousUploadType != null
                      ? previousUploadType.getUploadHandler()
                      : uploadTypes.get(UploadTypes.GENERIC_UPLOAD_TYPE).getUploadHandler();
              if (!previousUploadHandler.getClass().equals(newupUploadHandler.getClass())) {
                  facesMessages.addFromResourceBundleOrDefault(
                      FacesMessage.SEVERITY_ERROR,
                      "lacewiki.msg.upload.HandlersDontMatch",
                      "Wrong file type uploaded, please try again with a different file."
                  );
                  upload = null;
                  return null;
              }
          }
  
          log.debug("using upload handler to marshall data: " + newupUploadHandler.getClass());
          upload = newupUploadHandler.handleUpload(this, upload);
          handler = newupUploadHandler;
          log.debug("uploaded: " + upload);
          return upload.getClass().getSimpleName();
      }
  
      public boolean hasData() {
          return data != null && data.length > 0;
      }
  
      public boolean validateData() {
          if (data == null || data.length == 0) {
              facesMessages.addFromResourceBundleOrDefault(
                  FacesMessage.SEVERITY_WARN,
                  "lacewiki.msg.upload.NoData",
                  "Please select a file to upload"
              );
              return false;
          }
          return true;
      }
  
      public void reset() {
          filename = null;
          contentType = null;
          data = null;
      }
  
      // Use mime magic to find the "real" content type - but if there is an exception
      // (which is expected because JMimeMagic is crap) - use the browser-supplied type.
      protected void resolveContentType() {
          String mimeType = null;
          try {
              mimeType = Magic.getMagicMatch(data).getMimeType();
          } catch (Exception ex) {}
          contentType = mimeType != null ? mimeType : contentType;
      }
  
  }
  
  
  
  1.1      date: 2007/12/19 04:29:30;  author: cbauer;  state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/upload/UploadTypes.java
  
  Index: UploadTypes.java
  ===================================================================
  package org.jboss.seam.wiki.core.upload;
  
  import org.jboss.seam.ScopeType;
  import org.jboss.seam.annotations.AutoCreate;
  import org.jboss.seam.annotations.Name;
  import org.jboss.seam.annotations.Scope;
  import org.jboss.seam.annotations.Unwrap;
  import org.jboss.seam.wiki.core.upload.handler.WikiUploadHandler;
  import org.jboss.seam.wiki.core.upload.handler.WikiUploadImageHandler;
  
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * Metadata map, from file mime type strings to file meta info such as icons, upload handler, etc.
   * <p>
   * This application-scoped map is often searched by key, which are mime type strings.
   *
   * @author Christian Bauer
   */
  @Name("uploadTypes")
  @Scope(ScopeType.APPLICATION)
  @AutoCreate
  public class UploadTypes {
  
      public static final String GENERIC_UPLOAD_TYPE = "generic";
  
      private Map<String, UploadType> uploadTypes = new HashMap<String, UploadType>() {
          {
              put("image/jpg",                    new UploadType("icon.fileimg.gif", new WikiUploadImageHandler() ));
              put("image/jpeg",                   new UploadType("icon.fileimg.gif", new WikiUploadImageHandler() ));
              put("image/png",                    new UploadType("icon.fileimg.gif", new WikiUploadImageHandler() ));
              put("image/gif",                    new UploadType("icon.fileimg.gif", new WikiUploadHandler() ));
              put("text/plain",                   new UploadType("icon.filetxt.gif", new WikiUploadHandler() ));
              put("application/pdf",              new UploadType("icon.filepdf.gif", new WikiUploadHandler() ));
              put("application/octet-stream",     new UploadType("icon.filegeneric.gif", new WikiUploadHandler() ));
              put(GENERIC_UPLOAD_TYPE,            new UploadType("icon.filegeneric.gif", new WikiUploadHandler() ));
          }
      };
  
      @Unwrap
      public Map<String, UploadType> getUploadTypes() {
          return uploadTypes;
      }
  
  }
  
  
  
  1.1      date: 2007/12/19 04:29:30;  author: cbauer;  state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/upload/UploadType.java
  
  Index: UploadType.java
  ===================================================================
  package org.jboss.seam.wiki.core.upload;
  
  import org.jboss.seam.wiki.core.upload.handler.UploadHandler;
  
  public class UploadType {
  
      private String displayIcon;
      private UploadHandler uploadHandler;
  
      public String getDisplayIcon() {
          return displayIcon;
      }
  
      public UploadHandler getUploadHandler() {
          return uploadHandler;
      }
  
      public UploadType(String displayIcon, UploadHandler uploadHandler) {
          this.displayIcon = displayIcon;
          this.uploadHandler = uploadHandler;
      }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list