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