View Javadoc

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