1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 14:22:05 +01:00

Add FlexiblePacketTypeFilter

and make constructors of MessageTypeFilter and IQTypeFilter
private, because we now provide constants instead.
This commit is contained in:
Florian Schmaus 2014-05-29 16:05:20 +02:00
parent c606530e22
commit 2a091debc1
18 changed files with 114 additions and 73 deletions

View file

@ -28,7 +28,10 @@ import java.util.concurrent.CopyOnWriteArraySet;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FlexiblePacketTypeFilter;
import org.jivesoftware.smack.filter.FromMatchesFilter;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.OrFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.ThreadFilter;
import org.jivesoftware.smack.packet.Message;
@ -89,7 +92,16 @@ public class ChatManager extends Manager{
*/
BARE_JID;
}
private final PacketFilter packetFilter = new OrFilter(MessageTypeFilter.CHAT, new FlexiblePacketTypeFilter<Message>() {
@Override
protected boolean acceptSpecific(Message message) {
return normalIncluded ? message.getType() == Type.normal : false;
}
});
/**
* Determines whether incoming messages of type normal can create chats.
*/
@ -124,16 +136,6 @@ public class ChatManager extends Manager{
private ChatManager(XMPPConnection connection) {
super(connection);
PacketFilter filter = new PacketFilter() {
public boolean accept(Packet packet) {
if (!(packet instanceof Message)) {
return false;
}
Message.Type messageType = ((Message) packet).getType();
return (messageType == Type.chat) || (normalIncluded ? messageType == Type.normal : false);
}
};
// Add a listener for all message packets so that we can deliver
// messages to the best Chat instance available.
connection.addPacketListener(new PacketListener() {
@ -155,7 +157,7 @@ public class ChatManager extends Manager{
return;
deliverMessage(chat, message);
}
}, filter);
}, packetFilter);
INSTANCES.put(connection, this);
}

View file

@ -67,7 +67,7 @@ public class Roster {
private static final Logger LOGGER = Logger.getLogger(Roster.class.getName());
private static final PacketFilter ROSTER_PUSH_FILTER = new AndFilter(new PacketTypeFilter(
RosterPacket.class), new IQTypeFilter(IQ.Type.SET));
RosterPacket.class), IQTypeFilter.SET);
private static final PacketFilter PRESENCE_PACKET_FILTER = new PacketTypeFilter(Presence.class);

View file

