View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 com.eviware.soapui.impl.wsdl.support.CompressionSupport;
16  import com.eviware.soapui.impl.wsdl.support.http.ConnectionWithSocket;
17  import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport;
18  import com.eviware.soapui.support.StringUtils;
19  import com.eviware.soapui.support.Tools;
20  import org.apache.commons.httpclient.Header;
21  import org.apache.commons.httpclient.HttpConnection;
22  import org.apache.commons.httpclient.HttpMethodBase;
23  import org.apache.commons.httpclient.HttpState;
24  
25  import javax.net.ssl.SSLSocket;
26  import java.io.*;
27  import java.net.Socket;
28  
29  /***
30   * Extended PostMethod that supports limiting of response size and detailed
31   * timestamps
32   *
33   * @author Ole.Matzura
34   */
35  
36  public final class HttpMethodSupport
37  {
38     private long timeTaken;
39     private long startTime;
40     private long maxSize;
41     private long responseReadTime;
42  
43     private byte[] responseBody;
44  
45     private SSLInfo sslInfo;
46     private String dumpFile;
47     private final HttpMethodBase httpMethod;
48  
49     public HttpMethodSupport( HttpMethodBase httpMethod )
50     {
51        this.httpMethod = httpMethod;
52     }
53  
54     public String getDumpFile()
55     {
56        return dumpFile;
57     }
58  
59     public void setDumpFile( String dumpFile )
60     {
61        this.dumpFile = dumpFile;
62     }
63  
64     public void afterReadResponse( HttpState arg0, HttpConnection arg1 )
65     {
66        if( arg1 instanceof ConnectionWithSocket )
67        {
68           Socket socket = ( ( ConnectionWithSocket ) arg1 ).getConnectionSocket();
69           if( socket instanceof SSLSocket )
70           {
71              sslInfo = new SSLInfo( ( SSLSocket ) socket );
72           }
73        }
74     }
75  
76     public long getMaxSize()
77     {
78        return maxSize;
79     }
80  
81     public void setMaxSize( long maxSize )
82     {
83        this.maxSize = maxSize;
84     }
85  
86     public void afterWriteRequest( HttpState arg0, HttpConnection arg1 )
87     {
88        if( startTime == 0 )
89           startTime = System.nanoTime();
90     }
91  
92     public void initStartTime()
93     {
94        startTime = System.nanoTime();
95     }
96  
97     public long getTimeTaken()
98     {
99        if( timeTaken == 0 )
100          timeTaken = ( System.nanoTime() - startTime ) / 1000000;
101 
102       return timeTaken;
103    }
104 
105    public long getStartTime()
106    {
107       return startTime;
108    }
109 
110    public byte[] getResponseBody() throws IOException
111    {
112       if( responseBody != null )
113          return responseBody;
114 
115       long contentLength = httpMethod.getResponseContentLength();
116       long now = System.nanoTime();
117 
118       InputStream instream = httpMethod.getResponseBodyAsStream();
119 
120       if( maxSize == 0 || ( contentLength >= 0 && contentLength <= maxSize ) )
121       {
122          ByteArrayOutputStream out = new ByteArrayOutputStream();
123          Tools.writeAll( out, instream );
124          responseReadTime = System.nanoTime() - now;
125          responseBody = out.toByteArray();
126 
127          try
128          {
129             if( StringUtils.hasContent( dumpFile ) )
130             {
131                Tools.writeAll( new FileOutputStream( dumpFile ), new ByteArrayInputStream( responseBody ) );
132             }
133          }
134          catch( IOException e )
135          {
136             e.printStackTrace();
137          }
138 
139          String compressionAlg = HttpClientSupport.getResponseCompressionType( httpMethod );
140          if( compressionAlg != null )
141          {
142             try
143             {
144                responseBody = CompressionSupport.decompress( compressionAlg, responseBody );
145             }
146             catch( Exception e )
147             {
148                IOException ioe = new IOException( "Decompression of response failed" );
149                ioe.initCause( e );
150                throw ioe;
151             }
152          }
153       }
154       else
155       {
156          try
157          {
158             if( StringUtils.hasContent( dumpFile ) )
159             {
160                FileOutputStream fileOutputStream = new FileOutputStream( dumpFile );
161                Tools.writeAll( fileOutputStream, instream );
162                responseReadTime = System.nanoTime() - now;
163                fileOutputStream.close();
164                instream = new FileInputStream( dumpFile );
165             }
166          }
167          catch( IOException e )
168          {
169             e.printStackTrace();
170          }
171 
172          ByteArrayOutputStream outstream = Tools.readAll( instream, maxSize );
173          if( responseReadTime == 0 )
174             responseReadTime = System.nanoTime() - now;
175 
176          responseBody = outstream.toByteArray();
177       }
178 
179       // convert to ms
180       responseReadTime /= 1000000;
181 
182       return responseBody;
183    }
184 
185    public SSLInfo getSSLInfo()
186    {
187       return sslInfo;
188    }
189 
190    public String getResponseContentType()
191    {
192       Header[] headers = httpMethod.getResponseHeaders( "Content-Type" );
193       if( headers != null && headers.length > 0 )
194       {
195          return headers[0].getElements()[0].getName();
196       }
197 
198       return null;
199    }
200 
201    public long getResponseReadTime()
202    {
203       return responseReadTime;
204    }
205 }