ErrorIQ.java

  1. /**
  2.  *
  3.  * Copyright © 2014-2023 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.packet;

  18. import java.util.Objects;

  19. import javax.xml.namespace.QName;

  20. /**
  21.  * An XMPP error IQ.
  22.  * <p>
  23.  * According to RFC 6120 § 8.3.1 "4. An error stanza MUST contain an &lt;error/&gt; child element.", so this class can
  24.  * only be constructed if a stanza error is provided.
  25.  */
  26. public final class ErrorIQ extends IQ {

  27.     public static final String ELEMENT = StanzaError.ERROR;

  28.     private final IQ request;

  29.     private ErrorIQ(Builder builder, QName childElementQName) {
  30.         super(builder, childElementQName);
  31.         Objects.requireNonNull(builder.getError(), "Must provide an stanza error when building error IQs");
  32.         this.request = builder.request;
  33.     }

  34.     public static ErrorIQ createErrorResponse(final IQ request, final StanzaError error) {
  35.         Builder builder = new Builder(error, request);
  36.         builder.setError(error);
  37.         return builder.build();
  38.     }

  39.     @Override
  40.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  41.         if (request == null) {
  42.             return null;
  43.         }

  44.         return request.getIQChildElementBuilder(xml);
  45.     }

  46.     public static Builder builder(StanzaError error) {
  47.         return new Builder(error, IqData.EMPTY.ofType(IQ.Type.error));
  48.     }

  49.     public static Builder builder(StanzaError error, IqData iqData) {
  50.         return new Builder(error, iqData);
  51.     }

  52.     public static final class Builder extends IqBuilder<Builder, ErrorIQ> {

  53.         private IQ request;

  54.         Builder(StanzaError error, IqData iqData) {
  55.             super(iqData);
  56.             if (iqData.getType() != IQ.Type.error) {
  57.                 throw new IllegalArgumentException("Error IQs must be of type 'error'");
  58.             }
  59.             Objects.requireNonNull(error, "Must provide an stanza error when building error IQs");
  60.             setError(error);
  61.         }

  62.         Builder(StanzaError error, IQ request) {
  63.             this(error, AbstractIqBuilder.createErrorResponse(request));
  64.             this.request = request;
  65.         }

  66.         @Override
  67.         public Builder getThis() {
  68.             return this;
  69.         }

  70.         @Override
  71.         public ErrorIQ build() {
  72.             QName childElementQname = null;
  73.             if (request != null) {
  74.                 childElementQname = request.getChildElementQName();
  75.             }
  76.             return new ErrorIQ(this, childElementQname);
  77.         }

  78.     }
  79. }