View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.submit.transports.http;
14  
15  import java.io.ByteArrayOutputStream;
16  import java.io.IOException;
17  import java.io.InputStream;
18  import java.net.Socket;
19  
20  import javax.net.ssl.SSLSocket;
21  
22  import org.apache.commons.httpclient.HttpConnection;
23  import org.apache.commons.httpclient.HttpException;
24  import org.apache.commons.httpclient.HttpState;
25  import org.apache.commons.httpclient.methods.PostMethod;
26  
27  import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport;
28  import com.eviware.soapui.support.Tools;
29  
30  /***
31   * Extended PostMethod that supports limiting of response size and detailed
32   * timestamps
33   * 
34   * @author Ole.Matzura
35   */
36  
37  public final class TimeablePostMethod extends PostMethod
38  {
39  	private long timeTaken;
40  
41  	private long startTime;
42  
43  	private long maxSize;
44  
45  	private byte[] responseBody;
46  
47  	private SSLInfo sslInfo;
48  
49  	public TimeablePostMethod()
50  	{
51  	}
52  
53  	protected void readResponse(HttpState arg0, HttpConnection arg1)
54  			throws IOException, HttpException
55  	{
56  		super.readResponse(arg0, arg1);
57  		timeTaken = getTimeTakenUntilNow();
58  		
59  		Socket socket = arg1.getSocket();
60  		if( socket instanceof SSLSocket )
61  		{
62  			sslInfo = new SSLInfo( ( SSLSocket ) socket );
63  		}
64  
65  	}
66  
67  	public long getMaxSize()
68  	{
69  		return maxSize;
70  	}
71  
72  	public void setMaxSize(long maxSize)
73  	{
74  		this.maxSize = maxSize;
75  	}
76  
77  	public long getTimeTakenUntilNow()
78  	{
79  		long nanoTime = System.nanoTime();
80  		long result = ((nanoTime - startTime) + 500000) / 1000000;
81  
82  		if (result == 0)
83  		{
84  			System.out.println("time taken = 0 ms; " + (nanoTime - startTime)
85  					+ " ns");
86  			result = 1;
87  		}
88  
89  		return result;
90  	}
91  
92  	protected void writeRequest(HttpState arg0, HttpConnection arg1)
93  			throws IOException, HttpException
94  	{
95  		super.writeRequest(arg0, arg1);
96  
97  		if (startTime == 0)
98  			startTime = System.nanoTime();
99  	}
100 
101 	public void initStartTime()
102 	{
103 		startTime = System.nanoTime();
104 	}
105 
106 	public long getTimeTaken()
107 	{
108 		return timeTaken;
109 	}
110 
111 	public long getStartTime()
112 	{
113 		return startTime;
114 	}
115 
116 	public byte[] getResponseBody() throws IOException
117 	{
118 		if (responseBody != null)
119 			return responseBody;
120 
121 		long contentLength = getResponseContentLength();
122 
123 		if (maxSize == 0 || (contentLength >= 0 && contentLength <= maxSize))
124 			return super.getResponseBody();
125 
126 		InputStream instream = getResponseBodyAsStream();
127 
128 		ByteArrayOutputStream outstream = Tools.readAll(instream, maxSize);
129 		responseBody = outstream.toByteArray();
130 
131 		if (HttpClientSupport.isZippedResponse( this ))
132 		{
133 			responseBody = HttpClientSupport.decompress( responseBody );
134 		}
135 
136 		return responseBody;
137 	}
138 
139 	public SSLInfo getSSLInfo()
140 	{
141 		return sslInfo;
142 	}
143 	
144 	
145 }