01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.builder;
21:
22: import org.apache.axiom.om.OMAbstractFactory;
23: import org.apache.axiom.om.OMElement;
24: import org.apache.axis2.AxisFault;
25: import org.apache.axis2.context.MessageContext;
26: import org.apache.axis2.transport.http.HTTPConstants;
27: import org.apache.axis2.util.MultipleEntryHashMap;
28: import org.apache.commons.fileupload.FileItemFactory;
29: import org.apache.commons.fileupload.FileUploadException;
30: import org.apache.commons.fileupload.disk.DiskFileItem;
31: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
32: import org.apache.commons.fileupload.servlet.ServletFileUpload;
33: import org.apache.commons.fileupload.servlet.ServletRequestContext;
34:
35: import javax.servlet.http.HttpServletRequest;
36: import java.io.InputStream;
37: import java.util.Iterator;
38: import java.util.List;
39:
40: public class MultipartFormDataBuilder implements Builder {
41:
42: /**
43: * @return Returns the document element.
44: */
45: public OMElement processDocument(InputStream inputStream,
46: String contentType, MessageContext messageContext)
47: throws AxisFault {
48: MultipleEntryHashMap parameterMap;
49: HttpServletRequest request = (HttpServletRequest) messageContext
50: .getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
51: if (request == null) {
52: throw new AxisFault(
53: "Cannot create DocumentElement without HttpServletRequest");
54: }
55: try {
56: parameterMap = getParameterMap(request);
57: return BuilderUtil.buildsoapMessage(messageContext,
58: parameterMap, OMAbstractFactory.getSOAP12Factory());
59:
60: } catch (FileUploadException e) {
61: throw AxisFault.makeFault(e);
62: }
63:
64: }
65:
66: private MultipleEntryHashMap getParameterMap(
67: HttpServletRequest request) throws FileUploadException {
68:
69: MultipleEntryHashMap parameterMap = new MultipleEntryHashMap();
70:
71: List items = parseRequest(new ServletRequestContext(request));
72: Iterator iter = items.iterator();
73: while (iter.hasNext()) {
74: String[] value = new String[1];
75: DiskFileItem diskFileItem = (DiskFileItem) iter.next();
76: value[0] = diskFileItem.getString();
77: parameterMap.put(diskFileItem.getFieldName(), value);
78: }
79:
80: return parameterMap;
81: }
82:
83: private static List parseRequest(
84: ServletRequestContext requestContext)
85: throws FileUploadException {
86: // Create a factory for disk-based file items
87: FileItemFactory factory = new DiskFileItemFactory();
88: // Create a new file upload handler
89: ServletFileUpload upload = new ServletFileUpload(factory);
90: // Parse the request
91: return upload.parseRequest(requestContext);
92: }
93:
94: }
|