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 org.jivesoftware.smack.util.XmlStringBuilder;

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

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

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

  39.     public static class Feature implements ExtensionElement {

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

  41.         private final boolean optional;

  42.         public Feature(boolean optional) {
  43.             this.optional = optional;
  44.         }

  45.         public boolean isOptional() {
  46.             return optional;
  47.         }

  48.         @Override
  49.         public String getElementName() {
  50.             return ELEMENT;
  51.         }

  52.         @Override
  53.         public String getNamespace() {
  54.             return NAMESPACE;
  55.         }

  56.         @Override
  57.         public XmlStringBuilder toXML() {
  58.             XmlStringBuilder xml = new XmlStringBuilder(this);
  59.             if (optional) {
  60.                 xml.rightAngleBracket();
  61.                 xml.emptyElement(OPTIONAL_ELEMENT);
  62.                 xml.closeElement(this);
  63.             } else {
  64.                 xml.closeEmptyElement();
  65.             }
  66.             return xml;
  67.         }
  68.     }
  69. }