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.simple;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.transformer.DiscoverableTransformer;
11  import org.mule.api.transformer.TransformerException;
12  import org.mule.api.transport.OutputHandler;
13  import org.mule.config.i18n.MessageFactory;
14  import org.mule.transformer.AbstractTransformer;
15  import org.mule.transformer.types.DataTypeFactory;
16  import org.mule.util.IOUtils;
17  import org.mule.util.SerializationUtils;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.io.Serializable;
23  
24  /** <code>ObjectToOutputHandler</code> converts a byte array into a String. */
25  public class ObjectToOutputHandler extends AbstractTransformer implements DiscoverableTransformer
26  {
27  
28      /** Give core transformers a slighty higher priority */
29      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
30  
31      public ObjectToOutputHandler()
32      {
33          registerSourceType(DataTypeFactory.BYTE_ARRAY);
34          registerSourceType(DataTypeFactory.STRING);
35          registerSourceType(DataTypeFactory.INPUT_STREAM);
36          registerSourceType(DataTypeFactory.create(Serializable.class));
37          setReturnDataType(DataTypeFactory.create(OutputHandler.class));
38      }
39  
40      @Override
41      public Object doTransform(final Object src, final String encoding) throws TransformerException
42      {
43          if (src instanceof String)
44          {
45              return new OutputHandler()
46              {
47                  public void write(MuleEvent event, OutputStream out) throws IOException
48                  {
49                      out.write(((String) src).getBytes(encoding));
50                  }
51              };
52          }
53          else if (src instanceof byte[])
54          {
55              return new OutputHandler()
56              {
57                  public void write(MuleEvent event, OutputStream out) throws IOException
58                  {
59                      out.write((byte[]) src);
60                  }
61              };
62          }
63          else if (src instanceof InputStream)
64          {
65              return new OutputHandler()
66              {
67                  public void write(MuleEvent event, OutputStream out) throws IOException
68                  {
69                      InputStream is = (InputStream) src;
70                      try
71                      {
72                          IOUtils.copyLarge(is, out);
73                      }
74                      finally
75                      {
76                          is.close();
77                      }
78                  }
79              };
80          }
81          else if (src instanceof Serializable)
82          {
83              return new OutputHandler()
84              {
85                  public void write(MuleEvent event, OutputStream out) throws IOException
86                  {
87                      SerializationUtils.serialize((Serializable) src, out);
88                  }
89              };
90          }
91          else
92          {
93              throw new TransformerException(MessageFactory
94                      .createStaticMessage("Unable to convert " + src.getClass() + " to OutputHandler."));
95          }
96      }
97  
98      public int getPriorityWeighting()
99      {
100         return priorityWeighting;
101     }
102 
103     public void setPriorityWeighting(int priorityWeighting)
104     {
105         this.priorityWeighting = priorityWeighting;
106     }
107 }