001/**
002 *
003 * Copyright 2016-2017 Fernando Ramirez, Florian Schmaus
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 java.util.Collections;
020import java.util.List;
021
022import org.jivesoftware.smack.packet.IQ;
023
024import org.jivesoftware.smackx.blocking.BlockingCommandManager;
025
026import org.jxmpp.jid.Jid;
027
028/**
029 * Unblock contact IQ class.
030 *
031 * @author Fernando Ramirez
032 * @author Florian Schmaus
033 * @see <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
034 *      Command</a>
035 */
036public class UnblockContactsIQ extends IQ {
037
038    /**
039     * unblock element.
040     */
041    public static final String ELEMENT = "unblock";
042
043    /**
044     * the IQ NAMESPACE.
045     */
046    public static final String NAMESPACE = BlockingCommandManager.NAMESPACE;
047
048    private final List<Jid> jids;
049
050    /**
051     * Unblock contacts IQ constructor.
052     *
053     * @param jids TODO javadoc me please
054     */
055    @SuppressWarnings("this-escape")
056    public UnblockContactsIQ(List<Jid> jids) {
057        super(ELEMENT, NAMESPACE);
058        this.setType(Type.set);
059        if (jids != null) {
060            this.jids = Collections.unmodifiableList(jids);
061        } else {
062            this.jids = null;
063        }
064    }
065
066    /**
067     * Constructs a new unblock IQ which will unblock <b>all</b> JIDs.
068     */
069    public UnblockContactsIQ() {
070        this(null);
071    }
072
073    /**
074     * Get the JIDs. This may return null, which means that all JIDs should be or where unblocked.
075     *
076     * @return the list of JIDs or <code>null</code>.
077     */
078    public List<Jid> getJids() {
079        return jids;
080    }
081
082    @Override
083    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
084        if (jids == null) {
085            xml.setEmptyElement();
086            return xml;
087        }
088
089        xml.rightAngleBracket();
090        for (Jid jid : jids) {
091            xml.halfOpenElement("item");
092            xml.attribute("jid", jid);
093            xml.closeEmptyElement();
094        }
095
096        return xml;
097    }
098
099}