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  
  * 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  0
     private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
 35  
 
 36  
     public ObjectToString()
 37  0
     {
 38  0
         registerSourceType(DataTypeFactory.OBJECT);
 39  0
         registerSourceType(DataTypeFactory.BYTE_ARRAY);
 40  0
         registerSourceType(DataTypeFactory.INPUT_STREAM);
 41  0
         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  0
         setReturnDataType(DataTypeFactory.TEXT_STRING);
 45  0
     }
 46  
 
 47  
     @Override
 48  
     public Object doTransform(Object src, String outputEncoding) throws TransformerException
 49  
     {
 50  0
         String output = "";
 51  
 
 52  0
         if (src instanceof InputStream)
 53  
         {
 54  0
             output = createStringFromInputStream((InputStream) src, outputEncoding);
 55  
         }
 56  0
         else if (src instanceof OutputHandler)
 57  
         {
 58  0
             output = createStringFromOutputHandler((OutputHandler) src, outputEncoding);
 59  
         }
 60  0
         else if (src instanceof byte[])
 61  
         {
 62  0
             output = createStringFromByteArray((byte[]) src, outputEncoding);
 63  
         }
 64  
         else
 65  
         {
 66  0
             output = StringMessageUtils.toString(src);
 67  
         }
 68  
 
 69  0
         return output;
 70  
     }
 71  
 
 72  
     protected String createStringFromInputStream(InputStream input, String outputEncoding)
 73  
         throws TransformerException
 74  
     {
 75  
         try
 76  
         {
 77  0
             ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
 78  0
             IOUtils.copy(input, byteOut);
 79  0
             return byteOut.toString(outputEncoding);
 80  
         }
 81  0
         catch (IOException e)
 82  
         {
 83  0
             throw new TransformerException(CoreMessages.errorReadingStream(), e);
 84  
         }
 85  
         finally
 86  
         {
 87  0
             try
 88  
             {
 89  0
                 input.close();
 90  
             }
 91  0
             catch (IOException e)
 92  
             {
 93  0
                 logger.warn("Could not close stream", e);
 94  0
             }
 95  
         }
 96  
     }
 97  
 
 98  
     protected String createStringFromOutputHandler(OutputHandler handler, String outputEncoding)
 99  
         throws TransformerException
 100  
     {
 101  0
         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
 102  
         try
 103  
         {
 104  0
             handler.write(RequestContext.getEvent(), bytes);
 105  0
             return bytes.toString(outputEncoding);
 106  
         }
 107  0
         catch (IOException e)
 108  
         {
 109  0
             throw new TransformerException(this, e);
 110  
         }
 111  
     }
 112  
 
 113  
     protected String createStringFromByteArray(byte[] bytes, String outputEncoding)
 114  
         throws TransformerException
 115  
     {
 116  
         try
 117  
         {
 118  0
             return new String(bytes, outputEncoding);
 119  
         }
 120  0
         catch (UnsupportedEncodingException e)
 121  
         {
 122  0
             throw new TransformerException(this, e);
 123  
         }
 124  
     }
 125  
 
 126  
     public int getPriorityWeighting()
 127  
     {
 128  0
         return priorityWeighting;
 129  
     }
 130  
 
 131  
     public void setPriorityWeighting(int priorityWeighting)
 132  
     {
 133  0
         this.priorityWeighting = priorityWeighting;
 134  0
     }
 135  
 }