001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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.packet.IQ;
021import org.jivesoftware.smack.packet.Stanza;
022import org.jivesoftware.smack.util.StringUtils;
023
024import org.jxmpp.jid.Jid;
025
026/**
027 * A Version IQ packet, which is used by XMPP clients to discover version information
028 * about the software running at another entity's JID.<p>
029 *
030 * @author Gaston Dombiak
031 */
032public class Version extends IQ {
033    public static final String ELEMENT = QUERY_ELEMENT;
034    public static final String NAMESPACE = "jabber:iq:version";
035
036    private final String name;
037    private final String version;
038    private String os;
039
040    public Version() {
041        super(ELEMENT, NAMESPACE);
042        name = null;
043        version = null;
044        setType(Type.get);
045    }
046
047    /**
048     * Request version IQ.
049     *
050     * @param to the jid where to request version from
051     */
052    public Version(Jid to) {
053        this();
054        setTo(to);
055    }
056
057    public Version(String name, String version) {
058        this(name, version, null);
059    }
060
061    /**
062     * Creates a new Version object with given details.
063     *
064     * @param name The natural-language name of the software. This element is REQUIRED.
065     * @param version The specific version of the software. This element is REQUIRED.
066     * @param os The operating system of the queried entity. This element is OPTIONAL.
067     */
068    public Version(String name, String version, String os) {
069        super(ELEMENT, NAMESPACE);
070        this.setType(IQ.Type.result);
071        this.name = StringUtils.requireNotNullNorEmpty(name, "name must not be null");
072        this.version = StringUtils.requireNotNullNorEmpty(version, "version must not be null");
073        this.os = os;
074    }
075
076    public Version(Version original) {
077        this(original.name, original.version, original.os);
078    }
079
080    /**
081     * Returns the natural-language name of the software. This property will always be
082     * present in a result.
083     *
084     * @return the natural-language name of the software.
085     */
086    public String getName() {
087        return name;
088    }
089
090    /**
091     * Returns the specific version of the software. This property will always be
092     * present in a result.
093     *
094     * @return the specific version of the software.
095     */
096    public String getVersion() {
097        return version;
098    }
099
100    /**
101     * Returns the operating system of the queried entity. This property will always be
102     * present in a result.
103     *
104     * @return the operating system of the queried entity.
105     */
106    public String getOs() {
107        return os;
108    }
109
110    /**
111     * Sets the operating system of the queried entity. This message should only be
112     * invoked when parsing the XML and setting the property to a Version instance.
113     *
114     * @param os operating system of the queried entity.
115     */
116    public void setOs(String os) {
117        this.os = os;
118    }
119
120    @Override
121    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
122        xml.rightAngleBracket();
123        // Although not really optional elements, 'name' and 'version' are not set when sending a
124        // version request. So we must handle the case that those are 'null' here.
125        xml.optElement("name", name);
126        xml.optElement("version", version);
127        xml.optElement("os", os);
128        return xml;
129    }
130
131    public static Version createResultFor(Stanza request, Version version) {
132        Version result = new Version(version);
133        result.setStanzaId(request.getStanzaId());
134        result.setTo(request.getFrom());
135        return result;
136    }
137}