@ -0,0 +1,52 @@
/**
*
* Copyright 2014 Florian Schmaus
*
* 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.filter;
import java.lang.reflect.ParameterizedType;
import org.jivesoftware.smack.packet.Packet;
/**
* Filters for packets of a particular type and allows a custom method to further filter the packets.
*
* @author Florian Schmaus
*/
public abstract class FlexiblePacketTypeFilter<P extends Packet> implements PacketFilter {
final Class<P> packetType;
public FlexiblePacketTypeFilter(Class<P> packetType) {
this.packetType = packetType;
}
@SuppressWarnings("unchecked")
public FlexiblePacketTypeFilter() {
packetType = (Class<P>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
@Override
@SuppressWarnings("unchecked")
public boolean accept(Packet packet) {
if (packetType.isInstance(packet)) {
return acceptSpecific((P) packet);
}
return false;
}
protected abstract boolean acceptSpecific(P packet);
}

View file

@ -92,7 +92,7 @@ public class IQReplyFilter implements PacketFilter {
server = conn.getServiceName().toLowerCase(Locale.US);
packetId = iqPacket.getPacketID();
PacketFilter iqFilter = new OrFilter(new IQTypeFilter(IQ.Type.ERROR), new IQTypeFilter(IQ.Type.RESULT));
PacketFilter iqFilter = new OrFilter(IQTypeFilter.ERROR, IQTypeFilter.RESULT);
PacketFilter idFilter = new PacketIDFilter(iqPacket);
iqAndIdFilter = new AndFilter(iqFilter, idFilter);
fromFilter = new OrFilter();

View file

@ -17,7 +17,7 @@
package org.jivesoftware.smack.filter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.IQ.Type;
/**
* A filter for IQ packet types. Returns true only if the packet is an IQ packet
@ -26,20 +26,22 @@ import org.jivesoftware.smack.packet.Packet;
* @author Alexander Wenckus
*
*/
public class IQTypeFilter implements PacketFilter {
public class IQTypeFilter extends FlexiblePacketTypeFilter<IQ> {
public static final PacketFilter GET = new IQTypeFilter(Type.GET);
public static final PacketFilter SET = new IQTypeFilter(Type.SET);
public static final PacketFilter RESULT = new IQTypeFilter(Type.RESULT);
public static final PacketFilter ERROR = new IQTypeFilter(Type.ERROR);
private IQ.Type type;
private final IQ.Type type;
public IQTypeFilter(IQ.Type type) {
private IQTypeFilter(IQ.Type type) {
super(IQ.class);
this.type = type;
}
/*
* (non-Javadoc)
*
* @see org.jivesoftware.smack.filter.PacketFilter#accept(org.jivesoftware.smack.packet.Packet)
*/
public boolean accept(Packet packet) {
return (packet instanceof IQ && ((IQ) packet).getType().equals(type));
}
@Override
protected boolean acceptSpecific(IQ iq) {
return iq.getType().equals(type);
}
}

View file

@ -18,7 +18,8 @@
package org.jivesoftware.smack.filter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Message.Type;
/**
* Filters for packets of a specific type of Message (e.g. CHAT).
@ -26,7 +27,13 @@ import org.jivesoftware.smack.packet.Packet;
* @see org.jivesoftware.smack.packet.Message.Type
* @author Ward Harold
*/
public class MessageTypeFilter implements PacketFilter {
public class MessageTypeFilter extends FlexiblePacketTypeFilter<Message> {
public static final PacketFilter NORMAL = new MessageTypeFilter(Type.normal);
public static final PacketFilter CHAT = new MessageTypeFilter(Type.chat);
public static final PacketFilter GROUPCHAT = new MessageTypeFilter(Type.groupchat);
public static final PacketFilter HEADLINE = new MessageTypeFilter(Type.headline);
public static final PacketFilter ERROR = new MessageTypeFilter(Type.headline);
private final Message.Type type;
@ -35,17 +42,14 @@ public class MessageTypeFilter implements PacketFilter {
*
* @param type the message type.
*/
public MessageTypeFilter(Message.Type type) {
private MessageTypeFilter(Message.Type type) {
super(Message.class);
this.type = type;
}
public boolean accept(Packet packet) {
if (!(packet instanceof Message)) {
return false;
}
else {
return ((Message) packet).getType().equals(this.type);
}
@Override
protected boolean acceptSpecific(Message message) {
return message.getType().equals(this.type);
}
}

View file

@ -46,10 +46,6 @@ public class PacketTypeFilter implements PacketFilter {
* @param packetType the Class type.
*/
public PacketTypeFilter(Class<? extends Packet> packetType) {
// Ensure the packet type is a sub-class of Packet.
if (!Packet.class.isAssignableFrom(packetType)) {
throw new IllegalArgumentException("Packet type must be a sub-class of Packet.");
}
this.packetType = packetType;
}

View file

@ -22,7 +22,6 @@ import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Close;
@ -42,7 +41,7 @@ class CloseListener implements PacketListener {
/* packet filter for all In-Band Bytestream close requests */
private final PacketFilter closeFilter = new AndFilter(new PacketTypeFilter(
Close.class), new IQTypeFilter(IQ.Type.SET));
Close.class), IQTypeFilter.SET);
/**
* Constructor.

View file

@ -27,7 +27,6 @@ import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
@ -53,7 +52,7 @@ class InitiationListener implements PacketListener {
/* packet filter for all In-Band Bytestream requests */
private final PacketFilter initFilter = new AndFilter(new PacketTypeFilter(Open.class),
new IQTypeFilter(IQ.Type.SET));
IQTypeFilter.SET);
/* executor service to process incoming requests concurrently */
private final ExecutorService initiationListenerExecutor;

View file

@ -27,7 +27,6 @@ import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
@ -48,7 +47,7 @@ final class InitiationListener implements PacketListener {
/* packet filter for all SOCKS5 Bytestream requests */
private final PacketFilter initFilter = new AndFilter(new PacketTypeFilter(Bytestream.class),
new IQTypeFilter(IQ.Type.SET));
IQTypeFilter.SET);
/* executor service to process incoming requests concurrently */
private final ExecutorService initiationListenerExecutor;

View file

@ -89,7 +89,7 @@ public class FileTransferManager {
fireNewRequest((StreamInitiation) packet);
}
}, new AndFilter(new PacketTypeFilter(StreamInitiation.class),
new IQTypeFilter(IQ.Type.SET)));
IQTypeFilter.SET));
}
protected void fireNewRequest(StreamInitiation initiation) {

View file

@ -89,8 +89,8 @@ import org.jivesoftware.smackx.iqlast.packet.LastActivity;
public class LastActivityManager extends Manager {
private static final Map<XMPPConnection, LastActivityManager> instances = new WeakHashMap<XMPPConnection, LastActivityManager>();
private static final PacketFilter IQ_GET_LAST_FILTER = new AndFilter(new IQTypeFilter(
IQ.Type.GET), new PacketTypeFilter(LastActivity.class));
private static final PacketFilter IQ_GET_LAST_FILTER = new AndFilter(IQTypeFilter.GET,
new PacketTypeFilter(LastActivity.class));
private static boolean enabledPerDefault = true;

View file

@ -27,8 +27,8 @@ import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.iqversion.packet.Version;
@ -53,6 +53,8 @@ public class VersionManager extends Manager {
private static final Map<XMPPConnection, VersionManager> instances =
Collections.synchronizedMap(new WeakHashMap<XMPPConnection, VersionManager>());
private static final PacketFilter PACKET_FILTER = new AndFilter(new PacketTypeFilter(Version.class), IQTypeFilter.GET);
private Version own_version;
private VersionManager(final XMPPConnection connection) {
@ -77,7 +79,7 @@ public class VersionManager extends Manager {
connection().sendPacket(reply);
}
}
, new AndFilter(new PacketTypeFilter(Version.class), new IQTypeFilter(Type.GET)));
, PACKET_FILTER);
}
public static synchronized VersionManager getInstanceFor(XMPPConnection connection) {

View file

@ -1947,18 +1947,8 @@ public class MultiUserChat {
private void init() {
// Create filters
messageFilter =
new AndFilter(
FromMatchesFilter.create(room),
new MessageTypeFilter(Message.Type.groupchat));
messageFilter = new AndFilter(messageFilter, new PacketFilter() {
public boolean accept(Packet packet) {
Message msg = (Message) packet;
return msg.getBody() != null;
}
});
presenceFilter =
new AndFilter(FromMatchesFilter.create(room), new PacketTypeFilter(Presence.class));
messageFilter = new AndFilter(FromMatchesFilter.create(room), MessageTypeFilter.GROUPCHAT);
presenceFilter = new AndFilter(FromMatchesFilter.create(room), PacketTypeFilter.PRESENCE);
// Create a collector for incoming messages.
messageCollector = new ConnectionDetachedPacketCollector();

View file

@ -35,8 +35,6 @@ import org.jivesoftware.smack.packet.Presence;
*/
class PacketMultiplexListener implements PacketListener {
private static final PacketFilter MESSAGE_FILTER =
new MessageTypeFilter(Message.Type.groupchat);
private static final PacketFilter PRESENCE_FILTER = new PacketTypeFilter(Presence.class);
private static final PacketFilter SUBJECT_FILTER = new PacketFilter() {
public boolean accept(Packet packet) {
@ -79,7 +77,7 @@ class PacketMultiplexListener implements PacketListener {
if (PRESENCE_FILTER.accept(p)) {
presenceListener.processPacket(p);
}
else if (MESSAGE_FILTER.accept(p)) {
else if (MessageTypeFilter.GROUPCHAT.accept(p)) {
messageCollector.processPacket(p);
if (SUBJECT_FILTER.accept(p)) {

View file

@ -45,7 +45,6 @@ import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.ping.packet.Ping;
import org.jivesoftware.smackx.ping.packet.Pong;
@ -68,9 +67,9 @@ public class PingManager extends Manager {
.synchronizedMap(new WeakHashMap<XMPPConnection, PingManager>());
private static final PacketFilter PING_PACKET_FILTER = new AndFilter(
new PacketTypeFilter(Ping.class), new IQTypeFilter(Type.GET));
new PacketTypeFilter(Ping.class), IQTypeFilter.GET);
private static final PacketFilter PONG_PACKET_FILTER = new AndFilter(new PacketTypeFilter(
Pong.class), new IQTypeFilter(Type.RESULT));
Pong.class), IQTypeFilter.RESULT);
static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {

View file

@ -58,8 +58,8 @@ import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
public class PrivacyListManager extends Manager {
public static final String NAMESPACE = "jabber:iq:privacy";
private static final PacketFilter PACKET_FILTER = new AndFilter(new IQTypeFilter(IQ.Type.SET),
new PacketExtensionFilter("query", "jabber:iq:privacy"));
private static final PacketFilter PACKET_FILTER = new AndFilter(IQTypeFilter.SET,
new PacketExtensionFilter("query", NAMESPACE));
// Keep the list of instances of this class.
private static final Map<XMPPConnection, PrivacyListManager> instances = Collections

View file

@ -31,7 +31,6 @@ import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.time.packet.Time;
@ -41,7 +40,7 @@ public class EntityTimeManager extends Manager {
private static final Map<XMPPConnection, EntityTimeManager> INSTANCES = new WeakHashMap<XMPPConnection, EntityTimeManager>();
private static final PacketFilter TIME_PACKET_FILTER = new AndFilter(new PacketTypeFilter(
Time.class), new IQTypeFilter(Type.GET));
Time.class), IQTypeFilter.GET);
private static boolean autoEnable = true;