Author: sshyrkov Date: Tue Jun 12 17:20:11 2007 New Revision: 17550 URL: https://svndev.jahia.net/websvn/listing.php?sc=3D1&rev=3D17550&repname= =3Djahia Log: CONE-28: How to handle multipart data. =
http://www.jahia.net/jira/browse/CONE-28 Resolved: a special handler was added in order to support FormFile fields i= n Struts forms Added: trunk/core/src/java/org/jahia/tools/files/JahiaMultipartRequestHandler.= java Modified: trunk/core/src/java/org/jahia/tools/files/FileUpload.java trunk/core/src/webapp/WEB-INF/etc/struts/struts-config.xml Modified: trunk/core/src/java/org/jahia/tools/files/FileUpload.java URL: https://svndev.jahia.net/websvn/diff.php?path=3D/trunk/core/src/java/o= rg/jahia/tools/files/FileUpload.java&rev=3D17550&repname=3Djahia =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- trunk/core/src/java/org/jahia/tools/files/FileUpload.java (original) +++ trunk/core/src/java/org/jahia/tools/files/FileUpload.java Tue Jun 12 17= :20:11 2007 @@ -49,6 +49,7 @@ = private Map params; private Map files; + private Map filesByFieldName; = private HttpServletRequest req; = @@ -110,6 +111,7 @@ = params =3D new HashMap(); files =3D new HashMap(); + filesByFieldName =3D new HashMap(); = parseQueryString(); = @@ -140,6 +142,7 @@ } else { if (item.getSize() > 0) { files.put(item.getStoreLocation().getName(), i= tem); + filesByFieldName.put(item.getFieldName(), item= ); } } = @@ -200,6 +203,17 @@ return ((DiskFileItem) files.get(n)).getStoreLocation(); } = + /** + * Returns the map of all [EMAIL PROTECTED] FileUpload} objects keyed by the form fi= eld + * name. + * = + * @return the map of all [EMAIL PROTECTED] FileUpload} objects keyed by the form fi= eld + * name + */ + public Map getFileItems() { + return filesByFieldName; + } + public String getFileSystemName(String n) { return ((DiskFileItem) files.get(n)).getName(); } Added: trunk/core/src/java/org/jahia/tools/files/JahiaMultipartRequestHandl= er.java URL: https://svndev.jahia.net/websvn/filedetails.php?path=3D/trunk/core/src= /java/org/jahia/tools/files/JahiaMultipartRequestHandler.java&rev=3D17550&r= epname=3Djahia =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- trunk/core/src/java/org/jahia/tools/files/JahiaMultipartRequestHandler.= java (added) +++ trunk/core/src/java/org/jahia/tools/files/JahiaMultipartRequestHandler.= java Tue Jun 12 17:20:11 2007 @@ -0,0 +1,316 @@ +/* + * Copyright 2002-2006 Jahia Ltd + * + * Licensed under the JAHIA COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (J= CDDL), = + * Version 1.0 (the "License"), or (at your option) any later version; you= may = + * not use this file except in compliance with the License. You should hav= e = + * received a copy of the License along with this program; if not, you may= obtain = + * a copy of the License at = + * + * http://www.jahia.org/license/ + * + * 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.jahia.tools.files; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.fileupload.FileItem; +import org.apache.struts.action.ActionMapping; +import org.apache.struts.action.ActionServlet; +import org.apache.struts.upload.FormFile; +import org.apache.struts.upload.MultipartRequestHandler; +import org.jahia.params.ParamBean; + +/** + * Jahia specific handler for the "multipart/form-data" requests used to u= pload + * and handle files in Struts forms and actions. + * = + * @author Sergiy Shyrkov + */ +public class JahiaMultipartRequestHandler implements MultipartRequestHandl= er { + + /** + * This class implements the Struts <code>FormFile</code> interface by + * wrapping the Commons FileUpload <code>FileItem</code> interface. This + * implementation is <i>read-only</i>; any attempt to modify an instance = of + * this class will result in an <code>UnsupportedOperationException</code= >. + * This code is taken from the Struts code base. + */ + static class CommonsFormFile implements FormFile, Serializable { + + /** + * The <code>FileItem</code> instance wrapped by this object. + */ + FileItem fileItem; + + /** + * Constructs an instance of this class which wraps the supplied file + * item. + * = + * @param fileItem + * The Commons file item to be wrapped. + */ + public CommonsFormFile(FileItem fileItem) { + this.fileItem =3D fileItem; + } + + /** + * Destroy all content for this form file. Implementations should remove + * any temporary files or any temporary file data stored somewhere + */ + public void destroy() { + fileItem.delete(); + } + + /** + * Returns the base file name from the supplied file path. On the + * surface, this would appear to be a trivial task. Apparently, however, + * some Linux JDKs do not implement <code>File.getName()</code> + * correctly for Windows paths, so we attempt to take care of that here. + * = + * @param filePath + * The full path to the file. + * @return The base file name, from the end of the path. + */ + protected String getBaseFileName(String filePath) { + + // First, ask the JDK for the base file name. + String fileName =3D new File(filePath).getName(); + + // Now check for a Windows file name parsed incorrectly. + int colonIndex =3D fileName.indexOf(":"); + if (colonIndex =3D=3D -1) { + // Check for a Windows SMB file path. + colonIndex =3D fileName.indexOf("\\\\"); + } + int backslashIndex =3D fileName.lastIndexOf("\\"); + + if (colonIndex > -1 && backslashIndex > -1) { + // Consider this filename to be a full Windows path, and parse + // it + // accordingly to retrieve just the base file name. + fileName =3D fileName.substring(backslashIndex + 1); + } + + return fileName; + } + + /** + * Returns the content type for this file. + * = + * @return A String representing content type. + */ + public String getContentType() { + return fileItem.getContentType(); + } + + /** + * Returns the data for this file as a byte array. Note that this may + * result in excessive memory usage for large uploads. The use of the + * [EMAIL PROTECTED] #getInputStream() getInputStream} method is encouraged as an + * alternative. + * = + * @return An array of bytes representing the data contained in this + * form file. + * @exception FileNotFoundException + * If some sort of file representation cannot be found + * for the FormFile + * @exception IOException + * If there is some sort of IOException + */ + public byte[] getFileData() throws FileNotFoundException, IOException { + return fileItem.get(); + } + + /** + * Returns the (client-side) file name for this file. + * = + * @return The client-size file name. + */ + public String getFileName() { + return getBaseFileName(fileItem.getName()); + } + + /** + * Returns the size, in bytes, of this file. + * = + * @return The size of the file, in bytes. + */ + public int getFileSize() { + return (int) fileItem.getSize(); + } + + /** + * Get an InputStream that represents this file. This is the preferred + * method of getting file data. + * = + * @exception FileNotFoundException + * If some sort of file representation cannot be found + * for the FormFile + * @exception IOException + * If there is some sort of IOException + */ + public InputStream getInputStream() throws FileNotFoundException, + IOException { + return fileItem.getInputStream(); + } + + /** + * Sets the content type for this file. + * <p> + * NOTE: This method is not supported in this implementation. + * = + * @param contentType + * A string representing the content type. + */ + public void setContentType(String contentType) { + throw new UnsupportedOperationException( + "The setContentType() method is not supported."); + } + + /** + * Sets the (client-side) file name for this file. + * <p> + * NOTE: This method is not supported in this implementation. + * = + * @param fileName + * The client-side name for the file. + */ + public void setFileName(String fileName) { + throw new UnsupportedOperationException( + "The setFileName() method is not supported."); + } + + /** + * Sets the size, in bytes, for this file. + * <p> + * NOTE: This method is not supported in this implementation. + * = + * @param filesize + * The size of the file, in bytes. + */ + public void setFileSize(int filesize) { + throw new UnsupportedOperationException( + "The setFileSize() method is not supported."); + } + + /** + * Returns the (client-side) file name for this file. + * = + * @return The client-size file name. + */ + public String toString() { + return getFileName(); + } + } + + /** + * The combined text and file request parameters. + */ + private Hashtable elementsAll; + + /** + * The file request parameters. + */ + private Hashtable elementsFile; + + /** + * The text request parameters. + */ + private Hashtable elementsText; + + /** + * The action mapping with which this handler is associated. + */ + private ActionMapping mapping; + + /** + * The servlet with which this handler is associated. + */ + private ActionServlet servlet; + + public void finish() { + rollback(); + } + + public Hashtable getAllElements() { + return elementsAll; + } + + public Hashtable getFileElements() { + return elementsFile; + } + + public ActionMapping getMapping() { + return mapping; + } + + public ActionServlet getServlet() { + return servlet; + } + + public Hashtable getTextElements() { + return elementsText; + } + + public void handleRequest(HttpServletRequest request) + throws ServletException { + + ParamBean ctx =3D (ParamBean) request + .getAttribute("org.jahia.params.ParamBean"); + + if (ctx =3D=3D null) + throw new IllegalArgumentException( + "Unable find the ParamBean object in the request scope"); + + FileUpload fileUpload =3D ctx.getFileUpload(); + elementsText =3D new Hashtable(fileUpload.getParameterMap()); + + Map fileItems =3D fileUpload.getFileItems(); + elementsFile =3D new Hashtable(fileItems.size()); + for (Iterator iterator =3D fileItems.entrySet().iterator(); iterator + .hasNext();) { + Map.Entry item =3D (Map.Entry) iterator.next(); + elementsFile.put(item.getKey(), new CommonsFormFile((FileItem) item + .getValue())); + } + + elementsAll =3D new Hashtable(elementsText.size() + elementsFile.size()); + elementsAll.putAll(elementsText); + elementsAll.putAll(elementsFile); + } + + public void rollback() { + Iterator iter =3D elementsFile.values().iterator(); + + while (iter.hasNext()) { + FormFile formFile =3D (FormFile) iter.next(); + + formFile.destroy(); + } + } + + public void setMapping(ActionMapping mapping) { + this.mapping =3D mapping; + } + + public void setServlet(ActionServlet servlet) { + this.servlet =3D servlet; + } + +} Modified: trunk/core/src/webapp/WEB-INF/etc/struts/struts-config.xml URL: https://svndev.jahia.net/websvn/diff.php?path=3D/trunk/core/src/webapp= /WEB-INF/etc/struts/struts-config.xml&rev=3D17550&repname=3Djahia =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- trunk/core/src/webapp/WEB-INF/etc/struts/struts-config.xml (original) +++ trunk/core/src/webapp/WEB-INF/etc/struts/struts-config.xml Tue Jun 12 1= 7:20:11 2007 @@ -452,7 +452,8 @@ <!-- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Controller Configuration -= -> = <controller - processorClass=3D"org.jahia.bin.JahiaRequestProcessor" content= Type=3D"text/html; charset=3DUTF-8" /> + processorClass=3D"org.jahia.bin.JahiaRequestProcessor" content= Type=3D"text/html; charset=3DUTF-8" = + multipartClass=3D"org.jahia.tools.files.JahiaMultipartRequestH= andler"/> <!-- Message Resources --> <message-resources parameter=3D"ApplicationResources" null=3D"false"/> <!-- Tiles PlugIns --> _______________________________________________ cvs_list mailing list [email protected] http://lists.jahia.org/cgi-bin/mailman/listinfo/cvs_list
