View Javadoc

1   /*
2    * $Id$
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.routing.outbound;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.impl.MuleMessage;
15  import org.mule.umo.MessagingException;
16  import org.mule.umo.UMOMessage;
17  import org.mule.umo.UMOSession;
18  import org.mule.umo.endpoint.UMOEndpoint;
19  import org.mule.umo.routing.RoutingException;
20  import org.mule.umo.transformer.TransformerException;
21  import org.mule.umo.transformer.UMOTransformer;
22  
23  /**
24   * Simply applies a transformer before continuing on to the next router.
25   * This can be useful with the {@link ChainingRouter}.
26   */
27  public class TransformerRouter extends AbstractOutboundRouter
28  {
29      private UMOTransformer transformer;
30  
31      public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) throws MessagingException
32      {
33          if (transformer != null)
34          {
35              try
36              {
37                  Object payload = transformer.transform(message.getPayload());
38                  message = new MuleMessage(payload, message);
39              }
40              catch (TransformerException e)
41              {
42                  throw new RoutingException(
43                      CoreMessages.transformFailedBeforeFilter(),
44                      message, (UMOEndpoint)endpoints.get(0), e);
45              }
46          }
47          return message;
48      }
49  
50      public boolean isMatch(UMOMessage message) throws MessagingException
51      {
52          return true;
53      }
54  
55      public UMOTransformer getTransformer()
56      {
57          return transformer;
58      }
59  
60      public void setTransformer(UMOTransformer transformer)
61      {
62          this.transformer = transformer;
63      }
64  }