View Javadoc
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  public class TransformerPairWireFormat implements WireFormat
33  {
34      /**
35       * logger used by this class
36       */
37      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          this.muleContext = context;
46          inboundTransformer.setMuleContext(muleContext);
47          outboundTransformer.setMuleContext(muleContext);
48      }
49  
50      public Object read(InputStream in) throws MuleException
51      {
52          if (inboundTransformer == null)
53          {
54              throw new IllegalArgumentException(CoreMessages.objectIsNull("inboundTransformer").getMessage());
55          }
56          if (inboundTransformer.isSourceDataTypeSupported(DataTypeFactory.INPUT_STREAM))
57          {
58              return inboundTransformer.transform(in);
59          }
60          else
61          {
62              try
63              {
64                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
65                  IOUtils.copy(in, baos);
66                  return inboundTransformer.transform(baos.toByteArray());
67              }
68              catch (IOException e)
69              {
70                  throw new DefaultMuleException(CoreMessages.failedToReadPayload(), e);
71              }
72          }
73      }
74  
75      public void write(OutputStream out, Object o, String encoding) throws MuleException
76      {
77          if (outboundTransformer == null)
78          {
79              throw new IllegalArgumentException(CoreMessages.objectIsNull("outboundTransformer").getMessage());
80          }
81          try
82          {
83              Class returnClass = outboundTransformer.getReturnClass();
84              if (returnClass == null)
85              {
86                  logger.warn("No return class was set on transformer: " + outboundTransformer
87                                  + ". Attempting to work out how to treat the result transformation");
88  
89                  Object result = outboundTransformer.transform(o);
90  
91                  byte[] bytes;
92                  if (result instanceof byte[])
93                  {
94                      bytes = (byte[]) result;
95                  }
96                  else
97                  {
98                      bytes = result.toString().getBytes(encoding);
99                  }
100 
101                 out.write(bytes);
102             }
103             else if (returnClass.equals(byte[].class))
104             {
105                 byte[] b = (byte[]) outboundTransformer.transform(o);
106                 out.write(b);
107             }
108             else if (returnClass.equals(String.class))
109             {
110                 String s = (String) outboundTransformer.transform(o);
111                 out.write(s.getBytes(encoding));
112             }
113             else
114             {
115                 throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()));
116             }
117         }
118         catch (IOException e)
119         {
120             throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()), e);
121         }
122     }
123 
124     public Transformer getInboundTransformer()
125     {
126         return inboundTransformer;
127     }
128 
129     public void setInboundTransformer(Transformer inboundTransformer)
130     {
131         this.inboundTransformer = inboundTransformer;
132     }
133 
134     public Transformer getOutboundTransformer()
135     {
136         return outboundTransformer;
137     }
138 
139     public void setOutboundTransformer(Transformer outboundTransformer)
140     {
141         this.outboundTransformer = outboundTransformer;
142     }
143 }