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