diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/VersionManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/VersionManager.java index ec2a4f7d9..a170fc6d1 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/VersionManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/VersionManager.java @@ -1,6 +1,6 @@ /** * - * Copyright 2014 Georg Lukas. + * Copyright 2014 Georg Lukas, 2021 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.jivesoftware.smackx.iqversion; import java.util.Map; @@ -32,6 +31,7 @@ import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler; import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.StanzaError.Condition; +import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.iqversion.packet.Version; @@ -57,9 +57,9 @@ import org.jxmpp.jid.Jid; public final class VersionManager extends Manager { private static final Map INSTANCES = new WeakHashMap<>(); - private static Version defaultVersion; + private static VersionInformation defaultVersion; - private Version ourVersion = defaultVersion; + private VersionInformation ourVersion = defaultVersion; public static void setDefaultVersion(String name, String version) { setDefaultVersion(name, version, null); @@ -94,7 +94,13 @@ public final class VersionManager extends Manager { return IQ.createErrorResponse(iqRequest, Condition.not_acceptable); } - return Version.createResultFor(iqRequest, ourVersion); + Version versionRequest = (Version) iqRequest; + Version versionResponse = Version.builder(versionRequest) + .setName(ourVersion.name) + .setVersion(ourVersion.version) + .setOs(ourVersion.os) + .build(); + return versionResponse; } }); } @@ -147,13 +153,27 @@ public final class VersionManager extends Manager { if (!isSupported(jid)) { return null; } - return connection().createStanzaCollectorAndSend(new Version(jid)).nextResultOrThrow(); + XMPPConnection connection = connection(); + Version version = Version.builder(connection).to(jid).build(); + return connection().createStanzaCollectorAndSend(version).nextResultOrThrow(); } - private static Version generateVersionFrom(String name, String version, String os) { + private static VersionInformation generateVersionFrom(String name, String version, String os) { if (autoAppendSmackVersion) { name += " (Smack " + Smack.getVersion() + ')'; } - return new Version(name, version, os); + return new VersionInformation(name, version, os); + } + + private static final class VersionInformation { + private final String name; + private final String version; + private final String os; + + private VersionInformation(String name, String version, String os) { + this.name = StringUtils.requireNotNullNorEmpty(name, "Must provide a name"); + this.version = StringUtils.requireNotNullNorEmpty(version, "Must provide a version"); + this.os = os; + } } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/packet/Version.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/packet/Version.java index 503f418e7..b0dd5c1d0 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/packet/Version.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/packet/Version.java @@ -1,6 +1,6 @@ /** * - * Copyright 2003-2007 Jive Software. + * Copyright 2003-2007 Jive Software, 2021 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,19 +17,18 @@ package org.jivesoftware.smackx.iqversion.packet; +import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.packet.IQ; -import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smack.packet.IqData; import org.jivesoftware.smack.util.StringUtils; -import org.jxmpp.jid.Jid; - /** * A Version IQ packet, which is used by XMPP clients to discover version information * about the software running at another entity's JID.

* * @author Gaston Dombiak */ -public class Version extends IQ { +public class Version extends IQ implements VersionView { public static final String ELEMENT = QUERY_ELEMENT; public static final String NAMESPACE = "jabber:iq:version"; @@ -37,44 +36,17 @@ public class Version extends IQ { private final String version; private String os; - public Version() { - super(ELEMENT, NAMESPACE); - name = null; - version = null; - setType(Type.get); - } + Version(VersionBuilder versionBuilder) { + super(versionBuilder, ELEMENT, NAMESPACE); + name = versionBuilder.getName(); + version = versionBuilder.getVersion(); + os = versionBuilder.getOs(); - /** - * Request version IQ. - * - * @param to the jid where to request version from - */ - public Version(Jid to) { - this(); - setTo(to); - } - - public Version(String name, String version) { - this(name, version, null); - } - - /** - * Creates a new Version object with given details. - * - * @param name The natural-language name of the software. This element is REQUIRED. - * @param version The specific version of the software. This element is REQUIRED. - * @param os The operating system of the queried entity. This element is OPTIONAL. - */ - public Version(String name, String version, String os) { - super(ELEMENT, NAMESPACE); - this.setType(IQ.Type.result); - this.name = StringUtils.requireNotNullNorEmpty(name, "name must not be null"); - this.version = StringUtils.requireNotNullNorEmpty(version, "version must not be null"); - this.os = os; - } - - public Version(Version original) { - this(original.name, original.version, original.os); + if (getType() != IQ.Type.result) { + return; + } + StringUtils.requireNotNullNorEmpty(name, "Version results must contain a name"); + StringUtils.requireNotNullNorEmpty(version, "Version results must contain a version"); } /** @@ -83,6 +55,7 @@ public class Version extends IQ { * * @return the natural-language name of the software. */ + @Override public String getName() { return name; } @@ -93,6 +66,7 @@ public class Version extends IQ { * * @return the specific version of the software. */ + @Override public String getVersion() { return version; } @@ -103,20 +77,11 @@ public class Version extends IQ { * * @return the operating system of the queried entity. */ + @Override public String getOs() { return os; } - /** - * Sets the operating system of the queried entity. This message should only be - * invoked when parsing the XML and setting the property to a Version instance. - * - * @param os operating system of the queried entity. - */ - public void setOs(String os) { - this.os = os; - } - @Override protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { xml.rightAngleBracket(); @@ -128,10 +93,16 @@ public class Version extends IQ { return xml; } - public static Version createResultFor(Stanza request, Version version) { - Version result = new Version(version); - result.setStanzaId(request.getStanzaId()); - result.setTo(request.getFrom()); - return result; + public static VersionBuilder builder(XMPPConnection connection) { + return new VersionBuilder(connection); + } + + public static VersionBuilder builder(IqData iqData) { + return new VersionBuilder(iqData); + } + + public static VersionBuilder builder(Version versionRequest) { + IqData iqData = IqData.createResponseData(versionRequest); + return builder(iqData); } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/packet/VersionBuilder.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/packet/VersionBuilder.java new file mode 100644 index 000000000..25b9d6b6f --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/packet/VersionBuilder.java @@ -0,0 +1,80 @@ +/** + * + * Copyright 2021 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.iqversion.packet; + +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.packet.IqBuilder; +import org.jivesoftware.smack.packet.IqData; + +public class VersionBuilder extends IqBuilder implements VersionView { + + private String name; + private String version; + private String os; + + VersionBuilder(IqData iqCommon) { + super(iqCommon); + } + + VersionBuilder(XMPPConnection connection) { + super(connection); + } + + VersionBuilder(String stanzaId) { + super(stanzaId); + } + + public VersionBuilder setName(String name) { + this.name = name; + return getThis(); + } + + public VersionBuilder setVersion(String version) { + this.version = version; + return getThis(); + } + + public VersionBuilder setOs(String os) { + this.os = os; + return getThis(); + } + + @Override + public String getName() { + return name; + } + + @Override + public String getVersion() { + return version; + } + + @Override + public String getOs() { + return os; + } + + @Override + public Version build() { + return new Version(this); + } + + @Override + public VersionBuilder getThis() { + return this; + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/packet/VersionView.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/packet/VersionView.java new file mode 100644 index 000000000..1503b5788 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/packet/VersionView.java @@ -0,0 +1,29 @@ +/** + * + * Copyright 2021 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.iqversion.packet; + +import org.jivesoftware.smack.packet.IqView; + +public interface VersionView extends IqView { + + String getName(); + + String getVersion(); + + String getOs(); + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/provider/VersionProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/provider/VersionProvider.java index 328cdfccf..eb12b06a3 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/provider/VersionProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqversion/provider/VersionProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright 2014 Georg Lukas. + * Copyright 2014 Georg Lukas, 2021 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,24 +14,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.jivesoftware.smackx.iqversion.provider; import java.io.IOException; import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.packet.IqData; import org.jivesoftware.smack.packet.XmlEnvironment; -import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smack.provider.IqProvider; import org.jivesoftware.smack.xml.XmlPullParser; import org.jivesoftware.smack.xml.XmlPullParserException; import org.jivesoftware.smackx.iqversion.packet.Version; +import org.jivesoftware.smackx.iqversion.packet.VersionBuilder; -public class VersionProvider extends IQProvider { +public class VersionProvider extends IqProvider { @Override - public Version parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { - String name = null, version = null, os = null; + public Version parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) + throws XmlPullParserException, IOException { + VersionBuilder versionBuilder = Version.builder(iqData); outerloop: while (true) { XmlPullParser.Event eventType = parser.next(); @@ -40,13 +42,16 @@ public class VersionProvider extends IQProvider { String tagName = parser.getName(); switch (tagName) { case "name": - name = parser.nextText(); + String name = parser.nextText(); + versionBuilder.setName(name); break; case "version": - version = parser.nextText(); + String version = parser.nextText(); + versionBuilder.setVersion(version); break; case "os": - os = parser.nextText(); + String os = parser.nextText(); + versionBuilder.setOs(os); break; } break; @@ -60,9 +65,7 @@ public class VersionProvider extends IQProvider { break; } } - if (name == null && version == null && os == null) { - return new Version(); - } - return new Version(name, version, os); + + return versionBuilder.build(); } }