Commit fe357fc1 authored by Guus der Kinderen's avatar Guus der Kinderen

Remove duplicated code (no functional changes).

parent 0650e0db
...@@ -16,6 +16,12 @@ ...@@ -16,6 +16,12 @@
package org.jivesoftware.openfire.interceptor; package org.jivesoftware.openfire.interceptor;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.Packet;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
...@@ -23,12 +29,6 @@ import java.util.Map; ...@@ -23,12 +29,6 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.Packet;
/** /**
* An InterceptorManager manages the list of global interceptors and per-user * An InterceptorManager manages the list of global interceptors and per-user
* interceptors that are invoked before and after packets are read and sent. * interceptors that are invoked before and after packets are read and sent.
...@@ -227,27 +227,8 @@ public class InterceptorManager { ...@@ -227,27 +227,8 @@ public class InterceptorManager {
throws PacketRejectedException throws PacketRejectedException
{ {
// Invoke the global interceptors for this packet // Invoke the global interceptors for this packet
// Checking if collection is empty to prevent creating an iterator of invokeInterceptors( globalInterceptors, packet, session, read, processed );
// a CopyOnWriteArrayList that is an expensive operation
if (!globalInterceptors.isEmpty()) {
for (PacketInterceptor interceptor : globalInterceptors) {
try {
interceptor.interceptPacket(packet, session, read, processed);
}
catch (PacketRejectedException e) {
if (processed) {
Log.error("Post interceptor cannot reject packet.", e);
}
else {
// Throw this exception since we don't really want to catch it
throw e;
}
}
catch (Throwable e) {
Log.error("Error in interceptor: " + interceptor + " while intercepting: " + packet, e);
}
}
}
// Invoke the interceptors that are related to the address of the session // Invoke the interceptors that are related to the address of the session
if (usersInterceptors.isEmpty()) { if (usersInterceptors.isEmpty()) {
// Do nothing // Do nothing
...@@ -256,24 +237,51 @@ public class InterceptorManager { ...@@ -256,24 +237,51 @@ public class InterceptorManager {
String username = session != null ? session.getAddress().getNode() : null; String username = session != null ? session.getAddress().getNode() : null;
if (username != null && server.isLocal(session.getAddress())) { if (username != null && server.isLocal(session.getAddress())) {
Collection<PacketInterceptor> userInterceptors = usersInterceptors.get(username); Collection<PacketInterceptor> userInterceptors = usersInterceptors.get(username);
if (userInterceptors != null && !userInterceptors.isEmpty()) { invokeInterceptors( userInterceptors, packet, session, read, processed );
for (PacketInterceptor interceptor : userInterceptors) { }
try { }
interceptor.interceptPacket(packet, session, read, processed);
} /**
catch (PacketRejectedException e) { * Invokes a collection of interceptors for the provided packet.
if (processed) { *
Log.error("Post interceptor cannot reject packet.", e); * @param interceptors The interceptors to be triggered (can be null, can be empty).
} * @param packet the packet that has been read or is about to be sent.
else { * @param session the session that received the packet or that the packet
// Throw this exception since we don't really want to catch it * will be sent to.
throw e; * @param read true indicates that the packet was read. When false, the packet
} * is being sent to a user.
} * @param processed true if the packet has already processed (incoming or outgoing).
catch (Throwable e) { * If the packet hasn't already been processed, this flag will be false.
Log.error("Error in interceptor: " + interceptor + " while intercepting: " + packet, e); * @throws PacketRejectedException if the packet should be prevented from being processed.
} */
protected static void invokeInterceptors( Collection<PacketInterceptor> interceptors, Packet packet, Session session, boolean read, boolean processed ) throws PacketRejectedException
{
if ( interceptors == null || interceptors.isEmpty() )
{
return;
}
for ( final PacketInterceptor interceptor : interceptors )
{
try
{
interceptor.interceptPacket( packet, session, read, processed );
}
catch ( PacketRejectedException e )
{
if ( processed )
{
Log.error( "Post interceptor cannot reject packet.", e );
} }
else
{
// Throw this exception since we don't really want to catch it
throw e;
}
}
catch ( Throwable e )
{
Log.error( "Error in interceptor: " + interceptor + " while intercepting: " + packet, e );
} }
} }
} }
......
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