From 5d0dd49e61b6ab735eb50e49dd3911c7fcede134 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 29 Jan 2017 11:08:16 +0100 Subject: [PATCH] Introduce ToMatchesFilter and refactor the FromMatchesFilter into AbstractFromToMatchesFilter. --- .../filter/AbstractFromToMatchesFilter.java | 70 +++++++++++++++++++ .../smack/filter/FromMatchesFilter.java | 48 ++++--------- .../jivesoftware/smack/filter/ToFilter.java | 6 ++ .../smack/filter/ToMatchesFilter.java | 69 ++++++++++++++++++ .../smackx/muc/MultiUserChat.java | 4 +- 5 files changed, 159 insertions(+), 38 deletions(-) create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractFromToMatchesFilter.java create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/filter/ToMatchesFilter.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractFromToMatchesFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractFromToMatchesFilter.java new file mode 100644 index 000000000..7f71678cf --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractFromToMatchesFilter.java @@ -0,0 +1,70 @@ +/** + * + * Copyright 2017 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 org.jivesoftware.smack.packet.Stanza; +import org.jxmpp.jid.Jid; + +public abstract class AbstractFromToMatchesFilter implements StanzaFilter { + + private final Jid address; + + /** + * Flag that indicates if the checking will be done against bare JID addresses or full JIDs. + */ + private final boolean ignoreResourcepart; + + /** + * Creates a filter matching on the address returned by {@link #getAddressToCompare(Stanza)}. The address must be + * the same as the filter address. The second parameter specifies whether the full or the bare addresses are + * compared. + * + * @param address The address to filter for. If null is given, then + * {@link #getAddressToCompare(Stanza)} must also return null to match. + * @param ignoreResourcepart + */ + protected AbstractFromToMatchesFilter(Jid address, boolean ignoreResourcepart) { + if (address != null && ignoreResourcepart) { + this.address = address.asBareJid(); + } + else { + this.address = address; + } + this.ignoreResourcepart = ignoreResourcepart; + } + + public final boolean accept(final Stanza stanza) { + Jid stanzaAddress = getAddressToCompare(stanza); + + if (stanzaAddress == null) { + return address == null; + } + + if (ignoreResourcepart) { + stanzaAddress = stanzaAddress.asBareJid(); + } + + return stanzaAddress.equals(address); + } + + protected abstract Jid getAddressToCompare(Stanza stanza); + + public final String toString() { + String matchMode = ignoreResourcepart ? "ignoreResourcepart" : "full"; + return getClass().getSimpleName() + " (" + matchMode + "): " + address; + } +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/FromMatchesFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/FromMatchesFilter.java index 1ce3da300..1e46de307 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/FromMatchesFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/FromMatchesFilter.java @@ -1,6 +1,6 @@ /** * - * Copyright 2003-2014 Jive Software. + * Copyright 2003-2014 Jive Software, 2017 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.jivesoftware.smack.filter; import org.jivesoftware.smack.packet.Stanza; @@ -28,14 +27,9 @@ import org.jxmpp.jid.Jid; * * @author Gaston Dombiak */ -public class FromMatchesFilter implements StanzaFilter { +public final class FromMatchesFilter extends AbstractFromToMatchesFilter { - private final Jid address; - - /** - * Flag that indicates if the checking will be done against bare JID addresses or full JIDs. - */ - private final boolean ignoreResourcepart; + public final static FromMatchesFilter MATCH_NO_FROM_SET = create(null); /** * Creates a filter matching on the "from" field. The from address must be the same as the @@ -47,13 +41,7 @@ public class FromMatchesFilter implements StanzaFilter { * @param ignoreResourcepart */ public FromMatchesFilter(Jid address, boolean ignoreResourcepart) { - if (address != null && ignoreResourcepart) { - this.address = address.asBareJid(); - } - else { - this.address = address; - } - this.ignoreResourcepart = ignoreResourcepart; + super(address, ignoreResourcepart); } /** @@ -61,50 +49,38 @@ public class FromMatchesFilter implements StanzaFilter { * the filter address with the bare from address. Otherwise, compares the filter address * with the full from address. * - * @param address The address to filter for. If null is given, the stanza(/packet) must not + * @param address The address to filter for. If null is given, the stanza must not * have a from address. */ public static FromMatchesFilter create(Jid address) { - return new FromMatchesFilter(address, address.hasNoResource()) ; + return new FromMatchesFilter(address, address != null ? address.hasNoResource() : false) ; } /** * Creates a filter matching on the "from" field. Compares the bare version of from and filter * address. * - * @param address The address to filter for. If null is given, the stanza(/packet) must not + * @param address The address to filter for. If null is given, the stanza must not * have a from address. */ public static FromMatchesFilter createBare(Jid address) { return new FromMatchesFilter(address, true); } - /** - * Creates a filter matching on the "from" field. Compares the full version of from and filter + * Creates a filter matching on the "from" field. Compares the full version, if available, of from and filter * address. * - * @param address The address to filter for. If null is given, the stanza(/packet) must not + * @param address The address to filter for. If null is given, the stanza must not * have a from address. */ public static FromMatchesFilter createFull(Jid address) { return new FromMatchesFilter(address, false); } - public boolean accept(Stanza packet) { - Jid from = packet.getFrom(); - if (from == null) { - return address == null; - } - - if (ignoreResourcepart) { - from = from.asBareJid(); - } - return from.equals(address); + @Override + protected Jid getAddressToCompare(Stanza stanza) { + return stanza.getFrom(); } - public String toString() { - String matchMode = ignoreResourcepart ? "ignoreResourcepart" : "full"; - return getClass().getSimpleName() + " (" + matchMode + "): " + address; - } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/ToFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/ToFilter.java index 115c30f15..d8964b5f1 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/ToFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/ToFilter.java @@ -19,6 +19,12 @@ package org.jivesoftware.smack.filter; import org.jivesoftware.smack.packet.Stanza; import org.jxmpp.jid.Jid; +/** + * Match based on the 'to' attribute of a Stanza. + * + * @deprecated use {@link ToMatchesFilter} instead. + */ +@Deprecated public class ToFilter implements StanzaFilter { private final Jid to; diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/ToMatchesFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/ToMatchesFilter.java new file mode 100644 index 000000000..99ca2a762 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/ToMatchesFilter.java @@ -0,0 +1,69 @@ +/** + * + * Copyright 2017 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 org.jivesoftware.smack.packet.Stanza; +import org.jxmpp.jid.Jid; + +public final class ToMatchesFilter extends AbstractFromToMatchesFilter { + + public static final ToMatchesFilter MATCH_NO_TO_SET = create(null); + + public ToMatchesFilter(Jid address, boolean ignoreResourcepart) { + super(address, ignoreResourcepart); + } + + /** + * Creates a filter matching on the "to" field. If the filter address is bare, compares + * the filter address with the bare from address. Otherwise, compares the filter address + * with the full from address. + * + * @param address The address to filter for. If null is given, the stanza must not + * have a from address. + */ + public static ToMatchesFilter create(Jid address) { + return new ToMatchesFilter(address, address != null ? address.hasNoResource() : false) ; + } + + /** + * Creates a filter matching on the "to" field. Compares the bare version of to and filter + * address. + * + * @param address The address to filter for. If null is given, the stanza must not + * have a from address. + */ + public static ToMatchesFilter createBare(Jid address) { + return new ToMatchesFilter(address, true); + } + + /** + * Creates a filter matching on the "to" field. Compares the full version, if available, of to and filter + * address. + * + * @param address The address to filter for. If null is given, the stanza must not + * have a from address. + */ + public static ToMatchesFilter createFull(Jid address) { + return new ToMatchesFilter(address, false); + } + + @Override + protected Jid getAddressToCompare(Stanza stanza) { + return stanza.getTo(); + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java index 81e45e329..0cee4f7b0 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java @@ -46,7 +46,7 @@ import org.jivesoftware.smack.filter.NotFilter; import org.jivesoftware.smack.filter.StanzaFilter; import org.jivesoftware.smack.filter.StanzaExtensionFilter; import org.jivesoftware.smack.filter.StanzaTypeFilter; -import org.jivesoftware.smack.filter.ToFilter; +import org.jivesoftware.smack.filter.ToMatchesFilter; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Stanza; @@ -319,7 +319,7 @@ public class MultiUserChat { connection.addSyncStanzaListener(subjectListener, new AndFilter(fromRoomFilter, MessageWithSubjectFilter.INSTANCE, new NotFilter(MessageTypeFilter.ERROR))); connection.addSyncStanzaListener(declinesListener, DECLINE_FILTER); - connection.addPacketInterceptor(presenceInterceptor, new AndFilter(new ToFilter(room), + connection.addPacketInterceptor(presenceInterceptor, new AndFilter(ToMatchesFilter.create(room), StanzaTypeFilter.PRESENCE)); messageCollector = connection.createStanzaCollector(fromRoomGroupchatFilter);