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