View Javadoc

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