Coverage Report - org.mule.transformer.wire.TransformerPairWireFormat
 
Classes in this File Line Coverage Branch Coverage Complexity
TransformerPairWireFormat
0%
0/45
0%
0/14
3.286
 
 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.wire;
 8  
 
 9  
 import org.mule.api.DefaultMuleException;
 10  
 import org.mule.api.MuleContext;
 11  
 import org.mule.api.MuleException;
 12  
 import org.mule.api.transformer.Transformer;
 13  
 import org.mule.api.transformer.TransformerException;
 14  
 import org.mule.api.transformer.wire.WireFormat;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.transformer.types.DataTypeFactory;
 17  
 import org.mule.util.IOUtils;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.io.OutputStream;
 22  
 
 23  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 /**
 28  
  * A pairing of an outbound transformer and an inbound transformer that can be used to serialize and deserialize data.
 29  
  * THis is used when marshalling requests over the wire. IN Mule the MuleClient RemoteDispatcher uses wire formats to
 30  
  * communicate with the server.
 31  
  */
 32  0
 public class TransformerPairWireFormat implements WireFormat
 33  
 {
 34  
     /**
 35  
      * logger used by this class
 36  
      */
 37  0
     protected transient Log logger = LogFactory.getLog(getClass());
 38  
 
 39  
     protected Transformer inboundTransformer;
 40  
     protected Transformer outboundTransformer;
 41  
     protected MuleContext muleContext;
 42  
 
 43  
     public void setMuleContext(MuleContext context)
 44  
     {
 45  0
         this.muleContext = context;
 46  0
         inboundTransformer.setMuleContext(muleContext);
 47  0
         outboundTransformer.setMuleContext(muleContext);
 48  0
     }
 49  
 
 50  
     public Object read(InputStream in) throws MuleException
 51  
     {
 52  0
         if (inboundTransformer == null)
 53  
         {
 54  0
             throw new IllegalArgumentException(CoreMessages.objectIsNull("inboundTransformer").getMessage());
 55  
         }
 56  0
         if (inboundTransformer.isSourceDataTypeSupported(DataTypeFactory.INPUT_STREAM))
 57  
         {
 58  0
             return inboundTransformer.transform(in);
 59  
         }
 60  
         else
 61  
         {
 62  
             try
 63  
             {
 64  0
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 65  0
                 IOUtils.copy(in, baos);
 66  0
                 return inboundTransformer.transform(baos.toByteArray());
 67  
             }
 68  0
             catch (IOException e)
 69  
             {
 70  0
                 throw new DefaultMuleException(CoreMessages.failedToReadPayload(), e);
 71  
             }
 72  
         }
 73  
     }
 74  
 
 75  
     public void write(OutputStream out, Object o, String encoding) throws MuleException
 76  
     {
 77  0
         if (outboundTransformer == null)
 78  
         {
 79  0
             throw new IllegalArgumentException(CoreMessages.objectIsNull("outboundTransformer").getMessage());
 80  
         }
 81  
         try
 82  
         {
 83  0
             Class returnClass = outboundTransformer.getReturnClass();
 84  0
             if (returnClass == null)
 85  
             {
 86  0
                 logger.warn("No return class was set on transformer: " + outboundTransformer
 87  
                                 + ". Attempting to work out how to treat the result transformation");
 88  
 
 89  0
                 Object result = outboundTransformer.transform(o);
 90  
 
 91  
                 byte[] bytes;
 92  0
                 if (result instanceof byte[])
 93  
                 {
 94  0
                     bytes = (byte[]) result;
 95  
                 }
 96  
                 else
 97  
                 {
 98  0
                     bytes = result.toString().getBytes(encoding);
 99  
                 }
 100  
 
 101  0
                 out.write(bytes);
 102  0
             }
 103  0
             else if (returnClass.equals(byte[].class))
 104  
             {
 105  0
                 byte[] b = (byte[]) outboundTransformer.transform(o);
 106  0
                 out.write(b);
 107  0
             }
 108  0
             else if (returnClass.equals(String.class))
 109  
             {
 110  0
                 String s = (String) outboundTransformer.transform(o);
 111  0
                 out.write(s.getBytes(encoding));
 112  0
             }
 113  
             else
 114  
             {
 115  0
                 throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()));
 116  
             }
 117  
         }
 118  0
         catch (IOException e)
 119  
         {
 120  0
             throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()), e);
 121  0
         }
 122  0
     }
 123  
 
 124  
     public Transformer getInboundTransformer()
 125  
     {
 126  0
         return inboundTransformer;
 127  
     }
 128  
 
 129  
     public void setInboundTransformer(Transformer inboundTransformer)
 130  
     {
 131  0
         this.inboundTransformer = inboundTransformer;
 132  0
     }
 133  
 
 134  
     public Transformer getOutboundTransformer()
 135  
     {
 136  0
         return outboundTransformer;
 137  
     }
 138  
 
 139  
     public void setOutboundTransformer(Transformer outboundTransformer)
 140  
     {
 141  0
         this.outboundTransformer = outboundTransformer;
 142  0
     }
 143  
 }