Coverage Report - org.mule.interceptors.JXPathNormalizerInterceptor
 
Classes in this File Line Coverage Branch Coverage Complexity
JXPathNormalizerInterceptor
0%
0/25
0%
0/12
2.333
 
 1  
 /*
 2  
  * $Id: JXPathNormalizerInterceptor.java 7963 2007-08-21 08:53:15Z 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  0
 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  0
         if (beforeExpressions != null && beforeExpressionsList.size() > 0)
 47  
         {
 48  0
             JXPathContext ctx = JXPathContext.newContext(getOriginalPayload());
 49  0
             Object[] result = new Object[beforeExpressionsList.size()];
 50  0
             for (int i = 0; i < result.length; i++)
 51  
             {
 52  0
                 result[i] = ctx.getValue((String)beforeExpressionsList.get(i));
 53  
             }
 54  0
             if (result.length == 1)
 55  
             {
 56  0
                 return new MuleMessage(result[0], invocation.getMessage());
 57  
             }
 58  
             else
 59  
             {
 60  0
                 return new MuleMessage(result, invocation.getMessage());
 61  
             }
 62  
         }
 63  0
         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  0
         if (afterExpression != null)
 74  
         {
 75  0
             JXPathContext ctx = JXPathContext.newContext(getOriginalPayload());
 76  0
             ctx.setValue(afterExpression, invocation.getMessage().getPayload());
 77  0
             return new MuleMessage(getOriginalPayload(), invocation.getMessage());
 78  
         }
 79  0
         return null;
 80  
     }
 81  
 
 82  
     public String getBeforeExpressions()
 83  
     {
 84  0
         return beforeExpressions;
 85  
     }
 86  
 
 87  
     public void setBeforeExpressions(String beforeExpressions)
 88  
     {
 89  0
         this.beforeExpressions = beforeExpressions;
 90  0
         String[] exp = StringUtils.splitAndTrim(beforeExpressions, ",");
 91  0
         this.beforeExpressionsList = new ArrayList(exp.length);
 92  0
         for (int i = 0; i < exp.length; i++)
 93  
         {
 94  0
             this.beforeExpressionsList.add(exp[i]);
 95  
 
 96  
         }
 97  0
     }
 98  
 
 99  
     public String getAfterExpression()
 100  
     {
 101  0
         return afterExpression;
 102  
     }
 103  
 
 104  
     public void setAfterExpression(String afterExpression)
 105  
     {
 106  0
         this.afterExpression = afterExpression;
 107  0
     }
 108  
 }