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.support;
14  
15  import java.io.ByteArrayInputStream;
16  import java.io.ByteArrayOutputStream;
17  import java.io.IOException;
18  import java.util.zip.GZIPInputStream;
19  import java.util.zip.GZIPOutputStream;
20  
21  import org.apache.commons.codec.binary.Base64;
22  
23  import com.eviware.soapui.SoapUI;
24  import com.eviware.soapui.config.CompressedStringConfig;
25  import com.eviware.soapui.settings.WsdlSettings;
26  import com.eviware.soapui.support.Tools;
27  
28  /***
29   * Utility class for compressing/decompressing strings stored with
30   * CompressedString
31   * 
32   * @author ole.matzura
33   */
34  
35  public class CompressedStringSupport
36  {
37  	public static String getString( CompressedStringConfig compressedStringConfig )
38  	{
39  		synchronized( compressedStringConfig )
40  		{
41  			String compression = compressedStringConfig.getCompression();
42  			if( "gzip".equals( compression ) )
43  			{
44  				try
45  				{
46  					byte[] bytes = Base64.decodeBase64( compressedStringConfig.getStringValue().getBytes() );
47  					GZIPInputStream in = new GZIPInputStream( new ByteArrayInputStream( bytes ) );
48  					return Tools.readAll( in, -1 ).toString();
49  				}
50  				catch( IOException e )
51  				{
52  					SoapUI.logError( e );
53  				}
54  			}
55  
56  			return compressedStringConfig.getStringValue();
57  		}
58  	}
59  
60  	public static void setString( CompressedStringConfig compressedStringConfig, String value )
61  	{
62  		synchronized( compressedStringConfig )
63  		{
64  			long limit = SoapUI.getSettings().getLong( WsdlSettings.COMPRESSION_LIMIT, 0 );
65  			if( limit > 0 && value.length() >= limit )
66  			{
67  				try
68  				{
69  					ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
70  					GZIPOutputStream out = new GZIPOutputStream( byteOut );
71  					out.write( value.getBytes() );
72  					out.finish();
73  					value = new String( Base64.encodeBase64( byteOut.toByteArray() ) );
74  					compressedStringConfig.setCompression( "gzip" );
75  				}
76  				catch( IOException e )
77  				{
78  					SoapUI.logError( e );
79  					compressedStringConfig.unsetCompression();
80  				}
81  			}
82  			else if( compressedStringConfig.isSetCompression() )
83  			{
84  				compressedStringConfig.unsetCompression();
85  			}
86  
87  			compressedStringConfig.setStringValue( value );
88  		}
89  	}
90  }