001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2015-2021 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 */
017
018package org.jivesoftware.smack.packet;
019
020import javax.xml.namespace.QName;
021
022import org.jxmpp.jid.EntityFullJid;
023import org.jxmpp.jid.parts.Resourcepart;
024
025/**
026 * IQ stanza used by Smack to bind a resource and to obtain the jid assigned by the server.
027 * There are two ways to bind a resource. One is simply sending an empty Bind stanza where the
028 * server will assign a new resource for this connection. The other option is to set a desired
029 * resource but the server may return a modified version of the sent resource.<p>
030 *
031 * For more information refer to the following
032 * <a href=http://www.xmpp.org/specs/rfc3920.html#bind>link</a>.
033 *
034 * @author Gaston Dombiak
035 */
036public final class Bind extends IQ {
037
038    public static final String ELEMENT = "bind";
039    public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-bind";
040
041    private final Resourcepart resource;
042    private final EntityFullJid jid;
043
044    private Bind(Resourcepart resource, EntityFullJid jid) {
045        super(ELEMENT, NAMESPACE);
046        this.resource = resource;
047        this.jid = jid;
048    }
049
050    public Resourcepart getResource() {
051        return resource;
052    }
053
054    public EntityFullJid getJid() {
055        return jid;
056    }
057
058    public static Bind newSet(Resourcepart resource) {
059        Bind bind = new Bind(resource, null);
060        bind.setType(IQ.Type.set);
061        return bind;
062    }
063
064    public static Bind newResult(EntityFullJid jid) {
065        return new Bind(null, jid);
066    }
067
068    @Override
069    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
070        xml.rightAngleBracket();
071        xml.optElement("resource", resource);
072        xml.optElement("jid", jid);
073        return xml;
074    }
075
076    public static final class Feature implements ExtensionElement {
077
078        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
079        public static final Feature INSTANCE = new Feature();
080
081        private Feature() {
082        }
083
084        @Override
085        public String getElementName() {
086            return ELEMENT;
087        }
088
089        @Override
090        public String getNamespace() {
091            return NAMESPACE;
092        }
093
094        @Override
095        public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
096            return '<' + ELEMENT + " xmlns='" + NAMESPACE + "'/>";
097        }
098
099    }
100}