Coverage Report - org.mule.expression.transformers.ExpressionTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionTransformer
0%
0/28
0%
0/20
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.expression.transformers;
 8  
 
 9  
 import org.mule.api.MuleMessage;
 10  
 import org.mule.api.expression.ExpressionRuntimeException;
 11  
 import org.mule.api.expression.RequiredValueException;
 12  
 import org.mule.api.transformer.TransformerException;
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.transport.NullPayload;
 15  
 
 16  
 import java.util.Iterator;
 17  
 
 18  
 /**
 19  
  * This transformer will evaluate one or more expressions on the current message and return the
 20  
  * results as an Array. If only one expression is defined it will return the object returned from
 21  
  * the expression.
 22  
  * <p/>
 23  
  * You can use expressions to extract
 24  
  * <ul>
 25  
  * <li>headers (single, map or list)</li>
 26  
  * <li>attachments (single, map or list)</li>
 27  
  * <li>payload</li>
 28  
  * <li>xpath</li>
 29  
  * <li>groovy</li>
 30  
  * <li>bean</li>
 31  
  * </ul>
 32  
  * and more.
 33  
  * <p/>
 34  
  * This transformer provides a very powerful way to pull different bits of information from the
 35  
  * message and pass them to the service.
 36  
  */
 37  0
 public class ExpressionTransformer extends AbstractExpressionTransformer
 38  
 {
 39  0
     private boolean returnSourceIfNull = false;
 40  
 
 41  
     @Override
 42  
     public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
 43  
     {
 44  0
         Object results[] = new Object[arguments.size()];
 45  0
         int i = 0;
 46  0
         for (Iterator<ExpressionArgument> iterator = arguments.iterator(); iterator.hasNext(); i++)
 47  
         {
 48  0
             ExpressionArgument argument = iterator.next();
 49  
             try
 50  
             {
 51  0
                 results[i] = argument.evaluate(message);
 52  
             }
 53  0
             catch (RequiredValueException e)
 54  
             {
 55  0
                 if(!argument.isOptional())
 56  
                 {
 57  0
                     throw e;
 58  
                 }
 59  0
                 logger.warn(e.getMessage());
 60  
             }
 61  0
             catch (ExpressionRuntimeException e)
 62  
             {
 63  0
                 throw new TransformerException(this, e);
 64  0
             }
 65  
 
 66  0
             if (!argument.isOptional() && results[i] == null)
 67  
             {
 68  0
                 throw new TransformerException(CoreMessages.expressionEvaluatorReturnedNull(
 69  
                         argument.getExpressionConfig().getEvaluator(), argument.getExpressionConfig().getExpression()), this);
 70  
 
 71  
             }
 72  
 
 73  
         }
 74  0
         if (isReturnSourceIfNull() && checkIfAllAreNull(results))
 75  
         {
 76  0
             return message;
 77  
         }
 78  
 
 79  0
         if (results.length == 1)
 80  
         {
 81  0
             return results[0];
 82  
         }
 83  
         else
 84  
         {
 85  0
             return results;
 86  
         }
 87  
     }
 88  
 
 89  
     private boolean checkIfAllAreNull(Object[] objects)
 90  
     {
 91  0
         for (int i = 0; i < objects.length; i++)
 92  
         {
 93  0
             if (objects[i] != null && !(objects[i] instanceof NullPayload))
 94  
             {
 95  0
                 return false;
 96  
             }
 97  
         }
 98  0
         return true;
 99  
     }
 100  
 
 101  
     public boolean isReturnSourceIfNull()
 102  
     {
 103  0
         return returnSourceIfNull;
 104  
     }
 105  
 
 106  
     public void setReturnSourceIfNull(boolean returnSourceIfNull)
 107  
     {
 108  0
         this.returnSourceIfNull = returnSourceIfNull;
 109  0
     }
 110  
 }