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