PacketExtensionFilter.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 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.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.util.StringUtils;

  21. /**
  22.  * Filters for packets with a particular type of packet extension.
  23.  *
  24.  * @author Matt Tucker
  25.  * @deprecated use {@link StanzaExtensionFilter} instead.
  26.  */
  27. @Deprecated
  28. public class PacketExtensionFilter implements StanzaFilter {

  29.     private final String elementName;
  30.     private final String namespace;

  31.     /**
  32.      * Creates a new packet extension filter. Packets will pass the filter if
  33.      * they have a packet extension that matches the specified element name
  34.      * and namespace.
  35.      *
  36.      * @param elementName the XML element name of the packet extension.
  37.      * @param namespace the XML namespace of the packet extension.
  38.      */
  39.     public PacketExtensionFilter(String elementName, String namespace) {
  40.         StringUtils.requireNotNullOrEmpty(namespace, "namespace must not be null or empty");

  41.         this.elementName = elementName;
  42.         this.namespace = namespace;
  43.     }

  44.     /**
  45.      * Creates a new packet extension filter. Packets will pass the filter if they have a packet
  46.      * extension that matches the specified namespace.
  47.      *
  48.      * @param namespace the XML namespace of the packet extension.
  49.      */
  50.     public PacketExtensionFilter(String namespace) {
  51.         this(null, namespace);
  52.     }

  53.     /**
  54.      * Creates a new packet extension filter for the given packet extension.
  55.      *
  56.      * @param packetExtension
  57.      */
  58.     public PacketExtensionFilter(ExtensionElement packetExtension) {
  59.         this(packetExtension.getElementName(), packetExtension.getNamespace());
  60.     }

  61.     public boolean accept(Stanza packet) {
  62.         return packet.hasExtension(elementName, namespace);
  63.     }

  64.     @Override
  65.     public String toString() {
  66.         return getClass().getSimpleName() + ": element=" + elementName + " namespace=" + namespace;
  67.     }
  68. }