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