Session.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 javax.xml.namespace.QName;

  19. import org.jivesoftware.smack.util.XmlStringBuilder;

  20. /**
  21.  * IQ stanza that will be sent to the server to establish a session.<p>
  22.  *
  23.  * If a server supports sessions, it MUST include a <i>session</i> element in the
  24.  * stream features it advertises to a client after the completion of stream authentication.
  25.  * Upon being informed that session establishment is required by the server the client MUST
  26.  * establish a session if it desires to engage in instant messaging and presence functionality.<p>
  27.  *
  28.  * For more information refer to the following
  29.  * <a href=http://www.xmpp.org/specs/rfc3921.html#session>link</a>.
  30.  *
  31.  * @author Gaston Dombiak
  32.  */
  33. public class Session extends SimpleIQ {

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

  36.     public Session() {
  37.         super(ELEMENT, NAMESPACE);
  38.         setType(IQ.Type.set);
  39.     }

  40.     public static class Feature implements ExtensionElement {

  41.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  42.         public static final String OPTIONAL_ELEMENT = "optional";

  43.         private final boolean optional;

  44.         public Feature(boolean optional) {
  45.             this.optional = optional;
  46.         }

  47.         public boolean isOptional() {
  48.             return optional;
  49.         }

  50.         @Override
  51.         public String getElementName() {
  52.             return ELEMENT;
  53.         }

  54.         @Override
  55.         public String getNamespace() {
  56.             return NAMESPACE;
  57.         }

  58.         @Override
  59.         public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  60.             XmlStringBuilder xml = new XmlStringBuilder(this);
  61.             if (optional) {
  62.                 xml.rightAngleBracket();
  63.                 xml.emptyElement(OPTIONAL_ELEMENT);
  64.                 xml.closeElement(this);
  65.             } else {
  66.                 xml.closeEmptyElement();
  67.             }
  68.             return xml;
  69.         }
  70.     }
  71. }