Source Code Cross Referenced for MultipartCallingConvention.java in  » Web-Services » xins » com » mycompany » fileupload » multipart » 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 Services » xins » com.mycompany.fileupload.multipart 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: MultipartCallingConvention.java,v 1.8 2007/09/18 11:27:13 agoubard Exp $
003:         */
004:        package com.mycompany.fileupload.multipart;
005:
006:        import java.io.ByteArrayOutputStream;
007:        import java.io.InputStream;
008:        import java.io.IOException;
009:        import java.io.PrintWriter;
010:        import java.io.StringReader;
011:        import java.util.ArrayList;
012:        import java.util.Enumeration;
013:        import java.util.Iterator;
014:        import java.util.List;
015:
016:        import javax.servlet.http.HttpServletRequest;
017:        import javax.servlet.http.HttpServletResponse;
018:
019:        import org.apache.commons.fileupload.FileItem;
020:        import org.apache.commons.fileupload.FileItemFactory;
021:        import org.apache.commons.fileupload.FileUpload;
022:        import org.apache.commons.fileupload.FileUploadException;
023:        import org.apache.commons.fileupload.disk.DiskFileItemFactory;
024:        import org.apache.commons.fileupload.servlet.ServletFileUpload;
025:
026:        import org.xins.common.Utils;
027:        import org.xins.common.collections.BasicPropertyReader;
028:        import org.xins.common.text.ParseException;
029:        import org.xins.common.text.TextUtils;
030:        import org.xins.common.types.standard.Hex;
031:        import org.xins.common.xml.Element;
032:        import org.xins.common.xml.ElementParser;
033:
034:        import org.xins.server.CallResultOutputter;
035:        import org.xins.server.CustomCallingConvention;
036:        import org.xins.server.FunctionNotSpecifiedException;
037:        import org.xins.server.FunctionRequest;
038:        import org.xins.server.FunctionResult;
039:        import org.xins.server.InvalidRequestException;
040:
041:        /**
042:         * Calling convention that supports RFC 1867 multipart content. This content
043:         * type supports uploading of content items (typically: files.)
044:         *
045:         * <p>For an example of the HTML form, look at the upload.html file located in
046:         * the demo\xins-project\apis\fileupload directory.
047:         *
048:         * <p>For each defined input of type file, you must define two input parameters:
049:         * &lt;input field name&gt;Name of type _text for the name of the file and
050:         * &lt;input field name&gt;Content of type _hex for the content of the file.
051:         *
052:         * <p>Uploaded items will be retained in memory as long as they are reasonably
053:         * small. Larger items will be written to a temporary file on disk. Very large
054:         * upload requests are not permitted.
055:         *
056:         * @version $Revision: 1.8 $ $Date: 2007/09/18 11:27:13 $
057:         * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
058:         * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
059:         */
060:        public class MultipartCallingConvention extends CustomCallingConvention {
061:
062:            /**
063:             * Constructs a new <code>MultipartCallingConvention</code>.
064:             */
065:            public MultipartCallingConvention() {
066:                // empty
067:            }
068:
069:            protected boolean matches(HttpServletRequest httpRequest)
070:                    throws Exception {
071:
072:                // Requirement 1: The request must be multi-part
073:                if (!httpRequest.getContentType().startsWith(
074:                        FileUpload.MULTIPART_FORM_DATA)) {
075:                    return false;
076:                } else {
077:                    return true;
078:                }
079:            }
080:
081:            protected FunctionRequest convertRequestImpl(
082:                    HttpServletRequest httpRequest)
083:                    throws InvalidRequestException,
084:                    FunctionNotSpecifiedException {
085:
086:                // The request must be multi-part
087:                if (!httpRequest.getContentType().startsWith(
088:                        FileUpload.MULTIPART_FORM_DATA)) {
089:                    throw new InvalidRequestException(
090:                            "Request is not multi-part.");
091:                }
092:
093:                // Create a factory for disk-based file items
094:                FileItemFactory factory = new DiskFileItemFactory();
095:
096:                // Create a new file upload handler
097:                ServletFileUpload upload = new ServletFileUpload(factory);
098:
099:                // Parse the request (list contains FileItem instances)
100:                List itemList;
101:                try {
102:                    itemList = upload.parseRequest(httpRequest);
103:                } catch (FileUploadException exception) {
104:                    throw new InvalidRequestException(
105:                            "Failed to parse request.", exception);
106:                }
107:
108:                // Convert the list to a PropertyReader instance
109:                BasicPropertyReader params = new BasicPropertyReader();
110:                for (int i = 0; i < itemList.size(); i++) {
111:                    FileItem item = (FileItem) itemList.get(i);
112:                    String name = item.getFieldName();
113:                    if (item.isFormField()) {
114:                        String value = item.getString();
115:                        params.set(name, value);
116:                    } else {
117:                        String fileName = item.getName();
118:                        params.set(name + "Name", fileName);
119:                        try {
120:                            InputStream inputContent = item.getInputStream();
121:                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
122:                            int availableBytes = inputContent.available();
123:                            while (availableBytes > 0) {
124:                                byte[] buffer = new byte[availableBytes];
125:                                inputContent.read(buffer);
126:                                baos.write(buffer);
127:                                availableBytes = inputContent.available();
128:                            }
129:                            byte[] fileContent = baos.toByteArray();
130:                            inputContent.close();
131:                            baos.close();
132:                            params.set(name + "Content", Hex
133:                                    .toString(fileContent));
134:                        } catch (IOException ioe) {
135:                            throw new InvalidRequestException(
136:                                    "Failed to read the input file.", ioe);
137:                        }
138:                    }
139:                }
140:
141:                // Determine the function name
142:                String function = params.get("_function");
143:                if (function == null) {
144:                    throw new FunctionNotSpecifiedException();
145:                }
146:
147:                // Get data section
148:                String dataSectionValue = params.get("_data");
149:                Element dataElement = null;
150:                if (dataSectionValue != null && dataSectionValue.length() > 0) {
151:                    ElementParser parser = new ElementParser();
152:
153:                    // Parse the data section
154:                    StringReader reader = new StringReader(dataSectionValue);
155:                    try {
156:                        dataElement = parser.parse(reader);
157:
158:                        // I/O error, should never happen on a StringReader
159:                    } catch (IOException exception) {
160:                        throw Utils.logProgrammingError(exception);
161:
162:                        // Parsing error
163:                    } catch (ParseException exception) {
164:                        String detail = "Cannot parse the data section.";
165:                        throw new InvalidRequestException(detail, exception);
166:                    }
167:                }
168:
169:                // Construct and return the request object
170:                return new FunctionRequest(function, params, dataElement);
171:            }
172:
173:            protected void convertResultImpl(FunctionResult xinsResult,
174:                    HttpServletResponse httpResponse,
175:                    HttpServletRequest httpRequest) throws IOException {
176:
177:                // Send the XML output to the stream and flush
178:                httpResponse.setContentType("text/xml; charset=UTF-8");
179:                PrintWriter out = httpResponse.getWriter();
180:                httpResponse.setStatus(HttpServletResponse.SC_OK);
181:                CallResultOutputter.output(out, xinsResult);
182:                out.close();
183:            }
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.