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