JingleReason.java

  1. /**
  2.  *
  3.  * Copyright 2017-2022 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.smackx.jingle.element;

  18. import java.util.HashMap;
  19. import java.util.Map;

  20. import org.jivesoftware.smack.packet.XmlElement;
  21. import org.jivesoftware.smack.packet.XmlEnvironment;
  22. import org.jivesoftware.smack.util.StringUtils;
  23. import org.jivesoftware.smack.util.XmlStringBuilder;

  24. /**
  25.  * The Jingle 'reason' element.
  26.  *
  27.  * @see <a href="https://xmpp.org/extensions/xep-0166.html#def-reason">XEP-0166 ยง 7.4</a>
  28.  *
  29.  */
  30. public class JingleReason implements XmlElement {

  31.     public static final String ELEMENT = "reason";
  32.     public static final String NAMESPACE = Jingle.NAMESPACE;
  33.     public static final String TEXT_ELEMENT = "text";

  34.     public static AlternativeSession AlternativeSession(String sessionId) {
  35.         return new AlternativeSession(sessionId);
  36.     }

  37.     public static final JingleReason Busy = new JingleReason(Reason.busy);
  38.     public static final JingleReason Cancel = new JingleReason(Reason.cancel);
  39.     public static final JingleReason ConnectivityError = new JingleReason(Reason.connectivity_error);
  40.     public static final JingleReason Decline = new JingleReason(Reason.decline);
  41.     public static final JingleReason Expired = new JingleReason(Reason.expired);
  42.     public static final JingleReason FailedApplication = new JingleReason(Reason.failed_application);
  43.     public static final JingleReason FailedTransport = new JingleReason(Reason.failed_transport);
  44.     public static final JingleReason GeneralError = new JingleReason(Reason.general_error);
  45.     public static final JingleReason Gone = new JingleReason(Reason.gone);
  46.     public static final JingleReason IncompatibleParameters = new JingleReason(Reason.incompatible_parameters);
  47.     public static final JingleReason MediaError = new JingleReason(Reason.media_error);
  48.     public static final JingleReason SecurityError = new JingleReason(Reason.security_error);
  49.     public static final JingleReason Success = new JingleReason(Reason.success);
  50.     public static final JingleReason Timeout = new JingleReason(Reason.timeout);
  51.     public static final JingleReason UnsupportedApplications = new JingleReason(Reason.unsupported_applications);
  52.     public static final JingleReason UnsupportedTransports = new JingleReason(Reason.unsupported_transports);

  53.     public enum Reason {
  54.         alternative_session,
  55.         busy,
  56.         cancel,
  57.         connectivity_error,
  58.         decline,
  59.         expired,
  60.         failed_application,
  61.         failed_transport,
  62.         general_error,
  63.         gone,
  64.         incompatible_parameters,
  65.         media_error,
  66.         security_error,
  67.         success,
  68.         timeout,
  69.         unsupported_applications,
  70.         unsupported_transports,
  71.         ;

  72.         static final Map<String, Reason> LUT = new HashMap<>(Reason.values().length);

  73.         static {
  74.             for (Reason reason : Reason.values()) {
  75.                 LUT.put(reason.toString(), reason);
  76.             }
  77.         }

  78.         final String asString;

  79.         Reason() {
  80.             asString = name().replace('_', '-');
  81.         }

  82.         @Override
  83.         public String toString() {
  84.             return asString;
  85.         }

  86.         public static Reason fromString(String string) {
  87.             Reason reason = LUT.get(string);
  88.             if (reason == null) {
  89.                 throw new IllegalArgumentException("Unknown reason: " + string);
  90.             }
  91.             return reason;
  92.         }
  93.     }

  94.     protected final Reason reason;
  95.     private final String text;
  96.     private final XmlElement element;

  97.     public JingleReason(Reason reason) {
  98.         this(reason, null, null);
  99.     }

  100.     public JingleReason(Reason reason, String text, XmlElement element) {
  101.         this.reason = reason;
  102.         this.text = text;
  103.         this.element = element;
  104.     }

  105.     @Override
  106.     public String getElementName() {
  107.         return ELEMENT;
  108.     }

  109.     @Override
  110.     public String getNamespace() {
  111.         return NAMESPACE;
  112.     }

  113.     /**
  114.      * An optional text that provides human-readable information about the reason for the action.
  115.      *
  116.      * @return a human-readable text with information regarding this reason or <code>null</code>.
  117.      * @since 4.4.5
  118.      */
  119.     public String getText() {
  120.         return text;
  121.     }

  122.     /**
  123.      * An optional element that provides more detailed machine-readable information about the reason for the action.
  124.      *
  125.      * @return an element with machine-readable information about this reason or <code>null</code>.
  126.      * @since 4.4.5
  127.      */
  128.     public XmlElement getElement() {
  129.         return element;
  130.     }

  131.     @Override
  132.     public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
  133.         XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment);
  134.         xml.rightAngleBracket();

  135.         xml.emptyElement(reason);
  136.         xml.optElement(TEXT_ELEMENT, text);
  137.         xml.optAppend(element);

  138.         xml.closeElement(this);
  139.         return xml;
  140.     }

  141.     public Reason asEnum() {
  142.         return reason;
  143.     }


  144.     public static class AlternativeSession extends JingleReason {

  145.         public static final String SID = "sid";
  146.         private final String sessionId;

  147.         public AlternativeSession(String sessionId) {
  148.             this(sessionId, null, null);
  149.         }

  150.         public AlternativeSession(String sessionId, String text, XmlElement element) {
  151.             super(Reason.alternative_session, text, element);
  152.             if (StringUtils.isNullOrEmpty(sessionId)) {
  153.                 throw new NullPointerException("SessionID must not be null or empty.");
  154.             }
  155.             this.sessionId = sessionId;
  156.         }

  157.         @Override
  158.         public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  159.             XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
  160.             xml.rightAngleBracket();

  161.             xml.openElement(reason.asString);
  162.             xml.openElement(SID);
  163.             xml.append(sessionId);
  164.             xml.closeElement(SID);
  165.             xml.closeElement(reason.asString);

  166.             xml.closeElement(this);
  167.             return xml;
  168.         }

  169.         public String getAlternativeSessionId() {
  170.             return sessionId;
  171.         }
  172.     }
  173. }