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 com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
26  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
27  
28  import java.io.InputStream;
29  
30  import java.net.DatagramPacket;
31  
32  import java.util.zip.GZIPInputStream;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * <a href="MulticastDatagramHandler.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Michael C. Han
41   * @author Raymond Augé
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(
88              new UnsyncByteArrayInputStream(bytes));
89          UnsyncByteArrayOutputStream ubaos =
90              new UnsyncByteArrayOutputStream(bytes.length);
91  
92          byte[] buffer = new byte[1500];
93  
94          int c = 0;
95  
96          while (true) {
97              if (c == -1) {
98                  break;
99              }
100 
101             c = is.read(buffer, 0, 1500);
102 
103             if (c != -1) {
104                 ubaos.write(buffer, 0, c);
105             }
106         }
107 
108         is.close();
109 
110         ubaos.flush();
111         ubaos.close();
112 
113         return ubaos.toByteArray();
114     }
115 
116     private static Log _log = LogFactory.getLog(MulticastDatagramHandler.class);
117 
118     private boolean _gzipData;
119     private boolean _shortData;
120 
121 }