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