View Javadoc

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  public class MessageChunkingRouter extends FilteringOutboundRouter
30  {
31      private int chunkSize = 0;
32      private int numberOfMessages = 1;
33  
34      public int getChunkSize()
35      {
36          return chunkSize;
37      }
38  
39      public void setChunkSize(int chunkSize)
40      {
41          this.chunkSize = chunkSize;
42      }
43  
44      public int getNumberOfMessages()
45      {
46          return numberOfMessages;
47      }
48  
49      public void setNumberOfMessages(int numberOfMessages)
50      {
51          this.numberOfMessages = numberOfMessages;
52      }
53  
54      public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
55          throws RoutingException
56      {
57          if (chunkSize == 0 && numberOfMessages < 2)
58          {
59              return super.route(message, session, synchronous);
60          }
61          else if (chunkSize > 0)
62          {
63              byte[] data = new byte[0];
64              try
65              {
66                  data = message.getPayloadAsBytes();
67              }
68              catch (Exception e)
69              {
70                  throw new RoutingException(
71                      CoreMessages.failedToReadPayload(), 
72                      message, getEndpoint(0, message), e);
73              }
74  
75              int parts = data.length / chunkSize;
76              if ((parts * chunkSize) < data.length)
77              {
78                  parts++;
79              }
80              int len = chunkSize;
81              UMOMessage part = null;
82              int count = 0;
83              int pos = 0;
84              byte[] buffer = null;
85              try
86              {
87                  for (; count < parts; count++)
88                  {
89                      if ((pos + len) > data.length)
90                      {
91                          len = data.length - pos;
92                      }
93                      buffer = new byte[len];
94                      System.arraycopy(data, pos, buffer, 0, buffer.length);
95                      pos += len;
96                      part = new MuleMessage(buffer, message);
97                      part.setCorrelationId(message.getUniqueId());
98                      part.setCorrelationGroupSize(parts);
99                      part.setCorrelationSequence(count);
100                     super.route(part, session, synchronous);
101                 }
102 
103             }
104             catch (RoutingException e)
105             {
106                 // we'll want to send the whole message to the Exception handler
107                 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                 throw e;
113             }
114         }
115         return message;
116     }
117 }