1
2
3
4
5
6
7
8
9
10
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 }