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.transformer.types.DataTypeFactory; |
16 | |
import org.mule.util.IOUtils; |
17 | |
import org.mule.util.compression.GZipCompression; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.io.InputStream; |
21 | |
|
22 | |
import org.apache.commons.lang.SerializationUtils; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class GZipUncompressTransformer extends AbstractCompressionTransformer |
28 | |
{ |
29 | |
public GZipUncompressTransformer() |
30 | |
{ |
31 | 0 | super(); |
32 | 0 | this.setStrategy(new GZipCompression()); |
33 | 0 | this.registerSourceType(DataTypeFactory.BYTE_ARRAY); |
34 | 0 | this.registerSourceType(DataTypeFactory.INPUT_STREAM); |
35 | 0 | this.setReturnDataType(DataTypeFactory.BYTE_ARRAY); |
36 | 0 | } |
37 | |
|
38 | |
@Override |
39 | |
public Object doTransform(Object src, String outputEncoding) throws TransformerException |
40 | |
{ |
41 | |
byte[] buffer; |
42 | |
|
43 | |
try |
44 | |
{ |
45 | 0 | byte[] input = null; |
46 | 0 | if (src instanceof InputStream) |
47 | |
{ |
48 | 0 | InputStream inputStream = (InputStream) src; |
49 | |
try |
50 | |
{ |
51 | 0 | input = IOUtils.toByteArray(inputStream); |
52 | |
} |
53 | |
finally |
54 | |
{ |
55 | 0 | inputStream.close(); |
56 | 0 | } |
57 | 0 | } |
58 | |
else |
59 | |
{ |
60 | 0 | input = (byte[]) src; |
61 | |
} |
62 | |
|
63 | 0 | 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 | 0 | } |
70 | |
|
71 | 0 | if (!DataTypeFactory.BYTE_ARRAY.equals(getReturnDataType())) |
72 | |
{ |
73 | 0 | return SerializationUtils.deserialize(buffer); |
74 | |
} |
75 | |
|
76 | 0 | return buffer; |
77 | |
} |
78 | |
} |