View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.udp.functional;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.endpoint.EndpointURI;
13  import org.mule.api.endpoint.OutboundEndpoint;
14  import org.mule.api.processor.MessageProcessor;
15  import org.mule.api.routing.CouldNotRouteOutboundMessageException;
16  import org.mule.api.routing.RoutePathNotFoundException;
17  import org.mule.api.routing.RoutingException;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.endpoint.DynamicURIOutboundEndpoint;
20  import org.mule.endpoint.MuleEndpointURI;
21  import org.mule.routing.outbound.FilteringOutboundRouter;
22  
23  public class DynamicEndpointRouter extends FilteringOutboundRouter
24  {
25      @Override
26      public MuleEvent route(MuleEvent event) throws RoutingException
27      {
28          MuleMessage message = event.getMessage();
29          MuleEvent result;
30  
31          if (routes == null || routes.size() == 0)
32          {
33              throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), event, null);
34          }
35  
36          try
37          {
38              MessageProcessor ep = routes.get(0);
39              EndpointURI newUri;
40  
41              if (ep instanceof OutboundEndpoint)
42              {
43                  for (String propertyKey : message.getOutboundPropertyNames())
44                  {
45                      Object propertyValue = message.getOutboundProperty(propertyKey);
46                      if (propertyKey.equalsIgnoreCase("packet.port"))
47                      {
48                          int port = (Integer) propertyValue;
49                          newUri = new MuleEndpointURI("udp://localhost:" + port, muleContext);
50                          if (logger.isDebugEnabled())
51                          {
52                              logger.info("Uri after parsing is: " + newUri.getAddress());
53                          }
54                          ep = new DynamicURIOutboundEndpoint((OutboundEndpoint) ep, newUri);
55                          break;
56                      }
57                  }
58              }
59              result = sendRequest(event, message, ep, true);
60          }
61          catch (MuleException e)
62          {
63              throw new CouldNotRouteOutboundMessageException(event, routes.get(0), e);
64          }
65  
66          return result;
67      }
68  }