Source Code Cross Referenced for MultipartHttpParameters.java in  » Web-Framework » makumba » org » makumba » commons » attributes » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » makumba » org.makumba.commons.attributes 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.