View Javadoc

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