View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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 org.apache.commons.httpclient.Header;
16  
17  import com.eviware.soapui.impl.wsdl.WsdlRequest;
18  import com.eviware.soapui.model.iface.Attachment;
19  import com.eviware.soapui.model.iface.Request;
20  import com.eviware.soapui.model.settings.Settings;
21  import com.eviware.soapui.settings.HttpSettings;
22  import com.eviware.soapui.settings.WsdlSettings;
23  import com.eviware.soapui.support.StringUtils;
24  import com.eviware.soapui.support.types.StringToStringMap;
25  import com.eviware.soapui.support.xml.XmlUtils;
26  
27  final class SinglePartHttpResponse implements WsdlResponse
28  {
29  	private final WsdlRequest wsdlRequest;
30  	private final TimeablePostMethod postMethod;
31  	private long timeTaken;
32  	private String responseContent;
33  	private StringToStringMap requestHeaders;
34  	private StringToStringMap responseHeaders;
35  	private final String requestContent;
36  	private boolean prettyPrint;
37  	private SSLInfo sslInfo;
38  	private long timestamp;
39  	
40  	public SinglePartHttpResponse(WsdlRequest wsdlRequest, TimeablePostMethod postMethod, String requestContent )
41  	{
42  		this.wsdlRequest = wsdlRequest;
43  		this.postMethod = postMethod;
44  		this.requestContent = requestContent;
45  		this.timeTaken = postMethod.getTimeTaken();
46  		this.sslInfo = postMethod.getSSLInfo();
47  		this.timestamp = System.currentTimeMillis();
48  		
49  		// read response immediately since we need to release connection
50  		Settings settings = wsdlRequest.getSettings();
51  		
52  		try
53  		{
54  			byte[] responseBody = postMethod.getResponseBody();
55  			if (settings.getBoolean(HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN))
56  				timeTaken = postMethod.getTimeTakenUntilNow();
57  			
58  			String charset = postMethod.getResponseCharSet();
59  			if( charset == null )
60  				charset = wsdlRequest.getEncoding();
61  			
62  			charset = StringUtils.unquote( charset );
63  			
64  			responseContent = responseBody.length == 0 ? null : charset == null ? new String(responseBody) : new String(
65  					responseBody, charset);
66  			
67  			prettyPrint = wsdlRequest.getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES );
68  			
69  			initHeaders(postMethod);
70  		}
71  		catch( Exception e )
72  		{
73  			e.printStackTrace();
74  		}
75  	}
76  	
77  	private void initHeaders(TimeablePostMethod postMethod)
78  	{
79  		requestHeaders = new StringToStringMap();
80  		Header[] headers = postMethod.getRequestHeaders();
81  		for( Header header : headers )
82  		{
83  			requestHeaders.put( header.getName(), header.getValue() );
84  		}
85  		
86  		responseHeaders = new StringToStringMap();
87  		headers = postMethod.getResponseHeaders();
88  		for( Header header : headers )
89  		{
90  			responseHeaders.put( header.getName(), header.getValue() );
91  		}
92  		
93  		responseHeaders.put( "#status#", postMethod.getStatusLine().toString() );
94  	}
95  
96  	public String getContentAsString()
97  	{
98  		if( prettyPrint )
99  		{
100 			responseContent = XmlUtils.prettyPrintXml( responseContent );
101 			prettyPrint = false;
102 		}
103 		
104 		return responseContent;
105 	}
106 
107 	public long getContentLength()
108 	{
109 		return responseContent == null ? 0 : responseContent.length();
110 	}
111 
112 	public Request getRequest()
113 	{
114 		return wsdlRequest;
115 	}
116 
117 	public long getTimeTaken()
118 	{
119 		return timeTaken;
120 	}
121 
122 	public Attachment[] getAttachments()
123 	{
124 		return new Attachment[0];
125 	}
126 
127 	public StringToStringMap getRequestHeaders()
128 	{
129 		return requestHeaders;
130 	}
131 
132 	public StringToStringMap getResponseHeaders()
133 	{
134 		return responseHeaders;
135 	}
136 
137 	public Attachment[] getAttachmentsForPart(String partName)
138 	{
139 		return new Attachment[0];
140 	}
141 
142 	public String getRequestContent()
143 	{
144 		return requestContent;
145 	}
146 
147 	public void setResponseContent(String responseContent)
148 	{
149 		this.responseContent = responseContent;
150 	}
151 
152 	public SSLInfo getSSLInfo()
153 	{
154 		return sslInfo;
155 	}
156 
157 	public long getTimestamp()
158 	{
159 		return timestamp;
160 	}
161 }