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.SoapUI;
16 import com.eviware.soapui.impl.support.AbstractHttpRequestInterface;
17 import com.eviware.soapui.impl.wsdl.WsdlRequest;
18 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
19 import com.eviware.soapui.settings.WsdlSettings;
20 import com.eviware.soapui.support.StringUtils;
21 import com.eviware.soapui.support.xml.XmlUtils;
22
23 /***
24 * Simple response to a request
25 *
26 * @author ole.matzura
27 */
28
29 public class SinglePartHttpResponse extends BaseHttpResponse
30 {
31 private String responseContent;
32 private String requestContent;
33 private boolean prettyPrint;
34 private long responseSize;
35
36 public SinglePartHttpResponse( AbstractHttpRequestInterface<?> httpRequest, ExtendedHttpMethod httpMethod,
37 String requestContent, PropertyExpansionContext context )
38 {
39 super( httpMethod, httpRequest );
40
41 if( getRequestContent() == null )
42 this.requestContent = requestContent;
43
44 try
45 {
46 byte[] responseBody = httpMethod.getResponseBody();
47 int contentOffset = 0;
48 if( responseBody == null )
49 responseBody = new byte[0];
50
51 responseSize = responseBody.length;
52
53 String contentType = httpMethod.getResponseContentType();
54 String charset = httpMethod.getResponseCharSet();
55
56 if( contentType != null && contentType.toLowerCase().endsWith( "xml" ) )
57 {
58 if( responseSize > 3 && responseBody[0] == ( byte )239 && responseBody[1] == ( byte )187
59 && responseBody[2] == ( byte )191 )
60 {
61 charset = "UTF-8";
62 contentOffset = 3;
63 }
64 }
65
66 if( charset == null )
67 charset = httpRequest.getEncoding();
68
69 charset = StringUtils.unquote( charset );
70
71 responseContent = responseBody.length == 0 ? null : charset == null ? new String( responseBody ) : new String(
72 responseBody, contentOffset, ( int )( responseSize - contentOffset ), charset );
73
74 prettyPrint = httpRequest.getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES );
75
76
77
78
79
80
81
82
83
84
85
86
87 }
88 catch( Exception e )
89 {
90 SoapUI.logError( e );
91 }
92 }
93
94 public String getContentAsString()
95 {
96 if( prettyPrint )
97 {
98 responseContent = XmlUtils.prettyPrintXml( responseContent );
99 prettyPrint = false;
100 }
101
102 return responseContent;
103 }
104
105 public long getContentLength()
106 {
107 return responseSize;
108 }
109
110 public String getRequestContent()
111 {
112 return requestContent == null ? super.getRequestContent() : requestContent;
113 }
114
115 public void setResponseContent( String responseContent )
116 {
117 String oldContent = this.responseContent;
118 this.responseContent = responseContent;
119
120 getRequest().notifyPropertyChanged( WsdlRequest.RESPONSE_CONTENT_PROPERTY, oldContent, responseContent );
121 }
122
123
124
125
126
127
128
129
130
131
132
133 }