View Javadoc

1   /*
2    * $Id: JXPathNormalizerInterceptor.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
11  package org.mule.interceptors;
12  
13  import org.mule.impl.MuleMessage;
14  import org.mule.umo.Invocation;
15  import org.mule.umo.UMOException;
16  import org.mule.umo.UMOMessage;
17  import org.mule.util.StringUtils;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.jxpath.JXPathContext;
23  
24  /**
25   * <code>JXPathNormalizerInterceptor</code> can be used as a simple pre/post
26   * message transformer for a given component. <p/> Users can set JXPath expressions
27   * to execute before and after the component reeives the event. The
28   * <i>beforeExpressions</i> can be a single expression or a comma separated list of
29   * expressions, each of which result in an object that will be used as an argument to
30   * the method called on the component. The <i>afterExpression</i> is a single
31   * expression that will be used to set a value on the orginal payload.
32   */
33  public class JXPathNormalizerInterceptor extends MessageNormalizerInterceptor
34  {
35      private List beforeExpressionsList;
36      private String beforeExpressions;
37      private String afterExpression;
38  
39      /**
40       * This method is invoked before the event is processed
41       * 
42       * @param invocation the message invocation being processed
43       */
44      public UMOMessage before(Invocation invocation) throws UMOException
45      {
46          if (beforeExpressions != null && beforeExpressionsList.size() > 0)
47          {
48              JXPathContext ctx = JXPathContext.newContext(getOriginalPayload());
49              Object[] result = new Object[beforeExpressionsList.size()];
50              for (int i = 0; i < result.length; i++)
51              {
52                  result[i] = ctx.getValue((String)beforeExpressionsList.get(i));
53              }
54              if (result.length == 1)
55              {
56                  return new MuleMessage(result[0], invocation.getMessage());
57              }
58              else
59              {
60                  return new MuleMessage(result, invocation.getMessage());
61              }
62          }
63          return null;
64      }
65  
66      /**
67       * This method is invoked after the event has been processed
68       * 
69       * @param invocation the message invocation being processed
70       */
71      public UMOMessage after(Invocation invocation) throws UMOException
72      {
73          if (afterExpression != null)
74          {
75              JXPathContext ctx = JXPathContext.newContext(getOriginalPayload());
76              ctx.setValue(afterExpression, invocation.getMessage().getPayload());
77              return new MuleMessage(getOriginalPayload(), invocation.getMessage());
78          }
79          return null;
80      }
81  
82      public String getBeforeExpressions()
83      {
84          return beforeExpressions;
85      }
86  
87      public void setBeforeExpressions(String beforeExpressions)
88      {
89          this.beforeExpressions = beforeExpressions;
90          String[] exp = StringUtils.splitAndTrim(beforeExpressions, ",");
91          this.beforeExpressionsList = new ArrayList(exp.length);
92          for (int i = 0; i < exp.length; i++)
93          {
94              this.beforeExpressionsList.add(exp[i]);
95  
96          }
97      }
98  
99      public String getAfterExpression()
100     {
101         return afterExpression;
102     }
103 
104     public void setAfterExpression(String afterExpression)
105     {
106         this.afterExpression = afterExpression;
107     }
108 }