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.RequestContext;
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.CoreMessages;
14  import org.mule.transformer.AbstractTransformer;
15  import org.mule.transformer.types.DataTypeFactory;
16  import org.mule.util.IOUtils;
17  import org.mule.util.StringMessageUtils;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.UnsupportedEncodingException;
23  
24  /**
25   * <code>ObjectToString</code> transformer is useful for debugging. It will return
26   * human-readable output for various kinds of objects. Right now, it is just coded to
27   * handle Map and Collection objects. Others will be added.
28   */
29  public class ObjectToString extends AbstractTransformer implements DiscoverableTransformer
30  {
31      protected static final int DEFAULT_BUFFER_SIZE = 80;
32  
33      /** Give core transformers a slighty higher priority */
34      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
35  
36      public ObjectToString()
37      {
38          registerSourceType(DataTypeFactory.OBJECT);
39          registerSourceType(DataTypeFactory.BYTE_ARRAY);
40          registerSourceType(DataTypeFactory.INPUT_STREAM);
41          registerSourceType(DataTypeFactory.create(OutputHandler.class));
42          //deliberately set the mime for this transformer to text plain so that other transformers
43          //that serialize string types such as XML or JSON will not match this
44          setReturnDataType(DataTypeFactory.TEXT_STRING);
45      }
46  
47      @Override
48      public Object doTransform(Object src, String outputEncoding) throws TransformerException
49      {
50          String output = "";
51  
52          if (src instanceof InputStream)
53          {
54              output = createStringFromInputStream((InputStream) src, outputEncoding);
55          }
56          else if (src instanceof OutputHandler)
57          {
58              output = createStringFromOutputHandler((OutputHandler) src, outputEncoding);
59          }
60          else if (src instanceof byte[])
61          {
62              output = createStringFromByteArray((byte[]) src, outputEncoding);
63          }
64          else
65          {
66              output = StringMessageUtils.toString(src);
67          }
68  
69          return output;
70      }
71  
72      protected String createStringFromInputStream(InputStream input, String outputEncoding)
73          throws TransformerException
74      {
75          try
76          {
77              ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
78              IOUtils.copy(input, byteOut);
79              return byteOut.toString(outputEncoding);
80          }
81          catch (IOException e)
82          {
83              throw new TransformerException(CoreMessages.errorReadingStream(), e);
84          }
85          finally
86          {
87              try
88              {
89                  input.close();
90              }
91              catch (IOException e)
92              {
93                  logger.warn("Could not close stream", e);
94              }
95          }
96      }
97  
98      protected String createStringFromOutputHandler(OutputHandler handler, String outputEncoding)
99          throws TransformerException
100     {
101         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
102         try
103         {
104             handler.write(RequestContext.getEvent(), bytes);
105             return bytes.toString(outputEncoding);
106         }
107         catch (IOException e)
108         {
109             throw new TransformerException(this, e);
110         }
111     }
112 
113     protected String createStringFromByteArray(byte[] bytes, String outputEncoding)
114         throws TransformerException
115     {
116         try
117         {
118             return new String(bytes, outputEncoding);
119         }
120         catch (UnsupportedEncodingException e)
121         {
122             throw new TransformerException(this, e);
123         }
124     }
125 
126     public int getPriorityWeighting()
127     {
128         return priorityWeighting;
129     }
130 
131     public void setPriorityWeighting(int priorityWeighting)
132     {
133         this.priorityWeighting = priorityWeighting;
134     }
135 }