View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
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( contentType );
77  	}
78  	
79  	/***
80  	 * Init soapversion from content-type header.. should envelope be checked and/or override? 
81  	 */
82  	
83  	protected boolean initSoapVersion( String contentType )
84  	{
85  		soapVersion = contentType.startsWith( SoapVersion.Soap11.getContentType() ) ? SoapVersion.Soap11 : null;
86  		soapVersion = soapVersion == null && contentType.startsWith( SoapVersion.Soap12.getContentType() ) ? SoapVersion.Soap12 : soapVersion;
87  
88  		return true;
89  	}
90  	
91  	public SoapVersion getSoapVersion()
92  	{
93  		return soapVersion;
94  	}
95  
96  	public String getProtocol()
97  	{
98  		return protocol;
99  	}
100 
101 	private void readMultipartRequest( HttpServletRequest request ) throws MessagingException
102 	{
103 		StringToStringMap values = StringToStringMap.fromHttpHeader( request.getContentType() );
104 		mmSupport = new MultipartMessageSupport( new MockRequestDataSource( request ), values.get( "start" ) );
105 	}
106 
107 	private String readRequestContent( HttpServletRequest request ) throws Exception
108 	{
109 		String encoding = request.getCharacterEncoding();
110 		if( encoding == null )
111 			encoding = "UTF-8";
112 		else
113 			encoding = StringUtils.unquote( encoding );
114 		
115 		BufferedReader reader = new BufferedReader( new InputStreamReader( request.getInputStream(), encoding) );
116 		StringBuffer buf = new StringBuffer();
117 		String line = reader.readLine();
118 		while( line != null )
119 		{
120 			buf.append( line ).append( "\r\n" );
121 			line = reader.readLine();
122 		}
123 		
124 		return buf.toString();
125 	}
126 
127 
128 	public Attachment[] getRequestAttachments()
129 	{
130 		return mmSupport == null ? new Attachment[0] : mmSupport.getAttachments();
131 	}
132 
133 	public String getRequestContent()
134 	{
135 		return mmSupport == null ? requestContent : mmSupport.getContentAsString();
136 	}
137 
138 	public StringToStringMap getRequestHeaders()
139 	{
140 		return requestHeaders;
141 	}
142 
143 	public void setRequestContent( String requestContent )
144 	{
145 		this.requestContent = requestContent;
146 	}
147 
148 	public XmlObject getRequestXmlObject() throws XmlException
149 	{
150 		if( requestXmlObject == null )
151 			requestXmlObject = XmlObject.Factory.parse( getRequestContent() );
152 		
153 		return requestXmlObject;
154 	}
155 
156 	public XmlObject getBodyElement() throws XmlException
157 	{
158 		XmlObject[] envelope = getRequestXmlObject().selectChildren( soapVersion.getEnvelopeQName() );
159 		if( envelope.length != 1 )
160 		    throw new XmlException( "Missing/Invalid SOAP Envelope, expecting [" + soapVersion.getEnvelopeQName() + "]" );
161 		
162 		XmlObject[] body = envelope[0].selectChildren( soapVersion.getBodyQName() );
163 		if( body.length != 1 )
164 		    throw new XmlException( "Missing/Invalid SOAP Body, expecting [" + soapVersion.getBodyQName() + "]" );
165 		
166 		return body[0];
167 	}
168 
169 	public HttpServletResponse getHttpResponse()
170 	{
171 		return response;
172 	}
173 
174 	public HttpServletRequest getHttpRequest() 
175 	{
176 		return request;
177 	}
178 	
179 	public XmlObject getContentElement() throws XmlException
180 	{
181 		XmlObject bodyElement = getBodyElement();
182 		if( bodyElement != null )
183 		{
184 			XmlCursor cursor = bodyElement.newCursor();
185 			
186 			try
187 			{
188 				if( cursor.toFirstChild() )
189 				{
190 					while( !cursor.isContainer() )
191 						cursor.toNextSibling();
192 
193 					if( cursor.isContainer() )
194 					{
195 						return cursor.getObject();
196 					}
197 				}
198 			}
199 			catch( Exception e )
200 			{
201 				e.printStackTrace();
202 			}
203 			finally
204 			{
205 				cursor.dispose();
206 			}
207 		}
208 		
209 		return null;
210 	}
211 
212 	public String getPath()
213 	{
214 		return path;
215 	}
216 
217 	public WsdlMockRunContext getContext()
218 	{
219 		return context;
220 	}
221 }