Version.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  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.packet.IQ;
  19. import org.jivesoftware.smack.packet.Stanza;
  20. import org.jivesoftware.smack.util.StringUtils;
  21. import org.jxmpp.jid.Jid;

  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 {
  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.     public Version() {
  35.         super(ELEMENT, NAMESPACE);
  36.         name = null;
  37.         version = null;
  38.         setType(Type.get);
  39.     }

  40.     /**
  41.      * Request version IQ
  42.      *
  43.      * @param to the jid where to request version from
  44.      */
  45.     public Version(Jid to) {
  46.         this();
  47.         setTo(to);
  48.     }

  49.     public Version(String name, String version) {
  50.         this(name, version, null);
  51.     }

  52.     /**
  53.      * Creates a new Version object with given details.
  54.      *
  55.      * @param name The natural-language name of the software. This element is REQUIRED.
  56.      * @param version The specific version of the software. This element is REQUIRED.
  57.      * @param os The operating system of the queried entity. This element is OPTIONAL.
  58.      */
  59.     public Version(String name, String version, String os) {
  60.         super(ELEMENT, NAMESPACE);
  61.         this.setType(IQ.Type.result);
  62.         this.name = StringUtils.requireNotNullOrEmpty(name, "name must not be null");
  63.         this.version = StringUtils.requireNotNullOrEmpty(version, "version must not be null");
  64.         this.os = os;
  65.     }

  66.     public Version(Version original) {
  67.         this(original.name, original.version, original.os);
  68.     }

  69.     /**
  70.      * Returns the natural-language name of the software. This property will always be
  71.      * present in a result.
  72.      *
  73.      * @return the natural-language name of the software.
  74.      */
  75.     public String getName() {
  76.         return name;
  77.     }

  78.     /**
  79.      * Returns the specific version of the software. This property will always be
  80.      * present in a result.
  81.      *
  82.      * @return the specific version of the software.
  83.      */
  84.     public String getVersion() {
  85.         return version;
  86.     }

  87.     /**
  88.      * Returns the operating system of the queried entity. This property will always be
  89.      * present in a result.
  90.      *
  91.      * @return the operating system of the queried entity.
  92.      */
  93.     public String getOs() {
  94.         return os;
  95.     }

  96.     /**
  97.      * Sets the operating system of the queried entity. This message should only be
  98.      * invoked when parsing the XML and setting the property to a Version instance.
  99.      *
  100.      * @param os operating system of the queried entity.
  101.      */
  102.     public void setOs(String os) {
  103.         this.os = os;
  104.     }

  105.     @Override
  106.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  107.         xml.rightAngleBracket();
  108.         // Although not really optional elements, 'name' and 'version' are not set when sending a
  109.         // version request. So we must handle the case that those are 'null' here.
  110.         xml.optElement("name", name);
  111.         xml.optElement("version", version);
  112.         xml.optElement("os", os);
  113.         return xml;
  114.     }

  115.     public static Version createResultFor(Stanza request, Version version) {
  116.         Version result = new Version(version);
  117.         result.setStanzaId(request.getStanzaId());
  118.         result.setTo(request.getFrom());
  119.         return result;
  120.     }
  121. }