1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.submit.transports.http;
14
15 import com.eviware.soapui.impl.support.AbstractHttpRequest;
16 import com.eviware.soapui.model.iface.Attachment;
17 import com.eviware.soapui.model.settings.Settings;
18 import com.eviware.soapui.settings.HttpSettings;
19 import com.eviware.soapui.support.types.StringToStringMap;
20 import org.apache.commons.httpclient.Header;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.lang.ref.WeakReference;
25 import java.net.URL;
26
27 public abstract class BaseHttpResponse implements HttpResponse
28 {
29 private StringToStringMap requestHeaders;
30 private StringToStringMap responseHeaders;
31
32 private long timeTaken;
33 private long timestamp;
34 private String contentType;
35 private int statusCode;
36 private SSLInfo sslInfo;
37 private URL url;
38 private WeakReference<AbstractHttpRequest<?>> httpRequest;
39 private AbstractHttpRequest.RequestMethod method;
40 private String version;
41 private StringToStringMap properties;
42 private ByteArrayOutputStream rawRequestData = new ByteArrayOutputStream();
43 private ByteArrayOutputStream rawResponseData = new ByteArrayOutputStream();
44
45 public BaseHttpResponse( ExtendedHttpMethod httpMethod, AbstractHttpRequest<?> httpRequest )
46 {
47 this.httpRequest = new WeakReference<AbstractHttpRequest<?>>( httpRequest );
48 this.timeTaken = httpMethod.getTimeTaken();
49
50 method = httpMethod.getMethod();
51 version = httpMethod.getParams().getVersion().toString();
52
53 Settings settings = httpRequest.getSettings();
54 if( settings.getBoolean( HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN ) )
55 {
56 try
57 {
58 httpMethod.getResponseBody();
59 }
60 catch( IOException e )
61 {
62 e.printStackTrace();
63 }
64 timeTaken += httpMethod.getResponseReadTime();
65 }
66
67 this.timestamp = System.currentTimeMillis();
68 this.contentType = httpMethod.getResponseContentType();
69 this.statusCode = httpMethod.getStatusCode();
70 this.sslInfo = httpMethod.getSSLInfo();
71
72 try
73 {
74 this.url = new URL( httpMethod.getURI().toString() );
75 }
76 catch( Exception e )
77 {
78 e.printStackTrace();
79 }
80
81 initHeaders( httpMethod );
82 }
83
84 protected void initHeaders( ExtendedHttpMethod httpMethod )
85 {
86 try
87 {
88 rawResponseData.write( httpMethod.getStatusLine().toString().getBytes() );
89 rawResponseData.write( "\r\n".getBytes() );
90 rawRequestData.write( ( method + " " + url.toString() + " " + version + "\r\n" ).getBytes() );
91
92 requestHeaders = new StringToStringMap();
93 Header[] headers = httpMethod.getRequestHeaders();
94 for( Header header : headers )
95 {
96 requestHeaders.put( header.getName(), header.getValue() );
97 rawRequestData.write( header.toExternalForm().getBytes() );
98 }
99
100 responseHeaders = new StringToStringMap();
101 headers = httpMethod.getResponseHeaders();
102 for( Header header : headers )
103 {
104 responseHeaders.put( header.getName(), header.getValue() );
105 rawResponseData.write( header.toExternalForm().getBytes() );
106 }
107
108 responseHeaders.put( "#status#", httpMethod.getStatusLine().toString() );
109
110 if( httpMethod.getRequestEntity() != null )
111 {
112 rawRequestData.write( "\r\n".getBytes() );
113 if( httpMethod.getRequestEntity().isRepeatable() )
114 httpMethod.getRequestEntity().writeRequest( rawRequestData );
115 else
116 rawRequestData.write( "<request data not available>".getBytes() );
117 }
118
119 rawResponseData.write( "\r\n".getBytes() );
120 rawResponseData.write( httpMethod.getResponseBody() );
121 }
122 catch( Exception e )
123 {
124 e.printStackTrace();
125 }
126 }
127
128 public StringToStringMap getRequestHeaders()
129 {
130 return requestHeaders;
131 }
132
133 public StringToStringMap getResponseHeaders()
134 {
135 return responseHeaders;
136 }
137
138 public long getTimeTaken()
139 {
140 return timeTaken;
141 }
142
143 public SSLInfo getSSLInfo()
144 {
145 return sslInfo;
146 }
147
148 public long getTimestamp()
149 {
150 return timestamp;
151 }
152
153 public String getContentType()
154 {
155 return contentType;
156 }
157
158 public URL getURL()
159 {
160 return url;
161 }
162
163 public AbstractHttpRequest<?> getRequest()
164 {
165 return httpRequest.get();
166 }
167
168 public int getStatusCode()
169 {
170 return statusCode;
171 }
172
173 public Attachment[] getAttachments()
174 {
175 return new Attachment[0];
176 }
177
178 public Attachment[] getAttachmentsForPart( String partName )
179 {
180 return new Attachment[0];
181 }
182
183 public byte[] getRawRequestData()
184 {
185 return rawRequestData.toByteArray();
186 }
187
188 public byte[] getRawResponseData()
189 {
190 return rawResponseData.toByteArray();
191 }
192
193 public AbstractHttpRequest.RequestMethod getMethod()
194 {
195 return method;
196 }
197
198 public String getHttpVersion()
199 {
200 return version;
201 }
202
203 public void setProperty( String name, String value )
204 {
205 if( properties == null )
206 properties = new StringToStringMap();
207
208 properties.put( name, value );
209 }
210
211 public String getProperty( String name )
212 {
213 return properties == null ? null : properties.get( name );
214 }
215
216 public String[] getPropertyNames()
217 {
218 return properties == null ? new String[0] : properties.getKeys();
219 }
220 }