1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.transport;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.ByteArrayOutputStream;
27  import java.io.InputStream;
28  
29  import java.net.DatagramPacket;
30  
31  import java.util.zip.GZIPInputStream;
32  
33  /**
34   * <a href="MulticastDatagramHandler.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Michael C. Han
37   * @author Raymond Aug�
38   *
39   */
40  public class MulticastDatagramHandler implements DatagramHandler {
41  
42      public MulticastDatagramHandler(boolean gzipData, boolean shortData) {
43          _gzipData = gzipData;
44          _shortData = shortData;
45      }
46  
47      public void errorReceived(Throwable t) {
48          _log.error(t, t);
49      }
50  
51      public void process(DatagramPacket packet) {
52          byte[] bytes = packet.getData();
53  
54          if (_gzipData) {
55              try {
56                  bytes = getUnzippedBytes(bytes);
57              }
58              catch (Exception e) {
59                  _log.error(e, e);
60              }
61          }
62  
63          if (_shortData) {
64              byte[] temp = new byte[96];
65  
66              System.arraycopy(bytes, 0, temp, 0, 96);
67  
68              bytes = temp;
69          }
70  
71          StringBuilder sb = new StringBuilder();
72  
73          sb.append("[");
74          sb.append(packet.getSocketAddress());
75          sb.append("] ");
76          sb.append(new String(bytes));
77  
78          if (_log.isInfoEnabled()) {
79              _log.info(sb);
80          }
81      }
82  
83      protected byte[] getUnzippedBytes(byte[] bytes) throws Exception {
84          InputStream is = new GZIPInputStream(new ByteArrayInputStream(bytes));
85          ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
86  
87          byte[] buffer = new byte[1500];
88  
89          int c = 0;
90  
91          while (true) {
92              if (c == -1) {
93                  break;
94              }
95  
96              c = is.read(buffer, 0, 1500);
97  
98              if (c != -1) {
99                  baos.write(buffer, 0, c);
100             }
101         }
102 
103         is.close();
104 
105         baos.flush();
106         baos.close();
107 
108         return baos.toByteArray();
109     }
110 
111     private static final Log _log = LogFactoryUtil.getLog(
112         MulticastDatagramHandler.class);
113 
114     private boolean _gzipData;
115     private boolean _shortData;
116 
117 }