View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.mock;
14  
15  import java.io.BufferedReader;
16  import java.io.InputStreamReader;
17  import java.util.Enumeration;
18  
19  import javax.mail.MessagingException;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.xmlbeans.XmlCursor;
24  import org.apache.xmlbeans.XmlException;
25  import org.apache.xmlbeans.XmlObject;
26  
27  import com.eviware.soapui.impl.wsdl.submit.transports.http.MockRequestDataSource;
28  import com.eviware.soapui.impl.wsdl.submit.transports.http.MultipartMessageSupport;
29  import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
30  import com.eviware.soapui.model.iface.Attachment;
31  import com.eviware.soapui.model.mock.MockRequest;
32  import com.eviware.soapui.support.StringUtils;
33  import com.eviware.soapui.support.types.StringToStringMap;
34  
35  public class WsdlMockRequest implements MockRequest
36  {
37  	private StringToStringMap requestHeaders;
38  	private String requestContent;
39  	private MultipartMessageSupport mmSupport;
40  	private XmlObject requestXmlObject;
41  	private SoapVersion soapVersion;
42  	private final HttpServletResponse response;
43  	private String protocol;
44  	private String path;
45  	private final WsdlMockRunContext context;
46  	private final HttpServletRequest request;
47  
48  	public WsdlMockRequest( HttpServletRequest request, HttpServletResponse response, WsdlMockRunContext context ) throws Exception
49  	{
50  		this.request = request;
51  		this.response = response;
52  		this.context = context;
53  		
54  		requestHeaders = new StringToStringMap();
55  		for( Enumeration e = request.getHeaderNames(); e.hasMoreElements(); )
56  		{
57  			String header = ( String ) e.nextElement();
58  			requestHeaders.put( header, request.getHeader( header ) );
59  		}
60  		
61  		protocol = request.getProtocol();
62  		path = request.getPathInfo();
63  		
64  		String contentType = request.getContentType();
65  		
66  		if( contentType != null &&	contentType.toUpperCase().startsWith( "MULTIPART" ))
67  		{
68  			readMultipartRequest( request );
69  			contentType = mmSupport.getRootPart().getContentType();
70  		}
71  		else
72  		{
73  			this.requestContent = readRequestContent( request );
74  		}
75  		
76  		initSoapVersion( request );
77  	}
78  	
79  	/***
80  	 * Init soapversion from content-type header.. should envelope be checked and/or override? 
81  	 */
82  	
83  	protected boolean initSoapVersion( HttpServletRequest request )
84  	{
85  		String contentType = request.getContentType();
86  		
87  		soapVersion = contentType.startsWith( SoapVersion.Soap11.getContentType() ) ? SoapVersion.Soap11 : null;
88  		soapVersion = soapVersion == null && contentType.startsWith( SoapVersion.Soap12.getContentType() ) ? SoapVersion.Soap12 : soapVersion;
89  
90  		return true;
91  	}
92  	
93  	public SoapVersion getSoapVersion()
94  	{
95  		return soapVersion;
96  	}
97  
98  	public String getProtocol()
99  	{
100 		return protocol;
101 	}
102 
103 	private void readMultipartRequest( HttpServletRequest request ) throws MessagingException
104 	{
105 		StringToStringMap values = StringToStringMap.fromHttpHeader( request.getContentType() );
106 		mmSupport = new MultipartMessageSupport( new MockRequestDataSource( request ), values.get( "start" ) );
107 	}
108 
109 	private String readRequestContent( HttpServletRequest request ) throws Exception
110 	{
111 		String encoding = request.getCharacterEncoding();
112 		if( encoding == null )
113 			encoding = "UTF-8";
114 		else
115 			encoding = StringUtils.unquote( encoding );
116 		
117 		BufferedReader reader = new BufferedReader( new InputStreamReader( request.getInputStream(), encoding) );
118 		StringBuffer buf = new StringBuffer();
119 		String line = reader.readLine();
120 		while( line != null )
121 		{
122 			buf.append( line ).append( "\r\n" );
123 			line = reader.readLine();
124 		}
125 		
126 		return buf.toString();
127 	}
128 
129 
130 	public Attachment[] getRequestAttachments()
131 	{
132 		return mmSupport == null ? new Attachment[0] : mmSupport.getAttachments();
133 	}
134 
135 	public String getRequestContent()
136 	{
137 		return mmSupport == null ? requestContent : mmSupport.getContentAsString();
138 	}
139 
140 	public StringToStringMap getRequestHeaders()
141 	{
142 		return requestHeaders;
143 	}
144 
145 	public void setRequestContent( String requestContent )
146 	{
147 		this.requestContent = requestContent;
148 	}
149 
150 	public XmlObject getRequestXmlObject() throws XmlException
151 	{
152 		if( requestXmlObject == null )
153 			requestXmlObject = XmlObject.Factory.parse( getRequestContent() );
154 		
155 		return requestXmlObject;
156 	}
157 
158 	public XmlObject getBodyElement() throws XmlException
159 	{
160 		XmlObject[] envelope = getRequestXmlObject().selectChildren( soapVersion.getEnvelopeQName() );
161 		if( envelope.length != 1 )
162 		    throw new XmlException( "Missing/Invalid SOAP Envelope, expecting [" + soapVersion.getEnvelopeQName() + "]" );
163 		
164 		XmlObject[] body = envelope[0].selectChildren( soapVersion.getBodyQName() );
165 		if( body.length != 1 )
166 		    throw new XmlException( "Missing/Invalid SOAP Body, expecting [" + soapVersion.getBodyQName() + "]" );
167 		
168 		return body[0];
169 	}
170 
171 	public HttpServletResponse getHttpResponse()
172 	{
173 		return response;
174 	}
175 
176 	public HttpServletRequest getHttpRequest() 
177 	{
178 		return request;
179 	}
180 	
181 	public XmlObject getContentElement() throws XmlException
182 	{
183 		XmlObject bodyElement = getBodyElement();
184 		if( bodyElement != null )
185 		{
186 			XmlCursor cursor = bodyElement.newCursor();
187 			
188 			try
189 			{
190 				if( cursor.toFirstChild() )
191 				{
192 					while( !cursor.isContainer() )
193 						cursor.toNextSibling();
194 
195 					if( cursor.isContainer() )
196 					{
197 						return cursor.getObject();
198 					}
199 				}
200 			}
201 			catch( Exception e )
202 			{
203 				e.printStackTrace();
204 			}
205 			finally
206 			{
207 				cursor.dispose();
208 			}
209 		}
210 		
211 		return null;
212 	}
213 
214 	public String getPath()
215 	{
216 		return path;
217 	}
218 
219 	public WsdlMockRunContext getContext()
220 	{
221 		return context;
222 	}
223 }