001: //////////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020: // $Id: MultipartHttpParameters.java 1726 2007-10-02 09:11:59Z manuel_gay $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.commons.attributes;
025:
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Vector;
031:
032: import javax.servlet.http.HttpServletRequest;
033:
034: import org.apache.commons.fileupload.FileItem;
035: import org.apache.commons.fileupload.FileUploadException;
036: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
037: import org.apache.commons.fileupload.servlet.ServletFileUpload;
038: import org.makumba.Text;
039:
040: /**
041: * Parses the input stream of a http request as a multipart/form-data. Stores uploaded files as org.makumba.Text. Normal
042: * http parameters are stored as Text.toString (simple) or Vectors (multiple) data inside the request:
043: * <ul>
044: * <li>1st line: boundary + CR+LF</li>
045: * <li>headers & values + CR+LF (e.g. filename="file.doc" Content-Type: application/octec-stream)</li>
046: * <li>CR+LF (Konqueror 3.2.1 sends CR CR)</li>
047: * <li>content (related to the headers just read)</li>
048: * <li>CR+LF - boundary CR+LF - headers... and so forth ...</li>
049: * <li>and after the last boundary you will have '--' with CR+LF</li>
050: * </ul>
051: *
052: * @author Cristian Bogdan
053: * @author Andreas Pesenhofer
054: * @author Rudolf Mayer
055: * @version $Id: MultipartHttpParameters.java 1726 2007-10-02 09:11:59Z manuel_gay $
056: */
057: public class MultipartHttpParameters extends HttpParameters {
058: Hashtable<String, Object> parameters = new Hashtable<String, Object>();
059:
060: void computeAtStart() {
061: }
062:
063: public boolean knownAtStart(String s) {
064: return parameters.get(s) != null;
065: }
066:
067: public MultipartHttpParameters(HttpServletRequest req) {
068: super (req);
069:
070: java.util.logging.Logger
071: .getLogger("org.makumba." + "fileUpload")
072: .fine(
073: "\n\n---- code with apache.commons.fileupload ------\n");
074:
075: // Create a factory for disk-based file items
076: DiskFileItemFactory factory = new DiskFileItemFactory();
077:
078: // Create a new file upload handler
079: ServletFileUpload upload = new ServletFileUpload(factory);
080:
081: // Parse the request
082: List items = null;
083: try {
084: items = upload.parseRequest(request);
085: } catch (FileUploadException e1) {
086: // TODO Auto-generated catch block
087: e1.printStackTrace();
088: }
089:
090: // Process the uploaded items
091: Iterator iter = items.iterator();
092: while (iter.hasNext()) {
093: FileItem item = (FileItem) iter.next();
094:
095: if (item.isFormField()) {
096: // Process a regular form field
097: if (item.isFormField()) {
098: String name = item.getFieldName();
099: String value = item.getString();
100: addParameter(name, value);
101: }
102:
103: } else {
104: // Process a file upload
105: if (!item.isFormField()) {
106: Text contentToSave;
107: int contentSize;
108:
109: String name = item.getFieldName();
110: String fileName = item.getName();
111: String type = item.getContentType();
112: // boolean isInMemory = item.isInMemory();
113:
114: // ---- read the content and set parameters
115: contentToSave = new Text(item.get());
116: contentSize = contentToSave.length();
117:
118: parameters.put(name + "_contentType", type);
119: parameters.put(name + "_filename", fileName);
120: parameters.put(name + "_contentLength",
121: new Integer(contentSize));
122: parameters.put(name, contentToSave);
123:
124: java.util.logging.Logger.getLogger(
125: "org.makumba." + "fileUpload").fine(
126: "Parameters set: contentType=" + type
127: + ", fileName=" + fileName
128: + ", contentSize=" + contentSize);
129: }
130:
131: }
132: }
133: }// end of the method MultipartHttpParameters
134:
135: void addParameter(String name, String value) {
136: Object o = parameters.get(name);
137: if (o != null)
138: if (o instanceof Vector)
139: ((Vector) o).addElement(value);
140: else {
141: Vector<Object> v = new Vector<Object>();
142: v.addElement(o);
143: v.addElement(value);
144: parameters.put(name, v);
145: }
146: else
147: parameters.put(name, value);
148: }
149:
150: /**
151: * Composes what is read from the multipart with what is in the query string. The assumption is that the multipart
152: * cannot change during execution, while the query string may change due to e.g. forwards
153: *
154: * @param s the query string
155: * @return An Object holding the parameters
156: */
157: public Object getParameter(String s) {
158: return compose(parameters.get(s), super .getParameter(s));
159: }
160:
161: /**
162: * TODO this should not be here but in a util class Composes two objects, if both are vectors, unites them
163: *
164: * @param a1 the first object
165: * @param a2 the second object
166: * @return a composed object
167: */
168: static Object compose(Object a1, Object a2) {
169: if (a1 == null)
170: return a2;
171: if (a2 == null)
172: return a1;
173:
174: if (a1 instanceof Vector)
175: if (a2 instanceof Vector) {
176: for (Enumeration e = ((Vector) a2).elements(); e
177: .hasMoreElements();)
178: ((Vector) a1).addElement(e.nextElement());
179: return a1;
180: } else {
181: ((Vector) a1).addElement(a2);
182: return a1;
183: }
184: else if (a2 instanceof Vector) {
185: ((Vector) a2).addElement(a1);
186: return a2;
187: } else {
188: Vector<Object> v = new Vector<Object>();
189: v.addElement(a1);
190: v.addElement(a2);
191: return v;
192: }
193: }
194: }
|