001/**
002 *
003 * Copyright 2018-2020 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.compress.packet;
018
019import java.util.Objects;
020
021import javax.xml.namespace.QName;
022
023import org.jivesoftware.smack.packet.Nonza;
024import org.jivesoftware.smack.packet.StanzaError;
025import org.jivesoftware.smack.util.XmlStringBuilder;
026
027public class Failure implements Nonza {
028
029    public static final String ELEMENT = "failure";
030    public static final String NAMESPACE = Compress.NAMESPACE;
031    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
032
033    public enum CompressFailureError {
034        setup_failed,
035        processing_failed,
036        unsupported_method,
037        ;
038
039        private final String compressFailureError;
040
041        CompressFailureError() {
042            compressFailureError = name().replace('_', '-');
043        }
044
045        @Override
046        public String toString() {
047            return compressFailureError;
048        }
049    }
050
051    private final CompressFailureError compressFailureError;
052    private final StanzaError stanzaError;
053
054    public Failure(CompressFailureError compressFailureError) {
055        this(compressFailureError, null);
056    }
057
058    public Failure(CompressFailureError compressFailureError, StanzaError stanzaError) {
059        this.compressFailureError = Objects.requireNonNull(compressFailureError);
060        this.stanzaError = stanzaError;
061    }
062
063    @Override
064    public String getElementName() {
065        return ELEMENT;
066    }
067
068    @Override
069    public String getNamespace() {
070        return NAMESPACE;
071    }
072
073    public CompressFailureError getCompressFailureError() {
074        return compressFailureError;
075    }
076
077    public StanzaError getStanzaError() {
078        return stanzaError;
079    }
080
081    @Override
082    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
083        XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
084        xml.rightAngleBracket();
085
086        xml.emptyElement(compressFailureError);
087        xml.optElement(stanzaError);
088
089        xml.closeElement(this);
090        return xml;
091    }
092}