View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 com.eviware.soapui.impl.wsdl.WsdlOperation;
16  import com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments.MockRequestDataSource;
17  import com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments.MultipartMessageSupport;
18  import com.eviware.soapui.impl.wsdl.support.soap.SoapUtils;
19  import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
20  import com.eviware.soapui.impl.wsdl.support.wss.IncomingWss;
21  import com.eviware.soapui.model.iface.Attachment;
22  import com.eviware.soapui.model.mock.MockRequest;
23  import com.eviware.soapui.settings.WsdlSettings;
24  import com.eviware.soapui.support.StringUtils;
25  import com.eviware.soapui.support.Tools;
26  import com.eviware.soapui.support.types.StringToStringMap;
27  import com.eviware.soapui.support.xml.XmlUtils;
28  import org.apache.xmlbeans.XmlException;
29  import org.apache.xmlbeans.XmlObject;
30  import org.w3c.dom.Document;
31  
32  import javax.mail.MessagingException;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import java.io.ByteArrayOutputStream;
36  import java.io.StringWriter;
37  import java.util.Enumeration;
38  import java.util.Vector;
39  
40  /***
41   * Request-class created when receiving an external request to a WsdlMockService
42   * 
43   * @author ole.matzura
44   */
45  
46  public class WsdlMockRequest implements MockRequest
47  {
48  	private StringToStringMap requestHeaders;
49  	private String requestContent;
50  	private MultipartMessageSupport mmSupport;
51  	private XmlObject requestXmlObject;
52  	private SoapVersion soapVersion;
53  	private final HttpServletResponse response;
54  	private String protocol;
55  	private String path;
56  	private String soapAction;
57  	private final WsdlMockRunContext context;
58  	private final WsdlMockRunContext requestContext;
59  	private final HttpServletRequest request;
60  	private Vector<Object> wssResult;
61  	private MockRequestDataSource mockRequestDataSource;
62  	private String actualRequestContent;
63  	
64  	public WsdlMockRequest( HttpServletRequest request, HttpServletResponse response, WsdlMockRunContext context ) throws Exception
65  	{
66  		this.request = request;
67  		this.response = response;
68  		this.context = context;
69  		
70  		requestContext = new WsdlMockRunContext( context.getMockService(), null );
71  		
72  		requestHeaders = new StringToStringMap();
73  		for( Enumeration<?> e = request.getHeaderNames(); e.hasMoreElements(); )
74  		{
75  			String header = ( String ) e.nextElement();
76  			requestHeaders.put( header, request.getHeader( header ) );
77  		}
78  		
79  		protocol = request.getProtocol();
80  		path = request.getPathInfo();
81  
82        if( request.getMethod().equals( "POST" ))
83        {
84           initPostRequest( request, context );
85        }
86  	}
87  
88     protected void initPostRequest( HttpServletRequest request, WsdlMockRunContext context )
89             throws Exception
90     {
91        String contentType = request.getContentType();
92  
93        if( contentType != null &&	contentType.toUpperCase().startsWith( "MULTIPART" ))
94        {
95           readMultipartRequest( request );
96           contentType = mmSupport.getRootPart().getContentType();
97        }
98        else
99        {
100          this.requestContent = readRequestContent( request );
101 
102          if( StringUtils.hasContent( context.getMockService().getIncomingWss() ))
103          {
104             IncomingWss incoming = context.getMockService().getProject().getWssContainer().getIncomingWssByName( context.getMockService().getIncomingWss() );
105             if( incoming != null )
106             {
107                Document dom = XmlUtils.parseXml( requestContent );
108                try
109                {
110                   wssResult = incoming.processIncoming( dom, context );
111                   if( wssResult != null && wssResult.size() > 0 )
112                   {
113                      StringWriter writer = new StringWriter();
114                      XmlUtils.serialize( dom, writer );
115                      actualRequestContent = requestContent;
116                      requestContent = writer.toString();
117                   }
118                }
119                catch( Exception e )
120                {
121                   if( wssResult == null )
122                      wssResult = new Vector<Object>();
123                   wssResult.add( e );
124                }
125             }
126          }
127       }
128 
129       soapVersion = SoapUtils.deduceSoapVersion( contentType, getRequestXmlObject() );
130       if( soapVersion == null )
131          soapVersion = SoapVersion.Soap11;
132 
133       soapAction = SoapUtils.getSoapAction( soapVersion, requestHeaders );
134    }
135 
136    public SoapVersion getSoapVersion()
137 	{
138 		return soapVersion;
139 	}
140 
141 	public String getProtocol()
142 	{
143 		return protocol;
144 	}
145 	
146 	public Vector<?> getWssResult()
147 	{
148 		return wssResult;
149 	}
150 
151 	private void readMultipartRequest( HttpServletRequest request ) throws MessagingException
152 	{
153 		StringToStringMap values = StringToStringMap.fromHttpHeader( request.getContentType() );
154 		mockRequestDataSource = new MockRequestDataSource( request );
155 		mmSupport = new MultipartMessageSupport( mockRequestDataSource, values.get( "start" ), null, true, 
156 					requestContext.getMockService().getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES ));
157 	}
158 
159 	private String readRequestContent( HttpServletRequest request ) throws Exception
160 	{
161 		String responseContent = null;
162 		String encoding = request.getCharacterEncoding();
163 		if( encoding != null)
164 			encoding = StringUtils.unquote( encoding );
165 		
166 		
167 		ByteArrayOutputStream out = Tools.readAll( request.getInputStream(), Tools.READ_ALL );
168 		byte [] data = out.toByteArray();
169 		int contentOffset = 0;
170 		
171 		String contentType = request.getContentType();
172 		if( contentType != null && data.length  > 0 )
173 		{
174 			if( contentType.toLowerCase().endsWith("xml"))
175 			{
176 				if( data.length > 3 && data[0] == (byte)239 && data[1] == (byte)187 && data[2] == (byte)191 )
177 				{
178 					encoding = "UTF-8";
179 					contentOffset = 3;
180 				}
181 			}
182 			
183 			encoding = StringUtils.unquote( encoding );
184 			
185 			responseContent = encoding == null ? new String(data) : 
186 				new String(	data, contentOffset, (int)(data.length-contentOffset), encoding );
187 		}
188 
189 		if( encoding == null )
190 			encoding = "UTF-8";
191 		
192 		if( responseContent == null )
193 		{
194 			responseContent = new String( data, encoding );
195 		}
196 		
197 		return responseContent;
198 	}
199 
200 	public Attachment[] getRequestAttachments()
201 	{
202 		return mmSupport == null ? new Attachment[0] : mmSupport.getAttachments();
203 	}
204 
205 	public String getRequestContent()
206 	{
207 		return mmSupport == null ? requestContent : mmSupport.getContentAsString();
208 	}
209 
210 	public StringToStringMap getRequestHeaders()
211 	{
212 		return requestHeaders;
213 	}
214 
215 	public void setRequestContent( String requestContent )
216 	{
217 		this.requestContent = requestContent;
218 	}
219 
220 	public XmlObject getRequestXmlObject() throws XmlException
221 	{
222 		if( requestXmlObject == null )
223 			requestXmlObject = XmlObject.Factory.parse( getRequestContent() );
224 		
225 		return requestXmlObject;
226 	}
227 
228 	public HttpServletResponse getHttpResponse()
229 	{
230 		return response;
231 	}
232 
233 	public HttpServletRequest getHttpRequest() 
234 	{
235 		return request;
236 	}
237 	
238 	public XmlObject getContentElement() throws XmlException
239 	{
240 		return SoapUtils.getContentElement( getRequestXmlObject(), soapVersion );
241 	}
242 
243 	public String getPath()
244 	{
245 		return path;
246 	}
247 
248 	public WsdlMockRunContext getContext()
249 	{
250 		return context;
251 	}
252 
253 	public void setOperation( WsdlOperation operation )
254 	{
255 		if( mmSupport != null )
256 			mmSupport.setOperation( operation );
257 	}
258 
259 	public WsdlMockRunContext getRequestContext()
260 	{
261 		return requestContext;
262 	}
263 
264 	public String getSoapAction()
265 	{
266 		return soapAction;
267 	}
268 
269 	public byte [] getRawRequestData()
270 	{
271 		return mockRequestDataSource == null ? 
272 					actualRequestContent == null ? requestContent.getBytes() : actualRequestContent.getBytes()  
273 						: mockRequestDataSource.getData();
274 	}
275 }