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.submit.transports.http;
14  
15  import java.io.ByteArrayOutputStream;
16  import java.io.StringWriter;
17  import java.lang.ref.WeakReference;
18  import java.util.Vector;
19  
20  import org.apache.commons.httpclient.Header;
21  import org.w3c.dom.Document;
22  
23  import com.eviware.soapui.SoapUI;
24  import com.eviware.soapui.impl.wsdl.WsdlRequest;
25  import com.eviware.soapui.impl.wsdl.submit.filters.WssRequestFilter;
26  import com.eviware.soapui.impl.wsdl.support.wss.IncomingWss;
27  import com.eviware.soapui.model.iface.Attachment;
28  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
29  import com.eviware.soapui.model.settings.Settings;
30  import com.eviware.soapui.settings.HttpSettings;
31  import com.eviware.soapui.settings.WsdlSettings;
32  import com.eviware.soapui.support.StringUtils;
33  import com.eviware.soapui.support.types.StringToStringMap;
34  import com.eviware.soapui.support.xml.XmlUtils;
35  
36  /***
37   * Simple response to a request
38   * 
39   * @author ole.matzura
40   */
41  
42  final class SinglePartHttpResponse implements WsdlResponse
43  {
44  	private final WeakReference<WsdlRequest> wsdlRequest;
45  	@SuppressWarnings("unused")
46  	private final TimeablePostMethod postMethod;
47  	private long timeTaken;
48  	private String responseContent;
49  	private StringToStringMap requestHeaders;
50  	private StringToStringMap responseHeaders;
51  	private final String requestContent;
52  	private boolean prettyPrint;
53  	private SSLInfo sslInfo;
54  	private long timestamp;
55  	private long responseSize;
56  	private Vector wssResult;
57  	private byte[] requestData;
58  	private byte[] responseBody;
59  	
60  	public SinglePartHttpResponse(WsdlRequest wsdlRequest, TimeablePostMethod postMethod, String requestContent, PropertyExpansionContext context )
61  	{
62  		this.wsdlRequest = new WeakReference<WsdlRequest>(wsdlRequest);
63  		this.postMethod = postMethod;
64  		this.requestContent = requestContent;
65  		this.timeTaken = postMethod.getTimeTaken();
66  		this.sslInfo = postMethod.getSSLInfo();
67  		this.timestamp = System.currentTimeMillis();
68  		
69  		// read response immediately since we need to release connection
70  		Settings settings = wsdlRequest.getSettings();
71  		
72  		try
73  		{
74  			responseBody = postMethod.getResponseBody();
75  			responseSize = responseBody.length;
76  			if (settings.getBoolean(HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN))
77  				timeTaken = postMethod.getTimeTakenUntilNow();
78  			
79  			String charset = postMethod.getResponseCharSet();
80  			if( charset == null )
81  				charset = wsdlRequest.getEncoding();
82  			
83  			charset = StringUtils.unquote( charset );
84  			
85  			responseContent = responseBody.length == 0 ? null : charset == null ? new String(responseBody) : new String(
86  					responseBody, charset);
87  			
88  			processIncomingWss( wsdlRequest, context );
89  			
90  			prettyPrint = wsdlRequest.getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES );
91  			
92  			initHeaders(postMethod);
93  			
94  			ByteArrayOutputStream out = new ByteArrayOutputStream();
95  			postMethod.getRequestEntity().writeRequest( out );
96  			requestData = out.toByteArray();
97  		}
98  		catch( Exception e )
99  		{
100 			SoapUI.logError( e );
101 		}
102 	}
103 
104 	private void processIncomingWss( WsdlRequest wsdlRequest, PropertyExpansionContext context )
105 	{
106 		IncomingWss incomingWss = ( IncomingWss ) context.getProperty( WssRequestFilter.INCOMING_WSS_PROPERTY );
107 		if( incomingWss != null )
108 		{
109 			try
110 			{
111 				Document document = XmlUtils.parseXml( responseContent );
112 				wssResult = incomingWss.processIncoming( document, context );
113 				if( wssResult != null && wssResult.size() > 0 )
114 				{
115 					StringWriter writer = new StringWriter();
116 					XmlUtils.serializePretty( document, writer );
117 					responseContent = writer.toString();
118 				}
119 			}
120 			catch( Exception e )
121 			{
122 				if( wssResult == null )
123 					wssResult = new Vector();
124 				wssResult.add( e );
125 			}
126 		}
127 	}
128 	
129 	private void initHeaders(TimeablePostMethod postMethod)
130 	{
131 		requestHeaders = new StringToStringMap();
132 		Header[] headers = postMethod.getRequestHeaders();
133 		for( Header header : headers )
134 		{
135 			requestHeaders.put( header.getName(), header.getValue() );
136 		}
137 		
138 		responseHeaders = new StringToStringMap();
139 		headers = postMethod.getResponseHeaders();
140 		for( Header header : headers )
141 		{
142 			responseHeaders.put( header.getName(), header.getValue() );
143 		}
144 		
145 		responseHeaders.put( "#status#", postMethod.getStatusLine().toString() );
146 	}
147 
148 	public String getContentAsString()
149 	{
150 		if( prettyPrint )
151 		{
152 			responseContent = XmlUtils.prettyPrintXml( responseContent );
153 			prettyPrint = false;
154 		}
155 		
156 		return responseContent;
157 	}
158 
159 	public long getContentLength()
160 	{
161 		return responseSize;
162 	}
163 
164 	public WsdlRequest getRequest()
165 	{
166 		return wsdlRequest.get();
167 	}
168 
169 	public long getTimeTaken()
170 	{
171 		return timeTaken;
172 	}
173 
174 	public Attachment[] getAttachments()
175 	{
176 		return new Attachment[0];
177 	}
178 
179 	public StringToStringMap getRequestHeaders()
180 	{
181 		return requestHeaders;
182 	}
183 
184 	public StringToStringMap getResponseHeaders()
185 	{
186 		return responseHeaders;
187 	}
188 
189 	public Attachment[] getAttachmentsForPart(String partName)
190 	{
191 		return new Attachment[0];
192 	}
193 
194 	public String getRequestContent()
195 	{
196 		return requestContent;
197 	}
198 
199 	public void setResponseContent(String responseContent)
200 	{
201 		String oldContent = this.responseContent;
202 		this.responseContent = responseContent;
203 		
204 		getRequest().notifyPropertyChanged( WsdlRequest.RESPONSE_CONTENT_PROPERTY, oldContent, responseContent );
205 	}
206 
207 	public SSLInfo getSSLInfo()
208 	{
209 		return sslInfo;
210 	}
211 
212 	public long getTimestamp()
213 	{
214 		return timestamp;
215 	}
216 
217 	public Vector getWssResult()
218 	{
219 		return wssResult;
220 	}
221 
222 	public byte[] getRawRequestData()
223 	{
224 		return requestData;
225 	}
226 
227 	public byte[] getRawResponseData()
228 	{
229 		return responseBody;
230 	}
231 }