Coverage Report - org.mule.routing.outbound.MessageChunkingRouter
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageChunkingRouter
0%
0/45
0%
0/16
3.4
 
 1  
 /*
 2  
  * $Id: MessageChunkingRouter.java 10529 2008-01-25 05:58:36Z 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.DefaultMuleMessage;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.MuleSession;
 16  
 import org.mule.api.routing.RoutingException;
 17  
 import org.mule.config.i18n.CoreMessages;
 18  
 
 19  
 /**
 20  
  * A router that breaks up the current message onto smaller parts and sends them to the
 21  
  * same destination. The Destination service needs to have a MessageChunkingAggregator
 22  
  * inbound router in order to rebuild the message at the other end.
 23  
  * 
 24  
  * @see org.mule.routing.inbound.MessageChunkingAggregator
 25  
  */
 26  0
 public class MessageChunkingRouter extends FilteringOutboundRouter
 27  
 {
 28  0
     private int messageSize = 0;
 29  0
     private int numberOfMessages = 1;
 30  
 
 31  
     public int getMessageSize()
 32  
     {
 33  0
         return messageSize;
 34  
     }
 35  
 
 36  
     public void setMessageSize(int messageSize)
 37  
     {
 38  0
         this.messageSize = messageSize;
 39  0
     }
 40  
 
 41  
     public int getNumberOfMessages()
 42  
     {
 43  0
         return numberOfMessages;
 44  
     }
 45  
 
 46  
     public void setNumberOfMessages(int numberOfMessages)
 47  
     {
 48  0
         this.numberOfMessages = numberOfMessages;
 49  0
     }
 50  
 
 51  
     public MuleMessage route(MuleMessage message, MuleSession session, boolean synchronous)
 52  
         throws RoutingException
 53  
     {
 54  0
         if (messageSize == 0 && numberOfMessages < 2)
 55  
         {
 56  0
             return super.route(message, session, synchronous);
 57  
         }
 58  0
         else if (messageSize > 0)
 59  
         {
 60  0
             byte[] data = new byte[0];
 61  
             try
 62  
             {
 63  0
                 data = message.getPayloadAsBytes();
 64  
             }
 65  0
             catch (Exception e)
 66  
             {
 67  0
                 throw new RoutingException(CoreMessages.failedToReadPayload(), message, getEndpoint(0,
 68  
                     message), e);
 69  0
             }
 70  
 
 71  0
             int parts = data.length / messageSize;
 72  0
             if ((parts * messageSize) < data.length)
 73  
             {
 74  0
                 parts++;
 75  
             }
 76  0
             int len = messageSize;
 77  0
             MuleMessage part = null;
 78  0
             int count = 0;
 79  0
             int pos = 0;
 80  0
             byte[] buffer = null;
 81  
             try
 82  
             {
 83  0
                 for (; count < parts; count++)
 84  
                 {
 85  0
                     if ((pos + len) > data.length)
 86  
                     {
 87  0
                         len = data.length - pos;
 88  
                     }
 89  0
                     buffer = new byte[len];
 90  0
                     System.arraycopy(data, pos, buffer, 0, buffer.length);
 91  0
                     pos += len;
 92  0
                     part = new DefaultMuleMessage(buffer, message);
 93  0
                     part.setCorrelationId(message.getUniqueId());
 94  0
                     part.setCorrelationGroupSize(parts);
 95  0
                     part.setCorrelationSequence(count);
 96  
                     // TODO - remove or downgrade once MULE-1718 is fixed,
 97  
                     // for now these really help see the problem if you set the level for this class to INFO
 98  0
                     if (logger.isInfoEnabled())
 99  
                     {
 100  0
                         logger.info("sending part " + count + " of " + parts);
 101  
                     }
 102  0
                     super.route(part, session, synchronous);
 103  0
                     if (logger.isInfoEnabled())
 104  
                     {
 105  0
                         logger.info("sent");
 106  
                     }
 107  
                 }
 108  
 
 109  
             }
 110  0
             catch (RoutingException e)
 111  
             {
 112  
                 // we'll want to send the whole message to the Exception handler
 113  0
                 e = new RoutingException(e.getI18nMessage(), e.getUmoMessage(), e.getEndpoint(), e.getCause());
 114  
                 // e.addInfo("chunking", "true");
 115  
                 // buffer = new byte[data.length - len];
 116  
                 // System.arraycopy(data, len, buffer, 0, buffer.length);
 117  
                 // e.addInfo("remaining data", buffer);
 118  0
                 throw e;
 119  0
             }
 120  
         }
 121  0
         return message;
 122  
     }
 123  
 }