View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  
19  import com.eviware.soapui.SoapUI;
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  import com.eviware.soapui.support.StringUtils;
29  
30  /***
31   * RequestFilter that applies SoapUI HTTP-settings to the current request
32   * 
33   * @author Ole.Matzura
34   */
35  
36  public class HttpSettingsRequestFilter extends AbstractRequestFilter
37  {
38  	public void filterAbstractHttpRequest( SubmitContext context, AbstractHttpRequest<?> httpRequest )
39  	{
40  		ExtendedHttpMethod httpMethod = ( ExtendedHttpMethod )context.getProperty( BaseHttpRequestTransport.HTTP_METHOD );
41  
42  		// set maxsize
43  		Settings settings = httpRequest.getSettings();
44  
45  		// close connections?
46  		if( settings.getBoolean( HttpSettings.CLOSE_CONNECTIONS ) )
47  		{
48  			httpMethod.setRequestHeader( "Connection", "close" );
49  		}
50  
51  		// close connections?
52  		if( settings.getBoolean( HttpSettings.EXPECT_CONTINUE )  && httpMethod instanceof EntityEnclosingMethod)
53  		{
54  			httpMethod.getParams().setParameter( HttpMethodParams.USE_EXPECT_CONTINUE, Boolean.TRUE);
55  		}
56  		
57  		// compress request?
58  		String compressionAlg = settings.getString( HttpSettings.REQUEST_COMPRESSION, "None" );
59  		if( !"None".equals( compressionAlg ) )
60  			httpMethod.setRequestHeader( "Content-Encoding", compressionAlg );
61  
62  		// accept compressed responses?
63  		if( settings.getBoolean( HttpSettings.RESPONSE_COMPRESSION ) )
64  		{
65  			httpMethod.setRequestHeader( "Accept-Encoding", CompressionSupport.getAvailableAlgorithms( "," ) );
66  		}
67  
68  		String httpVersion = settings.getString( HttpSettings.HTTP_VERSION, "1.1" );
69  		if( httpVersion.equals( HttpSettings.HTTP_VERSION_1_1 ) )
70  		{
71  			httpMethod.getParams().setVersion( HttpVersion.HTTP_1_1 );
72  		}
73  		else if( httpVersion.equals( HttpSettings.HTTP_VERSION_1_0 ) )
74  		{
75  			httpMethod.getParams().setVersion( HttpVersion.HTTP_1_0 );
76  		}
77  		else if( httpVersion.equals( HttpSettings.HTTP_VERSION_0_9 ) )
78  		{
79  			httpMethod.getParams().setVersion( HttpVersion.HTTP_1_1 );
80  		}
81  
82  		// max size..
83  		long maxSize = httpRequest.getMaxSize();
84  		if( maxSize == 0 )
85  			maxSize = settings.getLong( HttpSettings.MAX_RESPONSE_SIZE, 0 );
86  		if( maxSize > 0 )
87  			httpMethod.setMaxSize( maxSize );
88  
89  		// follow redirects
90  		httpMethod.setFollowRedirects( httpRequest.isFollowRedirects() );
91  
92  		// apply global settings
93  		HttpClientSupport.applyHttpSettings( httpMethod, settings );
94  		
95  		String timeout = context.expand( httpRequest.getTimeout() );
96  		if( StringUtils.hasContent( timeout ))
97  		{
98  			try
99  			{
100 				httpMethod.getParams().setSoTimeout( Integer.parseInt( timeout ));
101 			}
102 			catch( NumberFormatException e )
103 			{
104 				SoapUI.logError( e );
105 			}
106 		}
107 	}
108 }