Bind.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software, 2015-2021 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.smack.packet;

  18. import javax.xml.namespace.QName;

  19. import org.jxmpp.jid.EntityFullJid;
  20. import org.jxmpp.jid.parts.Resourcepart;

  21. /**
  22.  * IQ stanza used by Smack to bind a resource and to obtain the jid assigned by the server.
  23.  * There are two ways to bind a resource. One is simply sending an empty Bind stanza where the
  24.  * server will assign a new resource for this connection. The other option is to set a desired
  25.  * resource but the server may return a modified version of the sent resource.<p>
  26.  *
  27.  * For more information refer to the following
  28.  * <a href=http://www.xmpp.org/specs/rfc3920.html#bind>link</a>.
  29.  *
  30.  * @author Gaston Dombiak
  31.  */
  32. public final class Bind extends IQ {

  33.     public static final String ELEMENT = "bind";
  34.     public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-bind";

  35.     private final Resourcepart resource;
  36.     private final EntityFullJid jid;

  37.     private Bind(Resourcepart resource, EntityFullJid jid) {
  38.         super(ELEMENT, NAMESPACE);
  39.         this.resource = resource;
  40.         this.jid = jid;
  41.     }

  42.     public Resourcepart getResource() {
  43.         return resource;
  44.     }

  45.     public EntityFullJid getJid() {
  46.         return jid;
  47.     }

  48.     public static Bind newSet(Resourcepart resource) {
  49.         Bind bind = new Bind(resource, null);
  50.         bind.setType(IQ.Type.set);
  51.         return bind;
  52.     }

  53.     public static Bind newResult(EntityFullJid jid) {
  54.         return new Bind(null, jid);
  55.     }

  56.     @Override
  57.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  58.         xml.rightAngleBracket();
  59.         xml.optElement("resource", resource);
  60.         xml.optElement("jid", jid);
  61.         return xml;
  62.     }

  63.     public static final class Feature implements ExtensionElement {

  64.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
  65.         public static final Feature INSTANCE = new Feature();

  66.         private Feature() {
  67.         }

  68.         @Override
  69.         public String getElementName() {
  70.             return ELEMENT;
  71.         }

  72.         @Override
  73.         public String getNamespace() {
  74.             return NAMESPACE;
  75.         }

  76.         @Override
  77.         public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  78.             return '<' + ELEMENT + " xmlns='" + NAMESPACE + "'/>";
  79.         }

  80.     }
  81. }