1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.transport;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
19  import com.liferay.portal.kernel.util.StringBundler;
20  
21  import java.io.InputStream;
22  
23  import java.net.DatagramPacket;
24  
25  import java.util.zip.GZIPInputStream;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * <a href="MulticastDatagramHandler.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Michael C. Han
34   * @author Raymond Augé
35   */
36  public class MulticastDatagramHandler implements DatagramHandler {
37  
38      public MulticastDatagramHandler(boolean gzipData, boolean shortData) {
39          _gzipData = gzipData;
40          _shortData = shortData;
41      }
42  
43      public void errorReceived(Throwable t) {
44          _log.error(t, t);
45      }
46  
47      public void process(DatagramPacket packet) {
48          byte[] bytes = packet.getData();
49  
50          if (_gzipData) {
51              try {
52                  bytes = getUnzippedBytes(bytes);
53              }
54              catch (Exception e) {
55                  _log.error(e, e);
56              }
57          }
58  
59          if (_shortData) {
60              byte[] temp = new byte[96];
61  
62              System.arraycopy(bytes, 0, temp, 0, 96);
63  
64              bytes = temp;
65          }
66  
67          StringBundler sb = new StringBundler(4);
68  
69          sb.append("[");
70          sb.append(packet.getSocketAddress());
71          sb.append("] ");
72          sb.append(new String(bytes));
73  
74          if (_log.isInfoEnabled()) {
75              _log.info(sb);
76          }
77      }
78  
79      protected byte[] getUnzippedBytes(byte[] bytes) throws Exception {
80          InputStream is = new GZIPInputStream(
81              new UnsyncByteArrayInputStream(bytes));
82          UnsyncByteArrayOutputStream ubaos =
83              new UnsyncByteArrayOutputStream(bytes.length);
84  
85          byte[] buffer = new byte[1500];
86  
87          int c = 0;
88  
89          while (true) {
90              if (c == -1) {
91                  break;
92              }
93  
94              c = is.read(buffer, 0, 1500);
95  
96              if (c != -1) {
97                  ubaos.write(buffer, 0, c);
98              }
99          }
100 
101         is.close();
102 
103         ubaos.flush();
104         ubaos.close();
105 
106         return ubaos.toByteArray();
107     }
108 
109     private static Log _log = LogFactory.getLog(MulticastDatagramHandler.class);
110 
111     private boolean _gzipData;
112     private boolean _shortData;
113 
114 }