Coverage Report - org.mule.transformers.wire.TransformerPairWireFormat
 
Classes in this File Line Coverage Branch Coverage Complexity
TransformerPairWireFormat
0%
0/38
0%
0/7
3.667
 
 1  
 /*
 2  
  * $Id: TransformerPairWireFormat.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transformers.wire;
 12  
 
 13  
 import org.mule.MuleException;
 14  
 import org.mule.MuleManager;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.umo.UMOException;
 17  
 import org.mule.umo.transformer.TransformerException;
 18  
 import org.mule.umo.transformer.UMOTransformer;
 19  
 import org.mule.util.IOUtils;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.io.OutputStream;
 24  
 
 25  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 /**
 30  
  * TODO
 31  
  */
 32  0
 public class TransformerPairWireFormat implements WireFormat
 33  
 {
 34  
 
 35  
     /**
 36  
      * logger used by this class
 37  
      */
 38  0
     protected transient Log logger = LogFactory.getLog(getClass());
 39  
 
 40  
     protected UMOTransformer inboundTransformer;
 41  
     protected UMOTransformer outboundTransformer;
 42  
 
 43  
     public Object read(InputStream in) throws UMOException
 44  
     {
 45  0
         if (inboundTransformer == null)
 46  
         {
 47  0
             throw new IllegalArgumentException(CoreMessages.objectIsNull("inboundTransformer").getMessage());
 48  
         }
 49  0
         if (inboundTransformer.isSourceTypeSupported(InputStream.class))
 50  
         {
 51  0
             return inboundTransformer.transform(in);
 52  
         }
 53  
         else
 54  
         {
 55  
             try
 56  
             {
 57  0
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 58  0
                 IOUtils.copy(in, baos);
 59  0
                 return inboundTransformer.transform(baos.toByteArray());
 60  
             }
 61  0
             catch (IOException e)
 62  
             {
 63  0
                 throw new MuleException(CoreMessages.failedToReadPayload(), e);
 64  
             }
 65  
         }
 66  
     }
 67  
 
 68  
     public void write(OutputStream out, Object o) throws UMOException
 69  
     {
 70  0
         if (outboundTransformer == null)
 71  
         {
 72  0
             throw new IllegalArgumentException(CoreMessages.objectIsNull("outboundTransformer").getMessage());
 73  
         }
 74  
         try
 75  
         {
 76  0
             Class returnClass = outboundTransformer.getReturnClass();
 77  0
             if (returnClass == null)
 78  
             {
 79  0
                 logger.warn("No return class was set on transformer: " + outboundTransformer
 80  
                                 + ". Attempting to work out how to treat the result transformation");
 81  
 
 82  0
                 Object result = outboundTransformer.transform(o);
 83  
 
 84  
                 byte[] bytes;
 85  0
                 if (result instanceof byte[])
 86  
                 {
 87  0
                     bytes = (byte[]) result;
 88  
                 }
 89  
                 else
 90  
                 {
 91  0
                     bytes = result.toString().getBytes(MuleManager.getConfiguration().getEncoding());
 92  
                 }
 93  
 
 94  0
                 out.write(bytes);
 95  
             }
 96  0
             else if (returnClass.equals(byte[].class))
 97  
             {
 98  0
                 byte[] b = (byte[]) outboundTransformer.transform(o);
 99  0
                 out.write(b);
 100  
             }
 101  0
             else if (returnClass.equals(String.class))
 102  
             {
 103  0
                 String s = (String) outboundTransformer.transform(o);
 104  0
                 out.write(s.getBytes(MuleManager.getConfiguration().getEncoding()));
 105  
             }
 106  
             else
 107  
             {
 108  0
                 throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()));
 109  
             }
 110  
         }
 111  0
         catch (IOException e)
 112  
         {
 113  0
             throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()), e);
 114  0
         }
 115  0
     }
 116  
 
 117  
     public UMOTransformer getInboundTransformer()
 118  
     {
 119  0
         return inboundTransformer;
 120  
     }
 121  
 
 122  
     public void setInboundTransformer(UMOTransformer inboundTransformer)
 123  
     {
 124  0
         this.inboundTransformer = inboundTransformer;
 125  0
     }
 126  
 
 127  
     public UMOTransformer getOutboundTransformer()
 128  
     {
 129  0
         return outboundTransformer;
 130  
     }
 131  
 
 132  
     public void setOutboundTransformer(UMOTransformer outboundTransformer)
 133  
     {
 134  0
         this.outboundTransformer = outboundTransformer;
 135  0
     }
 136  
 
 137  
 }