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.teststeps;
14  
15  import java.io.PrintWriter;
16  import java.lang.ref.SoftReference;
17  import java.util.ArrayList;
18  import java.util.List;
19  import java.util.Vector;
20  
21  import com.eviware.soapui.impl.wsdl.WsdlOperation;
22  import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
23  import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse;
24  import com.eviware.soapui.impl.wsdl.support.assertions.AssertedXPathsContainer;
25  import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
26  import com.eviware.soapui.impl.wsdl.teststeps.actions.ShowMessageExchangeAction;
27  import com.eviware.soapui.model.ModelItem;
28  import com.eviware.soapui.model.iface.Attachment;
29  import com.eviware.soapui.model.iface.MessageExchange;
30  import com.eviware.soapui.model.testsuite.AssertedXPath;
31  import com.eviware.soapui.model.testsuite.MessageExchangeTestStepResult;
32  import com.eviware.soapui.model.testsuite.ResponseAssertedMessageExchange;
33  import com.eviware.soapui.support.action.swing.ActionList;
34  import com.eviware.soapui.support.types.StringToStringMap;
35  import com.eviware.soapui.support.types.StringToStringsMap;
36  import com.eviware.soapui.support.xml.XmlUtils;
37  
38  /***
39   * TestStepResult for a WsdlTestRequestStep
40   * 
41   * @author ole.matzura
42   */
43  
44  public class WsdlTestRequestStepResult extends WsdlTestStepResult implements ResponseAssertedMessageExchange,
45  		AssertedXPathsContainer, MessageExchangeTestStepResult, WsdlMessageExchange
46  {
47  	private SoftReference<String> softRequestContent;
48  	private SoftReference<WsdlResponse> softResponse;
49  	private String domain;
50  	private String username;
51  	private String endpoint;
52  	private String encoding;
53  	private String password;
54  	private StringToStringMap properties;
55  	private boolean addedAction;
56  	private List<AssertedXPath> assertedXPaths;
57  	private WsdlResponse response;
58  	private String requestContent;
59  	private WsdlOperation operation;
60  
61  	public WsdlTestRequestStepResult( WsdlTestRequestStep step )
62  	{
63  		super( step );
64  
65  		operation = step.getOperation();
66  	}
67  
68  	public WsdlOperation getOperation()
69  	{
70  		return operation;
71  	}
72  
73  	public SoapVersion getSoapVersion()
74  	{
75  		return ( ( WsdlOperation )getOperation() ).getInterface().getSoapVersion();
76  	}
77  
78  	public ModelItem getModelItem()
79  	{
80  		return getResponse() == null ? null : getResponse().getRequest();
81  	}
82  
83  	public String getRequestContent()
84  	{
85  		if( isDiscarded() )
86  			return "<discarded>";
87  
88  		return requestContent != null ? requestContent : softRequestContent == null ? null : softRequestContent.get();
89  	}
90  
91  	public void setRequestContent( String requestContent, boolean useSoftReference )
92  	{
93  		if( useSoftReference )
94  			this.softRequestContent = new SoftReference<String>( requestContent );
95  		else
96  			this.requestContent = requestContent;
97  	}
98  
99  	public WsdlResponse getResponse()
100 	{
101 		return response != null ? response : softResponse == null ? null : softResponse.get();
102 	}
103 
104 	@Override
105 	public ActionList getActions()
106 	{
107 		if( !addedAction )
108 		{
109 			addAction( new ShowMessageExchangeAction( this, "TestStep" ), true );
110 			addedAction = true;
111 		}
112 
113 		return super.getActions();
114 	}
115 
116 	public void setResponse( WsdlResponse response, boolean useSoftReference )
117 	{
118 		if( useSoftReference )
119 			this.softResponse = new SoftReference<WsdlResponse>( response );
120 		else
121 			this.response = response;
122 	}
123 
124 	public String getDomain()
125 	{
126 		return domain;
127 	}
128 
129 	public void setDomain( String domain )
130 	{
131 		this.domain = domain;
132 		addProperty( "domain", domain );
133 	}
134 
135 	private void addProperty( String key, String value )
136 	{
137 		if( properties == null )
138 			properties = new StringToStringMap();
139 
140 		properties.put( key, value );
141 	}
142 
143 	public String getEncoding()
144 	{
145 		return encoding;
146 	}
147 
148 	public void setEncoding( String encoding )
149 	{
150 		this.encoding = encoding;
151 		addProperty( "Encoding", encoding );
152 	}
153 
154 	public String getEndpoint()
155 	{
156 		return endpoint;
157 	}
158 
159 	public void setEndpoint( String endpoint )
160 	{
161 		this.endpoint = endpoint;
162 		addProperty( "Endpoint", endpoint );
163 	}
164 
165 	public String getPassword()
166 	{
167 		return password;
168 	}
169 
170 	public void setPassword( String password )
171 	{
172 		this.password = password;
173 		addProperty( "Password", password );
174 	}
175 
176 	public String getUsername()
177 	{
178 		return username;
179 	}
180 
181 	public void setUsername( String username )
182 	{
183 		this.username = username;
184 		addProperty( "Username", username );
185 	}
186 
187 	public void discard()
188 	{
189 		super.discard();
190 
191 		softRequestContent = null;
192 		softResponse = null;
193 		properties = null;
194 		assertedXPaths = null;
195 		response = null;
196 	}
197 
198 	public void writeTo( PrintWriter writer )
199 	{
200 		super.writeTo( writer );
201 		writer.println( "\r\n----------------- Properties ------------------------------" );
202 		if( properties != null )
203 		{
204 			for( String key : properties.keySet() )
205 			{
206 				if( properties.get( key ) != null )
207 					writer.println( key + ": " + properties.get( key ) );
208 			}
209 		}
210 
211 		writer.println( "\r\n---------------- Request ---------------------------" );
212 		WsdlResponse resp = getResponse();
213 		if( resp != null )
214 		{
215 			writer.println( "Request Headers: " + resp.getRequestHeaders().toString() + "\r\n" );
216 		}
217 
218 		if( getRequestContent() != null )
219 			writer.println( XmlUtils.prettyPrintXml( getRequestContent() ) );
220 		else
221 			writer.println( "- missing request / garbage collected -" );
222 
223 		writer.println( "\r\n---------------- Response --------------------------" );
224 		if( resp != null )
225 		{
226 			writer.println( "Response Headers: " + resp.getResponseHeaders().toString() + "\r\n" );
227 
228 			String respContent = resp.getContentAsString();
229 			if( respContent != null )
230 				writer.println( XmlUtils.prettyPrintXml( respContent ) );
231 		}
232 		else
233 			writer.println( "- missing response / garbage collected -" );
234 	}
235 
236 	public StringToStringMap getProperties()
237 	{
238 		return properties;
239 	}
240 
241 	public String getProperty( String name )
242 	{
243 		return properties.get( name );
244 	}
245 
246 	public Attachment[] getRequestAttachments()
247 	{
248 		if( getResponse() == null || getResponse().getRequest() == null )
249 			return new Attachment[0];
250 
251 		return getResponse().getRequest().getAttachments();
252 	}
253 
254 	public StringToStringsMap getRequestHeaders()
255 	{
256 		return getResponse() == null ? null : getResponse().getRequestHeaders();
257 	}
258 
259 	public Attachment[] getResponseAttachments()
260 	{
261 		return getResponse() == null ? null : getResponse().getAttachments();
262 	}
263 
264 	public String getResponseContent()
265 	{
266 		if( isDiscarded() )
267 			return "<discarded>";
268 
269 		if( getResponse() == null )
270 			return "<missing response>";
271 
272 		return getResponse().getContentAsString();
273 	}
274 
275 	public String getRequestContentAsXml()
276 	{
277 		return XmlUtils.seemsToBeXml( getRequestContent() ) ? getRequestContent() : "<not-xml/>";
278 	}
279 
280 	public String getResponseContentAsXml()
281 	{
282 		String responseContent = getResponseContent();
283 		return XmlUtils.seemsToBeXml( responseContent ) ? responseContent : null;
284 	}
285 
286 	public StringToStringsMap getResponseHeaders()
287 	{
288 		return getResponse() == null ? null : getResponse().getResponseHeaders();
289 	}
290 
291 	public long getTimestamp()
292 	{
293 		WsdlResponse resp = getResponse();
294 		return resp == null ? 0 : resp.getTimestamp();
295 	}
296 
297 	public AssertedXPath[] getAssertedXPathsForResponse()
298 	{
299 		return assertedXPaths == null ? new AssertedXPath[0] : assertedXPaths.toArray( new AssertedXPath[assertedXPaths
300 				.size()] );
301 	}
302 
303 	public void addAssertedXPath( AssertedXPath assertedXPath )
304 	{
305 		if( assertedXPaths == null )
306 			assertedXPaths = new ArrayList<AssertedXPath>();
307 
308 		assertedXPaths.add( assertedXPath );
309 	}
310 
311 	public MessageExchange[] getMessageExchanges()
312 	{
313 		return new MessageExchange[] { this };
314 	}
315 
316 	public byte[] getRawRequestData()
317 	{
318 		return getResponse() == null ? null : getResponse().getRawRequestData();
319 	}
320 
321 	public byte[] getRawResponseData()
322 	{
323 		return getResponse() == null ? null : getResponse().getRawResponseData();
324 	}
325 
326 	public Attachment[] getRequestAttachmentsForPart( String partName )
327 	{
328 		return null;
329 	}
330 
331 	public Attachment[] getResponseAttachmentsForPart( String partName )
332 	{
333 		return null;
334 	}
335 
336 	public boolean hasRawData()
337 	{
338 		return false;
339 	}
340 
341 	public boolean hasRequest( boolean b )
342 	{
343 		return true;
344 	}
345 
346 	public boolean hasResponse()
347 	{
348 		return getResponse() != null;
349 	}
350 
351 	public Vector<?> getRequestWssResult()
352 	{
353 		return null;
354 	}
355 
356 	public Vector<?> getResponseWssResult()
357 	{
358 		return getResponse() == null ? null : getResponse().getWssResult();
359 	}
360 
361 	public int getResponseStatusCode()
362 	{
363 		return getResponse() == null ? null : getResponse().getStatusCode();
364 	}
365 
366 	public String getResponseContentType()
367 	{
368 		return getResponse() == null ? null : getResponse().getContentType();
369 	}
370 }