001/**
002 *
003 * Copyright © 2016 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.push_notifications.element;
018
019import org.jivesoftware.smack.packet.ExtensionElement;
020import org.jivesoftware.smack.packet.Message;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022
023import org.jivesoftware.smackx.pubsub.packet.PubSub;
024
025import org.jxmpp.jid.Jid;
026
027/**
028 * Push Notifications elements.
029 *
030 * @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
031 *      Notifications</a>
032 * @author Fernando Ramirez
033 *
034 */
035public class PushNotificationsElements {
036
037    public static final String NAMESPACE = "urn:xmpp:push:0";
038
039    public static class RemoteDisablingExtension implements ExtensionElement {
040
041        public static final String NAMESPACE = PubSub.NAMESPACE;
042        public static final String ELEMENT = PubSub.ELEMENT;
043
044        private final String node;
045        private final Jid userJid;
046
047        public RemoteDisablingExtension(String node, Jid userJid) {
048            this.node = node;
049            this.userJid = userJid;
050        }
051
052        @Override
053        public String getElementName() {
054            return PubSub.ELEMENT;
055        }
056
057        @Override
058        public String getNamespace() {
059            return PubSub.NAMESPACE;
060        }
061
062        /**
063         * Get the node.
064         *
065         * @return the node
066         */
067        public String getNode() {
068            return node;
069        }
070
071        /**
072         * Get the user JID.
073         *
074         * @return the user JID
075         */
076        public Jid getUserJid() {
077            return userJid;
078        }
079
080        @Override
081        public CharSequence toXML(String enclosingNamespace) {
082            XmlStringBuilder xml = new XmlStringBuilder(this);
083
084            xml.attribute("node", node);
085            xml.rightAngleBracket();
086
087            xml.halfOpenElement("affiliation");
088            xml.attribute("jid", userJid);
089            xml.attribute("affiliation", "none");
090            xml.closeEmptyElement();
091
092            xml.closeElement(this);
093            return xml;
094        }
095
096        public static RemoteDisablingExtension from(Message message) {
097            return message.getExtension(ELEMENT, NAMESPACE);
098        }
099
100    }
101
102}