StreamOpen.java

  1. /**
  2.  *
  3.  * Copyright © 2014 Florian Schmaus
  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.StringUtils;
  19. import org.jivesoftware.smack.util.XmlStringBuilder;

  20. /**
  21.  *
  22.  */
  23. public class StreamOpen extends FullStreamElement {

  24.     public static final String ELEMENT = "stream:stream";

  25.     public static final String CLIENT_NAMESPACE = "jabber:client";
  26.     public static final String SERVER_NAMESPACE = "jabber:server";

  27.     /**
  28.      * RFC 6120 § 4.7.5
  29.      */
  30.     public static final String VERSION = "1.0";

  31.     /**
  32.      * RFC 6120 § 4.7.1
  33.      */
  34.     private final String from;

  35.     /**
  36.      * RFC 6120 § 4.7.2
  37.      */
  38.     private final String to;

  39.     /**
  40.      * RFC 6120 § 4.7.3
  41.      */
  42.     private final String id;

  43.     /**
  44.      * RFC 6120 § 4.7.4
  45.      */
  46.     private final String lang;

  47.     /**
  48.      * RFC 6120 § 4.8.2
  49.      */
  50.     private final String contentNamespace;

  51.     public StreamOpen(CharSequence to) {
  52.        this(to, null, null, null, StreamContentNamespace.client);
  53.     }

  54.     public StreamOpen(CharSequence to, CharSequence from, String id) {
  55.         this(to, from, id, "en", StreamContentNamespace.client);
  56.     }

  57.     public StreamOpen(CharSequence to, CharSequence from, String id, String lang, StreamContentNamespace ns) {
  58.         this.to = StringUtils.maybeToString(to);
  59.         this.from = StringUtils.maybeToString(from);
  60.         this.id = id;
  61.         this.lang = lang;
  62.         switch (ns) {
  63.         case client:
  64.             this.contentNamespace = CLIENT_NAMESPACE;
  65.             break;
  66.         case server:
  67.             this.contentNamespace = SERVER_NAMESPACE;
  68.             break;
  69.         default:
  70.             throw new IllegalStateException();
  71.         }
  72.     }

  73.     @Override
  74.     public String getNamespace() {
  75.         return contentNamespace;
  76.     }

  77.     @Override
  78.     public String getElementName() {
  79.         return ELEMENT;
  80.     }

  81.     @Override
  82.     public XmlStringBuilder toXML() {
  83.         XmlStringBuilder xml = new XmlStringBuilder(this);
  84.         xml.attribute("to", to);
  85.         xml.attribute("xmlns:stream", "http://etherx.jabber.org/streams");
  86.         xml.attribute("version", VERSION);
  87.         xml.optAttribute("from", from);
  88.         xml.optAttribute("id", id);
  89.         xml.xmllangAttribute(lang);
  90.         xml.rightAngleBracket();
  91.         return xml;
  92.     }

  93.     public enum StreamContentNamespace {
  94.         client,
  95.         server;
  96.     }
  97. }