1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-10-31 17:25:58 +01:00

Introduce ToMatchesFilter

and refactor the FromMatchesFilter into AbstractFromToMatchesFilter.
This commit is contained in:
Florian Schmaus 2017-01-29 11:08:16 +01:00
parent 51378aebee
commit 5d0dd49e61
5 changed files with 159 additions and 38 deletions

View file

@ -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 <code>null</code> is given, then
* {@link #getAddressToCompare(Stanza)} must also return <code>null</code> 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;
}
}

View file

@ -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 <code>null</code> is given, the stanza(/packet) must not
* @param address The address to filter for. If <code>null</code> 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 <code>null</code> is given, the stanza(/packet) must not
* @param address The address to filter for. If <code>null</code> 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 <code>null</code> is given, the stanza(/packet) must not
* @param address The address to filter for. If <code>null</code> 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;
@Override
protected Jid getAddressToCompare(Stanza stanza) {
return stanza.getFrom();
}
if (ignoreResourcepart) {
from = from.asBareJid();
}
return from.equals(address);
}
public String toString() {
String matchMode = ignoreResourcepart ? "ignoreResourcepart" : "full";
return getClass().getSimpleName() + " (" + matchMode + "): " + address;
}
}

View file

@ -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;

View file

@ -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 <code>null</code> 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 <code>null</code> 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 <code>null</code> 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();
}
}

View file

@ -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);