JingleError.java

  1. /**
  2.  *
  3.  * Copyright 2003-2005 Jive Software, 2017-2021 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.Locale;

  19. import org.jivesoftware.smack.packet.XmlElement;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;

  21. public final class JingleError implements XmlElement {

  22.     public static String NAMESPACE = "urn:xmpp:jingle:errors:1";

  23.     public static final JingleError OUT_OF_ORDER = new JingleError("out-of-order");

  24.     public static final JingleError TIE_BREAK = new JingleError("tie-break");

  25.     public static final JingleError UNKNOWN_SESSION = new JingleError("unknown-session");

  26.     public static final JingleError UNSUPPORTED_INFO = new JingleError("unsupported-info");

  27.     private final String errorName;

  28.     /**
  29.      * Creates a new error with the specified code and errorName.
  30.      *
  31.      * @param errorName a name describing the error.
  32.      */
  33.     private JingleError(final String errorName) {
  34.         this.errorName = errorName;
  35.     }

  36.     /**
  37.      * Returns the name of the Jingle error.
  38.      *
  39.      * @return the name of the error.
  40.      */
  41.     public String getMessage() {
  42.         return errorName;
  43.     }

  44.     @Override
  45.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  46.         XmlStringBuilder xml = new XmlStringBuilder(this);
  47.         xml.closeEmptyElement();
  48.         return xml;
  49.     }

  50.     /**
  51.      * Returns a error instance associated with the String value.
  52.      *
  53.      * @param value the input String.
  54.      * @return the jingle error instance associated with the input String.
  55.      */
  56.     public static JingleError fromString(String value) {
  57.         value = value.toLowerCase(Locale.US);
  58.         switch (value) {
  59.         case "out-of-order":
  60.             return OUT_OF_ORDER;
  61.         case "unknown-session":
  62.             return UNKNOWN_SESSION;
  63.         case "tie-break":
  64.             return TIE_BREAK;
  65.         case "unsupported-info":
  66.             return UNSUPPORTED_INFO;
  67.         default:
  68.             throw new IllegalArgumentException();
  69.         }
  70.     }

  71.     @Override
  72.     public String toString() {
  73.         return getMessage();
  74.     }

  75.     @Override
  76.     public String getElementName() {
  77.         return errorName;
  78.     }

  79.     @Override
  80.     public String getNamespace() {
  81.         return NAMESPACE;
  82.     }

  83. }