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.lang.ref.WeakReference;
16  
17  import org.apache.commons.httpclient.Header;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.impl.wsdl.WsdlRequest;
21  import com.eviware.soapui.model.iface.Attachment;
22  import com.eviware.soapui.model.settings.Settings;
23  import com.eviware.soapui.settings.HttpSettings;
24  import com.eviware.soapui.settings.WsdlSettings;
25  import com.eviware.soapui.support.StringUtils;
26  import com.eviware.soapui.support.types.StringToStringMap;
27  import com.eviware.soapui.support.xml.XmlUtils;
28  
29  /***
30   * Simple response to a request
31   * 
32   * @author ole.matzura
33   */
34  
35  final class SinglePartHttpResponse implements WsdlResponse
36  {
37  	private final WeakReference<WsdlRequest> wsdlRequest;
38  	@SuppressWarnings("unused")
39  	private final TimeablePostMethod postMethod;
40  	private long timeTaken;
41  	private String responseContent;
42  	private StringToStringMap requestHeaders;
43  	private StringToStringMap responseHeaders;
44  	private final String requestContent;
45  	private boolean prettyPrint;
46  	private SSLInfo sslInfo;
47  	private long timestamp;
48  	private long responseSize;
49  	
50  	public SinglePartHttpResponse(WsdlRequest wsdlRequest, TimeablePostMethod postMethod, String requestContent )
51  	{
52  		this.wsdlRequest = new WeakReference<WsdlRequest>(wsdlRequest);
53  		this.postMethod = postMethod;
54  		this.requestContent = requestContent;
55  		this.timeTaken = postMethod.getTimeTaken();
56  		this.sslInfo = postMethod.getSSLInfo();
57  		this.timestamp = System.currentTimeMillis();
58  		
59  		// read response immediately since we need to release connection
60  		Settings settings = wsdlRequest.getSettings();
61  		
62  		try
63  		{
64  			byte[] responseBody = postMethod.getResponseBody();
65  			responseSize = responseBody.length;
66  			if (settings.getBoolean(HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN))
67  				timeTaken = postMethod.getTimeTakenUntilNow();
68  			
69  			String charset = postMethod.getResponseCharSet();
70  			if( charset == null )
71  				charset = wsdlRequest.getEncoding();
72  			
73  			charset = StringUtils.unquote( charset );
74  			
75  			responseContent = responseBody.length == 0 ? null : charset == null ? new String(responseBody) : new String(
76  					responseBody, charset);
77  			
78  			prettyPrint = wsdlRequest.getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES );
79  			
80  			initHeaders(postMethod);
81  		}
82  		catch( Exception e )
83  		{
84  			SoapUI.logError( e );
85  		}
86  	}
87  	
88  	private void initHeaders(TimeablePostMethod postMethod)
89  	{
90  		requestHeaders = new StringToStringMap();
91  		Header[] headers = postMethod.getRequestHeaders();
92  		for( Header header : headers )
93  		{
94  			requestHeaders.put( header.getName(), header.getValue() );
95  		}
96  		
97  		responseHeaders = new StringToStringMap();
98  		headers = postMethod.getResponseHeaders();
99  		for( Header header : headers )
100 		{
101 			responseHeaders.put( header.getName(), header.getValue() );
102 		}
103 		
104 		responseHeaders.put( "#status#", postMethod.getStatusLine().toString() );
105 	}
106 
107 	public String getContentAsString()
108 	{
109 		if( prettyPrint )
110 		{
111 			responseContent = XmlUtils.prettyPrintXml( responseContent );
112 			prettyPrint = false;
113 		}
114 		
115 		return responseContent;
116 	}
117 
118 	public long getContentLength()
119 	{
120 		return responseSize;
121 	}
122 
123 	public WsdlRequest getRequest()
124 	{
125 		return wsdlRequest.get();
126 	}
127 
128 	public long getTimeTaken()
129 	{
130 		return timeTaken;
131 	}
132 
133 	public Attachment[] getAttachments()
134 	{
135 		return new Attachment[0];
136 	}
137 
138 	public StringToStringMap getRequestHeaders()
139 	{
140 		return requestHeaders;
141 	}
142 
143 	public StringToStringMap getResponseHeaders()
144 	{
145 		return responseHeaders;
146 	}
147 
148 	public Attachment[] getAttachmentsForPart(String partName)
149 	{
150 		return new Attachment[0];
151 	}
152 
153 	public String getRequestContent()
154 	{
155 		return requestContent;
156 	}
157 
158 	public void setResponseContent(String responseContent)
159 	{
160 		this.responseContent = responseContent;
161 	}
162 
163 	public SSLInfo getSSLInfo()
164 	{
165 		return sslInfo;
166 	}
167 
168 	public long getTimestamp()
169 	{
170 		return timestamp;
171 	}
172 }