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.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  public class ExpressionTransformer extends AbstractExpressionTransformer
38  {
39      private boolean returnSourceIfNull = false;
40  
41      @Override
42      public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
43      {
44          Object results[] = new Object[arguments.size()];
45          int i = 0;
46          for (Iterator<ExpressionArgument> iterator = arguments.iterator(); iterator.hasNext(); i++)
47          {
48              ExpressionArgument argument = iterator.next();
49              try
50              {
51                  results[i] = argument.evaluate(message);
52              }
53              catch (RequiredValueException e)
54              {
55                  if(!argument.isOptional())
56                  {
57                      throw e;
58                  }
59                  logger.warn(e.getMessage());
60              }
61              catch (ExpressionRuntimeException e)
62              {
63                  throw new TransformerException(this, e);
64              }
65  
66              if (!argument.isOptional() && results[i] == null)
67              {
68                  throw new TransformerException(CoreMessages.expressionEvaluatorReturnedNull(
69                          argument.getExpressionConfig().getEvaluator(), argument.getExpressionConfig().getExpression()), this);
70  
71              }
72  
73          }
74          if (isReturnSourceIfNull() && checkIfAllAreNull(results))
75          {
76              return message;
77          }
78  
79          if (results.length == 1)
80          {
81              return results[0];
82          }
83          else
84          {
85              return results;
86          }
87      }
88  
89      private boolean checkIfAllAreNull(Object[] objects)
90      {
91          for (int i = 0; i < objects.length; i++)
92          {
93              if (objects[i] != null && !(objects[i] instanceof NullPayload))
94              {
95                  return false;
96              }
97          }
98          return true;
99      }
100 
101     public boolean isReturnSourceIfNull()
102     {
103         return returnSourceIfNull;
104     }
105 
106     public void setReturnSourceIfNull(boolean returnSourceIfNull)
107     {
108         this.returnSourceIfNull = returnSourceIfNull;
109     }
110 }