1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.transport;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.io.ByteArrayInputStream;
29  import java.io.ByteArrayOutputStream;
30  import java.io.InputStream;
31  
32  import java.net.DatagramPacket;
33  
34  import java.util.zip.GZIPInputStream;
35  
36  /**
37   * <a href="MulticastDatagramHandler.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Michael C. Han
40   * @author Raymond Aug�
41   *
42   */
43  public class MulticastDatagramHandler implements DatagramHandler {
44  
45      public MulticastDatagramHandler(boolean gzipData, boolean shortData) {
46          _gzipData = gzipData;
47          _shortData = shortData;
48      }
49  
50      public void errorReceived(Throwable t) {
51          _log.error(t, t);
52      }
53  
54      public void process(DatagramPacket packet) {
55          byte[] bytes = packet.getData();
56  
57          if (_gzipData) {
58              try {
59                  bytes = getUnzippedBytes(bytes);
60              }
61              catch (Exception e) {
62                  _log.error(e, e);
63              }
64          }
65  
66          if (_shortData) {
67              byte[] temp = new byte[96];
68  
69              System.arraycopy(bytes, 0, temp, 0, 96);
70  
71              bytes = temp;
72          }
73  
74          StringBuilder sb = new StringBuilder();
75  
76          sb.append("[");
77          sb.append(packet.getSocketAddress());
78          sb.append("] ");
79          sb.append(new String(bytes));
80  
81          if (_log.isInfoEnabled()) {
82              _log.info(sb);
83          }
84      }
85  
86      protected byte[] getUnzippedBytes(byte[] bytes) throws Exception {
87          InputStream is = new GZIPInputStream(new ByteArrayInputStream(bytes));
88          ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
89  
90          byte[] buffer = new byte[1500];
91  
92          int c = 0;
93  
94          while (true) {
95              if (c == -1) {
96                  break;
97              }
98  
99              c = is.read(buffer, 0, 1500);
100 
101             if (c != -1) {
102                 baos.write(buffer, 0, c);
103             }
104         }
105 
106         is.close();
107 
108         baos.flush();
109         baos.close();
110 
111         return baos.toByteArray();
112     }
113 
114     private static final Log _log = LogFactoryUtil.getLog(
115         MulticastDatagramHandler.class);
116 
117     private boolean _gzipData;
118     private boolean _shortData;
119 
120 }