View Javadoc

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