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