Failure.java

  1. /**
  2.  *
  3.  * Copyright 2018-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.compress.packet;

  18. import java.util.Objects;

  19. import javax.xml.namespace.QName;

  20. import org.jivesoftware.smack.packet.Nonza;
  21. import org.jivesoftware.smack.packet.StanzaError;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;

  23. public class Failure implements Nonza {

  24.     public static final String ELEMENT = "failure";
  25.     public static final String NAMESPACE = Compress.NAMESPACE;
  26.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  27.     public enum CompressFailureError {
  28.         setup_failed,
  29.         processing_failed,
  30.         unsupported_method,
  31.         ;

  32.         private final String compressFailureError;

  33.         CompressFailureError() {
  34.             compressFailureError = name().replace('_', '-');
  35.         }

  36.         @Override
  37.         public String toString() {
  38.             return compressFailureError;
  39.         }
  40.     }

  41.     private final CompressFailureError compressFailureError;
  42.     private final StanzaError stanzaError;

  43.     public Failure(CompressFailureError compressFailureError) {
  44.         this(compressFailureError, null);
  45.     }

  46.     public Failure(CompressFailureError compressFailureError, StanzaError stanzaError) {
  47.         this.compressFailureError = Objects.requireNonNull(compressFailureError);
  48.         this.stanzaError = stanzaError;
  49.     }

  50.     @Override
  51.     public String getElementName() {
  52.         return ELEMENT;
  53.     }

  54.     @Override
  55.     public String getNamespace() {
  56.         return NAMESPACE;
  57.     }

  58.     public CompressFailureError getCompressFailureError() {
  59.         return compressFailureError;
  60.     }

  61.     public StanzaError getStanzaError() {
  62.         return stanzaError;
  63.     }

  64.     @Override
  65.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  66.         XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
  67.         xml.rightAngleBracket();

  68.         xml.emptyElement(compressFailureError);
  69.         xml.optElement(stanzaError);

  70.         xml.closeElement(this);
  71.         return xml;
  72.     }
  73. }