View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.IOException;
16  import java.io.OutputStream;
17  import java.text.SimpleDateFormat;
18  import java.util.Date;
19  import java.util.Enumeration;
20  import java.util.Vector;
21  
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.mortbay.jetty.HttpFields;
25  
26  import com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockResultMessageExchange;
27  import com.eviware.soapui.impl.wsdl.teststeps.actions.ShowMessageExchangeAction;
28  import com.eviware.soapui.model.mock.MockResponse;
29  import com.eviware.soapui.model.mock.MockResult;
30  import com.eviware.soapui.support.action.swing.ActionList;
31  import com.eviware.soapui.support.action.swing.DefaultActionList;
32  import com.eviware.soapui.support.types.StringToStringMap;
33  
34  /***
35   * The result of a handled WsdlMockRequest
36   * 
37   * @author ole.matzura
38   */
39  
40  public class WsdlMockResult implements MockResult
41  {
42  	private WsdlMockResponse mockResponse;
43  	private String responseContent;
44  	private long timeTaken;
45  	private long timestamp;
46  	private DefaultActionList actions;
47  	private StringToStringMap responseHeaders = new StringToStringMap();
48  	private WsdlMockRequest mockRequest;
49  	private byte[] rawResponseData;
50  	private WsdlMockOperation mockOperation;
51  	private String responseContentType;
52  	private int responseStatus = 200;
53  
54  	private SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS" );
55  
56  	public WsdlMockResult( WsdlMockRequest request ) throws Exception
57  	{
58  		timestamp = System.currentTimeMillis();
59  		mockRequest = request;
60  	}
61  
62  	public WsdlMockRequest getMockRequest()
63  	{
64  		return mockRequest;
65  	}
66  
67  	public ActionList getActions()
68  	{
69  		if( actions == null )
70  		{
71  			actions = new DefaultActionList( "MockResult" );
72  			actions.setDefaultAction( new ShowMessageExchangeAction(
73  					new WsdlMockResultMessageExchange( this, mockResponse ), "MockResult" ) );
74  		}
75  
76  		return actions;
77  	}
78  
79  	public WsdlMockResponse getMockResponse()
80  	{
81  		return mockResponse;
82  	}
83  
84  	public String getResponseContent()
85  	{
86  		return responseContent;
87  	}
88  
89  	public long getTimeTaken()
90  	{
91  		return timeTaken;
92  	}
93  
94  	public long getTimestamp()
95  	{
96  		return timestamp;
97  	}
98  
99  	public void setTimestamp( long timestamp )
100 	{
101 		this.timestamp = timestamp;
102 	}
103 
104 	public void setTimeTaken( long timeTaken )
105 	{
106 		this.timeTaken = timeTaken;
107 	}
108 
109 	public StringToStringMap getResponseHeaders()
110 	{
111 		return responseHeaders;
112 	}
113 
114 	public void setMockResponse( WsdlMockResponse mockResponse )
115 	{
116 		this.mockResponse = mockResponse;
117 		mockRequest.getRequestContext().setMockResponse( mockResponse );
118 	}
119 
120 	/***
121 	 * @deprecated
122 	 */
123 
124 	public void setReponseContent( String responseContent )
125 	{
126 		this.responseContent = responseContent;
127 	}
128 
129 	public void setResponseContent( String responseContent )
130 	{
131 		this.responseContent = responseContent;
132 	}
133 
134 	@SuppressWarnings( "unchecked" )
135 	public void finish()
136 	{
137 		if( mockRequest.getHttpResponse() instanceof org.mortbay.jetty.Response )
138 		{
139 			HttpFields httpFields = ( ( org.mortbay.jetty.Response )mockRequest.getHttpResponse() ).getHttpFields();
140 
141 			Enumeration<String> e = httpFields.getFieldNames();
142 			while( e.hasMoreElements() )
143 			{
144 				String nextElement = e.nextElement();
145 				responseHeaders.put( nextElement, httpFields.getStringField( nextElement ) );
146 			}
147 		}
148 	}
149 
150 	public void addHeader( String name, String value )
151 	{
152 		if( mockRequest.getHttpResponse() != null )
153 			mockRequest.getHttpResponse().addHeader( name, value );
154 
155 		responseHeaders.put( name, value );
156 	}
157 
158 	public boolean isCommitted()
159 	{
160 		return mockRequest.getHttpResponse().isCommitted();
161 	}
162 
163 	public void setContentType( String contentType )
164 	{
165 		mockRequest.getHttpResponse().setContentType( contentType );
166 		responseContentType = contentType;
167 	}
168 
169 	public OutputStream getOutputStream() throws IOException
170 	{
171 		return mockRequest.getHttpResponse().getOutputStream();
172 	}
173 
174 	public void initResponse()
175 	{
176 		mockRequest.getHttpResponse().setStatus( HttpServletResponse.SC_OK );
177 		responseStatus = HttpServletResponse.SC_OK;
178 	}
179 
180 	public boolean isDiscarded()
181 	{
182 		return false;
183 	}
184 
185 	public Vector<?> getRequestWssResult()
186 	{
187 		return mockRequest.getWssResult();
188 	}
189 
190 	public byte[] getRawResponseData()
191 	{
192 		return rawResponseData;
193 	}
194 
195 	public void setRawResponseData( byte[] rawResponseData )
196 	{
197 		this.rawResponseData = rawResponseData;
198 	}
199 
200 	public void writeRawResponseData( byte[] bs ) throws IOException
201 	{
202 		getOutputStream().write( bs );
203 		setRawResponseData( bs );
204 	}
205 
206 	public void setMockOperation( WsdlMockOperation mockOperation )
207 	{
208 		this.mockOperation = mockOperation;
209 	}
210 
211 	public WsdlMockOperation getMockOperation()
212 	{
213 		if( mockOperation != null )
214 			return mockOperation;
215 
216 		return mockResponse == null ? null : mockResponse.getMockOperation();
217 	}
218 
219 	public String getResponseContentType()
220 	{
221 		return responseContentType;
222 	}
223 
224 	public int getResponseStatus()
225 	{
226 		return responseStatus;
227 	}
228 
229 	public void setResponseStatus( int responseStatus )
230 	{
231 		this.responseStatus = responseStatus;
232 	}
233 
234 	public void setResponseContentType( String responseContentType )
235 	{
236 		this.responseContentType = responseContentType;
237 	}
238 
239 	public String toString()
240 	{
241 		StringBuilder msg = new StringBuilder( dateFormat.format( new Date( getTimestamp() ) ) );
242 		MockResponse mockResponse = getMockResponse();
243 
244 		if( mockResponse == null )
245 		{
246 			msg.append( ": [dispatch error; missing response]" );
247 		}
248 		else
249 		{
250 			try
251 			{
252 				msg.append( ": [" + mockResponse.getMockOperation().getName() );
253 			}
254 			catch( Throwable e )
255 			{
256 				msg.append( ": [removed operation?]" );
257 			}
258 
259 			msg.append( "] " + getTimeTaken() + "ms" );
260 		}
261 		return msg.toString();
262 	}
263 }