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 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.support.wss.IncomingWss;
26 import com.eviware.soapui.impl.wsdl.support.wss.WssContainer;
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
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 WssContainer wssContainer = wsdlRequest.getOperation().getInterface().getProject().getWssContainer();
107 IncomingWss incomingWss = wssContainer.getIncomingByName( wsdlRequest.getIncomingWss() );
108 if( incomingWss != null )
109 {
110 try
111 {
112 Document document = XmlUtils.parseXml( responseContent );
113 wssResult = incomingWss.processIncoming( document, context );
114 if( wssResult != null && wssResult.size() > 0 )
115 {
116 StringWriter writer = new StringWriter();
117 XmlUtils.serializePretty( document, writer );
118 responseContent = writer.toString();
119 }
120 }
121 catch( Exception e )
122 {
123 if( wssResult != null )
124 wssResult = new Vector();
125 wssResult.add( e );
126 }
127 }
128 }
129
130 private void initHeaders(TimeablePostMethod postMethod)
131 {
132 requestHeaders = new StringToStringMap();
133 Header[] headers = postMethod.getRequestHeaders();
134 for( Header header : headers )
135 {
136 requestHeaders.put( header.getName(), header.getValue() );
137 }
138
139 responseHeaders = new StringToStringMap();
140 headers = postMethod.getResponseHeaders();
141 for( Header header : headers )
142 {
143 responseHeaders.put( header.getName(), header.getValue() );
144 }
145
146 responseHeaders.put( "#status#", postMethod.getStatusLine().toString() );
147 }
148
149 public String getContentAsString()
150 {
151 if( prettyPrint )
152 {
153 responseContent = XmlUtils.prettyPrintXml( responseContent );
154 prettyPrint = false;
155 }
156
157 return responseContent;
158 }
159
160 public long getContentLength()
161 {
162 return responseSize;
163 }
164
165 public WsdlRequest getRequest()
166 {
167 return wsdlRequest.get();
168 }
169
170 public long getTimeTaken()
171 {
172 return timeTaken;
173 }
174
175 public Attachment[] getAttachments()
176 {
177 return new Attachment[0];
178 }
179
180 public StringToStringMap getRequestHeaders()
181 {
182 return requestHeaders;
183 }
184
185 public StringToStringMap getResponseHeaders()
186 {
187 return responseHeaders;
188 }
189
190 public Attachment[] getAttachmentsForPart(String partName)
191 {
192 return new Attachment[0];
193 }
194
195 public String getRequestContent()
196 {
197 return requestContent;
198 }
199
200 public void setResponseContent(String responseContent)
201 {
202 String oldContent = this.responseContent;
203 this.responseContent = responseContent;
204
205 getRequest().notifyPropertyChanged( WsdlRequest.RESPONSE_CONTENT_PROPERTY, oldContent, responseContent );
206 }
207
208 public SSLInfo getSSLInfo()
209 {
210 return sslInfo;
211 }
212
213 public long getTimestamp()
214 {
215 return timestamp;
216 }
217
218 public Vector getWssResult()
219 {
220 return wssResult;
221 }
222
223 public byte[] getRawRequestData()
224 {
225 return requestData;
226 }
227
228 public byte[] getRawResponseData()
229 {
230 return responseBody;
231 }
232 }