Coverage Report - org.mule.routing.outbound.MessageChunkingRouter
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageChunkingRouter
0%
0/41
0%
0/5
3
 
 1  
 /*
 2  
  * $Id: MessageChunkingRouter.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.MuleMessage;
 15  
 import org.mule.umo.UMOMessage;
 16  
 import org.mule.umo.UMOSession;
 17  
 import org.mule.umo.routing.RoutingException;
 18  
 
 19  
 /**
 20  
  * A router that breaks up the current message onto smaller parts and sends them to
 21  
  * the same destination. The Destination component needs to have a
 22  
  * MessageChunkingAggregator inbound router in order to rebuild the message at the
 23  
  * other end.
 24  
  * 
 25  
  * @see org.mule.routing.inbound.MessageChunkingAggregator
 26  
  * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
 27  
  * @version $Revision: 7976 $
 28  
  */
 29  0
 public class MessageChunkingRouter extends FilteringOutboundRouter
 30  
 {
 31  0
     private int chunkSize = 0;
 32  0
     private int numberOfMessages = 1;
 33  
 
 34  
     public int getChunkSize()
 35  
     {
 36  0
         return chunkSize;
 37  
     }
 38  
 
 39  
     public void setChunkSize(int chunkSize)
 40  
     {
 41  0
         this.chunkSize = chunkSize;
 42  0
     }
 43  
 
 44  
     public int getNumberOfMessages()
 45  
     {
 46  0
         return numberOfMessages;
 47  
     }
 48  
 
 49  
     public void setNumberOfMessages(int numberOfMessages)
 50  
     {
 51  0
         this.numberOfMessages = numberOfMessages;
 52  0
     }
 53  
 
 54  
     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
 55  
         throws RoutingException
 56  
     {
 57  0
         if (chunkSize == 0 && numberOfMessages < 2)
 58  
         {
 59  0
             return super.route(message, session, synchronous);
 60  
         }
 61  0
         else if (chunkSize > 0)
 62  
         {
 63  0
             byte[] data = new byte[0];
 64  
             try
 65  
             {
 66  0
                 data = message.getPayloadAsBytes();
 67  
             }
 68  0
             catch (Exception e)
 69  
             {
 70  0
                 throw new RoutingException(
 71  
                     CoreMessages.failedToReadPayload(), 
 72  
                     message, getEndpoint(0, message), e);
 73  0
             }
 74  
 
 75  0
             int parts = data.length / chunkSize;
 76  0
             if ((parts * chunkSize) < data.length)
 77  
             {
 78  0
                 parts++;
 79  
             }
 80  0
             int len = chunkSize;
 81  0
             UMOMessage part = null;
 82  0
             int count = 0;
 83  0
             int pos = 0;
 84  0
             byte[] buffer = null;
 85  
             try
 86  
             {
 87  0
                 for (; count < parts; count++)
 88  
                 {
 89  0
                     if ((pos + len) > data.length)
 90  
                     {
 91  0
                         len = data.length - pos;
 92  
                     }
 93  0
                     buffer = new byte[len];
 94  0
                     System.arraycopy(data, pos, buffer, 0, buffer.length);
 95  0
                     pos += len;
 96  0
                     part = new MuleMessage(buffer, message);
 97  0
                     part.setCorrelationId(message.getUniqueId());
 98  0
                     part.setCorrelationGroupSize(parts);
 99  0
                     part.setCorrelationSequence(count);
 100  0
                     super.route(part, session, synchronous);
 101  
                 }
 102  
 
 103  
             }
 104  0
             catch (RoutingException e)
 105  
             {
 106  
                 // we'll want to send the whole message to the Exception handler
 107  0
                 e = new RoutingException(e.getI18nMessage(), e.getUmoMessage(), e.getEndpoint(), e.getCause());
 108  
                 // e.addInfo("chunking", "true");
 109  
                 // buffer = new byte[data.length - len];
 110  
                 // System.arraycopy(data, len, buffer, 0, buffer.length);
 111  
                 // e.addInfo("remaining data", buffer);
 112  0
                 throw e;
 113  0
             }
 114  
         }
 115  0
         return message;
 116  
     }
 117  
 }