AbstractStreamOpen.java

  1. /**
  2.  *
  3.  * Copyright 2020-2021 Florian Schmaus, 2020 Aditya Borikar
  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.XMPPConnection;
  19. import org.jivesoftware.smack.packet.StreamOpen.StreamContentNamespace;
  20. import org.jivesoftware.smack.util.StringUtils;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;

  22. /**
  23.  * AbstractStreamOpen is actually a {@link TopLevelStreamElement}, however we
  24.  * implement {@link Nonza} here. This is because, {@link XMPPConnection} doesn't
  25.  * yet support sending {@link TopLevelStreamElement} directly and the same can only
  26.  * be achieved through {@link XMPPConnection#sendNonza(Nonza)}.
  27.  */
  28. public abstract class AbstractStreamOpen implements Nonza {
  29.     public static final String ETHERX_JABBER_STREAMS_NAMESPACE = "http://etherx.jabber.org/streams";
  30.     public static final String CLIENT_NAMESPACE = "jabber:client";
  31.     public static final String SERVER_NAMESPACE = "jabber:server";

  32.     /**
  33.      * RFC 6120 § 4.7.5.
  34.      */
  35.     public static final String VERSION = "1.0";

  36.     /**
  37.      * RFC 6120 § 4.7.1.
  38.      */
  39.     protected final String from;

  40.     /**
  41.      * RFC 6120 § 4.7.2.
  42.      */
  43.     protected final String to;

  44.     /**
  45.      * RFC 6120 § 4.7.3.
  46.      */
  47.     protected final String id;

  48.     /**
  49.      * RFC 6120 § 4.7.4.
  50.      */
  51.     protected final String lang;

  52.     /**
  53.      * RFC 6120 § 4.8.2.
  54.      */
  55.     protected final String contentNamespace;

  56.     public AbstractStreamOpen(CharSequence to, CharSequence from, String id, String lang) {
  57.         this(to, from, id, lang, StreamContentNamespace.client);
  58.     }

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

  75.     protected final void addCommonAttributes(XmlStringBuilder xml) {
  76.         xml.optAttribute("to", to);
  77.         xml.optAttribute("version", VERSION);
  78.     }
  79. }