You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by jv...@apache.org on 2011/09/23 10:29:04 UTC
svn commit: r1174585 - in /mina/trunk/core/src:
main/java/org/apache/mina/filter/logging/LoggingFilter.java
main/java/org/apache/mina/util/ByteBufferDumper.java
test/java/org/apache/mina/util/ByteBufferDumperTest.java
Author: jvermillard
Date: Fri Sep 23 08:29:04 2011
New Revision: 1174585
URL: http://svn.apache.org/viewvc?rev=1174585&view=rev
Log:
DIRMINA-864 smart bytebuffer dumper, which detect if the content is binary data or string
Added:
mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java (with props)
mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java (with props)
Modified:
mina/trunk/core/src/main/java/org/apache/mina/filter/logging/LoggingFilter.java
Modified: mina/trunk/core/src/main/java/org/apache/mina/filter/logging/LoggingFilter.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/logging/LoggingFilter.java?rev=1174585&r1=1174584&r2=1174585&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/filter/logging/LoggingFilter.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/filter/logging/LoggingFilter.java Fri Sep 23 08:29:04 2011
@@ -19,11 +19,14 @@
*/
package org.apache.mina.filter.logging;
+import java.nio.ByteBuffer;
+
import org.apache.mina.api.IdleStatus;
import org.apache.mina.api.IoFilter;
import org.apache.mina.api.IoSession;
import org.apache.mina.filterchain.ReadFilterChainController;
import org.apache.mina.filterchain.WriteFilterChainController;
+import org.apache.mina.util.ByteBufferDumper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -171,13 +174,22 @@ public class LoggingFilter implements Io
@Override
public void messageReceived(IoSession session, Object message, ReadFilterChainController controller) {
- log(messageReceivedLevel, "RECEIVED: {}", message);
+ if (message instanceof ByteBuffer) {
+ log(messageReceivedLevel, "RECEIVED: {}", ByteBufferDumper.dump((ByteBuffer) message));
+ } else {
+ log(messageReceivedLevel, "RECEIVED: {}", message);
+ }
+
controller.callReadNextFilter(session, message);
}
@Override
public void messageWriting(IoSession session, Object message, WriteFilterChainController controller) {
- log(messageWritingLevel, "WRITTING: {}", message);
+ if (message instanceof ByteBuffer) {
+ log(messageReceivedLevel, "WRITTING: {}", ByteBufferDumper.dump((ByteBuffer) message));
+ } else {
+ log(messageReceivedLevel, "WRITTING: {}", message);
+ }
controller.callWriteNextFilter(session, message);
}
Added: mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java?rev=1174585&view=auto
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java (added)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java Fri Sep 23 08:29:04 2011
@@ -0,0 +1,107 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.mina.util;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Utility class for smart dumping {@link ByteBuffer}
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class ByteBufferDumper {
+
+ /**
+ * The high digits lookup table.
+ */
+ private static final byte[] highDigits;
+
+ /**
+ * The low digits lookup table.
+ */
+ private static final byte[] lowDigits;
+
+ /**
+ * Initialize lookup tables.
+ */
+ static {
+ final byte[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+
+ int i;
+ byte[] high = new byte[256];
+ byte[] low = new byte[256];
+
+ for (i = 0; i < 256; i++) {
+ high[i] = digits[i >>> 4];
+ low[i] = digits[i & 0x0F];
+ }
+
+ highDigits = high;
+ lowDigits = low;
+ }
+
+ public static String dump(ByteBuffer buffer) {
+
+ // is not ASCII printable ?
+ boolean binaryContent = false;
+ for (int i = 0; i < buffer.remaining(); i++) {
+ byte b = buffer.get(i);
+ if ((b < 32 || b > 126) && b != 13 && b != 10) {
+ binaryContent = true;
+ break;
+ }
+ }
+
+ if (binaryContent) {
+ int pos = buffer.position();
+ int size = buffer.remaining();
+ StringBuilder out = new StringBuilder(size * 3 + 24);
+ out.append("ByteBuffer[len=").append(size).append(",bytes='");
+ int mark = buffer.position();
+
+ // fill the first
+ int byteValue = buffer.get() & 0xFF;
+ out.append((char) highDigits[byteValue]);
+ out.append((char) lowDigits[byteValue]);
+ size--;
+
+ // and the others, too
+ for (; size > 0; size--) {
+ out.append(' ');
+ byteValue = buffer.get() & 0xFF;
+ out.append((char) highDigits[byteValue]);
+ out.append((char) lowDigits[byteValue]);
+ }
+ buffer.position(pos);
+ out.append("'");
+ return out.toString();
+
+ } else {
+ byte[] data = new byte[buffer.remaining()];
+ int pos = buffer.position();
+ buffer.get(data);
+ buffer.position(pos);
+ StringBuilder out = new StringBuilder(buffer.remaining() + 22);
+ out.append("ByteBuffer[len=").append(buffer.remaining()).append(",str='").append(new String(data))
+ .append("'");
+ return out.toString();
+ }
+ }
+
+}
Propchange: mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java?rev=1174585&view=auto
==============================================================================
--- mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java (added)
+++ mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java Fri Sep 23 08:29:04 2011
@@ -0,0 +1,47 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.mina.util;
+
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+
+import org.junit.Test;
+
+public class ByteBufferDumperTest {
+
+ @Test
+ public void string_test() {
+ ByteBufferDumper dumper = new ByteBufferDumper();
+
+ String toTest = "yopYOP\n\r";
+ byte[] charData = toTest.getBytes();
+ assertEquals(toTest.length(), charData.length);
+
+ ByteBuffer myBuffer = ByteBuffer.allocate(toTest.length());
+ for (int i = 0; i < toTest.length(); i++) {
+ myBuffer.put(charData[i]);
+ }
+ myBuffer.flip();
+
+ String dump = dumper.dump(myBuffer);
+ assertEquals("ByteBuffer[len=8,str='" + toTest + "'", dump);
+ }
+
+}
Propchange: mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain