Commit 27c5cc74 authored by Guus der Kinderen's avatar Guus der Kinderen Committed by GitHub

Merge pull request #610 from GregDThomas/OF-1152

OF-1152: Ensure that raw XML is logged;
parents bfc13370 b227b3cb
......@@ -2,7 +2,7 @@
<html>
<head>
<title>Broadcast Plugin Changelog</title>
<title>XML Debugger Plugin Changelog</title>
<style type="text/css">
BODY {
font-size : 100%;
......@@ -43,6 +43,11 @@
<h1>
XML Debugger Plugin Changelog
</h1>
<p><b>1.6.0</b> -- June 20, 2016</p>
<li>[<a href='http://issues.igniterealtime.org/browse/OF-1152'>OF-1152</a>] - Ensure that the plugin prints raw XML.</li>
<li>Requires Openfire 4</li>
<p><b>1.5.0</b> -- October 12, 2015</p>
<ul>
<li>[<a href='http://www.igniterealtime.org/issues/browse/OF-953'>OF-953</a>] - Updated JSP libraries.</li>
......
......@@ -8,8 +8,8 @@
<name>Debugger Plugin</name>
<description>Prints XML traffic to the stdout (raw and interpreted XML)</description>
<author>Jive Software</author>
<version>1.5.0</version>
<date>10/12/2015</date>
<version>1.6.0</version>
<date>06/20/2016</date>
<minServerVersion>4.0.0</minServerVersion>
<adminconsole>
......
......@@ -27,6 +27,7 @@ import org.apache.mina.transport.socket.SocketAcceptor;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.container.PluginManagerListener;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.spi.ConnectionManagerImpl;
import org.jivesoftware.util.JiveGlobals;
......@@ -49,30 +50,45 @@ public class DebuggerPlugin implements Plugin, PropertyEventListener {
private InterpretedXMLPrinter interpretedPrinter;
public void initializePlugin(PluginManager manager, File pluginDirectory) {
public void initializePlugin(final PluginManager pluginManager, final File pluginDirectory) {
if (pluginManager.isExecuted()) {
addInterceptors();
} else {
pluginManager.addPluginManagerListener(new PluginManagerListener() {
public void pluginsMonitored() {
// Stop listening for plugin events
pluginManager.removePluginManagerListener(this);
// Start listeners
addInterceptors();
}
});
}
}
private void addInterceptors() {
// Add filter to filter chain builder
ConnectionManagerImpl connManager = (ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager();
defaultPortFilter = new RawPrintFilter("C2S");
SocketAcceptor socketAcceptor = connManager.getSocketAcceptor();
if (socketAcceptor != null) {
socketAcceptor.getFilterChain().addBefore("xmpp", "rawDebugger", defaultPortFilter);
socketAcceptor.getFilterChain().addFirst("rawDebugger", defaultPortFilter);
}
oldPortFilter = new RawPrintFilter("SSL");
SocketAcceptor sslAcceptor = connManager.getSSLSocketAcceptor();
if (sslAcceptor != null) {
sslAcceptor.getFilterChain().addBefore("xmpp", "rawDebugger", oldPortFilter);
sslAcceptor.getFilterChain().addFirst("rawDebugger", oldPortFilter);
}
componentPortFilter = new RawPrintFilter("ExComp");
SocketAcceptor componentAcceptor = connManager.getComponentAcceptor();
if (componentAcceptor != null) {
componentAcceptor.getFilterChain().addBefore("xmpp", "rawDebugger", componentPortFilter);
componentAcceptor.getFilterChain().addFirst("rawDebugger", componentPortFilter);
}
multiplexerPortFilter = new RawPrintFilter("CM");
SocketAcceptor multiplexerAcceptor = connManager.getMultiplexerSocketAcceptor();
if (multiplexerAcceptor != null) {
multiplexerAcceptor.getFilterChain().addBefore("xmpp", "rawDebugger", multiplexerPortFilter);
multiplexerAcceptor.getFilterChain().addFirst("rawDebugger", multiplexerPortFilter);
}
interpretedPrinter = new InterpretedXMLPrinter();
......
......@@ -20,12 +20,12 @@
package org.jivesoftware.openfire.plugin;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.WriteRequest;
......@@ -51,29 +51,33 @@ public class RawPrintFilter extends IoFilterAdapter {
@Override
public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
// Decode the bytebuffer and print it to the stdout
if (enabled && message instanceof ByteBuffer) {
ByteBuffer byteBuffer = (ByteBuffer) message;
if (enabled && message instanceof IoBuffer) {
logBuffer(session, (IoBuffer) message, "RECV");
}
// Pass the message to the next filter
super.messageReceived(nextFilter, session, message);
}
private void logBuffer(final IoSession session, final IoBuffer ioBuffer, final String receiveOrSend) {
// Keep current position in the buffer
int currentPos = byteBuffer.position();
int currentPos = ioBuffer.position();
// Decode buffer
Charset encoder = Charset.forName("UTF-8");
CharBuffer charBuffer = encoder.decode(byteBuffer.asReadOnlyBuffer());
CharBuffer charBuffer = Charset.forName("UTF-8").decode(ioBuffer.buf());
// Print buffer content
System.out.println(prefix + " - RECV (" + session.hashCode() + "): " + charBuffer);
System.out.println(messagePrefix(session, receiveOrSend) + ": " + charBuffer);
// Reset to old position in the buffer
byteBuffer.position(currentPos);
ioBuffer.position(currentPos);
}
// Pass the message to the next filter
super.messageReceived(nextFilter, session, message);
private String messagePrefix(final IoSession session, final String messageType) {
return prefix + " - " + messageType + String.format(" - (%1$11s)", session.hashCode());
}
@Override
public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
if (enabled && writeRequest.getMessage() instanceof ByteBuffer) {
System.out.println(prefix + " - SENT (" + session.hashCode() + "): " +
Charset.forName("UTF-8").decode(((ByteBuffer) writeRequest.getMessage()).asReadOnlyBuffer()));
if (enabled && writeRequest.getMessage() instanceof IoBuffer) {
logBuffer(session, (IoBuffer) writeRequest.getMessage(), "SENT");
}
// Pass the message to the next filter
super.messageSent(nextFilter, session, writeRequest);
}
......@@ -99,7 +103,10 @@ public class RawPrintFilter extends IoFilterAdapter {
public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
// Keep track of sessions using this filter
sessions.add(session);
if (enabled) {
// Print that a session was closed
System.out.println(messagePrefix(session, "OPEN"));
}
super.sessionCreated(nextFilter, session);
}
......@@ -109,9 +116,8 @@ public class RawPrintFilter extends IoFilterAdapter {
sessions.remove(session);
if (enabled) {
// Print that a session was closed
System.out.println("CLOSED (" + session.hashCode() + ") ");
System.out.println(messagePrefix(session, "CLSD"));
}
super.sessionClosed(nextFilter, session);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment