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.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
83
84 public static InputStream createCompressionInputStream( String alg, byte[] content ) throws Exception
85 {
86 checkAlg(alg);
87 ByteArrayInputStream bais = new ByteArrayInputStream( content );
88 if ( ALG_GZIP.equals(alg))
89 return new GZIPInputStream( bais );
90 else if ( ALG_DEFLATE.equals(alg))
91 return new InflaterInputStream( bais );
92 else
93 return null;
94 }
95
96 private static byte[] GZIPCompress(byte[] requestContent) throws IOException
97 {
98 ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
99 GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
100 gzipstream.write(requestContent);
101 gzipstream.finish();
102
103
104 return compressedContent.toByteArray();
105 }
106
107 private static byte[] GZIPDecompress(byte[] content) throws IOException
108 {
109 GZIPInputStream zipin;
110 InputStream in = new ByteArrayInputStream( content );
111 zipin = new GZIPInputStream( in );
112 ByteArrayOutputStream out = Tools.readAll( zipin, -1 );
113 content = out.toByteArray();
114 out.close();
115 zipin.close();
116
117 return content;
118 }
119
120 private static byte[] DeflaterCompress(byte[] requestContent) throws IOException
121 {
122 ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
123 DeflaterOutputStream defstream = new DeflaterOutputStream(compressedContent);
124 defstream.write(requestContent);
125 defstream.finish();
126
127
128 return compressedContent.toByteArray();
129 }
130
131 private static byte[] DeflaterDecompress(byte[] content) throws IOException
132 {
133 InflaterInputStream zipin;
134 InputStream in = new ByteArrayInputStream( content );
135 zipin = new InflaterInputStream( in );
136 ByteArrayOutputStream out = Tools.readAll( zipin, -1 );
137 content = out.toByteArray();
138 out.close();
139 zipin.close();
140
141 return content;
142 }
143 }