View Javadoc

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