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.io.InputStream;
19  import java.util.zip.DeflaterOutputStream;
20  import java.util.zip.GZIPInputStream;
21  import java.util.zip.GZIPOutputStream;
22  import java.util.zip.InflaterInputStream;
23  
24  import com.eviware.soapui.support.Tools;
25  
26  public class CompressionSupport
27  {
28  	public static final String ALG_GZIP = "gzip";
29  	public static final String ALG_DEFLATE = "deflate";
30  	private static final String[] algs = { ALG_GZIP, ALG_DEFLATE };
31  
32  	public static String getAvailableAlgorithms( String separator )
33  	{
34  		StringBuffer buf = new StringBuffer();
35  		for( int i = 0; i < algs.length; ++i )
36  		{
37  			if( i > 0 )
38  				buf.append( separator );
39  			buf.append( algs[i] );
40  		}
41  
42  		return buf.toString();
43  	}
44  
45  	public static String getAvailableAlgorithm( String httpContentEncoding )
46  	{
47  		for( int i = 0; i < algs.length; ++i )
48  			if( httpContentEncoding.toLowerCase().endsWith( algs[i] ) )
49  				return algs[i];
50  
51  		return null;
52  	}
53  
54  	private static void checkAlg( String alg ) throws Exception
55  	{
56  		if( !ALG_GZIP.equals( alg ) && !ALG_DEFLATE.equals( alg ) )
57  			throw new Exception( "Compression algorithm not supported: " + alg );
58  	}
59  
60  	public static byte[] compress( String alg, byte[] content ) throws Exception
61  	{
62  		checkAlg( alg );
63  		if( ALG_GZIP.equals( alg ) )
64  			return GZIPCompress( content );
65  		else if( ALG_DEFLATE.equals( alg ) )
66  			return DeflaterCompress( content );
67  		else
68  			return null;
69  	}
70  
71  	public static byte[] decompress( String alg, byte[] content ) throws Exception
72  	{
73  		checkAlg( alg );
74  		if( ALG_GZIP.equals( alg ) )
75  			return GZIPDecompress( content );
76  		else if( ALG_DEFLATE.equals( alg ) )
77  			return DeflaterDecompress( content );
78  		else
79  			return null;
80  	}
81  
82  	// createCompressionInputStream can be used in the future if
83  	// PipedInputStreams are used
84  	// for sending compressed data instead of creating compressed byte array
85  	// first and then sending
86  	public static InputStream createCompressionInputStream( String alg, byte[] content ) throws Exception
87  	{
88  		checkAlg( alg );
89  		ByteArrayInputStream bais = new ByteArrayInputStream( content );
90  		if( ALG_GZIP.equals( alg ) )
91  			return new GZIPInputStream( bais );
92  		else if( ALG_DEFLATE.equals( alg ) )
93  			return new InflaterInputStream( bais );
94  		else
95  			return null;
96  	}
97  
98  	private static byte[] GZIPCompress( byte[] requestContent ) throws IOException
99  	{
100 		ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
101 		GZIPOutputStream gzipstream = new GZIPOutputStream( compressedContent );
102 		gzipstream.write( requestContent );
103 		gzipstream.finish();
104 
105 		// get the compressed content
106 		return compressedContent.toByteArray();
107 	}
108 
109 	private static byte[] GZIPDecompress( byte[] content ) throws IOException
110 	{
111 		GZIPInputStream zipin;
112 		InputStream in = new ByteArrayInputStream( content );
113 		zipin = new GZIPInputStream( in );
114 		ByteArrayOutputStream out = Tools.readAll( zipin, -1 );
115 		content = out.toByteArray();
116 		out.close();
117 		zipin.close();
118 
119 		return content;
120 	}
121 
122 	private static byte[] DeflaterCompress( byte[] requestContent ) throws IOException
123 	{
124 		ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
125 		DeflaterOutputStream defstream = new DeflaterOutputStream( compressedContent );
126 		defstream.write( requestContent );
127 		defstream.finish();
128 
129 		// get the compressed content
130 		return compressedContent.toByteArray();
131 	}
132 
133 	private static byte[] DeflaterDecompress( byte[] content ) throws IOException
134 	{
135 		InflaterInputStream zipin;
136 		InputStream in = new ByteArrayInputStream( content );
137 		zipin = new InflaterInputStream( in );
138 		ByteArrayOutputStream out = Tools.readAll( zipin, -1 );
139 		content = out.toByteArray();
140 		out.close();
141 		zipin.close();
142 
143 		return content;
144 	}
145 }