Bind.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  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 org.jxmpp.jid.FullJid;

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

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

  33.     private final String resource;
  34.     private final FullJid jid;

  35.     public Bind(String resource, FullJid jid) {
  36.         super(ELEMENT, NAMESPACE);
  37.         this.resource = resource;
  38.         this.jid = jid;
  39.     }

  40.     public String getResource() {
  41.         return resource;
  42.     }

  43.     public FullJid getJid() {
  44.         return jid;
  45.     }

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

  51.     public static Bind newResult(FullJid jid) {
  52.         return new Bind(null, jid);
  53.     }

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

  61.     public static class Feature implements ExtensionElement {

  62.         public static final Feature INSTANCE = new Feature();

  63.         private Feature() {
  64.         }

  65.         @Override
  66.         public String getElementName() {
  67.             return ELEMENT;
  68.         }

  69.         @Override
  70.         public String getNamespace() {
  71.             return NAMESPACE;
  72.         }

  73.         @Override
  74.         public String toXML() {
  75.             return '<' + ELEMENT + " xmlns='" + NAMESPACE + "'/>";
  76.         }

  77.     }
  78. }