001/**
002 *
003 * Copyright 2016-2017 Fernando Ramirez
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 */
017package org.jivesoftware.smackx.blocking.element;
018
019import org.jivesoftware.smack.packet.ExtensionElement;
020import org.jivesoftware.smack.packet.Message;
021import org.jivesoftware.smack.packet.StanzaError;
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024import org.jivesoftware.smackx.blocking.BlockingCommandManager;
025
026/**
027 * Blocked error extension class.
028 *
029 * @author Fernando Ramirez
030 * @see <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
031 *      Command</a>
032 */
033public class BlockedErrorExtension implements ExtensionElement {
034
035    public static final String ELEMENT = "blocked";
036    public static final String NAMESPACE = BlockingCommandManager.NAMESPACE + ":errors";
037
038    @Override
039    public String getElementName() {
040        return ELEMENT;
041    }
042
043    @Override
044    public String getNamespace() {
045        return NAMESPACE;
046    }
047
048    @Override
049    public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
050        XmlStringBuilder xml = new XmlStringBuilder(this);
051        xml.closeEmptyElement();
052        return xml;
053    }
054
055    public static BlockedErrorExtension from(Message message) {
056        StanzaError error = message.getError();
057        if (error == null) {
058            return null;
059        }
060        return error.getExtension(ELEMENT, NAMESPACE);
061    }
062
063    /**
064     * Check if a message contains a BlockedErrorExtension, which means that a
065     * message was blocked because the JID blocked the sender, and that was
066     * reflected back as an error message.
067     *
068     * @param message TODO javadoc me please
069     * @return true if the message contains a BlockedErrorExtension, false if
070     *         not
071     */
072    public static boolean isInside(Message message) {
073        return from(message) != null;
074    }
075
076}