001/**
002 *
003 * Copyright 2003-2014 Jive Software.
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 */
017
018package org.jivesoftware.smack.filter;
019
020import java.util.Locale;
021
022import org.jivesoftware.smack.packet.Stanza;
023import org.jxmpp.util.XmppStringUtils;
024
025/**
026 * Filter for packets where the "from" field exactly matches a specified JID. If the specified
027 * address is a bare JID then the filter will match any address whose bare JID matches the
028 * specified JID. But if the specified address is a full JID then the filter will only match
029 * if the sender of the stanza(/packet) matches the specified resource.
030 *
031 * @author Gaston Dombiak
032 */
033public class FromMatchesFilter implements StanzaFilter {
034
035    private final String address;
036
037    /**
038     * Flag that indicates if the checking will be done against bare JID addresses or full JIDs.
039     */
040    private final boolean matchBareJID;
041
042    /**
043     * Creates a filter matching on the "from" field. The from address must be the same as the
044     * filter address. The second parameter specifies whether the full or the bare addresses are
045     * compared.
046     *
047     * @param address The address to filter for. If <code>null</code> is given, the stanza(/packet) must not
048     *        have a from address.
049     * @param matchBare
050     */
051    public FromMatchesFilter(String address, boolean matchBare) {
052        this.address = (address == null) ? null : address.toLowerCase(Locale.US);
053        matchBareJID = matchBare;
054    }
055
056    /**
057     * Creates a filter matching on the "from" field. If the filter address is bare, compares
058     * the filter address with the bare from address. Otherwise, compares the filter address
059     * with the full from address.
060     *
061     * @param address The address to filter for. If <code>null</code> is given, the stanza(/packet) must not
062     *        have a from address.
063     */
064    public static FromMatchesFilter create(String address) {
065        return new FromMatchesFilter(address, "".equals(XmppStringUtils.parseResource(address))) ;
066    }
067
068    /**
069     * Creates a filter matching on the "from" field. Compares the bare version of from and filter
070     * address.
071     *
072     * @param address The address to filter for. If <code>null</code> is given, the stanza(/packet) must not
073     *        have a from address.
074     */
075    public static FromMatchesFilter createBare(String address) {
076        address = (address == null) ? null : XmppStringUtils.parseBareJid(address);
077        return new FromMatchesFilter(address, true);
078    }
079
080
081    /**
082     * Creates a filter matching on the "from" field. Compares the full version of from and filter
083     * address.
084     *
085     * @param address The address to filter for. If <code>null</code> is given, the stanza(/packet) must not
086     *        have a from address.
087     */
088    public static FromMatchesFilter createFull(String address) {
089        return new FromMatchesFilter(address, false);
090    }
091
092    public boolean accept(Stanza packet) {
093        String from = packet.getFrom();
094        if (from == null) {
095            return address == null;
096        }
097        // Simplest form of NAMEPREP/STRINGPREP
098        from = from.toLowerCase(Locale.US);
099        if (matchBareJID) {
100            from = XmppStringUtils.parseBareJid(from);
101        }
102        return from.equals(address);
103    }
104
105    public String toString() {
106        String matchMode = matchBareJID ? "bare" : "full";
107        return getClass().getSimpleName() + " (" + matchMode + "): " + address;
108    }
109}