View Javadoc

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