1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transformer.compression; |
12 | |
|
13 | |
import org.mule.api.transformer.TransformerException; |
14 | |
import org.mule.config.i18n.MessageFactory; |
15 | |
import org.mule.util.IOUtils; |
16 | |
import org.mule.util.compression.GZipCompression; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.io.InputStream; |
20 | |
|
21 | |
import org.apache.commons.lang.SerializationUtils; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class GZipUncompressTransformer extends AbstractCompressionTransformer |
27 | |
{ |
28 | |
|
29 | |
public GZipUncompressTransformer() |
30 | |
{ |
31 | 32 | super(); |
32 | 32 | this.setStrategy(new GZipCompression()); |
33 | 32 | this.registerSourceType(byte[].class); |
34 | 32 | this.registerSourceType(InputStream.class); |
35 | 32 | this.setReturnClass(byte[].class); |
36 | 32 | } |
37 | |
|
38 | |
|
39 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
40 | |
{ |
41 | |
byte[] buffer; |
42 | |
|
43 | |
try |
44 | |
{ |
45 | 18 | byte[] input = null; |
46 | 18 | if (src instanceof InputStream) |
47 | |
{ |
48 | 2 | InputStream inputStream = (InputStream) src; |
49 | |
try |
50 | |
{ |
51 | 2 | input = IOUtils.toByteArray(inputStream); |
52 | |
} |
53 | |
finally |
54 | |
{ |
55 | 2 | inputStream.close(); |
56 | 2 | } |
57 | 2 | } |
58 | |
else |
59 | |
{ |
60 | 16 | input = (byte[]) src; |
61 | |
} |
62 | |
|
63 | 18 | buffer = getStrategy().uncompressByteArray(input); |
64 | |
} |
65 | 0 | catch (IOException e) |
66 | |
{ |
67 | 0 | throw new TransformerException( |
68 | |
MessageFactory.createStaticMessage("Failed to uncompress message."), this, e); |
69 | 18 | } |
70 | |
|
71 | 18 | if (!getReturnClass().equals(byte[].class)) |
72 | |
{ |
73 | 12 | return SerializationUtils.deserialize(buffer); |
74 | |
} |
75 | |
|
76 | 6 | return buffer; |
77 | |
} |
78 | |
|
79 | |
} |