View Javadoc

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