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.teststeps;
14  
15  import java.io.PrintWriter;
16  import java.lang.ref.SoftReference;
17  
18  import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse;
19  import com.eviware.soapui.impl.wsdl.teststeps.actions.ShowMessageExchangeAction;
20  import com.eviware.soapui.model.iface.Attachment;
21  import com.eviware.soapui.model.iface.MessageExchange;
22  import com.eviware.soapui.model.iface.Response;
23  import com.eviware.soapui.support.action.ActionList;
24  import com.eviware.soapui.support.types.StringToStringMap;
25  import com.eviware.soapui.support.xml.XmlUtils;
26  
27  public class WsdlTestRequestStepResult extends WsdlTestStepResult implements MessageExchange
28  {
29  	private SoftReference<String> requestContent;
30  	private SoftReference<WsdlResponse> response;
31  	private String domain;
32  	private String username;
33  	private String endpoint;
34  	private String encoding;
35  	private String password;
36  	private StringToStringMap properties;
37  	
38  	public WsdlTestRequestStepResult(WsdlTestRequestStep step )
39  	{
40  		super( step );
41  	}
42  
43  	public String getRequestContent()
44  	{
45  		if( isDiscarded() )
46  			return "<discarded>";
47  		
48  		if( requestContent != null && requestContent.get() == null ) 
49  			return "<garbage-collected>";
50  		
51  		return requestContent == null ? null : requestContent.get();
52  	}
53  
54  	public void setRequestContent(String requestContent)
55  	{
56  		this.requestContent = new SoftReference<String>(requestContent);
57  	}
58  
59  	public WsdlResponse getResponse()
60  	{
61  		if( isDiscarded() )
62  			return null;
63  		
64  		return response == null ? null : response.get();
65  	}
66  	
67  	@Override
68  	public ActionList getActions()
69  	{
70  		ActionList actions = super.getActions();
71  		if( actions == null ) 
72  		{
73  			if( isDiscarded() )
74  				return actions;
75  			else 
76  				addAction( new ShowMessageExchangeAction( this, "TestStep" ), true );
77  			
78  			return super.getActions();
79  		}
80  		
81  		return actions;
82  	}
83  
84  	public void setResponse(WsdlResponse response)
85  	{
86  		this.response = new SoftReference<WsdlResponse>(response);
87  	}
88  
89  	public String getDomain()
90  	{
91  		return domain;
92  	}
93  
94  	public void setDomain(String domain)
95  	{
96  		this.domain = domain;
97  		addProperty( "domain", domain );
98  	}
99  
100 	private void addProperty( String key, String value )
101 	{
102 		if( properties == null )
103 			properties = new StringToStringMap();
104 		
105 		properties.put( key, value );
106 	}
107 
108 	public String getEncoding()
109 	{
110 		return encoding;
111 	}
112 
113 	public void setEncoding(String encoding)
114 	{
115 		this.encoding = encoding;
116 		addProperty( "encoding", encoding );
117 	}
118 
119 	public String getEndpoint()
120 	{
121 		return endpoint;
122 	}
123 
124 	public void setEndpoint(String endpoint)
125 	{
126 		this.endpoint = endpoint;
127 		addProperty( "endpoint", endpoint );
128 	}
129 
130 	public String getPassword()
131 	{
132 		return password;
133 	}
134 
135 	public void setPassword(String password)
136 	{
137 		this.password = password;
138 		addProperty( "password", password );
139 	}
140 
141 	public String getUsername()
142 	{
143 		return username;
144 	}
145 
146 	public void setUsername(String username)
147 	{
148 		this.username = username;
149 		addProperty( "username", username );
150 	}
151 
152 	public void discard()
153 	{
154 		super.discard();
155 		
156 		requestContent = null;
157 		response = null;
158 		properties = null;
159 	}
160 
161 	public void writeTo(PrintWriter writer)
162 	{
163 		super.writeTo( writer );
164 		
165 		writer.println( "----------------------------------------------------" );
166 		writer.println( "Encoding: " + getEncoding() );
167 		writer.println( "Endpoint: " + getEndpoint() );
168 		writer.println( "Username: " + getUsername() );
169 		writer.println( "Password: " + getPassword() );
170 		writer.println( "Domain: " + getDomain() );
171 
172 		Response resp = response == null ? null : response.get();
173 		
174 		writer.println( "---------------- Request ---------------------------" );
175 		String request = requestContent == null ? null : requestContent.get();
176 		if( request != null )
177 			writer.println( XmlUtils.prettyPrintXml( request ) );
178 		else
179 			writer.println( "- missing request / garbage collected -" );
180 
181 		if( resp != null )
182 		{
183 			writer.println( "Request Headers: " + resp.getRequestHeaders().toString() );
184 		}
185 		
186 		writer.println( "---------------- Response --------------------------" );
187 		if( resp != null )
188 		{
189 			String respContent = resp.getContentAsString();
190 			if( respContent != null )
191 				writer.println( XmlUtils.prettyPrintXml( respContent ) );
192 
193 			writer.println( "Response Headers: " + resp.getResponseHeaders().toString() );
194 		}
195 		else
196 			writer.println( "- missing response / garbage collected -" );
197 	}
198 
199 	public StringToStringMap getProperties()
200 	{
201 		return properties;
202 	}
203 
204 	public Attachment[] getRequestAttachments()
205 	{
206 		if( isDiscarded() || response == null|| response.get() == null )
207 			return null;
208 		
209 		return response.get().getRequest().getAttachments();
210 	}
211 
212 	public StringToStringMap getRequestHeaders()
213 	{
214 		if( isDiscarded() || response == null || response.get() == null )
215 			return null;
216 		
217 		return response.get().getRequestHeaders();
218 	}
219 
220 	public Attachment[] getResponseAttachments()
221 	{
222 		if( isDiscarded() || response == null || response.get() == null )
223 			return null;
224 		
225 		return response.get().getAttachments();
226 	}
227 
228 	public String getResponseContent()
229 	{
230 		if( isDiscarded() )
231 			return "<discarded>";
232 		
233 		if( response == null )
234 			return "<missing response>";
235 		
236 		if( response != null && response.get() == null ) 
237 			return "<garbage-collected>";
238 		
239 		return response.get().getContentAsString();
240 	}
241 
242 	public StringToStringMap getResponseHeaders()
243 	{
244 		if( isDiscarded() || response == null || response.get() == null )
245 			return null;
246 		
247 		return response.get().getResponseHeaders();
248 	}
249 
250 	public long getTimestamp()
251 	{
252 		if( isDiscarded() || response == null || response.get() == null )
253 			return -1;
254 		
255 		return response.get().getTimestamp();
256 	}
257 }