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.io.ByteArrayOutputStream;
016: import java.io.IOException;
017: import java.io.InputStream;
018: import java.net.Socket;
019:
020: import javax.net.ssl.SSLSocket;
021:
022: import org.apache.commons.httpclient.HttpConnection;
023: import org.apache.commons.httpclient.HttpException;
024: import org.apache.commons.httpclient.HttpState;
025: import org.apache.commons.httpclient.methods.PostMethod;
026:
027: import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport;
028: import com.eviware.soapui.support.Tools;
029:
030: /**
031: * Extended PostMethod that supports limiting of response size and detailed
032: * timestamps
033: *
034: * @author Ole.Matzura
035: */
036:
037: public final class TimeablePostMethod extends PostMethod {
038: private long timeTaken;
039:
040: private long startTime;
041:
042: private long maxSize;
043:
044: private byte[] responseBody;
045:
046: private SSLInfo sslInfo;
047:
048: public TimeablePostMethod() {
049: }
050:
051: protected void readResponse(HttpState arg0, HttpConnection arg1)
052: throws IOException, HttpException {
053: super .readResponse(arg0, arg1);
054: timeTaken = getTimeTakenUntilNow();
055:
056: Socket socket = arg1.getSocket();
057: if (socket instanceof SSLSocket) {
058: sslInfo = new SSLInfo((SSLSocket) socket);
059: }
060:
061: }
062:
063: public long getMaxSize() {
064: return maxSize;
065: }
066:
067: public void setMaxSize(long maxSize) {
068: this .maxSize = maxSize;
069: }
070:
071: public long getTimeTakenUntilNow() {
072: long nanoTime = System.nanoTime();
073: long result = ((nanoTime - startTime) + 500000) / 1000000;
074:
075: if (result == 0) {
076: System.out.println("time taken = 0 ms; "
077: + (nanoTime - startTime) + " ns");
078: result = 1;
079: }
080:
081: return result;
082: }
083:
084: protected void writeRequest(HttpState arg0, HttpConnection arg1)
085: throws IOException, HttpException {
086: super .writeRequest(arg0, arg1);
087:
088: if (startTime == 0)
089: startTime = System.nanoTime();
090: }
091:
092: public void initStartTime() {
093: startTime = System.nanoTime();
094: }
095:
096: public long getTimeTaken() {
097: return timeTaken;
098: }
099:
100: public long getStartTime() {
101: return startTime;
102: }
103:
104: public byte[] getResponseBody() throws IOException {
105: if (responseBody != null)
106: return responseBody;
107:
108: long contentLength = getResponseContentLength();
109:
110: if (maxSize == 0
111: || (contentLength >= 0 && contentLength <= maxSize)) {
112: responseBody = super .getResponseBody();
113: } else {
114: InputStream instream = getResponseBodyAsStream();
115:
116: ByteArrayOutputStream outstream = Tools.readAll(instream,
117: maxSize);
118: responseBody = outstream.toByteArray();
119: }
120:
121: if (HttpClientSupport.isZippedResponse(this )) {
122: responseBody = HttpClientSupport.decompress(responseBody);
123: }
124:
125: return responseBody;
126: }
127:
128: public SSLInfo getSSLInfo() {
129: return sslInfo;
130: }
131:
132: }
|