1 10 11 package org.mule.transformers.wire; 12 13 import org.mule.umo.UMOException; 14 import org.mule.umo.transformer.UMOTransformer; 15 import org.mule.umo.transformer.TransformerException; 16 import org.mule.config.i18n.Message; 17 import org.mule.config.i18n.Messages; 18 import org.mule.util.IOUtils; 19 import org.mule.MuleException; 20 import org.mule.MuleManager; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 29 32 public class TransformerPairWireFormat implements WireFormat 33 { 34 35 38 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 if (inboundTransformer == null) 46 { 47 throw new NullPointerException (new Message(Messages.X_IS_NULL, "inboundTransformer").getMessage()); 48 } 49 if (inboundTransformer.isSourceTypeSupported(InputStream .class)) 50 { 51 return inboundTransformer.transform(in); 52 } 53 else 54 { 55 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 56 try 57 { 58 IOUtils.copy(in, baos); 59 return inboundTransformer.transform(baos.toByteArray()); 60 } 61 catch (IOException e) 62 { 63 throw new MuleException(new Message(Messages.FAILED_TO_READ_PAYLOAD, e)); 64 } 65 } 66 } 67 68 public void write(OutputStream out, Object o) throws UMOException 69 { 70 if (outboundTransformer == null) 71 { 72 throw new NullPointerException ( 73 new Message(Messages.X_IS_NULL, "outboundTransformer").getMessage()); 74 } 75 try 76 { 77 Class returnClass = outboundTransformer.getReturnClass(); 78 if (returnClass == null) 79 { 80 logger.warn("No return class was set on transformer: " + outboundTransformer + ". Attempting to work out" + 81 "how to treat the result transformation"); 82 Object result = outboundTransformer.transform(o); 83 byte[] bytes; 84 if(result instanceof byte[]) 85 { 86 bytes = (byte[])result; 87 } 88 else 89 { 90 bytes = result.toString().getBytes(MuleManager.getConfiguration().getEncoding()); 91 } 92 93 out.write(bytes); 94 } 95 else if (returnClass.equals(byte[].class)) 96 { 97 byte[] b = (byte[])outboundTransformer.transform(o); 98 out.write(b); 99 } 100 else 101 { 102 if (returnClass != null && returnClass.equals(String .class)) 103 { 104 String s = (String )outboundTransformer.transform(o); 105 out.write(s.getBytes(MuleManager.getConfiguration().getEncoding())); 106 } 107 else 108 { 109 throw new TransformerException( 110 new Message(Messages.TRANSFORM_FAILED_FROM_X, o.getClass())); 111 } 112 } 113 } 114 catch (IOException e) 115 { 116 throw new TransformerException(new Message(Messages.TRANSFORM_FAILED_FROM_X, o.getClass(), e)); 117 } 118 } 119 120 public UMOTransformer getInboundTransformer() 121 { 122 return inboundTransformer; 123 } 124 125 public void setInboundTransformer(UMOTransformer inboundTransformer) 126 { 127 this.inboundTransformer = inboundTransformer; 128 } 129 130 public UMOTransformer getOutboundTransformer() 131 { 132 return outboundTransformer; 133 } 134 135 public void setOutboundTransformer(UMOTransformer outboundTransformer) 136 { 137 this.outboundTransformer = outboundTransformer; 138 } 139 } 140 | Popular Tags |