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