View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    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 		{
    125 			responseBody = super.getResponseBody();
    126 		}
    127 		else
    128 		{
    129 			InputStream instream = getResponseBodyAsStream();
    130
    131 			ByteArrayOutputStream outstream = Tools.readAll(instream, maxSize);
    132 			responseBody = outstream.toByteArray();
    133 		}
    134
    135 		if (HttpClientSupport.isZippedResponse( this ))
    136 		{
    137 			responseBody = HttpClientSupport.decompress( responseBody );
    138 		}
    139
    140 		return responseBody;
    141 	}
    142
    143 	public SSLInfo getSSLInfo()
    144 	{
    145 		return sslInfo;
    146 	}
    147
    148
    149 }