001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.mock;
014:
015: import java.io.BufferedReader;
016: import java.io.InputStreamReader;
017: import java.util.Enumeration;
018:
019: import javax.mail.MessagingException;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import org.apache.xmlbeans.XmlCursor;
024: import org.apache.xmlbeans.XmlException;
025: import org.apache.xmlbeans.XmlObject;
026:
027: import com.eviware.soapui.SoapUI;
028: import com.eviware.soapui.impl.wsdl.WsdlOperation;
029: import com.eviware.soapui.impl.wsdl.submit.transports.http.MockRequestDataSource;
030: import com.eviware.soapui.impl.wsdl.submit.transports.http.MultipartMessageSupport;
031: import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
032: import com.eviware.soapui.model.iface.Attachment;
033: import com.eviware.soapui.model.mock.MockRequest;
034: import com.eviware.soapui.support.StringUtils;
035: import com.eviware.soapui.support.types.StringToStringMap;
036:
037: /**
038: * Request-class created when receiving an external request to a WsdlMockService
039: *
040: * @author ole.matzura
041: */
042:
043: public class WsdlMockRequest implements MockRequest {
044: private StringToStringMap requestHeaders;
045: private String requestContent;
046: private MultipartMessageSupport mmSupport;
047: private XmlObject requestXmlObject;
048: private SoapVersion soapVersion;
049: private final HttpServletResponse response;
050: private String protocol;
051: private String path;
052: private final WsdlMockRunContext context;
053: private final WsdlMockRunContext requestContext;
054: private final HttpServletRequest request;
055:
056: public WsdlMockRequest(HttpServletRequest request,
057: HttpServletResponse response, WsdlMockRunContext context)
058: throws Exception {
059: this .request = request;
060: this .response = response;
061: this .context = context;
062:
063: requestContext = new WsdlMockRunContext(context
064: .getMockService(), null);
065:
066: requestHeaders = new StringToStringMap();
067: for (Enumeration e = request.getHeaderNames(); e
068: .hasMoreElements();) {
069: String header = (String) e.nextElement();
070: requestHeaders.put(header, request.getHeader(header));
071: }
072:
073: protocol = request.getProtocol();
074: path = request.getPathInfo();
075:
076: String contentType = request.getContentType();
077:
078: if (contentType != null
079: && contentType.toUpperCase().startsWith("MULTIPART")) {
080: readMultipartRequest(request);
081: contentType = mmSupport.getRootPart().getContentType();
082: } else {
083: this .requestContent = readRequestContent(request);
084: }
085:
086: initSoapVersion(contentType);
087: }
088:
089: /**
090: * Init soapversion from content-type header.. should envelope be checked and/or override?
091: */
092:
093: protected boolean initSoapVersion(String contentType) {
094: soapVersion = contentType.startsWith(SoapVersion.Soap11
095: .getContentType()) ? SoapVersion.Soap11 : null;
096: soapVersion = soapVersion == null
097: && contentType.startsWith(SoapVersion.Soap12
098: .getContentType()) ? SoapVersion.Soap12
099: : soapVersion;
100:
101: return true;
102: }
103:
104: public SoapVersion getSoapVersion() {
105: return soapVersion;
106: }
107:
108: public String getProtocol() {
109: return protocol;
110: }
111:
112: private void readMultipartRequest(HttpServletRequest request)
113: throws MessagingException {
114: StringToStringMap values = StringToStringMap
115: .fromHttpHeader(request.getContentType());
116: mmSupport = new MultipartMessageSupport(
117: new MockRequestDataSource(request),
118: values.get("start"), null, true);
119: }
120:
121: private String readRequestContent(HttpServletRequest request)
122: throws Exception {
123: String encoding = request.getCharacterEncoding();
124: if (encoding == null)
125: encoding = "UTF-8";
126: else
127: encoding = StringUtils.unquote(encoding);
128:
129: BufferedReader reader = new BufferedReader(
130: new InputStreamReader(request.getInputStream(),
131: encoding));
132: StringBuffer buf = new StringBuffer();
133: String line = reader.readLine();
134: while (line != null) {
135: buf.append(line).append("\r\n");
136: line = reader.readLine();
137: }
138:
139: return buf.toString();
140: }
141:
142: public Attachment[] getRequestAttachments() {
143: return mmSupport == null ? new Attachment[0] : mmSupport
144: .getAttachments();
145: }
146:
147: public String getRequestContent() {
148: return mmSupport == null ? requestContent : mmSupport
149: .getContentAsString();
150: }
151:
152: public StringToStringMap getRequestHeaders() {
153: return requestHeaders;
154: }
155:
156: public void setRequestContent(String requestContent) {
157: this .requestContent = requestContent;
158: }
159:
160: public XmlObject getRequestXmlObject() throws XmlException {
161: if (requestXmlObject == null)
162: requestXmlObject = XmlObject.Factory
163: .parse(getRequestContent());
164:
165: return requestXmlObject;
166: }
167:
168: public XmlObject getBodyElement() throws XmlException {
169: XmlObject[] envelope = getRequestXmlObject().selectChildren(
170: soapVersion.getEnvelopeQName());
171: if (envelope.length != 1)
172: throw new XmlException(
173: "Missing/Invalid SOAP Envelope, expecting ["
174: + soapVersion.getEnvelopeQName() + "]");
175:
176: XmlObject[] body = envelope[0].selectChildren(soapVersion
177: .getBodyQName());
178: if (body.length != 1)
179: throw new XmlException(
180: "Missing/Invalid SOAP Body, expecting ["
181: + soapVersion.getBodyQName() + "]");
182:
183: return body[0];
184: }
185:
186: public HttpServletResponse getHttpResponse() {
187: return response;
188: }
189:
190: public HttpServletRequest getHttpRequest() {
191: return request;
192: }
193:
194: public XmlObject getContentElement() throws XmlException {
195: XmlObject bodyElement = getBodyElement();
196: if (bodyElement != null) {
197: XmlCursor cursor = bodyElement.newCursor();
198:
199: try {
200: if (cursor.toFirstChild()) {
201: while (!cursor.isContainer())
202: cursor.toNextSibling();
203:
204: if (cursor.isContainer()) {
205: return cursor.getObject();
206: }
207: }
208: } catch (Exception e) {
209: SoapUI.logError(e);
210: } finally {
211: cursor.dispose();
212: }
213: }
214:
215: return null;
216: }
217:
218: public String getPath() {
219: return path;
220: }
221:
222: public WsdlMockRunContext getContext() {
223: return context;
224: }
225:
226: public void setOperation(WsdlOperation operation) {
227: if (mmSupport != null)
228: mmSupport.setOperation(operation);
229: }
230:
231: public WsdlMockRunContext getRequestContext() {
232: return requestContext;
233: }
234: }
|