View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * <code>GZipCompressTransformer</code> will uncompress a byte[] or InputStream
23   */
24  public class GZipUncompressTransformer extends AbstractCompressionTransformer
25  {
26      public GZipUncompressTransformer()
27      {
28          super();
29          this.setStrategy(new GZipCompression());
30          this.registerSourceType(DataTypeFactory.BYTE_ARRAY);
31          this.registerSourceType(DataTypeFactory.INPUT_STREAM);
32          // No type checking for the return type by default. It could either be a byte array or an object.
33          this.setReturnDataType(DataTypeFactory.OBJECT);
34      }
35  
36      @Override
37      public Object doTransform(Object src, String outputEncoding) throws TransformerException
38      {
39          byte[] buffer;
40  
41          try
42          {
43              byte[] input = null;
44              if (src instanceof InputStream)
45              {
46                  InputStream inputStream = (InputStream) src;
47                  try
48                  {
49                      input = IOUtils.toByteArray(inputStream);
50                  }
51                  finally
52                  {
53                      inputStream.close();
54                  }
55              }
56              else
57              {
58                  input = (byte[]) src;
59              }
60  
61              buffer = getStrategy().uncompressByteArray(input);
62          }
63          catch (IOException e)
64          {
65              throw new TransformerException(
66                      MessageFactory.createStaticMessage("Failed to uncompress message."), this, e);
67          }
68  
69          // If a return type different than a byte array has been specified, then deserialize the uncompressed byte array.
70          if (!DataTypeFactory.OBJECT.equals(getReturnDataType()) && !DataTypeFactory.BYTE_ARRAY.equals(getReturnDataType()))
71          {
72              return SerializationUtils.deserialize(buffer, muleContext);
73          }
74          else
75          {
76              // First try to deserialize the byte array. If it can be deserialized, then it was originally serialized.
77              try
78              {
79                  return SerializationUtils.deserialize(buffer, muleContext);
80              }
81              catch (SerializationException e)
82              {
83                  // If it fails, ignore it. We assume it was not serialized in the first place and return the buffer as it is.
84                  return buffer;
85              }
86          }
87      }
88  }