View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.IOException;
17  import java.lang.ref.WeakReference;
18  import java.net.URL;
19  
20  import org.apache.commons.httpclient.Header;
21  
22  import com.eviware.soapui.SoapUI;
23  import com.eviware.soapui.impl.rest.RestRequestInterface;
24  import com.eviware.soapui.impl.rest.support.MediaTypeHandler;
25  import com.eviware.soapui.impl.rest.support.MediaTypeHandlerRegistry;
26  import com.eviware.soapui.impl.support.AbstractHttpRequestInterface;
27  import com.eviware.soapui.model.iface.Attachment;
28  import com.eviware.soapui.model.settings.Settings;
29  import com.eviware.soapui.settings.HttpSettings;
30  import com.eviware.soapui.support.types.StringToStringMap;
31  
32  public abstract class BaseHttpResponse implements HttpResponse
33  {
34  	private StringToStringMap requestHeaders;
35  	private StringToStringMap responseHeaders;
36  
37  	private long timeTaken;
38  	private long timestamp;
39  	private String contentType;
40  	private int statusCode;
41  	private SSLInfo sslInfo;
42  	private URL url;
43  	private WeakReference<AbstractHttpRequestInterface<?>> httpRequest;
44  	private RestRequestInterface.RequestMethod method;
45  	private String version;
46  	private StringToStringMap properties;
47  	private byte[] rawRequestData;
48  	private byte[] rawResponseData;
49  	private int requestContentPos = -1;
50  	private String xmlContent;
51  
52  	public BaseHttpResponse( ExtendedHttpMethod httpMethod, AbstractHttpRequestInterface<?> httpRequest )
53  	{
54  		this.httpRequest = new WeakReference<AbstractHttpRequestInterface<?>>( httpRequest );
55  		this.timeTaken = httpMethod.getTimeTaken();
56  
57  		method = httpMethod.getMethod();
58  		version = httpMethod.getParams().getVersion().toString();
59  		try
60  		{
61  			this.url = new URL( httpMethod.getURI().toString() );
62  		}
63  		catch( Exception e1 )
64  		{
65  			SoapUI.logError( e1 );
66  		}
67  
68  		if( !httpMethod.isFailed() )
69  		{
70  			Settings settings = httpRequest.getSettings();
71  			if( settings.getBoolean( HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN ) )
72  			{
73  				try
74  				{
75  					httpMethod.getResponseBody();
76  				}
77  				catch( IOException e )
78  				{
79  					e.printStackTrace();
80  				}
81  				timeTaken += httpMethod.getResponseReadTime();
82  			}
83  
84  			try
85  			{
86  				this.timestamp = System.currentTimeMillis();
87  				this.contentType = httpMethod.getResponseContentType();
88  
89  				if( httpMethod.hasResponse() )
90  				{
91  					this.statusCode = httpMethod.getStatusCode();
92  					this.sslInfo = httpMethod.getSSLInfo();
93  				}
94  
95  				this.url = new URL( httpMethod.getURI().toString() );
96  			}
97  			catch( Throwable e )
98  			{
99  				e.printStackTrace();
100 			}
101 		}
102 
103 		initHeaders( httpMethod );
104 	}
105 
106 	protected void initHeaders( ExtendedHttpMethod httpMethod )
107 	{
108 		try
109 		{
110 			ByteArrayOutputStream rawResponse = new ByteArrayOutputStream();
111 			ByteArrayOutputStream rawRequest = new ByteArrayOutputStream();
112 
113 			if( !httpMethod.isFailed() )
114 			{
115 				rawResponse.write( String.valueOf( httpMethod.getStatusLine() ).getBytes() );
116 				rawResponse.write( "\r\n".getBytes() );
117 			}
118 
119 			rawRequest.write( ( method + " " + String.valueOf( url ) + " " + version + "\r\n" ).getBytes() );
120 
121 			requestHeaders = new StringToStringMap();
122 			Header[] headers = httpMethod.getRequestHeaders();
123 			for( Header header : headers )
124 			{
125 				requestHeaders.put( header.getName(), header.getValue() );
126 				rawRequest.write( header.toExternalForm().getBytes() );
127 			}
128 
129 			if( !httpMethod.isFailed() )
130 			{
131 				responseHeaders = new StringToStringMap();
132 				headers = httpMethod.getResponseHeaders();
133 				for( Header header : headers )
134 				{
135 					responseHeaders.put( header.getName(), header.getValue() );
136 					rawResponse.write( header.toExternalForm().getBytes() );
137 				}
138 
139 				responseHeaders.put( "#status#", String.valueOf( httpMethod.getStatusLine() ) );
140 			}
141 
142 			if( httpMethod.getRequestEntity() != null )
143 			{
144 				rawRequest.write( "\r\n".getBytes() );
145 				if( httpMethod.getRequestEntity().isRepeatable() )
146 				{
147 					requestContentPos = rawRequest.size();
148 					httpMethod.getRequestEntity().writeRequest( rawRequest );
149 				}
150 				else
151 					rawResponse.write( "<request data not available>".getBytes() );
152 			}
153 
154 			if( !httpMethod.isFailed() )
155 			{
156 				rawResponse.write( "\r\n".getBytes() );
157 				rawResponse.write( httpMethod.getResponseBody() );
158 			}
159 
160 			rawResponseData = rawResponse.toByteArray();
161 			rawRequestData = rawRequest.toByteArray();
162 		}
163 		catch( Throwable e )
164 		{
165 			e.printStackTrace();
166 		}
167 	}
168 
169 	public StringToStringMap getRequestHeaders()
170 	{
171 		return requestHeaders;
172 	}
173 
174 	public StringToStringMap getResponseHeaders()
175 	{
176 		return responseHeaders;
177 	}
178 
179 	public long getTimeTaken()
180 	{
181 		return timeTaken;
182 	}
183 
184 	public SSLInfo getSSLInfo()
185 	{
186 		return sslInfo;
187 	}
188 
189 	public long getTimestamp()
190 	{
191 		return timestamp;
192 	}
193 
194 	public String getContentType()
195 	{
196 		return contentType;
197 	}
198 
199 	public URL getURL()
200 	{
201 		return url;
202 	}
203 
204 	public AbstractHttpRequestInterface<?> getRequest()
205 	{
206 		return httpRequest.get();
207 	}
208 
209 	public int getStatusCode()
210 	{
211 		return statusCode;
212 	}
213 
214 	public Attachment[] getAttachments()
215 	{
216 		return new Attachment[0];
217 	}
218 
219 	public Attachment[] getAttachmentsForPart( String partName )
220 	{
221 		return new Attachment[0];
222 	}
223 
224 	public byte[] getRawRequestData()
225 	{
226 		return rawRequestData;
227 	}
228 
229 	public byte[] getRawResponseData()
230 	{
231 		return rawResponseData;
232 	}
233 
234 	public RestRequestInterface.RequestMethod getMethod()
235 	{
236 		return method;
237 	}
238 
239 	public String getHttpVersion()
240 	{
241 		return version;
242 	}
243 
244 	public void setProperty( String name, String value )
245 	{
246 		if( properties == null )
247 			properties = new StringToStringMap();
248 
249 		properties.put( name, value );
250 	}
251 
252 	public String getProperty( String name )
253 	{
254 		return properties == null ? null : properties.get( name );
255 	}
256 
257 	public String[] getPropertyNames()
258 	{
259 		return properties == null ? new String[0] : properties.getKeys();
260 	}
261 
262 	public String getRequestContent()
263 	{
264 		return requestContentPos == -1 || rawRequestData == null ? null : new String( rawRequestData, requestContentPos, rawRequestData.length
265 				- requestContentPos );
266 	}
267 
268 	public String getContentAsXml()
269 	{
270 		if( xmlContent == null )
271 		{
272 			MediaTypeHandler typeHandler = MediaTypeHandlerRegistry.getTypeHandler( getContentType() );
273 			xmlContent = ( typeHandler == null ) ? "<xml/>" : typeHandler.createXmlRepresentation( this );
274 		}
275 		return xmlContent;
276 	}
277 }