1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +01:00

[iqversion] Use IQ builder pattern

This commit is contained in:
Florian Schmaus 2021-05-02 17:16:09 +02:00
parent 469d4fb0dc
commit 6af38482de
5 changed files with 181 additions and 78 deletions

View file

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.jivesoftware.smackx.iqversion; package org.jivesoftware.smackx.iqversion;
import java.util.Map; 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.iqrequest.IQRequestHandler.Mode;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.StanzaError.Condition; import org.jivesoftware.smack.packet.StanzaError.Condition;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.iqversion.packet.Version; import org.jivesoftware.smackx.iqversion.packet.Version;
@ -57,9 +57,9 @@ import org.jxmpp.jid.Jid;
public final class VersionManager extends Manager { public final class VersionManager extends Manager {
private static final Map<XMPPConnection, VersionManager> INSTANCES = new WeakHashMap<>(); private static final Map<XMPPConnection, VersionManager> 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) { public static void setDefaultVersion(String name, String version) {
setDefaultVersion(name, version, null); setDefaultVersion(name, version, null);
@ -94,7 +94,13 @@ public final class VersionManager extends Manager {
return IQ.createErrorResponse(iqRequest, Condition.not_acceptable); 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)) { if (!isSupported(jid)) {
return null; 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) { if (autoAppendSmackVersion) {
name += " (Smack " + Smack.getVersion() + ')'; 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;
}
} }
} }

View file

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,19 +17,18 @@
package org.jivesoftware.smackx.iqversion.packet; package org.jivesoftware.smackx.iqversion.packet;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ; 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.jivesoftware.smack.util.StringUtils;
import org.jxmpp.jid.Jid;
/** /**
* A Version IQ packet, which is used by XMPP clients to discover version information * A Version IQ packet, which is used by XMPP clients to discover version information
* about the software running at another entity's JID.<p> * about the software running at another entity's JID.<p>
* *
* @author Gaston Dombiak * @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 ELEMENT = QUERY_ELEMENT;
public static final String NAMESPACE = "jabber:iq:version"; public static final String NAMESPACE = "jabber:iq:version";
@ -37,44 +36,17 @@ public class Version extends IQ {
private final String version; private final String version;
private String os; private String os;
public Version() { Version(VersionBuilder versionBuilder) {
super(ELEMENT, NAMESPACE); super(versionBuilder, ELEMENT, NAMESPACE);
name = null; name = versionBuilder.getName();
version = null; version = versionBuilder.getVersion();
setType(Type.get); os = versionBuilder.getOs();
}
/** if (getType() != IQ.Type.result) {
* Request version IQ. return;
* }
* @param to the jid where to request version from StringUtils.requireNotNullNorEmpty(name, "Version results must contain a name");
*/ StringUtils.requireNotNullNorEmpty(version, "Version results must contain a version");
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);
} }
/** /**
@ -83,6 +55,7 @@ public class Version extends IQ {
* *
* @return the natural-language name of the software. * @return the natural-language name of the software.
*/ */
@Override
public String getName() { public String getName() {
return name; return name;
} }
@ -93,6 +66,7 @@ public class Version extends IQ {
* *
* @return the specific version of the software. * @return the specific version of the software.
*/ */
@Override
public String getVersion() { public String getVersion() {
return version; return version;
} }
@ -103,20 +77,11 @@ public class Version extends IQ {
* *
* @return the operating system of the queried entity. * @return the operating system of the queried entity.
*/ */
@Override
public String getOs() { public String getOs() {
return os; 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 @Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
xml.rightAngleBracket(); xml.rightAngleBracket();
@ -128,10 +93,16 @@ public class Version extends IQ {
return xml; return xml;
} }
public static Version createResultFor(Stanza request, Version version) { public static VersionBuilder builder(XMPPConnection connection) {
Version result = new Version(version); return new VersionBuilder(connection);
result.setStanzaId(request.getStanzaId()); }
result.setTo(request.getFrom());
return result; public static VersionBuilder builder(IqData iqData) {
return new VersionBuilder(iqData);
}
public static VersionBuilder builder(Version versionRequest) {
IqData iqData = IqData.createResponseData(versionRequest);
return builder(iqData);
} }
} }

View file

@ -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<VersionBuilder, Version> 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;
}
}

View file

@ -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();
}

View file

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.jivesoftware.smackx.iqversion.provider; package org.jivesoftware.smackx.iqversion.provider;
import java.io.IOException; import java.io.IOException;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IqData;
import org.jivesoftware.smack.packet.XmlEnvironment; 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.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException; import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.iqversion.packet.Version; import org.jivesoftware.smackx.iqversion.packet.Version;
import org.jivesoftware.smackx.iqversion.packet.VersionBuilder;
public class VersionProvider extends IQProvider<Version> { public class VersionProvider extends IqProvider<Version> {
@Override @Override
public Version parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { public Version parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
String name = null, version = null, os = null; throws XmlPullParserException, IOException {
VersionBuilder versionBuilder = Version.builder(iqData);
outerloop: while (true) { outerloop: while (true) {
XmlPullParser.Event eventType = parser.next(); XmlPullParser.Event eventType = parser.next();
@ -40,13 +42,16 @@ public class VersionProvider extends IQProvider<Version> {
String tagName = parser.getName(); String tagName = parser.getName();
switch (tagName) { switch (tagName) {
case "name": case "name":
name = parser.nextText(); String name = parser.nextText();
versionBuilder.setName(name);
break; break;
case "version": case "version":
version = parser.nextText(); String version = parser.nextText();
versionBuilder.setVersion(version);
break; break;
case "os": case "os":
os = parser.nextText(); String os = parser.nextText();
versionBuilder.setOs(os);
break; break;
} }
break; break;
@ -60,9 +65,7 @@ public class VersionProvider extends IQProvider<Version> {
break; break;
} }
} }
if (name == null && version == null && os == null) {
return new Version(); return versionBuilder.build();
}
return new Version(name, version, os);
} }
} }