Coverage Report - org.mule.routing.outbound.ExceptionBasedRouter
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionBasedRouter
0%
0/57
0%
0/38
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.DefaultMuleMessage;
 10  
 import org.mule.api.ExceptionPayload;
 11  
 import org.mule.api.MuleEvent;
 12  
 import org.mule.api.MuleException;
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.endpoint.OutboundEndpoint;
 15  
 import org.mule.api.expression.RequiredValueException;
 16  
 import org.mule.api.routing.CouldNotRouteOutboundMessageException;
 17  
 import org.mule.api.routing.RoutePathNotFoundException;
 18  
 import org.mule.api.routing.RoutingException;
 19  
 import org.mule.config.i18n.CoreMessages;
 20  
 import org.mule.config.i18n.MessageFactory;
 21  
 import org.mule.routing.CorrelationMode;
 22  
 
 23  
 import java.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * <code>ExceptionBasedRouter</code> Will send the current event to the first
 29  
  * endpoint that doesn't throw an exception. If all attempted targets fail then an
 30  
  * exception is thrown. <p/> The router will override the sync/async mode of the
 31  
  * endpoint and force the sync mode for all targets except the last one.
 32  
  */
 33  0
 public class ExceptionBasedRouter extends ExpressionRecipientList
 34  
 {
 35  
     @Override
 36  
     public MuleEvent route(MuleEvent event) throws RoutingException
 37  
     {
 38  0
         MuleMessage message = event.getMessage();
 39  
 
 40  0
         List recipients = null;
 41  
         try
 42  
         {
 43  0
             recipients = getRecipients(event);
 44  
         }
 45  0
         catch (RequiredValueException e)
 46  
         {
 47  
             // ignore because the recipient list is optional for this router
 48  0
         }
 49  
 
 50  0
         if (recipients == null)
 51  
         {
 52  0
             int endpointsCount = routes.size();
 53  0
             recipients = new ArrayList(endpointsCount);
 54  0
             for (int i = 0; i < endpointsCount; i++)
 55  
             {
 56  0
                 recipients.add(getRoute(i, event));
 57  
             }
 58  
         }        
 59  
         
 60  0
         if (recipients.size() == 0)
 61  
         {
 62  0
             throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), event, null);
 63  
         }
 64  
 
 65  0
         if (enableCorrelation != CorrelationMode.NEVER)
 66  
         {
 67  0
             boolean correlationSet = message.getCorrelationId() != null;
 68  0
             if (correlationSet && (enableCorrelation == CorrelationMode.IF_NOT_SET))
 69  
             {
 70  0
                 logger.debug("CorrelationId is already set, not setting Correlation group size");
 71  
             }
 72  
             else
 73  
             {
 74  
                 // the correlationId will be set by the AbstractOutboundRouter
 75  0
                 message.setCorrelationGroupSize(recipients.size());
 76  
             }
 77  
         }
 78  
 
 79  0
         MuleEvent result = null;
 80  0
         OutboundEndpoint endpoint = null;
 81  0
         MuleMessage request = null;
 82  0
         boolean success = false;
 83  
 
 84  0
         for (Iterator iterator = recipients.iterator(); iterator.hasNext();)
 85  
         {
 86  0
             request = new DefaultMuleMessage(message.getPayload(), message, muleContext);
 87  
             try
 88  
             {
 89  0
                 endpoint = getRecipientEndpoint(request, iterator.next());
 90  0
                 boolean lastEndpoint = !iterator.hasNext();
 91  
     
 92  
                 // TODO MULE-4476
 93  0
                 if (!lastEndpoint && !endpoint.getExchangePattern().hasResponse())
 94  
                 {
 95  0
                     throw new CouldNotRouteOutboundMessageException(
 96  
                         MessageFactory.createStaticMessage("The ExceptionBasedRouter does not support asynchronous endpoints, make sure all endpoints on the router are configured as synchronous"), event, endpoint);
 97  
                 }
 98  
                 
 99  0
                 if (endpoint.getExchangePattern().hasResponse())
 100  
                 {
 101  0
                     MuleMessage resultMessage = null;
 102  0
                     result = sendRequest(event, request, endpoint, true);
 103  0
                     if (result != null)
 104  
                     {
 105  0
                         resultMessage = result.getMessage();
 106  
                     }
 107  0
                     if (resultMessage != null)
 108  
                     {
 109  0
                         resultMessage.applyTransformers(result, endpoint.getResponseTransformers());
 110  
                     }
 111  0
                     if (!exceptionPayloadAvailable(resultMessage))
 112  
                     {
 113  0
                         if (logger.isDebugEnabled())
 114  
                         {
 115  0
                             logger.debug("Successful invocation detected, stopping further processing.");
 116  
                         }
 117  0
                         success = true;
 118  0
                         break;
 119  
                     }
 120  0
                 }
 121  
                 else
 122  
                 {
 123  0
                     sendRequest(event, request, endpoint, false);
 124  0
                     success = true;
 125  0
                     break;
 126  
                 }
 127  
             }
 128  0
             catch (MuleException e)
 129  
             {
 130  0
                 logger.info("Failed to send/dispatch to endpoint: " + endpoint.getEndpointURI().toString()
 131  
                             + ". Error was: " + e.getMessage() + ". Trying next endpoint");
 132  0
             }
 133  
         }
 134  
 
 135  0
         if (!success)
 136  
         {
 137  0
             throw new CouldNotRouteOutboundMessageException(event, endpoint);
 138  
         }
 139  
 
 140  0
         return result;
 141  
     }
 142  
 
 143  
     /**
 144  
      * @param message message to check
 145  
      * @return true if there was an exception payload set
 146  
      */
 147  
     protected boolean exceptionPayloadAvailable(MuleMessage message)
 148  
     {
 149  0
         if (message == null)
 150  
         {
 151  0
             return false;
 152  
         }
 153  
 
 154  0
         final ExceptionPayload exceptionPayload = message.getExceptionPayload();
 155  0
         if (exceptionPayload != null)
 156  
         {
 157  0
             logger.info("Failure returned, will try next endpoint. Exception payload is: " + exceptionPayload);
 158  0
             return true;
 159  
         }
 160  
         else
 161  
         {
 162  0
             return false;
 163  
         }
 164  
     }
 165  
     
 166  
 }