MultipartPortletRequestWrapper.java

  1. package org.andromda.cartridges.jsf.portlet.myfaces.tomahawk.support;

  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.UnsupportedEncodingException;
  7. import java.security.Principal;
  8. import java.util.Collections;
  9. import java.util.Enumeration;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. import java.util.Locale;
  14. import java.util.Map;
  15. import javax.portlet.ActionRequest;
  16. import javax.portlet.PortalContext;
  17. import javax.portlet.PortletMode;
  18. import javax.portlet.PortletPreferences;
  19. import javax.portlet.PortletSession;
  20. import javax.portlet.WindowState;
  21. import org.apache.commons.fileupload.FileItem;
  22. import org.apache.commons.fileupload.FileUploadBase;
  23. import org.apache.commons.fileupload.FileUploadException;
  24. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  25. import org.apache.commons.fileupload.portlet.PortletFileUpload;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.myfaces.webapp.filter.MultipartRequestWrapper;

  29. /**
  30.  * This class handles multipart/form-date request for Portlet. It will be called
  31.  * if the request is multipart/form-data.
  32.  *
  33.  * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
  34.  * @author Sylvain Vieujot
  35.  */
  36. public class MultipartPortletRequestWrapper
  37. implements ActionRequest, MultipartRequest
  38. {
  39.     private static Log log = LogFactory.getLog(MultipartPortletRequestWrapper.class);

  40.     private ActionRequest request = null;

  41.     private Map parametersMap = null;

  42.     private PortletFileUpload fileUpload = null;

  43.     private Map fileItems = null;

  44.     private final int maxSize;

  45.     private final int thresholdSize;

  46.     private final String repositoryPath;

  47.     /**
  48.      * @param request
  49.      * @param maxSize
  50.      * @param thresholdSize
  51.      * @param repositoryPath
  52.      */
  53.     public MultipartPortletRequestWrapper(
  54.         final ActionRequest request,
  55.         final int maxSize,
  56.         final int thresholdSize,
  57.         final String repositoryPath)
  58.     {
  59.         this.request = request;
  60.         this.maxSize = maxSize;
  61.         this.thresholdSize = thresholdSize;
  62.         this.repositoryPath = repositoryPath;
  63.     }

  64.     private void parseRequest()
  65.     {
  66.         final DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();

  67.         diskFileItemFactory.setSizeThreshold(thresholdSize);

  68.         if (repositoryPath != null && repositoryPath.trim().length() > 0)
  69.         {
  70.             diskFileItemFactory.setRepository(new File(repositoryPath));
  71.         }

  72.         fileUpload = new PortletFileUpload();
  73.         fileUpload.setFileItemFactory(diskFileItemFactory);
  74.         fileUpload.setSizeMax(maxSize);

  75.         final String charset = request.getCharacterEncoding();
  76.         fileUpload.setHeaderEncoding(charset);

  77.         List requestParameters = null;
  78.         try
  79.         {
  80.             requestParameters = fileUpload.parseRequest(request);
  81.         }
  82.         catch (final FileUploadBase.SizeLimitExceededException e)
  83.         {
  84.             // TODO: find a way to notify the user about the fact that the
  85.             // uploaded file exceeded size limit

  86.             if (MultipartPortletRequestWrapper.log.isInfoEnabled())
  87.             {
  88.                 MultipartPortletRequestWrapper.log.info("user tried to upload a file that exceeded file-size limitations.", e);
  89.             }

  90.             requestParameters = Collections.EMPTY_LIST;

  91.         }
  92.         catch (final FileUploadException fue)
  93.         {
  94.             MultipartPortletRequestWrapper.log.error("Exception while uploading file.", fue);
  95.             requestParameters = Collections.EMPTY_LIST;
  96.         }

  97.         parametersMap = new HashMap(requestParameters.size());
  98.         fileItems = new HashMap();

  99.         for (final Iterator iter = requestParameters.iterator(); iter.hasNext();)
  100.         {
  101.             final FileItem fileItem = (FileItem)iter.next();

  102.             if (fileItem.isFormField())
  103.             {
  104.                 final String name = fileItem.getFieldName();

  105.                 // The following code avoids commons-fileupload charset problem.
  106.                 // After fixing commons-fileupload, this code should be
  107.                 //
  108.                 // String value = fileItem.getString();
  109.                 //
  110.                 String value = null;
  111.                 if (charset == null)
  112.                 {
  113.                     value = fileItem.getString();
  114.                 }
  115.                 else
  116.                 {
  117.                     try
  118.                     {
  119.                         value = new String(fileItem.get(), charset);
  120.                     }
  121.                     catch (final UnsupportedEncodingException e)
  122.                     {
  123.                         value = fileItem.getString();
  124.                     }
  125.                 }

  126.                 addTextParameter(name, value);
  127.             }
  128.             else
  129.             { // fileItem is a File
  130.                 if (fileItem.getName() != null)
  131.                 {
  132.                     fileItems.put(fileItem.getFieldName(), fileItem);
  133.                 }
  134.             }
  135.         }

  136.         // Add the query string paramters
  137.         for (final Iterator it = request.getParameterMap().entrySet().iterator(); it.hasNext();)
  138.         {
  139.             final Map.Entry entry = (Map.Entry)it.next();
  140.             final String[] valuesArray = (String[])entry.getValue();
  141.             for (final String element : valuesArray)
  142.             {
  143.                 addTextParameter((String)entry.getKey(), element);
  144.             }
  145.         }
  146.     }

  147.     private void addTextParameter(final String name, final String value)
  148.     {
  149.         if (!parametersMap.containsKey(name))
  150.         {
  151.             final String[] valuesArray =
  152.             {
  153.                 value
  154.             };
  155.             parametersMap.put(name, valuesArray);
  156.         }
  157.         else
  158.         {
  159.             final String[] storedValues = (String[])parametersMap.get(name);
  160.             final int lengthSrc = storedValues.length;
  161.             final String[] valuesArray = new String[lengthSrc + 1];
  162.             System.arraycopy(storedValues, 0, valuesArray, 0, lengthSrc);
  163.             valuesArray[lengthSrc] = value;
  164.             parametersMap.put(name, valuesArray);
  165.         }
  166.     }

  167.     /**
  168.      * @see javax.portlet.PortletRequest#getParameterNames()
  169.      */
  170.     public Enumeration getParameterNames()
  171.     {
  172.         if (parametersMap == null)
  173.         {
  174.             parseRequest();
  175.         }

  176.         return Collections.enumeration(parametersMap.keySet());
  177.     }

  178.     /**
  179.      * @see javax.portlet.PortletRequest#getParameter(String)
  180.      */
  181.     public String getParameter(final String name)
  182.     {
  183.         if (parametersMap == null)
  184.         {
  185.             parseRequest();
  186.         }

  187.         final String[] values = (String[])parametersMap.get(name);
  188.         if (values == null)
  189.         {
  190.             return null;
  191.         }
  192.         return values[0];
  193.     }

  194.     /**
  195.      * @see javax.portlet.PortletRequest#getParameterValues(String)
  196.      */
  197.     public String[] getParameterValues(final String name)
  198.     {
  199.         if (parametersMap == null)
  200.         {
  201.             parseRequest();
  202.         }

  203.         return (String[])parametersMap.get(name);
  204.     }

  205.     /**
  206.      * @see javax.portlet.PortletRequest#getParameterMap()
  207.      */
  208.     public Map getParameterMap()
  209.     {
  210.         if (parametersMap == null)
  211.         {
  212.             parseRequest();
  213.         }

  214.         return parametersMap;
  215.     }

  216.     // Hook for the x:inputFileUpload tag.
  217.     /**
  218.      * @see org.andromda.cartridges.jsf.portlet.myfaces.tomahawk.support.MultipartRequest#getFileItem(String)
  219.      */
  220.     public FileItem getFileItem(final String fieldName)
  221.     {
  222.         if (fileItems == null)
  223.         {
  224.             parseRequest();
  225.         }

  226.         return (FileItem)fileItems.get(fieldName);
  227.     }

  228.     /**
  229.      * Not used internally by MyFaces, but provides a way to handle the uploaded
  230.      * files out of MyFaces.
  231.      * @return fileItems
  232.      */
  233.     public Map getFileItems()
  234.     {
  235.         if (fileItems == null)
  236.         {
  237.             parseRequest();
  238.         }

  239.         return fileItems;
  240.     }

  241.     /**
  242.      * @see javax.portlet.PortletRequest#getAttribute(String)
  243.      */
  244.     public Object getAttribute(final String arg0)
  245.     {
  246.         if (arg0.equals(MultipartRequestWrapper.UPLOADED_FILES_ATTRIBUTE))
  247.         {
  248.             return getFileItems();
  249.         }
  250.         return request.getAttribute(arg0);
  251.     }

  252.     /**
  253.      * @see javax.portlet.PortletRequest#getAttributeNames()
  254.      */
  255.     public Enumeration getAttributeNames()
  256.     {
  257.         return request.getAttributeNames();
  258.     }

  259.     /**
  260.      * @see javax.portlet.PortletRequest#getAuthType()
  261.      */
  262.     public String getAuthType()
  263.     {
  264.         return request.getAuthType();
  265.     }

  266.     /**
  267.      * @see javax.portlet.PortletRequest#getContextPath()
  268.      */
  269.     public String getContextPath()
  270.     {
  271.         return request.getContextPath();
  272.     }

  273.     /**
  274.      * @see javax.portlet.PortletRequest#getLocale()
  275.      */
  276.     public Locale getLocale()
  277.     {
  278.         return request.getLocale();
  279.     }

  280.     /**
  281.      * @see javax.portlet.PortletRequest#getLocales()
  282.      */
  283.     public Enumeration getLocales()
  284.     {
  285.         return request.getLocales();
  286.     }

  287.     /**
  288.      * @see javax.portlet.PortletRequest#getPortalContext()
  289.      */
  290.     public PortalContext getPortalContext()
  291.     {
  292.         return request.getPortalContext();
  293.     }

  294.     /**
  295.      * @see javax.portlet.PortletRequest#getPortletMode()
  296.      */
  297.     public PortletMode getPortletMode()
  298.     {
  299.         return request.getPortletMode();
  300.     }

  301.     /**
  302.      * @see javax.portlet.PortletRequest#getPortletSession()
  303.      */
  304.     public PortletSession getPortletSession()
  305.     {
  306.         return request.getPortletSession();
  307.     }

  308.     /**
  309.      * @see javax.portlet.PortletRequest#getPortletSession(boolean)
  310.      */
  311.     public PortletSession getPortletSession(final boolean arg0)
  312.     {
  313.         return request.getPortletSession(arg0);
  314.     }

  315.     /**
  316.      * @see javax.portlet.PortletRequest#getPreferences()
  317.      */
  318.     public PortletPreferences getPreferences()
  319.     {
  320.         return request.getPreferences();
  321.     }

  322.     /**
  323.      * @see javax.portlet.PortletRequest#getProperties(String)
  324.      */
  325.     public Enumeration getProperties(final String arg0)
  326.     {
  327.         return request.getProperties(arg0);
  328.     }

  329.     /**
  330.      * @see javax.portlet.PortletRequest#getProperty(String)
  331.      */
  332.     public String getProperty(final String arg0)
  333.     {
  334.         return request.getProperty(arg0);
  335.     }

  336.     /**
  337.      * @see javax.portlet.PortletRequest#getPropertyNames()
  338.      */
  339.     public Enumeration getPropertyNames()
  340.     {
  341.         return request.getPropertyNames();
  342.     }

  343.     /**
  344.      * @see javax.portlet.PortletRequest#getRemoteUser()
  345.      */
  346.     public String getRemoteUser()
  347.     {
  348.         return request.getRemoteUser();
  349.     }

  350.     /**
  351.      * @see javax.portlet.PortletRequest#getRequestedSessionId()
  352.      */
  353.     public String getRequestedSessionId()
  354.     {
  355.         return request.getRequestedSessionId();
  356.     }

  357.     /**
  358.      * @see javax.portlet.PortletRequest#getResponseContentType()
  359.      */
  360.     public String getResponseContentType()
  361.     {
  362.         return request.getResponseContentType();
  363.     }

  364.     /**
  365.      * @see javax.portlet.PortletRequest#getResponseContentTypes()
  366.      */
  367.     public Enumeration getResponseContentTypes()
  368.     {
  369.         return request.getResponseContentTypes();
  370.     }

  371.     /**
  372.      * @see javax.portlet.PortletRequest#getScheme()
  373.      */
  374.     public String getScheme()
  375.     {
  376.         return request.getScheme();
  377.     }

  378.     /**
  379.      * @see javax.portlet.PortletRequest#getServerName()
  380.      */
  381.     public String getServerName()
  382.     {
  383.         return request.getServerName();
  384.     }

  385.     /**
  386.      * @see javax.portlet.PortletRequest#getServerPort()
  387.      */
  388.     public int getServerPort()
  389.     {
  390.         return request.getServerPort();
  391.     }

  392.     /**
  393.      * @see javax.portlet.PortletRequest#getUserPrincipal()
  394.      */
  395.     public Principal getUserPrincipal()
  396.     {
  397.         return request.getUserPrincipal();
  398.     }

  399.     /**
  400.      * @see javax.portlet.PortletRequest#getWindowState()
  401.      */
  402.     public WindowState getWindowState()
  403.     {
  404.         return request.getWindowState();
  405.     }

  406.     /**
  407.      * @see javax.portlet.PortletRequest#isPortletModeAllowed(javax.portlet.PortletMode)
  408.      */
  409.     public boolean isPortletModeAllowed(final PortletMode arg0)
  410.     {
  411.         return request.isPortletModeAllowed(arg0);
  412.     }

  413.     /**
  414.      * @see javax.portlet.PortletRequest#isRequestedSessionIdValid()
  415.      */
  416.     public boolean isRequestedSessionIdValid()
  417.     {
  418.         return request.isRequestedSessionIdValid();
  419.     }

  420.     /**
  421.      * @see javax.portlet.PortletRequest#isSecure()
  422.      */
  423.     public boolean isSecure()
  424.     {
  425.         return request.isSecure();
  426.     }

  427.     /**
  428.      * @see javax.portlet.PortletRequest#isUserInRole(String)
  429.      */
  430.     public boolean isUserInRole(final String arg0)
  431.     {
  432.         return request.isUserInRole(arg0);
  433.     }

  434.     /**
  435.      * @see javax.portlet.PortletRequest#isWindowStateAllowed(javax.portlet.WindowState)
  436.      */
  437.     public boolean isWindowStateAllowed(final WindowState arg0)
  438.     {
  439.         return request.isWindowStateAllowed(arg0);
  440.     }

  441.     /**
  442.      * @see javax.portlet.PortletRequest#removeAttribute(String)
  443.      */
  444.     public void removeAttribute(final String arg0)
  445.     {
  446.         request.removeAttribute(arg0);
  447.     }

  448.     /**
  449.      * @see javax.portlet.PortletRequest#setAttribute(String,
  450.      *      Object)
  451.      */
  452.     public void setAttribute(final String arg0, final Object arg1)
  453.     {
  454.         request.setAttribute(arg0, arg1);
  455.     }

  456.     /**
  457.      * @see javax.portlet.ActionRequest#getCharacterEncoding()
  458.      */
  459.     public String getCharacterEncoding()
  460.     {
  461.         return request.getCharacterEncoding();
  462.     }

  463.     /**
  464.      * @see javax.portlet.ActionRequest#getContentLength()
  465.      */
  466.     public int getContentLength()
  467.     {
  468.         return request.getContentLength();
  469.     }

  470.     /**
  471.      * @see javax.portlet.ActionRequest#getContentType()
  472.      */
  473.     public String getContentType()
  474.     {
  475.         return request.getContentType();
  476.     }

  477.     /**
  478.      * @see javax.portlet.ActionRequest#getPortletInputStream()
  479.      */
  480.     public InputStream getPortletInputStream() throws IOException
  481.     {
  482.         return request.getPortletInputStream();
  483.     }

  484.     /**
  485.      * @see javax.portlet.ActionRequest#getReader()
  486.      */
  487.     public BufferedReader getReader() throws UnsupportedEncodingException, IOException
  488.     {
  489.         return request.getReader();
  490.     }

  491.     /**
  492.      * @see javax.portlet.ActionRequest#setCharacterEncoding(String)
  493.      */
  494.     public void setCharacterEncoding(final String arg0) throws UnsupportedEncodingException
  495.     {
  496.         request.setCharacterEncoding(arg0);
  497.     }
  498. }