Friend.java

  1. /**
  2.  *
  3.  * Copyright © 2016-2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.iot.provisioning.element;

  18. import javax.xml.namespace.QName;

  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.packet.Message;
  21. import org.jivesoftware.smack.util.Objects;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;

  23. import org.jxmpp.jid.BareJid;

  24. public class Friend implements ExtensionElement {

  25.     public static final String ELEMENT = "friend";
  26.     public static final String NAMESPACE = Constants.IOT_PROVISIONING_NAMESPACE;
  27.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  28.     private final BareJid friend;

  29.     public Friend(BareJid friend) {
  30.         this.friend = Objects.requireNonNull(friend, "Friend must not be null");
  31.     }

  32.     @Override
  33.     public String getElementName() {
  34.         return ELEMENT;
  35.     }

  36.     @Override
  37.     public String getNamespace() {
  38.         return NAMESPACE;
  39.     }

  40.     @Override
  41.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  42.         XmlStringBuilder xml = new XmlStringBuilder(this);
  43.         xml.attribute("jid", friend);
  44.         xml.closeEmptyElement();
  45.         return xml;
  46.     }

  47.     public BareJid getFriend() {
  48.         return friend;
  49.     }

  50.     public static Friend from(Message message) {
  51.         return message.getExtension(Friend.class);
  52.     }
  53. }