StreamOpen.java

  1. /**
  2.  *
  3.  * Copyright © 2014-2020 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.XmlStringBuilder;

  19. /**
  20.  * The stream open <b>tag</b>.
  21.  */
  22. public final class StreamOpen extends AbstractStreamOpen {
  23.     public static final String UNPREFIXED_ELEMENT = "stream";

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

  25.     public StreamOpen(CharSequence to) {
  26.        this(to, null, null, null, StreamContentNamespace.client);
  27.     }

  28.     public StreamOpen(CharSequence to, CharSequence from, String id) {
  29.         this(to, from, id, "en", StreamContentNamespace.client);
  30.     }

  31.     public StreamOpen(CharSequence to, CharSequence from, String id, String lang) {
  32.         super(to, from, id, lang);
  33.     }

  34.     public StreamOpen(CharSequence to, CharSequence from, String id, String lang, StreamContentNamespace ns) {
  35.         super(to, from, id, lang, ns);
  36.     }

  37.     @Override
  38.     public String getNamespace() {
  39.         return contentNamespace;
  40.     }

  41.     @Override
  42.     public String getElementName() {
  43.         return ELEMENT;
  44.     }

  45.     @Override
  46.     public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
  47.         XmlStringBuilder xml = new XmlStringBuilder();
  48.         xml.halfOpenElement(getElementName());

  49.         String namespace = CLIENT_NAMESPACE;
  50.         // We always want to state 'xmlns' for stream open tags.
  51.         if (enclosingXmlEnvironment != null) {
  52.             namespace = enclosingXmlEnvironment.getEffectiveNamespaceOrUse(CLIENT_NAMESPACE);
  53.         }
  54.         xml.attribute("xmlns", namespace);

  55.         xml.attribute("to", to);
  56.         xml.attribute("xmlns:stream", "http://etherx.jabber.org/streams");
  57.         xml.attribute("version", VERSION);
  58.         xml.optAttribute("from", from);
  59.         xml.optAttribute("id", id);
  60.         xml.xmllangAttribute(lang);
  61.         xml.rightAngleBracket();
  62.         return xml;
  63.     }

  64.     public enum StreamContentNamespace {
  65.         client,
  66.         server,
  67.     }
  68. }