View Javadoc

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