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 java.io.ByteArrayOutputStream;
16  
17  import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
18  import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
19  import org.apache.commons.httpclient.methods.RequestEntity;
20  import org.apache.log4j.Logger;
21  
22  import com.eviware.soapui.impl.support.AbstractHttpRequest;
23  import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport;
24  import com.eviware.soapui.impl.wsdl.submit.transports.http.ExtendedHttpMethod;
25  import com.eviware.soapui.impl.wsdl.support.CompressionSupport;
26  import com.eviware.soapui.model.iface.SubmitContext;
27  import com.eviware.soapui.model.settings.Settings;
28  import com.eviware.soapui.settings.HttpSettings;
29  
30  public class HttpCompressionRequestFilter extends AbstractRequestFilter
31  {
32  	private final static Logger log = Logger.getLogger( HttpCompressionRequestFilter.class );
33  
34  	@Override
35  	public void filterAbstractHttpRequest( SubmitContext context, AbstractHttpRequest<?> httpRequest )
36  	{
37  		Settings settings = httpRequest.getSettings();
38  		String compressionAlg = settings.getString( HttpSettings.REQUEST_COMPRESSION, "None" );
39  		if( !"None".equals( compressionAlg ) )
40  		{
41  			try
42  			{
43  				ExtendedHttpMethod method = ( ExtendedHttpMethod )context
44  						.getProperty( BaseHttpRequestTransport.HTTP_METHOD );
45  				if( method instanceof EntityEnclosingMethod )
46  				{
47  					RequestEntity requestEntity = method.getRequestEntity();
48  					if( requestEntity != null )
49  					{
50  						ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
51  						requestEntity.writeRequest( tempOut );
52  
53  						byte[] compressedData = CompressionSupport.compress( compressionAlg, tempOut.toByteArray() );
54  						( ( EntityEnclosingMethod )method ).setRequestEntity( new ByteArrayRequestEntity( compressedData ) );
55  					}
56  				}
57  			}
58  			catch( Exception e )
59  			{
60  				e.printStackTrace();
61  			}
62  		}
63  	}
64  }