Coverage Report - org.mule.routing.outbound.FilteringOutboundRouter
 
Classes in this File Line Coverage Branch Coverage Complexity
FilteringOutboundRouter
0%
0/74
0%
0/32
0
 
 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.routing.outbound;
 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.EndpointException;
 13  
 import org.mule.api.endpoint.EndpointURI;
 14  
 import org.mule.api.endpoint.ImmutableEndpoint;
 15  
 import org.mule.api.endpoint.OutboundEndpoint;
 16  
 import org.mule.api.expression.ExpressionManager;
 17  
 import org.mule.api.lifecycle.InitialisationException;
 18  
 import org.mule.api.processor.MessageProcessor;
 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.api.routing.TransformingMatchable;
 23  
 import org.mule.api.routing.filter.Filter;
 24  
 import org.mule.api.transformer.Transformer;
 25  
 import org.mule.config.i18n.CoreMessages;
 26  
 import org.mule.endpoint.DynamicURIOutboundEndpoint;
 27  
 import org.mule.endpoint.MuleEndpointURI;
 28  
 import org.mule.util.TemplateParser;
 29  
 
 30  
 import java.util.HashMap;
 31  
 import java.util.LinkedList;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 
 35  
 /**
 36  
  * <code>FilteringRouter</code> is a router that accepts events based on a filter
 37  
  * set.
 38  
  */
 39  
 
 40  0
 public class FilteringOutboundRouter extends AbstractOutboundRouter implements TransformingMatchable
 41  
 {
 42  
     protected ExpressionManager expressionManager;
 43  
 
 44  0
     private List<Transformer> transformers = new LinkedList<Transformer>();
 45  
 
 46  
     private Filter filter;
 47  
 
 48  0
     private boolean useTemplates = true;
 49  
     
 50  
     // We used Square templates as they can exist as part of an URI.
 51  0
     private TemplateParser parser = TemplateParser.createSquareBracesStyleParser();
 52  
 
 53  
     @Override
 54  
     public void initialise() throws InitialisationException
 55  
     {
 56  0
         super.initialise();
 57  0
         expressionManager = muleContext.getExpressionManager();
 58  0
     }
 59  
 
 60  
     @Override
 61  
     public MuleEvent route(MuleEvent event) throws RoutingException
 62  
     {
 63  
         MuleEvent result;
 64  
 
 65  0
         MuleMessage message = event.getMessage();
 66  
 
 67  0
         if (routes == null || routes.size() == 0)
 68  
         {
 69  0
             throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), event, null);
 70  
         }
 71  
 
 72  0
         MessageProcessor ep = getRoute(0, event);
 73  
 
 74  
         try
 75  
         {
 76  0
             result = sendRequest(event, message, ep, true);
 77  
         }
 78  0
         catch (RoutingException e)
 79  
         {
 80  0
             throw e;
 81  
         }
 82  0
         catch (MuleException e)
 83  
         {
 84  0
             throw new CouldNotRouteOutboundMessageException(event, ep, e);
 85  0
         }
 86  0
         return result;
 87  
     }
 88  
 
 89  
     public Filter getFilter()
 90  
     {
 91  0
         return filter;
 92  
     }
 93  
 
 94  
     public void setFilter(Filter filter)
 95  
     {
 96  0
         this.filter = filter;
 97  0
     }
 98  
 
 99  
     public boolean isMatch(MuleMessage message) throws MuleException
 100  
     {
 101  0
         if (getFilter() == null)
 102  
         {
 103  0
             return true;
 104  
         }
 105  
         
 106  0
         message.applyTransformers(null, transformers);
 107  
         
 108  0
         return getFilter().accept(message);
 109  
     }
 110  
 
 111  
     public List<Transformer> getTransformers()
 112  
     {
 113  0
         return transformers;
 114  
     }
 115  
 
 116  
     public void setTransformers(List<Transformer> transformers)
 117  
     {
 118  0
         this.transformers = transformers;
 119  0
     }
 120  
 
 121  
     @Override
 122  
     public synchronized void addRoute(MessageProcessor target) throws MuleException
 123  
     {
 124  0
         if (!useTemplates)
 125  
         {
 126  0
             if (target instanceof ImmutableEndpoint)
 127  
             {
 128  0
                 ImmutableEndpoint endpoint = (ImmutableEndpoint) target;
 129  0
                 if (parser.isContainsTemplate(endpoint.getEndpointURI().toString()))
 130  
                 {
 131  0
                     useTemplates = true;
 132  
                 }
 133  
             }
 134  
         }
 135  0
         super.addRoute(target);
 136  0
     }
 137  
 
 138  
     /**
 139  
      * Will Return the target at the given index and will resolve any template tags
 140  
      * on the Endpoint URI if necessary
 141  
      * 
 142  
      * @param index the index of the endpoint to get
 143  
      * @param event the current event. This is required if template matching is
 144  
      *            being used
 145  
      * @return the endpoint at the index, with any template tags resolved
 146  
      * @throws CouldNotRouteOutboundMessageException if the template causs the
 147  
      *             endpoint to become illegal or malformed
 148  
      */
 149  
     public MessageProcessor getRoute(int index, MuleEvent event) throws CouldNotRouteOutboundMessageException
 150  
     {
 151  0
         if (!useTemplates)
 152  
         {
 153  0
             return routes.get(index);
 154  
         }
 155  
         else
 156  
         {
 157  0
             MuleMessage message = event.getMessage();
 158  0
             MessageProcessor mp = routes.get(index);
 159  0
             if (!(mp instanceof ImmutableEndpoint))
 160  
             {
 161  0
                 return routes.get(index);
 162  
             }
 163  0
             OutboundEndpoint ep = (OutboundEndpoint) mp;
 164  0
             String uri = ep.getAddress();
 165  
 
 166  0
             if (logger.isDebugEnabled())
 167  
             {
 168  0
                 logger.debug("Uri before parsing is: " + uri);
 169  
             }
 170  
 
 171  0
             Map<String, Object> props = new HashMap<String, Object>();
 172  
             // Also add the endpoint properties so that users can set fallback values
 173  
             // when the property is not set on the event
 174  0
             props.putAll(ep.getProperties());
 175  0
             for (String propertyKey : message.getOutboundPropertyNames())
 176  
             {
 177  0
                 Object value = message.getOutboundProperty(propertyKey);
 178  0
                 props.put(propertyKey, value);
 179  0
             }
 180  
 
 181  0
             propagateMagicProperties(message, message);
 182  
 
 183  0
             if (!parser.isContainsTemplate(uri))
 184  
             {
 185  0
                 logger.debug("Uri does not contain template(s)");
 186  0
                 return ep;
 187  
             }
 188  
             else
 189  
             {
 190  
 
 191  0
                 String newUriString = parser.parse(props, uri);
 192  0
                 if (parser.isContainsTemplate(newUriString))
 193  
                 {
 194  0
                     newUriString = this.getMuleContext().getExpressionManager().parse(newUriString, message, true);
 195  
                 }
 196  0
                 if (logger.isDebugEnabled())
 197  
                 {
 198  0
                     logger.debug("Uri after parsing is: " + uri);
 199  
                 }
 200  
                 try
 201  
                 {
 202  0
                     EndpointURI newUri = new MuleEndpointURI(newUriString, muleContext);
 203  0
                     EndpointURI endpointURI = ep.getEndpointURI();
 204  0
                     if (endpointURI != null && !newUri.getScheme().equalsIgnoreCase(endpointURI.getScheme()))
 205  
                     {
 206  0
                         throw new CouldNotRouteOutboundMessageException(
 207  
                             CoreMessages.schemeCannotChangeForRouter(ep.getEndpointURI().getScheme(),
 208  
                                 newUri.getScheme()), event, ep);
 209  
                     }
 210  0
                     newUri.initialise();
 211  
 
 212  0
                     return new DynamicURIOutboundEndpoint(ep, newUri);
 213  
                 }
 214  0
                 catch (EndpointException e)
 215  
                 {
 216  0
                     throw new CouldNotRouteOutboundMessageException(
 217  
                         CoreMessages.templateCausedMalformedEndpoint(uri, newUriString), event, ep, e);
 218  
                 }
 219  0
                 catch (InitialisationException e)
 220  
                 {
 221  0
                     throw new CouldNotRouteOutboundMessageException(
 222  
                         CoreMessages.templateCausedMalformedEndpoint(uri, newUriString), event, ep, e);
 223  
                 }
 224  
             }
 225  
         }
 226  
     }
 227  
 
 228  
     public boolean isUseTemplates()
 229  
     {
 230  0
         return useTemplates;
 231  
     }
 232  
 
 233  
     public void setUseTemplates(boolean useTemplates)
 234  
     {
 235  0
         this.useTemplates = useTemplates;
 236  0
     }
 237  
     
 238  
     public boolean isTransformBeforeMatch()
 239  
     {
 240  0
         return !transformers.isEmpty();
 241  
     }
 242  
 
 243  
 }