AbstractIqBuilder.java

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

  18. import org.jivesoftware.smack.packet.id.StanzaIdSource;
  19. import org.jivesoftware.smack.util.ToStringUtil;

  20. public abstract class AbstractIqBuilder<IB extends AbstractIqBuilder<IB>> extends StanzaBuilder<IB> implements IqView {

  21.     protected IQ.Type type = IQ.Type.get;

  22.     AbstractIqBuilder(IQ other, String stanzaId) {
  23.         super(other, stanzaId);
  24.     }

  25.     AbstractIqBuilder(AbstractIqBuilder<?> other) {
  26.         super(other);
  27.         type = other.type;
  28.     }

  29.     AbstractIqBuilder(StanzaIdSource stanzaIdSource) {
  30.         super(stanzaIdSource);
  31.     }

  32.     AbstractIqBuilder(String stanzaId) {
  33.         super(stanzaId);
  34.     }

  35.     // TODO: Deprecate and use corresponding method in IqData instead.
  36.     public static IqData createResponse(IqView request) {
  37.         return createResponse(request, IQ.ResponseType.result);
  38.     }

  39.     // TODO: Deprecate and use corresponding method in IqData instead.
  40.     public static IqData createErrorResponse(IqView request) {
  41.         return createResponse(request, IQ.ResponseType.error);
  42.     }

  43.     protected static IqData createResponse(IqView request, IQ.ResponseType responseType) {
  44.         if (!request.isRequestIQ()) {
  45.             throw new IllegalArgumentException("IQ request must be of type 'set' or 'get'. Original IQ: " + request);
  46.         }

  47.         IqData commonResponseIqData = buildResponse(request, s -> {
  48.             return StanzaBuilder.buildIqData(s);
  49.         });
  50.         commonResponseIqData.ofType(responseType.getType());

  51.         return commonResponseIqData;
  52.     }

  53.     @Override
  54.     protected final void addStanzaSpecificAttributes(ToStringUtil.Builder builder) {
  55.         builder.addValue("type", getType());
  56.     }

  57.     @Override
  58.     public final IQ.Type getType() {
  59.         return type;
  60.     }
  61. }