View Javadoc

1   /*
2    * $Id: TemplateEndpointRouter.java 9617 2007-11-06 12:47:33Z holger $
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.endpoint.MuleEndpointURI;
15  import org.mule.umo.UMOException;
16  import org.mule.umo.UMOMessage;
17  import org.mule.umo.UMOSession;
18  import org.mule.umo.endpoint.UMOEndpoint;
19  import org.mule.umo.endpoint.UMOEndpointURI;
20  import org.mule.umo.routing.CouldNotRouteOutboundMessageException;
21  import org.mule.umo.routing.RoutePathNotFoundException;
22  import org.mule.umo.routing.RoutingException;
23  import org.mule.util.TemplateParser;
24  
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.Map;
28  
29  /**
30   * The TemplateEndpointRouter allows endpoints to be altered at runtime based on
31   * properties set on the current event or fallback values set on the endpoint properties.
32   * Templated values are expressed using square braces around a property name, i.e.
33   * axis:http://localhost:8082/MyService?method=[SOAP_METHOD]. Note that Ant style property
34   * templates cannot be used in valid URI strings, so we must use square braces instead.
35   */
36  public class TemplateEndpointRouter extends FilteringOutboundRouter
37  {
38  
39      // We used square templates as they can exist as part of an URI.
40      private TemplateParser parser = TemplateParser.createSquareBracesStyleParser();
41  
42      public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
43          throws RoutingException
44      {
45          UMOMessage result = null;
46  
47          if (endpoints == null || endpoints.size() == 0)
48          {
49              throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), message, null);
50          }
51  
52          try
53          {
54              UMOEndpoint ep = (UMOEndpoint) endpoints.get(0);
55              String uri = ep.getEndpointURI().toString();
56  
57              if (logger.isDebugEnabled())
58              {
59                  logger.debug("Uri before parsing is: " + uri);
60              }
61  
62              // Also add the endpoint properties so that users can set fallback values
63              // when the property is not set on the event
64              Map props = new HashMap();
65              props.putAll(ep.getProperties());
66  
67              for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
68              {
69                  String propertyKey = (String) iterator.next();
70                  props.put(propertyKey, message.getProperty(propertyKey));
71              }
72  
73              uri = parser.parse(props, uri);
74  
75              if (logger.isDebugEnabled())
76              {
77                  logger.debug("Uri after parsing is: " + uri);
78              }
79  
80              UMOEndpointURI newUri = new MuleEndpointURI(uri);
81  
82              if (!newUri.getScheme().equalsIgnoreCase(ep.getEndpointURI().getScheme()))
83              {
84                  throw new CouldNotRouteOutboundMessageException(CoreMessages.schemeCannotChangeForRouter(
85                      ep.getEndpointURI().getScheme(), newUri.getScheme()), message, ep);
86              }
87  
88              // dispatch to a clone of the endpoint, don't modify the original
89              UMOEndpoint resolvedEndpoint = (UMOEndpoint)ep.clone();
90              resolvedEndpoint.setEndpointURI(new MuleEndpointURI(uri));
91  
92              if (synchronous)
93              {
94                  result = send(session, message, resolvedEndpoint);
95              }
96              else
97              {
98                  dispatch(session, message, resolvedEndpoint);
99              }
100         }
101         catch (UMOException e)
102         {
103             throw new CouldNotRouteOutboundMessageException(message, (UMOEndpoint) endpoints.get(0), e);
104         }
105 
106         return result;
107     }
108 
109 }