Coverage Report - org.mule.transformer.compression.GZipUncompressTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
GZipUncompressTransformer
0%
0/23
0%
0/6
0
 
 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  0
         super();
 29  0
         this.setStrategy(new GZipCompression());
 30  0
         this.registerSourceType(DataTypeFactory.BYTE_ARRAY);
 31  0
         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  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  
         // If a return type different than a byte array has been specified, then deserialize the uncompressed byte array.
 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  
             // First try to deserialize the byte array. If it can be deserialized, then it was originally serialized.
 77  
             try
 78  
             {
 79  0
                 return SerializationUtils.deserialize(buffer, muleContext);
 80  
             }
 81  0
             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  0
                 return buffer;
 85  
             }
 86  
         }
 87  
     }
 88  
 }