1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-26 14:17:57 +01:00

Replace PacketInterceptor with PacketListener

both serve the same purpose: As callback for Packets. There is no need
to have both, so remace PacketInterceptor and let PacketListener take
its place. Some classes like ChatStateManager can now use
MessageListener as interceptor callback, which is more convenient.
This commit is contained in:
Florian Schmaus 2014-10-12 19:19:44 +02:00
parent fd7b747987
commit 63d52f6eaa
8 changed files with 57 additions and 95 deletions

View file

@ -124,12 +124,12 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
new HashMap<PacketListener, ListenerWrapper>();
/**
* List of PacketInterceptors that will be notified when a new packet is about to be
* List of PacketListeners that will be notified when a new packet is about to be
* sent to the server. These interceptors may modify the packet before it is being
* actually sent to the server.
*/
private final Map<PacketInterceptor, InterceptorWrapper> interceptors =
new HashMap<PacketInterceptor, InterceptorWrapper>();
private final Map<PacketListener, InterceptorWrapper> interceptors =
new HashMap<PacketListener, InterceptorWrapper>();
protected final Lock connectionLock = new ReentrantLock();
@ -726,7 +726,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
}
@Override
public void addPacketInterceptor(PacketInterceptor packetInterceptor,
public void addPacketInterceptor(PacketListener packetInterceptor,
PacketFilter packetFilter) {
if (packetInterceptor == null) {
throw new NullPointerException("Packet interceptor is null.");
@ -738,7 +738,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
}
@Override
public void removePacketInterceptor(PacketInterceptor packetInterceptor) {
public void removePacketInterceptor(PacketListener packetInterceptor) {
synchronized (interceptors) {
interceptors.remove(packetInterceptor);
}
@ -753,7 +753,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
* @param packet the packet that is going to be sent to the server
*/
private void firePacketInterceptors(Packet packet) {
List<PacketInterceptor> interceptorsToInvoke = new LinkedList<PacketInterceptor>();
List<PacketListener> interceptorsToInvoke = new LinkedList<PacketListener>();
synchronized (interceptors) {
for (InterceptorWrapper interceptorWrapper : interceptors.values()) {
if (interceptorWrapper.filterMatches(packet)) {
@ -761,8 +761,12 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
}
}
}
for (PacketInterceptor interceptor : interceptorsToInvoke) {
interceptor.interceptPacket(packet);
for (PacketListener interceptor : interceptorsToInvoke) {
try {
interceptor.processPacket(packet);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Packet interceptor threw exception", e);
}
}
}
@ -957,7 +961,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
*/
protected static class InterceptorWrapper {
private final PacketInterceptor packetInterceptor;
private final PacketListener packetInterceptor;
private final PacketFilter packetFilter;
/**
@ -966,7 +970,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
* @param packetInterceptor the interceptor.
* @param packetFilter the associated filter or null if it intercepts all packets.
*/
public InterceptorWrapper(PacketInterceptor packetInterceptor, PacketFilter packetFilter) {
public InterceptorWrapper(PacketListener packetInterceptor, PacketFilter packetFilter) {
this.packetInterceptor = packetInterceptor;
this.packetFilter = packetFilter;
}
@ -975,7 +979,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return packetFilter == null || packetFilter.accept(packet);
}
public PacketInterceptor getInterceptor() {
public PacketListener getInterceptor() {
return packetInterceptor;
}
}

View file

@ -130,8 +130,8 @@ public class ChatManager extends Manager{
private Set<ChatManagerListener> chatManagerListeners
= new CopyOnWriteArraySet<ChatManagerListener>();
private Map<PacketInterceptor, PacketFilter> interceptors
= new WeakHashMap<PacketInterceptor, PacketFilter>();
private Map<MessageListener, PacketFilter> interceptors
= new WeakHashMap<MessageListener, PacketFilter>();
private ChatManager(XMPPConnection connection) {
super(connection);
@ -338,10 +338,10 @@ public class ChatManager extends Manager{
}
void sendMessage(Chat chat, Message message) throws NotConnectedException {
for(Map.Entry<PacketInterceptor, PacketFilter> interceptor : interceptors.entrySet()) {
for(Map.Entry<MessageListener, PacketFilter> interceptor : interceptors.entrySet()) {
PacketFilter filter = interceptor.getValue();
if(filter != null && filter.accept(message)) {
interceptor.getKey().interceptPacket(message);
interceptor.getKey().processMessage(message);
}
}
// Ensure that messages being sent have a proper FROM value
@ -359,16 +359,17 @@ public class ChatManager extends Manager{
/**
* Adds an interceptor which intercepts any messages sent through chats.
*
* @param packetInterceptor the interceptor.
* @param messageInterceptor the interceptor.
*/
public void addOutgoingMessageInterceptor(PacketInterceptor packetInterceptor) {
addOutgoingMessageInterceptor(packetInterceptor, null);
public void addOutgoingMessageInterceptor(MessageListener messageInterceptor) {
addOutgoingMessageInterceptor(messageInterceptor, null);
}
public void addOutgoingMessageInterceptor(PacketInterceptor packetInterceptor, PacketFilter filter) {
if (packetInterceptor != null) {
interceptors.put(packetInterceptor, filter);
public void addOutgoingMessageInterceptor(MessageListener messageInterceptor, PacketFilter filter) {
if (messageInterceptor == null) {
return;
}
interceptors.put(messageInterceptor, filter);
}
/**

View file

@ -1,47 +0,0 @@
/**
*
* Copyright 2003-2005 Jive Software.
*
* Licensed 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.jivesoftware.smack;
import org.jivesoftware.smack.packet.Packet;
/**
* Provides a mechanism to intercept and modify packets that are going to be
* sent to the server. PacketInterceptors are added to the {@link XMPPConnection}
* together with a {@link org.jivesoftware.smack.filter.PacketFilter} so that only
* certain packets are intercepted and processed by the interceptor.<p>
*
* This allows event-style programming -- every time a new packet is found,
* the {@link #interceptPacket(Packet)} method will be called.
*
* @see XMPPConnection#addPacketInterceptor(PacketInterceptor, org.jivesoftware.smack.filter.PacketFilter)
* @author Gaston Dombiak
*/
public interface PacketInterceptor {
/**
* Process the packet that is about to be sent to the server. The intercepted
* packet can be modified by the interceptor.<p>
*
* Interceptors are invoked using the same thread that requested the packet
* to be sent, so it's very important that implementations of this method
* not block for any extended period of time.
*
* @param packet the packet to is going to be sent to the server.
*/
public void interceptPacket(Packet packet);
}

View file

@ -26,6 +26,12 @@ import org.jivesoftware.smack.packet.Packet;
* the {@link #processPacket(Packet)} method will be called. This is the
* opposite approach to the functionality provided by a {@link PacketCollector}
* which lets you block while waiting for results.
* <p>
* Additionally you are able to intercept Packets that are going to be send and
* make modifications to them. You can register a PacketListener as interceptor
* by using {@link XMPPConnection#addPacketInterceptor(PacketListener,
* org.jivesoftware.smack.filter.PacketFilter)}
* </p>
*
* @see XMPPConnection#addPacketListener(PacketListener, org.jivesoftware.smack.filter.PacketFilter)
* @author Matt Tucker
@ -33,11 +39,12 @@ import org.jivesoftware.smack.packet.Packet;
public interface PacketListener {
/**
* Process the next packet sent to this packet listener.<p>
*
* Process the next packet sent to this packet listener.
* <p>
* A single thread is responsible for invoking all listeners, so
* it's very important that implementations of this method not block
* for any extended period of time.
* </p>
*
* @param packet the packet to process.
*/

View file

@ -225,7 +225,7 @@ public interface XMPPConnection {
* which packets will be delivered to the listener. If the same packet listener
* is added again with a different filter, only the new filter will be used.
*
* NOTE: If you want get a similar callback for outgoing packets, see {@link #addPacketInterceptor(PacketInterceptor, PacketFilter)}.
* NOTE: If you want get a similar callback for outgoing packets, see {@link #addPacketInterceptor(PacketListener, PacketFilter)}.
*
* @param packetListener the packet listener to notify of new received packets.
* @param packetFilter the packet filter to use.
@ -272,14 +272,14 @@ public interface XMPPConnection {
* @param packetInterceptor the packet interceptor to notify of packets about to be sent.
* @param packetFilter the packet filter to use.
*/
public void addPacketInterceptor(PacketInterceptor packetInterceptor, PacketFilter packetFilter);
public void addPacketInterceptor(PacketListener packetInterceptor, PacketFilter packetFilter);
/**
* Removes a packet interceptor.
*
* @param packetInterceptor the packet interceptor to remove.
*/
public void removePacketInterceptor(PacketInterceptor packetInterceptor);
public void removePacketInterceptor(PacketListener packetInterceptor);
/**
* Returns the current value of the reply timeout in milliseconds for request for this

View file

@ -22,7 +22,6 @@ import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
@ -344,8 +343,8 @@ public class EntityCapsManager extends Manager {
// Intercept presence packages and add caps data when intended.
// XEP-0115 specifies that a client SHOULD include entity capabilities
// with every presence notification it sends.
PacketInterceptor packetInterceptor = new PacketInterceptor() {
public void interceptPacket(Packet packet) {
PacketListener packetInterceptor = new PacketListener() {
public void processPacket(Packet packet) {
if (!entityCapsEnabled)
return;

View file

@ -24,15 +24,14 @@ import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ChatMessageListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.filter.NotFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.chatstates.packet.ChatStateExtension;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
@ -150,10 +149,10 @@ public class ChatStateManager extends Manager {
}
}
private class OutgoingMessageInterceptor implements PacketInterceptor {
private class OutgoingMessageInterceptor implements MessageListener {
public void interceptPacket(Packet packet) {
Message message = (Message) packet;
@Override
public void processMessage(Message message) {
Chat chat = chatManager.getThreadChat(message.getThread());
if (chat == null) {
return;

View file

@ -40,7 +40,6 @@ import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.PresenceListener;
import org.jivesoftware.smack.SmackException;
@ -108,7 +107,7 @@ public class MultiUserChat {
new ArrayList<ParticipantStatusListener>();
private final Set<MessageListener> messageListeners = new CopyOnWriteArraySet<MessageListener>();
private final Set<PresenceListener> presenceListeners = new CopyOnWriteArraySet<PresenceListener>();
private final Set<PacketInterceptor> presenceInterceptors = new CopyOnWriteArraySet<PacketInterceptor>();
private final Set<PacketListener> presenceInterceptors = new CopyOnWriteArraySet<PacketListener>();
private final ConnectionDetachedPacketCollector<Message> messageCollector = new ConnectionDetachedPacketCollector<Message>();
private final PacketFilter fromRoomFilter;
@ -453,8 +452,8 @@ public class MultiUserChat {
}
joinPresence.addExtension(mucInitialPresence);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketInterceptor packetInterceptor : presenceInterceptors) {
packetInterceptor.interceptPacket(joinPresence);
for (PacketListener packetInterceptor : presenceInterceptors) {
packetInterceptor.processPacket(joinPresence);
}
// Wait for a presence packet back from the server.
@ -660,8 +659,8 @@ public class MultiUserChat {
Presence leavePresence = new Presence(Presence.Type.unavailable);
leavePresence.setTo(room + "/" + nickname);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketInterceptor packetInterceptor : presenceInterceptors) {
packetInterceptor.interceptPacket(leavePresence);
for (PacketListener packetInterceptor : presenceInterceptors) {
packetInterceptor.processPacket(leavePresence);
}
connection.sendPacket(leavePresence);
// Reset occupant information.
@ -968,24 +967,24 @@ public class MultiUserChat {
}
/**
* Adds a new {@link PacketInterceptor} that will be invoked every time a new presence
* Adds a new {@link PacketListener} that will be invoked every time a new presence
* is going to be sent by this MultiUserChat to the server. Packet interceptors may
* add new extensions to the presence that is going to be sent to the MUC service.
*
* @param presenceInterceptor the new packet interceptor that will intercept presence packets.
*/
public void addPresenceInterceptor(PacketInterceptor presenceInterceptor) {
public void addPresenceInterceptor(PacketListener presenceInterceptor) {
presenceInterceptors.add(presenceInterceptor);
}
/**
* Removes a {@link PacketInterceptor} that was being invoked every time a new presence
* Removes a {@link PacketListener} that was being invoked every time a new presence
* was being sent by this MultiUserChat to the server. Packet interceptors may
* add new extensions to the presence that is going to be sent to the MUC service.
*
* @param presenceInterceptor the packet interceptor to remove.
*/
public void removePresenceInterceptor(PacketInterceptor presenceInterceptor) {
public void removePresenceInterceptor(PacketListener presenceInterceptor) {
presenceInterceptors.remove(presenceInterceptor);
}
@ -1070,8 +1069,8 @@ public class MultiUserChat {
Presence joinPresence = new Presence(Presence.Type.available);
joinPresence.setTo(room + "/" + nickname);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketInterceptor packetInterceptor : presenceInterceptors) {
packetInterceptor.interceptPacket(joinPresence);
for (PacketListener packetInterceptor : presenceInterceptors) {
packetInterceptor.processPacket(joinPresence);
}
// Wait for a presence packet back from the server.
@ -1115,8 +1114,8 @@ public class MultiUserChat {
joinPresence.setMode(mode);
joinPresence.setTo(room + "/" + nickname);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketInterceptor packetInterceptor : presenceInterceptors) {
packetInterceptor.interceptPacket(joinPresence);
for (PacketListener packetInterceptor : presenceInterceptors) {
packetInterceptor.processPacket(joinPresence);
}
// Send join packet.