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    public UnblockContactsIQ(List<Jid> jids) {
056        super(ELEMENT, NAMESPACE);
057        this.setType(Type.set);
058        if (jids != null) {
059            this.jids = Collections.unmodifiableList(jids);
060        } else {
061            this.jids = null;
062        }
063    }
064
065    /**
066     * Constructs a new unblock IQ which will unblock <b>all</b> JIDs.
067     */
068    public UnblockContactsIQ() {
069        this(null);
070    }
071
072    /**
073     * Get the JIDs. This may return null, which means that all JIDs should be or where unblocked.
074     *
075     * @return the list of JIDs or <code>null</code>.
076     */
077    public List<Jid> getJids() {
078        return jids;
079    }
080
081    @Override
082    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
083        if (jids == null) {
084            xml.setEmptyElement();
085            return xml;
086        }
087
088        xml.rightAngleBracket();
089        for (Jid jid : jids) {
090            xml.halfOpenElement("item");
091            xml.attribute("jid", jid);
092            xml.closeEmptyElement();
093        }
094
095        return xml;
096    }
097
098}