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