FromMatchesFilter.java

  1. /**
  2.  *
  3.  * Copyright 2003-2014 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smack.filter;

  18. import org.jivesoftware.smack.packet.Stanza;
  19. import org.jxmpp.jid.Jid;

  20. /**
  21.  * Filter for packets where the "from" field exactly matches a specified JID. If the specified
  22.  * address is a bare JID then the filter will match any address whose bare JID matches the
  23.  * specified JID. But if the specified address is a full JID then the filter will only match
  24.  * if the sender of the packet matches the specified resource.
  25.  *
  26.  * @author Gaston Dombiak
  27.  */
  28. public class FromMatchesFilter implements StanzaFilter {

  29.     private final Jid address;

  30.     /**
  31.      * Flag that indicates if the checking will be done against bare JID addresses or full JIDs.
  32.      */
  33.     private final boolean ignoreResourcepart;

  34.     /**
  35.      * Creates a filter matching on the "from" field. The from address must be the same as the
  36.      * filter address. The second parameter specifies whether the full or the bare addresses are
  37.      * compared.
  38.      *
  39.      * @param address The address to filter for. If <code>null</code> is given, the packet must not
  40.      *        have a from address.
  41.      * @param ignoreResourcepart
  42.      */
  43.     public FromMatchesFilter(Jid address, boolean ignoreResourcepart) {
  44.         if (address != null && ignoreResourcepart) {
  45.             this.address = address.withoutResource();
  46.         }
  47.         else {
  48.             this.address = address;
  49.         }
  50.         this.ignoreResourcepart = ignoreResourcepart;
  51.     }

  52.     /**
  53.      * Creates a filter matching on the "from" field. If the filter address is bare, compares
  54.      * the filter address with the bare from address. Otherwise, compares the filter address
  55.      * with the full from address.
  56.      *
  57.      * @param address The address to filter for. If <code>null</code> is given, the packet must not
  58.      *        have a from address.
  59.      */
  60.     public static FromMatchesFilter create(Jid address) {
  61.         return new FromMatchesFilter(address, address.hasNoResource()) ;
  62.     }

  63.     /**
  64.      * Creates a filter matching on the "from" field. Compares the bare version of from and filter
  65.      * address.
  66.      *
  67.      * @param address The address to filter for. If <code>null</code> is given, the packet must not
  68.      *        have a from address.
  69.      */
  70.     public static FromMatchesFilter createBare(Jid address) {
  71.         address = (address == null) ? null : address;
  72.         return new FromMatchesFilter(address, true);
  73.     }


  74.     /**
  75.      * Creates a filter matching on the "from" field. Compares the full version of from and filter
  76.      * address.
  77.      *
  78.      * @param address The address to filter for. If <code>null</code> is given, the packet must not
  79.      *        have a from address.
  80.      */
  81.     public static FromMatchesFilter createFull(Jid address) {
  82.         return new FromMatchesFilter(address, false);
  83.     }

  84.     public boolean accept(Stanza packet) {
  85.         Jid from = packet.getFrom();
  86.         if (from == null) {
  87.             return address == null;
  88.         }

  89.         if (ignoreResourcepart) {
  90.             from = from.withoutResource();
  91.         }
  92.         return from.equals(address);
  93.     }

  94.     public String toString() {
  95.         String matchMode = ignoreResourcepart ? "ignoreResourcepart" : "full";
  96.         return getClass().getSimpleName() + " (" + matchMode + "): " + address;
  97.     }
  98. }