001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.submit.transports.http;
014:
015: import java.lang.ref.WeakReference;
016:
017: import org.apache.commons.httpclient.Header;
018:
019: import com.eviware.soapui.SoapUI;
020: import com.eviware.soapui.impl.wsdl.WsdlRequest;
021: import com.eviware.soapui.model.iface.Attachment;
022: import com.eviware.soapui.model.settings.Settings;
023: import com.eviware.soapui.settings.HttpSettings;
024: import com.eviware.soapui.settings.WsdlSettings;
025: import com.eviware.soapui.support.StringUtils;
026: import com.eviware.soapui.support.types.StringToStringMap;
027: import com.eviware.soapui.support.xml.XmlUtils;
028:
029: /**
030: * Simple response to a request
031: *
032: * @author ole.matzura
033: */
034:
035: final class SinglePartHttpResponse implements WsdlResponse {
036: private final WeakReference<WsdlRequest> wsdlRequest;
037: @SuppressWarnings("unused")
038: private final TimeablePostMethod postMethod;
039: private long timeTaken;
040: private String responseContent;
041: private StringToStringMap requestHeaders;
042: private StringToStringMap responseHeaders;
043: private final String requestContent;
044: private boolean prettyPrint;
045: private SSLInfo sslInfo;
046: private long timestamp;
047: private long responseSize;
048:
049: public SinglePartHttpResponse(WsdlRequest wsdlRequest,
050: TimeablePostMethod postMethod, String requestContent) {
051: this .wsdlRequest = new WeakReference<WsdlRequest>(wsdlRequest);
052: this .postMethod = postMethod;
053: this .requestContent = requestContent;
054: this .timeTaken = postMethod.getTimeTaken();
055: this .sslInfo = postMethod.getSSLInfo();
056: this .timestamp = System.currentTimeMillis();
057:
058: // read response immediately since we need to release connection
059: Settings settings = wsdlRequest.getSettings();
060:
061: try {
062: byte[] responseBody = postMethod.getResponseBody();
063: responseSize = responseBody.length;
064: if (settings
065: .getBoolean(HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN))
066: timeTaken = postMethod.getTimeTakenUntilNow();
067:
068: String charset = postMethod.getResponseCharSet();
069: if (charset == null)
070: charset = wsdlRequest.getEncoding();
071:
072: charset = StringUtils.unquote(charset);
073:
074: responseContent = responseBody.length == 0 ? null
075: : charset == null ? new String(responseBody)
076: : new String(responseBody, charset);
077:
078: prettyPrint = wsdlRequest.getSettings().getBoolean(
079: WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES);
080:
081: initHeaders(postMethod);
082: } catch (Exception e) {
083: SoapUI.logError(e);
084: }
085: }
086:
087: private void initHeaders(TimeablePostMethod postMethod) {
088: requestHeaders = new StringToStringMap();
089: Header[] headers = postMethod.getRequestHeaders();
090: for (Header header : headers) {
091: requestHeaders.put(header.getName(), header.getValue());
092: }
093:
094: responseHeaders = new StringToStringMap();
095: headers = postMethod.getResponseHeaders();
096: for (Header header : headers) {
097: responseHeaders.put(header.getName(), header.getValue());
098: }
099:
100: responseHeaders.put("#status#", postMethod.getStatusLine()
101: .toString());
102: }
103:
104: public String getContentAsString() {
105: if (prettyPrint) {
106: responseContent = XmlUtils.prettyPrintXml(responseContent);
107: prettyPrint = false;
108: }
109:
110: return responseContent;
111: }
112:
113: public long getContentLength() {
114: return responseSize;
115: }
116:
117: public WsdlRequest getRequest() {
118: return wsdlRequest.get();
119: }
120:
121: public long getTimeTaken() {
122: return timeTaken;
123: }
124:
125: public Attachment[] getAttachments() {
126: return new Attachment[0];
127: }
128:
129: public StringToStringMap getRequestHeaders() {
130: return requestHeaders;
131: }
132:
133: public StringToStringMap getResponseHeaders() {
134: return responseHeaders;
135: }
136:
137: public Attachment[] getAttachmentsForPart(String partName) {
138: return new Attachment[0];
139: }
140:
141: public String getRequestContent() {
142: return requestContent;
143: }
144:
145: public void setResponseContent(String responseContent) {
146: this .responseContent = responseContent;
147: }
148:
149: public SSLInfo getSSLInfo() {
150: return sslInfo;
151: }
152:
153: public long getTimestamp() {
154: return timestamp;
155: }
156: }
|