BlockedErrorExtension.java

  1. /**
  2.  *
  3.  * Copyright 2016-2017 Fernando Ramirez
  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.smackx.blocking.element;

  18. import org.jivesoftware.smack.packet.ExtensionElement;
  19. import org.jivesoftware.smack.packet.Message;
  20. import org.jivesoftware.smack.packet.StanzaError;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;

  22. import org.jivesoftware.smackx.blocking.BlockingCommandManager;

  23. /**
  24.  * Blocked error extension class.
  25.  *
  26.  * @author Fernando Ramirez
  27.  * @see <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
  28.  *      Command</a>
  29.  */
  30. public class BlockedErrorExtension implements ExtensionElement {

  31.     public static final String ELEMENT = "blocked";
  32.     public static final String NAMESPACE = BlockingCommandManager.NAMESPACE + ":errors";

  33.     @Override
  34.     public String getElementName() {
  35.         return ELEMENT;
  36.     }

  37.     @Override
  38.     public String getNamespace() {
  39.         return NAMESPACE;
  40.     }

  41.     @Override
  42.     public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  43.         XmlStringBuilder xml = new XmlStringBuilder(this);
  44.         xml.closeEmptyElement();
  45.         return xml;
  46.     }

  47.     public static BlockedErrorExtension from(Message message) {
  48.         StanzaError error = message.getError();
  49.         if (error == null) {
  50.             return null;
  51.         }
  52.         return error.getExtension(ELEMENT, NAMESPACE);
  53.     }

  54.     /**
  55.      * Check if a message contains a BlockedErrorExtension, which means that a
  56.      * message was blocked because the JID blocked the sender, and that was
  57.      * reflected back as an error message.
  58.      *
  59.      * @param message TODO javadoc me please
  60.      * @return true if the message contains a BlockedErrorExtension, false if
  61.      *         not
  62.      */
  63.     public static boolean isInside(Message message) {
  64.         return from(message) != null;
  65.     }

  66. }