Coverage Report - org.mule.routing.outbound.TemplateEndpointRouter
 
Classes in this File Line Coverage Branch Coverage Complexity
TemplateEndpointRouter
0%
0/30
0%
0/14
11
 
 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  0
 public class TemplateEndpointRouter extends FilteringOutboundRouter
 37  
 {
 38  
 
 39  
     // We used square templates as they can exist as part of an URI.
 40  0
     private TemplateParser parser = TemplateParser.createSquareBracesStyleParser();
 41  
 
 42  
     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
 43  
         throws RoutingException
 44  
     {
 45  0
         UMOMessage result = null;
 46  
 
 47  0
         if (endpoints == null || endpoints.size() == 0)
 48  
         {
 49  0
             throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), message, null);
 50  
         }
 51  
 
 52  
         try
 53  
         {
 54  0
             UMOEndpoint ep = (UMOEndpoint) endpoints.get(0);
 55  0
             String uri = ep.getEndpointURI().toString();
 56  
 
 57  0
             if (logger.isDebugEnabled())
 58  
             {
 59  0
                 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  0
             Map props = new HashMap();
 65  0
             props.putAll(ep.getProperties());
 66  
 
 67  0
             for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
 68  
             {
 69  0
                 String propertyKey = (String) iterator.next();
 70  0
                 props.put(propertyKey, message.getProperty(propertyKey));
 71  0
             }
 72  
 
 73  0
             uri = parser.parse(props, uri);
 74  
 
 75  0
             if (logger.isDebugEnabled())
 76  
             {
 77  0
                 logger.debug("Uri after parsing is: " + uri);
 78  
             }
 79  
 
 80  0
             UMOEndpointURI newUri = new MuleEndpointURI(uri);
 81  
 
 82  0
             if (!newUri.getScheme().equalsIgnoreCase(ep.getEndpointURI().getScheme()))
 83  
             {
 84  0
                 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  0
             UMOEndpoint resolvedEndpoint = (UMOEndpoint)ep.clone();
 90  0
             resolvedEndpoint.setEndpointURI(new MuleEndpointURI(uri));
 91  
 
 92  0
             if (synchronous)
 93  
             {
 94  0
                 result = send(session, message, resolvedEndpoint);
 95  
             }
 96  
             else
 97  
             {
 98  0
                 dispatch(session, message, resolvedEndpoint);
 99  
             }
 100  
         }
 101  0
         catch (UMOException e)
 102  
         {
 103  0
             throw new CouldNotRouteOutboundMessageException(message, (UMOEndpoint) endpoints.get(0), e);
 104  0
         }
 105  
 
 106  0
         return result;
 107  
     }
 108  
 
 109  
 }