You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Michael McGrady <mi...@michaelmcgrady.com> on 2004/10/24 00:48:06 UTC

Make DefaultFileItem in commons upload a real default instead of a mandatory "default"?

I am suggesting that the commons upload code in relation to FileItem be 
changed so that the DefaultFileItem is not really "MandatoryFileItem" 
which (as it is) requires the developer to subclass the default 
implementation.  Since the intended architecture seems to be one of a 
provider framework for different file items, I suggest something like 
the following standard implementation.  This would require few tweaks to 
the code and would make implementations of FileItem flexible and easy.  
I include Struts because it is built on top of commons.


    public abstract class FileItem
        implements Serializable {
      public static final int    DEFAULT_FORM_FIELD_SIZE_THRESHOLD = 1024;
      private static      Map    implementations                   = null;

      private static synchronized void init() {
        if(implementations == null) {
          implementations = new HashMap();

          // load implementation class names and keys
          // from a Properties file, translate names
          // into Class objects using Class.forName and
          // store mappings.
        }
      }

      public static FileItem getInstance(String  key,
                                         String  fieldName,
                                         String  contentType,
                                         boolean isFormField,
                                         String  fileName,
                                         File    repository) {

        init();
        Class fileItem = (Class)implementations.get(key);

        if(fileItem == null) {
          if (isFormField) {
            return new DefaultFileItem(fieldName,
    contentType,isFormField, fileName,
    DEFAULT_FORM_FIELD_SIZE_THRESHOLD,repository);
          } else {
            return new DefaultFileItem(fieldName, contentType,
    isFormField, fileName, repository);
          }
        }

        FileItem fi = null;

        try {
          fi = (FileItem)fileItem.newInstance();
        } catch (InstantiationException ie) {
          // Fill in as desired
        } catch (IllegalAccessException iae) {
          // Fill in as desired
        }

        fi.setContentType(contentType);
        fi.setFieldName(fieldName);
        fi.setName(fileName);
        fi.setFormField(isFormField);

        return fi;
      }

      public abstract byte[]       get();
      public abstract String       getContentType();
      public abstract String       getFieldName();
      public abstract InputStream  getInputStream() throws IOException;
      public abstract String       getName();
      public abstract OutputStream getOutputStream() throws IOException;
      public abstract long         getSize();
      public abstract String       getString();
      public abstract String       getString(String encoding) throws
    UnsupportedEncodingException;

      public abstract void         setContentType(String name);
      public abstract void         setFieldName(String name);
      public abstract void         setName(String name);
      public abstract void         setFormField(boolean state);
      public abstract void         delete();
      public abstract boolean      isFormField();
      public abstract boolean      isInMemory();
      public abstract void         write(File file) throws Exception;
    }

Thanks,

Michael McGrady


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org