View Javadoc

1   /*
2    * $Id: TemplateEndpointRouter.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.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 template endpoint router allows endpoints to be alered at runtime based on
31   * properties set on the current event of fallback values set on the endpoint
32   * properties. Templated values are expressed using square braces around a property
33   * name i.e. axis:http://localhost:8082/MyService?method=[SOAP_METHOD] Note that Ant
34   * style property templates cannot be used in valid URI strings, so we must use
35   * Square braces instead
36   * 
37   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
38   * @version $Revision: 7976 $
39   */
40  public class TemplateEndpointRouter extends FilteringOutboundRouter
41  {
42  
43      // We used Square templates as they can exist as part of an uri.
44      private TemplateParser parser = TemplateParser.createSquareBracesStyleParser();
45  
46      public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
47          throws RoutingException
48      {
49          UMOMessage result = null;
50          if (endpoints == null || endpoints.size() == 0)
51          {
52              throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), message, null);
53          }
54          try
55          {
56              UMOEndpoint ep = (UMOEndpoint) endpoints.get(0);
57              String uri = ep.getEndpointURI().toString();
58              if (logger.isDebugEnabled())
59              {
60                  logger.debug("Uri before parsing is: " + uri);
61              }
62              Map props = new HashMap();
63              // Also add the endpoint propertie so that users can set fallback values
64              // when the property is not set on the event
65              props.putAll(ep.getProperties());
66              for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
67              {
68                  String propertyKey = (String) iterator.next();
69                  props.put(propertyKey, message.getProperty(propertyKey));
70              }
71              uri = parser.parse(props, uri);
72              if (logger.isDebugEnabled())
73              {
74                  logger.debug("Uri after parsing is: " + uri);
75              }
76              UMOEndpointURI newUri = new MuleEndpointURI(uri);
77              if (!newUri.getScheme().equalsIgnoreCase(ep.getEndpointURI().getScheme()))
78              {
79                  throw new CouldNotRouteOutboundMessageException(
80                      CoreMessages.schemeCannotChangeForRouter(ep.getEndpointURI().getScheme(),
81                      newUri.getScheme()), message, ep);
82              }
83              ep.setEndpointURI(new MuleEndpointURI(uri));
84              if (synchronous)
85              {
86                  result = send(session, message, ep);
87              }
88              else
89              {
90                  dispatch(session, message, ep);
91              }
92          }
93          catch (UMOException e)
94          {
95              throw new CouldNotRouteOutboundMessageException(message, (UMOEndpoint) endpoints.get(0), e);
96          }
97          return result;
98      }
99  
100 }