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