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.filters;
14  
15  import com.eviware.soapui.impl.support.AbstractHttpRequest;
16  import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport;
17  import com.eviware.soapui.impl.wsdl.submit.transports.http.ExtendedHttpMethod;
18  import com.eviware.soapui.impl.wsdl.support.CompressionSupport;
19  import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport;
20  import com.eviware.soapui.model.iface.SubmitContext;
21  import com.eviware.soapui.model.settings.Settings;
22  import com.eviware.soapui.settings.HttpSettings;
23  import org.apache.commons.httpclient.HttpVersion;
24  import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
25  
26  /***
27   * RequestFilter that applies SoapUI HTTP-settings to the current request
28   *
29   * @author Ole.Matzura
30   */
31  
32  public class HttpSettingsRequestFilter extends AbstractRequestFilter
33  {
34     public void filterAbstractHttpRequest( SubmitContext context, AbstractHttpRequest<?> httpRequest )
35     {
36        ExtendedHttpMethod httpMethod = (ExtendedHttpMethod) context.getProperty( BaseHttpRequestTransport.HTTP_METHOD );
37  
38        //	 set maxsize
39        Settings settings = httpRequest.getSettings();
40  
41        // close connections?
42        if( settings.getBoolean( HttpSettings.CLOSE_CONNECTIONS ) )
43        {
44           httpMethod.setRequestHeader( "Connection", "close" );
45        }
46  
47        // compress request?
48        String compressionAlg = settings.getString( HttpSettings.REQUEST_COMPRESSION, "None" );
49        if( !"None".equals( compressionAlg ) )
50           httpMethod.setRequestHeader( "Content-Encoding", compressionAlg );
51  
52        // accept compressed responses?
53        if( settings.getBoolean( HttpSettings.RESPONSE_COMPRESSION ) )
54        {
55           httpMethod.setRequestHeader( "Accept-Encoding", CompressionSupport.getAvailableAlgorithms( "," ) );
56        }
57  
58        // no chunking?
59        if( settings.getBoolean( HttpSettings.DISABLE_CHUNKING ) && httpMethod instanceof EntityEnclosingMethod )
60        {
61           ( (EntityEnclosingMethod) httpMethod ).setContentChunked( false );
62        }
63  
64        String httpVersion = settings.getString( HttpSettings.HTTP_VERSION, "1.1" );
65        if( httpVersion.equals( HttpSettings.HTTP_VERSION_1_1 ) )
66        {
67           httpMethod.getParams().setVersion( HttpVersion.HTTP_1_1 );
68        }
69        else if( httpVersion.equals( HttpSettings.HTTP_VERSION_1_0 ) )
70        {
71           httpMethod.getParams().setVersion( HttpVersion.HTTP_1_0 );
72        }
73        else if( httpVersion.equals( HttpSettings.HTTP_VERSION_0_9 ) )
74        {
75           httpMethod.getParams().setVersion( HttpVersion.HTTP_0_9 );
76        }
77  
78        // max size..
79        long maxSize = httpRequest.getMaxSize();
80        if( maxSize == 0 )
81           maxSize = settings.getLong( HttpSettings.MAX_RESPONSE_SIZE, 0 );
82        if( maxSize > 0 )
83           httpMethod.setMaxSize( maxSize );
84  
85        // follow redirects
86        httpMethod.setFollowRedirects( httpRequest.isFollowRedirects() );
87  
88        // apply global settings
89        HttpClientSupport.applyHttpSettings( httpMethod, settings );
90     }
91  }