1
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
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 }