| MulticastDatagramHandler.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 *
5 *
6 *
7 * The contents of this file are subject to the terms of the Liferay Enterprise
8 * Subscription License ("License"). You may not use this file except in
9 * compliance with the License. You can obtain a copy of the License by
10 * contacting Liferay, Inc. See the License for the specific language governing
11 * permissions and limitations under the License, including but not limited to
12 * distribution rights 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 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 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
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 public class MulticastDatagramHandler implements DatagramHandler {
43
44 public MulticastDatagramHandler(boolean gzipData, boolean shortData) {
45 _gzipData = gzipData;
46 _shortData = shortData;
47 }
48
49 public void errorReceived(Throwable t) {
50 _log.error(t, t);
51 }
52
53 public void process(DatagramPacket packet) {
54 byte[] bytes = packet.getData();
55
56 if (_gzipData) {
57 try {
58 bytes = getUnzippedBytes(bytes);
59 }
60 catch (Exception e) {
61 _log.error(e, e);
62 }
63 }
64
65 if (_shortData) {
66 byte[] temp = new byte[96];
67
68 System.arraycopy(bytes, 0, temp, 0, 96);
69
70 bytes = temp;
71 }
72
73 StringBuilder sb = new StringBuilder();
74
75 sb.append("[");
76 sb.append(packet.getSocketAddress());
77 sb.append("] ");
78 sb.append(new String(bytes));
79
80 if (_log.isInfoEnabled()) {
81 _log.info(sb);
82 }
83 }
84
85 protected byte[] getUnzippedBytes(byte[] bytes) throws Exception {
86 InputStream is = new GZIPInputStream(new ByteArrayInputStream(bytes));
87 ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
88
89 byte[] buffer = new byte[1500];
90
91 int c = 0;
92
93 while (true) {
94 if (c == -1) {
95 break;
96 }
97
98 c = is.read(buffer, 0, 1500);
99
100 if (c != -1) {
101 baos.write(buffer, 0, c);
102 }
103 }
104
105 is.close();
106
107 baos.flush();
108 baos.close();
109
110 return baos.toByteArray();
111 }
112
113 private static Log _log = LogFactory.getLog(MulticastDatagramHandler.class);
114
115 private boolean _gzipData;
116 private boolean _shortData;
117
118 }