You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by jw...@apache.org on 2012/10/08 20:38:40 UTC

svn commit: r1395711 - in /myfaces/trinidad/trunk: trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/ trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/

Author: jwaldman
Date: Mon Oct  8 18:38:39 2012
New Revision: 1395711

URL: http://svn.apache.org/viewvc?rev=1395711&view=rev
Log:
TRINIDAD-2316 Add support for adding a properties Map to UploadedFile 
Thanks to Kentaro Kinebuchi for the patch

Added:
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/ExtendedUploadedFile.java
Modified:
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/CompositeUploadedFileProcessorImpl.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/FileUploadConfiguratorImpl.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/UploadedFileImpl.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/UploadedFiles.java

Added: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/ExtendedUploadedFile.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/ExtendedUploadedFile.java?rev=1395711&view=auto
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/ExtendedUploadedFile.java (added)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/ExtendedUploadedFile.java Mon Oct  8 18:38:39 2012
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.trinidad.model;
+
+import java.io.Serializable;
+
+import java.util.Map;
+
+/**
+ * AbstractUploadedFile defines a single file that has been uploaded
+ * to the server. Defines the additional ability to return arbitrary
+ * properties for a file.
+ *
+ */
+public abstract class ExtendedUploadedFile implements UploadedFile, Serializable
+{
+  /**
+   * Returns the properties associated with the file.
+   */ 
+  public abstract Map<String, Object> getProperties();
+ 
+  static private final long serialVersionUID = 3794385484318348479L;
+}

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/CompositeUploadedFileProcessorImpl.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/CompositeUploadedFileProcessorImpl.java?rev=1395711&r1=1395710&r2=1395711&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/CompositeUploadedFileProcessorImpl.java (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/CompositeUploadedFileProcessorImpl.java Mon Oct  8 18:38:39 2012
@@ -37,6 +37,7 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletRequest;
 
 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
+import org.apache.myfaces.trinidad.model.ExtendedUploadedFile;
 import org.apache.myfaces.trinidad.model.UploadedFile;
 import org.apache.myfaces.trinidad.util.ClassLoaderUtils;
 import org.apache.myfaces.trinidad.webapp.UploadedFileProcessor;
@@ -130,7 +131,7 @@ public class CompositeUploadedFileProces
    * Wrapper around UploadedFileImpl to listen for dispose() call and dispatch to
    * other UploadedFileImpls returned by other chained processors
    */
-  private static class WrappedUploadedFileImpl implements UploadedFile, Serializable
+  private static class WrappedUploadedFileImpl extends ExtendedUploadedFile
   {
 
     public WrappedUploadedFileImpl(UploadedFile original, List<UploadedFile> files)
@@ -192,6 +193,13 @@ public class CompositeUploadedFileProces
     {
       return original.getOpaqueData();
     }
+    
+    public Map<String, Object> getProperties()
+    {
+      if (original instanceof ExtendedUploadedFile)
+        return ((ExtendedUploadedFile) original).getProperties();
+      return Collections.emptyMap();
+    }
 
     public InputStream getInputStream() throws IOException
     {

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/FileUploadConfiguratorImpl.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/FileUploadConfiguratorImpl.java?rev=1395711&r1=1395710&r2=1395711&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/FileUploadConfiguratorImpl.java (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/FileUploadConfiguratorImpl.java Mon Oct  8 18:38:39 2012
@@ -18,20 +18,15 @@
  */
 package org.apache.myfaces.trinidadinternal.config.upload;
 
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
 import java.io.SequenceInputStream;
 
-import java.io.Serializable;
-
 import java.lang.reflect.Proxy;
 
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -46,6 +41,7 @@ import javax.servlet.http.HttpServletReq
 import org.apache.myfaces.trinidad.config.Configurator;
 import org.apache.myfaces.trinidad.context.RequestContext;
 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
+import org.apache.myfaces.trinidad.model.ExtendedUploadedFile;
 import org.apache.myfaces.trinidad.model.UploadedFile;
 import org.apache.myfaces.trinidad.util.ClassLoaderUtils;
 import org.apache.myfaces.trinidad.util.ExternalContextUtils;
@@ -54,6 +50,7 @@ import org.apache.myfaces.trinidad.util.
 import org.apache.myfaces.trinidad.webapp.UploadedFileProcessor;
 import org.apache.myfaces.trinidadinternal.share.util.MultipartFormHandler;
 import org.apache.myfaces.trinidadinternal.share.util.MultipartFormItem;
+import org.apache.myfaces.trinidadinternal.ui.marshal.MarshalingService;
 
 /**
  * This configurator will handle the FileUploads for Trinidad.
@@ -158,7 +155,7 @@ public class FileUploadConfiguratorImpl 
           else if (item.getFilename().length() > 0)
           {
             // Upload a file
-            _doUploadFile(requestContext, externalContext, files, item);
+            _doUploadFile(requestContext, externalContext, files, item, parameters);
           }
         }
         if (parameters.containsKey(_MULTIPLE_UPLOAD_PARAM))
@@ -290,15 +287,20 @@ public class FileUploadConfiguratorImpl 
   {
     super.init(externalContext);
     //TODO initialize _maxAllowedBytes
+    
+    // Get the marshaling service
+    _marshalingService = _getMarshalingService();
   }
 
   private void _doUploadFile(
       final RequestContext   requestContext,
       final ExternalContext  externalContext,
       final UploadedFiles     files,
-      final MultipartFormItem item) throws IOException
+      final MultipartFormItem item,
+      final Map<String, String[]> parameters) throws IOException
   {
-    String filename = item.getFilename();              
+    String filename = item.getFilename();
+    Map<String, Object> properties = _getPropertiesMapFromParameters(parameters, item.getName(), filename);
     String chunkFilename = externalContext.getRequestHeaderMap().get(_MULTIPLE_UPLOAD_CHUNK_FILENAME_PARAM);
     if (chunkFilename != null)
     {
@@ -309,7 +311,7 @@ public class FileUploadConfiguratorImpl 
       chunkFilename = chunkFilename + ".part" + chunkNum;
       filename = chunkFilename;
     }
-    final UploadedFile temp = new TempUploadedFile(filename, item);
+    final UploadedFile temp = new TempUploadedFile(filename, item, properties);
     Map<String, Object> sessionMap = externalContext.getSessionMap();
     Map<String, Object> requestMap = externalContext.getRequestMap();
     
@@ -412,6 +414,51 @@ public class FileUploadConfiguratorImpl 
     }
   }
   
+  static private Map<String, Object> _getPropertiesMapFromParameters(Map<String, String[]> parameters, String name, String filename)
+  {
+    Map<String, Object> properties = new HashMap<String, Object>();
+    StringBuilder propertiesParamBuilder = new StringBuilder(_MULTIPLE_UPLOAD_FILE_PROPERTY_PARAM);
+    propertiesParamBuilder.append(":");
+    propertiesParamBuilder.append(name);
+    propertiesParamBuilder.append(":");
+    propertiesParamBuilder.append(filename);
+    String propertiesParam = propertiesParamBuilder.toString();
+    if (parameters.get(propertiesParam) == null || parameters.get(propertiesParam).length == 0)
+      return Collections.emptyMap();
+    
+    String marshaledProperties = parameters.get(propertiesParam)[0];
+
+    if (marshaledProperties != null)
+    {
+      if (_marshalingService != null)
+      {
+        Object propertiesList = _marshalingService.unmarshal(marshaledProperties);
+        if (propertiesList instanceof List)
+        {
+          for (int i = 0; i < ((List) propertiesList).size(); i = i + 2)
+          {
+            String propertyName = (String) ((List) propertiesList).get(i);
+            Object propertyValue = ((List) propertiesList).get(i + 1);
+            properties.put(propertyName, propertyValue);
+          }
+        }
+        return properties;
+      }
+    }
+    return Collections.emptyMap();
+  }
+  
+  static private MarshalingService _getMarshalingService()
+  {
+    MarshalingService service = null;
+    List<MarshalingService> marshalingServices = ClassLoaderUtils.getServices(MarshalingService.class.getName());
+    if (marshalingServices != null && !marshalingServices.isEmpty())
+    {
+      service = marshalingServices.get(0);
+    }
+    return service;
+  }
+  
   //This will ensure the property is removed on the next request
   @ExcludeFromManagedRequestScope
   static private class AppliedClass
@@ -419,12 +466,15 @@ public class FileUploadConfiguratorImpl 
     static public final AppliedClass APPLIED = new AppliedClass();
   }
 
-  static private class TempUploadedFile implements UploadedFile
+  static private final class TempUploadedFile extends ExtendedUploadedFile
   {
-    public TempUploadedFile(String filename, MultipartFormItem item)
+    public TempUploadedFile(String filename, MultipartFormItem item, Map<String, Object> properties)
     {
       _filename = filename;
       _item = item;
+      if (properties != null)
+        _properties = properties;
+      assert(properties == null);
       assert(item.getValue() == null);
     }
 
@@ -448,6 +498,12 @@ public class FileUploadConfiguratorImpl 
     {
       return null;
     }
+    
+    @Override
+    public Map<String, Object> getProperties()
+    {
+      return _properties;
+    }
 
     public InputStream getInputStream() throws IOException
     {
@@ -461,8 +517,9 @@ public class FileUploadConfiguratorImpl 
 
     private MultipartFormItem _item;
     private String _filename = null;
+    private Map<String, Object> _properties = new HashMap<String, Object>();
   }
-  static private class ChunkedUploadedFile implements UploadedFile, Serializable
+  static private class ChunkedUploadedFile extends ExtendedUploadedFile
   {
     private List<UploadedFile> _uploadedFileChunkList = null;
     private String _filename = null;
@@ -514,6 +571,18 @@ public class FileUploadConfiguratorImpl 
       }
       return null;
     }
+    
+    @Override
+    public Map<String, Object> getProperties()
+    {
+      if (_uploadedFileChunkList.size() > 0)
+      {
+        UploadedFile file = _uploadedFileChunkList.get(0);
+        if (file instanceof ExtendedUploadedFile)
+          return ((ExtendedUploadedFile) file).getProperties();
+      }
+      return Collections.emptyMap();
+    }
 
     public InputStream getInputStream() throws IOException
     {
@@ -544,10 +613,12 @@ public class FileUploadConfiguratorImpl 
   static private final String _PARAMS = FileUploadConfiguratorImpl.class.getName()+".PARAMS";
   static private final boolean _ENHANCED_PORTLET_SUPPORTED = ExternalContextUtils.isRequestTypeSupported(RequestType.RESOURCE);
   static private final String _MULTIPLE_UPLOAD_PARAM = "org.apache.myfaces.trinidad.UploadedFiles";
+  static private final String _MULTIPLE_UPLOAD_FILE_PROPERTY_PARAM = "org.apache.myfaces.trinidad.UploadedFiles.FileProperty";
   static private final String _MULTIPLE_UPLOAD_CHUNK_NUM_PARAM = "X-Trinidad-chunkNum";
   static private final String _MULTIPLE_UPLOAD_CHUNK_COUNT_PARAM = "X-Trinidad-chunkCount";
   static private final String _MULTIPLE_UPLOAD_CHUNK_FILENAME_PARAM = "X-Trinidad-chunkFilename";
   static private final String _UPLOADED_CHUNK_FILES_LIST_KEY = "org.apache.myfaces.trinidadinternal.webapp.UploadedFiles.ChunkList";
+  static private volatile MarshalingService _marshalingService = null;
   
   private long _maxAllowedBytes = 1L << 27;
 }

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/UploadedFileImpl.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/UploadedFileImpl.java?rev=1395711&r1=1395710&r2=1395711&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/UploadedFileImpl.java (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/UploadedFileImpl.java Mon Oct  8 18:38:39 2012
@@ -29,8 +29,13 @@ import java.io.OutputStream;
 import java.io.Serializable;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 
+import java.util.Map;
+
+import org.apache.myfaces.trinidad.model.ExtendedUploadedFile;
 import org.apache.myfaces.trinidad.model.UploadedFile;
 
 /**
@@ -39,7 +44,7 @@ import org.apache.myfaces.trinidad.model
  *
  * @version $Name:  $ ($Revision: adfrt/faces/adf-faces-impl/src/main/java/oracle/adfinternal/view/faces/webapp/UploadedFileImpl.java#0 $) $Date: 10-nov-2005.18:49:03 $
  */
-public class UploadedFileImpl implements UploadedFile, Serializable
+public class UploadedFileImpl extends ExtendedUploadedFile
 {
   UploadedFileImpl()
   {
@@ -73,6 +78,13 @@ public class UploadedFileImpl implements
   {
     return null;
   }
+  
+
+  @Override
+  public Map<String, Object> getProperties()
+  {
+    return _properties;
+  }
 
   /**
    * Returns an InputStream that can be used to read the file.
@@ -165,6 +177,8 @@ public class UploadedFileImpl implements
   {
     _filename    = file.getFilename();
     _contentType = file.getContentType();
+    if (file instanceof ExtendedUploadedFile)
+      _properties = ((ExtendedUploadedFile) file).getProperties();
 
     InputStream in = file.getInputStream();
 
@@ -454,6 +468,7 @@ public class UploadedFileImpl implements
   private String     _filename;
   private String     _tempFilename;
   private String     _contentType;
+  private Map<String, Object> _properties = new HashMap<String, Object>();
 
   // Total length fo the content, whether in memory or on disk
   transient private long       _length;
@@ -473,5 +488,5 @@ public class UploadedFileImpl implements
   // use a larger buffer.
   static private final int _MEMORY_BUFFER_SIZE = 2048;
   static private final int _DISK_BUFFER_SIZE   = 8192;
-  static private final long serialVersionUID = 1592383482116327478L;
+  static private final long serialVersionUID = 3895593282556327786L;
 }

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/UploadedFiles.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/UploadedFiles.java?rev=1395711&r1=1395710&r2=1395711&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/UploadedFiles.java (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/UploadedFiles.java Mon Oct  8 18:38:39 2012
@@ -42,6 +42,7 @@ import org.apache.myfaces.trinidad.conte
 import org.apache.myfaces.trinidad.context.Window;
 import org.apache.myfaces.trinidad.context.WindowManager;
 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
+import org.apache.myfaces.trinidad.model.ExtendedUploadedFile;
 import org.apache.myfaces.trinidad.model.UploadedFile;
 import org.apache.myfaces.trinidadinternal.share.util.CaboHttpUtils;
 
@@ -290,7 +291,12 @@ final public class UploadedFiles impleme
     {
       files = new ArrayList<UploadedFile>();
     }
-    files.add(new FixFilename(file, _characterEncoding));
+    UploadedFile fixFilename = null;
+    if (file instanceof ExtendedUploadedFile)
+      fixFilename = new FixFilenameProperties((ExtendedUploadedFile) file, _characterEncoding);
+    else
+      fixFilename = new FixFilename(file, _characterEncoding);
+    files.add(fixFilename);
     _map.put(name, files);
   }
 
@@ -382,7 +388,7 @@ final public class UploadedFiles impleme
   static public class FixFilename implements UploadedFile, Serializable
   {
     /**
-     *
+     * FixFilename provides the ability to specify an encoding for the filename
      */
     private static final long serialVersionUID = -8586594511769079566L;
 
@@ -399,23 +405,25 @@ final public class UploadedFiles impleme
 
     public String getFilename()
     {
-      String filename = _file.getFilename();
-      if (_encoding == null)
-        return filename;
-
-      try
-      {
-        return CaboHttpUtils.decodeRequestParameter(filename,
-                                                    _encoding,
-                                                    null);
-      }
-      catch (UnsupportedEncodingException uee)
+      if (_fixFilename == null)
       {
-        // Should never happen, since the encoding should have
-        // already been vetted in UploadedRequestWrapper
-        assert false;
-        return filename;
+        String filename = _file.getFilename();
+        if (_encoding == null)
+          return filename;
+
+        try
+        {
+          _fixFilename = CaboHttpUtils.decodeRequestParameter(filename, _encoding, null);
+        }
+        catch (UnsupportedEncodingException uee)
+        {
+          // Should never happen, since the encoding should have
+          // already been vetted in UploadedRequestWrapper
+          assert false;
+          _fixFilename = filename;
+        }
       }
+      return _fixFilename;
     }
 
     public String getContentType()
@@ -446,5 +454,63 @@ final public class UploadedFiles impleme
 
     private UploadedFile _file;
     private String       _encoding;
+    private String       _fixFilename = null;
+  }
+  
+  static public class FixFilenameProperties extends ExtendedUploadedFile
+  {
+    /**
+     * FixFilename provides the ability to specify an encoding for the filename with properties
+     */
+    private static final long serialVersionUID = -7477694533769079788L;
+
+    public FixFilenameProperties()
+    {
+      // For serialization
+    }
+
+    public FixFilenameProperties(final ExtendedUploadedFile file, final String encoding)
+    {
+      _original = file;
+      _file = new FixFilename(file, encoding);
+    }
+
+    public String getFilename()
+    {
+      return _file.getFilename();
+    }
+
+    public String getContentType()
+    {
+      return _file.getContentType();
+    }
+
+    public long getLength()
+    {
+      return _file.getLength();
+    }
+
+    public Object getOpaqueData()
+    {
+      return _file.getOpaqueData();
+    }
+
+    public InputStream getInputStream() throws IOException
+    {
+      return _file.getInputStream();
+    }
+
+    public Map<String, Object> getProperties()
+    {
+      return _original.getProperties();
+    }
+    
+    public void dispose()
+    {
+      _file.dispose();
+    }
+
+    private FixFilename  _file;
+    private ExtendedUploadedFile _original;
   }
 }