View Javadoc

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