View Javadoc

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