Version.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software, 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.iqversion.packet;

  18. import org.jivesoftware.smack.XMPPConnection;
  19. import org.jivesoftware.smack.packet.IQ;
  20. import org.jivesoftware.smack.packet.IqData;
  21. import org.jivesoftware.smack.util.StringUtils;

  22. /**
  23.  * A Version IQ packet, which is used by XMPP clients to discover version information
  24.  * about the software running at another entity's JID.<p>
  25.  *
  26.  * @author Gaston Dombiak
  27.  */
  28. public class Version extends IQ implements VersionView {
  29.     public static final String ELEMENT = QUERY_ELEMENT;
  30.     public static final String NAMESPACE = "jabber:iq:version";

  31.     private final String name;
  32.     private final String version;
  33.     private String os;

  34.     Version(VersionBuilder versionBuilder) {
  35.         super(versionBuilder, ELEMENT, NAMESPACE);
  36.         name = versionBuilder.getName();
  37.         version = versionBuilder.getVersion();
  38.         os = versionBuilder.getOs();

  39.         if (getType() != IQ.Type.result) {
  40.             return;
  41.         }
  42.         StringUtils.requireNotNullNorEmpty(name, "Version results must contain a name");
  43.         StringUtils.requireNotNullNorEmpty(version, "Version results must contain a version");
  44.     }

  45.     /**
  46.      * Returns the natural-language name of the software. This property will always be
  47.      * present in a result.
  48.      *
  49.      * @return the natural-language name of the software.
  50.      */
  51.     @Override
  52.     public String getName() {
  53.         return name;
  54.     }

  55.     /**
  56.      * Returns the specific version of the software. This property will always be
  57.      * present in a result.
  58.      *
  59.      * @return the specific version of the software.
  60.      */
  61.     @Override
  62.     public String getVersion() {
  63.         return version;
  64.     }

  65.     /**
  66.      * Returns the operating system of the queried entity. This property will always be
  67.      * present in a result.
  68.      *
  69.      * @return the operating system of the queried entity.
  70.      */
  71.     @Override
  72.     public String getOs() {
  73.         return os;
  74.     }

  75.     @Override
  76.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  77.         xml.rightAngleBracket();
  78.         // Although not really optional elements, 'name' and 'version' are not set when sending a
  79.         // version request. So we must handle the case that those are 'null' here.
  80.         xml.optElement("name", name);
  81.         xml.optElement("version", version);
  82.         xml.optElement("os", os);
  83.         return xml;
  84.     }

  85.     public static VersionBuilder builder(XMPPConnection connection) {
  86.         return new VersionBuilder(connection);
  87.     }

  88.     public static VersionBuilder builder(IqData iqData) {
  89.         return new VersionBuilder(iqData);
  90.     }

  91.     public static VersionBuilder builder(Version versionRequest) {
  92.         IqData iqData = IqData.createResponseData(versionRequest);
  93.         return builder(iqData);
  94.     }
  95. }