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