View Javadoc

1   /*
2    * $Id: MessageNormalizerInterceptor.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.config.MuleProperties;
14  import org.mule.impl.RequestContext;
15  import org.mule.umo.Invocation;
16  import org.mule.umo.UMOException;
17  import org.mule.umo.UMOInterceptor;
18  import org.mule.umo.UMOMessage;
19  
20  /**
21   * <code>MessageNormalizerInterceptor</code> can be used as a simple pre/post message
22   * normalizer for a given component. This is useful in situations where you have an
23   * existing component that may accept a one or more child objects of the incoming object.
24   * For example, you may Have a BankQuoteRequest object that contains customer, credit and
25   * loan details, but one component is only interested in enriching the customer
26   * information. Rather than have your component understand how to deal with a
27   * BankLoanRequest this interceptor can be used to extract the customer and pass that to
28   * the component. Once the component have finshed processing this interceptor update the
29   * BankLoanRequest with the new customer information.
30   */
31  public abstract class MessageNormalizerInterceptor implements UMOInterceptor
32  {
33      private Object originalPayload = null;
34  
35      /**
36       * This method is invoked before the event is processed
37       * 
38       * @param invocation the message invocation being processed
39       */
40      public abstract UMOMessage before(Invocation invocation) throws UMOException;
41  
42      /**
43       * This method is invoked after the event has been processed
44       * 
45       * @param invocation the message invocation being processed
46       */
47      public abstract UMOMessage after(Invocation invocation) throws UMOException;
48  
49      public final UMOMessage intercept(Invocation invocation) throws UMOException
50      {
51          // store the original payload as we will need it later
52          originalPayload = invocation.getEvent().getTransformedMessage();
53  
54          // get the updated message
55          UMOMessage bMessage = before(invocation);
56          if (bMessage != null)
57          {
58              // update the current event
59              RequestContext.rewriteEvent(bMessage);
60              // update the message in the invocation
61              invocation.setMessage(bMessage);
62              // remove any method override as it will not apply to the new
63              // message payload
64              invocation.getMessage().removeProperty(MuleProperties.MULE_METHOD_PROPERTY);
65          }
66          // invoke
67          UMOMessage message = invocation.execute();
68          // Update the message
69          invocation.setMessage(message);
70          UMOMessage aMessage = after(invocation);
71          if (aMessage == null)
72          {
73              return message;
74          }
75          else
76          {
77              return aMessage;
78          }
79      }
80  
81      protected Object getOriginalPayload()
82      {
83          return originalPayload;
84      }
85  
86      protected void setOriginalPayload(Object originalPayload)
87      {
88          this.originalPayload = originalPayload;
89      }
90  }