View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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 CompressedString
    31   *
    32   * @author ole.matzura
    33   */
    34
    35  public class CompressedStringSupport
    36  {
    37  	public static String getString( CompressedStringConfig compressedStringConfig )
    38  	{
    39  		String compression = compressedStringConfig.getCompression();
    40  		if( "gzip".equals( compression ))
    41  		{
    42  			try
    43  			{
    44  				BASE64Decoder decoder = new BASE64Decoder();
    45  				byte[] bytes = decoder.decodeBuffer( compressedStringConfig.getStringValue() );
    46  				GZIPInputStream in = new GZIPInputStream( new ByteArrayInputStream( bytes ));
    47  				return Tools.readAll( in, -1 ).toString();
    48  			}
    49  			catch( IOException e )
    50  			{
    51  				SoapUI.logError( e );
    52  			}
    53  		}
    54
    55  		return compressedStringConfig.getStringValue();
    56  	}
    57
    58  	public static void setString( CompressedStringConfig compressedStringConfig, String value )
    59  	{
    60  		long limit = SoapUI.getSettings().getLong( WsdlSettings.COMPRESSION_LIMIT, 0 );
    61  		if( limit > 0 && value.length() >=  limit )
    62  		{
    63  			try
    64  			{
    65  				ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    66  				GZIPOutputStream out = new GZIPOutputStream( byteOut );
    67  				out.write( value.getBytes() );
    68  				out.finish();
    69  				BASE64Encoder encoder = new BASE64Encoder();
    70  				value = encoder.encode( byteOut.toByteArray() );
    71  				compressedStringConfig.setCompression( "gzip" );
    72  			}
    73  			catch( IOException e )
    74  			{
    75  				SoapUI.logError( e );
    76  				compressedStringConfig.unsetCompression();
    77  			}
    78  		}
    79  		else if( compressedStringConfig.isSetCompression() )
    80  		{
    81  			compressedStringConfig.unsetCompression();
    82  		}
    83
    84  		compressedStringConfig.setStringValue( value );
    85  	}
    86  }