001/** 002 * 003 * Copyright 2003-2014 Jive Software, 2017 Florian Schmaus. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.filter; 018 019import org.jivesoftware.smack.packet.Stanza; 020 021import org.jxmpp.jid.Jid; 022 023/** 024 * Filter for packets where the "from" field exactly matches a specified JID. If the specified 025 * address is a bare JID then the filter will match any address whose bare JID matches the 026 * specified JID. But if the specified address is a full JID then the filter will only match 027 * if the sender of the stanza(/packet) matches the specified resource. 028 * 029 * @author Gaston Dombiak 030 */ 031public final class FromMatchesFilter extends AbstractFromToMatchesFilter { 032 033 public final static FromMatchesFilter MATCH_NO_FROM_SET = create(null); 034 035 /** 036 * Creates a filter matching on the "from" field. The from address must be the same as the 037 * filter address. The second parameter specifies whether the full or the bare addresses are 038 * compared. 039 * 040 * @param address The address to filter for. If <code>null</code> is given, the stanza(/packet) must not 041 * have a from address. 042 * @param ignoreResourcepart 043 */ 044 public FromMatchesFilter(Jid address, boolean ignoreResourcepart) { 045 super(address, ignoreResourcepart); 046 } 047 048 /** 049 * Creates a filter matching on the "from" field. If the filter address is bare, compares 050 * the filter address with the bare from address. Otherwise, compares the filter address 051 * with the full from address. 052 * 053 * @param address The address to filter for. If <code>null</code> is given, the stanza must not 054 * have a from address. 055 */ 056 public static FromMatchesFilter create(Jid address) { 057 return new FromMatchesFilter(address, address != null ? address.hasNoResource() : false) ; 058 } 059 060 /** 061 * Creates a filter matching on the "from" field. Compares the bare version of from and filter 062 * address. 063 * 064 * @param address The address to filter for. If <code>null</code> is given, the stanza must not 065 * have a from address. 066 */ 067 public static FromMatchesFilter createBare(Jid address) { 068 return new FromMatchesFilter(address, true); 069 } 070 071 /** 072 * Creates a filter matching on the "from" field. Compares the full version, if available, of from and filter 073 * address. 074 * 075 * @param address The address to filter for. If <code>null</code> is given, the stanza must not 076 * have a from address. 077 */ 078 public static FromMatchesFilter createFull(Jid address) { 079 return new FromMatchesFilter(address, false); 080 } 081 082 @Override 083 protected Jid getAddressToCompare(Stanza stanza) { 084 return stanza.getFrom(); 085 } 086 087}