001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2021-2024 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 */
017
018package org.jivesoftware.smackx.iqversion.packet;
019
020import org.jivesoftware.smack.XMPPConnection;
021import org.jivesoftware.smack.packet.IQ;
022import org.jivesoftware.smack.packet.IqData;
023import org.jivesoftware.smack.util.StringUtils;
024
025/**
026 * A Version IQ packet, which is used by XMPP clients to discover version information
027 * about the software running at another entity's JID.
028 *
029 * @author Gaston Dombiak
030 */
031public class Version extends IQ implements VersionView {
032    public static final String ELEMENT = QUERY_ELEMENT;
033    public static final String NAMESPACE = "jabber:iq:version";
034
035    private final String name;
036    private final String version;
037    private String os;
038
039    Version(VersionBuilder versionBuilder) {
040        super(versionBuilder, ELEMENT, NAMESPACE);
041        name = versionBuilder.getName();
042        version = versionBuilder.getVersion();
043        os = versionBuilder.getOs();
044
045        if (getType() != IQ.Type.result) {
046            return;
047        }
048        StringUtils.requireNotNullNorEmpty(name, "Version results must contain a name");
049        StringUtils.requireNotNullNorEmpty(version, "Version results must contain a version");
050    }
051
052    /**
053     * Returns the natural-language name of the software. This property will always be
054     * present in a result.
055     *
056     * @return the natural-language name of the software.
057     */
058    @Override
059    public String getName() {
060        return name;
061    }
062
063    /**
064     * Returns the specific version of the software. This property will always be
065     * present in a result.
066     *
067     * @return the specific version of the software.
068     */
069    @Override
070    public String getVersion() {
071        return version;
072    }
073
074    /**
075     * Returns the operating system of the queried entity. This property will always be
076     * present in a result.
077     *
078     * @return the operating system of the queried entity.
079     */
080    @Override
081    public String getOs() {
082        return os;
083    }
084
085    @Override
086    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
087        xml.rightAngleBracket();
088        // Although not really optional elements, 'name' and 'version' are not set when sending a
089        // version request. So we must handle the case that those are 'null' here.
090        xml.optElement("name", name);
091        xml.optElement("version", version);
092        xml.optElement("os", os);
093        return xml;
094    }
095
096    public static VersionBuilder builder(XMPPConnection connection) {
097        return new VersionBuilder(connection);
098    }
099
100    public static VersionBuilder builder(IqData iqData) {
101        return new VersionBuilder(iqData);
102    }
103
104    public static VersionBuilder builder(Version versionRequest) {
105        IqData iqData = IqData.createResponseData(versionRequest);
106        return builder(iqData);
107    }
108}