mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-22 12:02:05 +01:00
SMACK-53: create VersionProvider and VersionManager
The VersionProvider is used to parse Version IQs. Integration: providerManager.addIQProvider("query", Version.NAMESPACE, new VersionProvider()); The VersionManager is used to reply to Version IQs. Integration: VersionManger.getInstanceFor(connection).setVersion( new Version("App Name", "1.23", "Operating System"));
This commit is contained in:
parent
61bf5cd4ce
commit
62de78ef7c
5 changed files with 244 additions and 5 deletions
|
@ -0,0 +1,95 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2014 Georg Lukas.
|
||||
*
|
||||
* 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;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.filter.AndFilter;
|
||||
import org.jivesoftware.smack.filter.IQTypeFilter;
|
||||
import org.jivesoftware.smack.filter.PacketTypeFilter;
|
||||
import org.jivesoftware.smack.packet.IQ.Type;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.iqversion.packet.Version;
|
||||
|
||||
/**
|
||||
* A Version Manager that automatically responds to version IQs with a predetermined reply.
|
||||
*
|
||||
* <p>
|
||||
* The VersionManager takes care of handling incoming version request IQs, according to
|
||||
* XEP-0092 (Software Version). You can configure the version reply for a given connection
|
||||
* by running the following code:
|
||||
* </p>
|
||||
*
|
||||
* <pre>
|
||||
* Version MY_VERSION = new Version("My Little XMPP Application", "v1.23", "OS/2 32-bit");
|
||||
* VersionManager.getInstanceFor(mConnection).setVersion(MY_VERSION);
|
||||
* </pre>
|
||||
*
|
||||
* @author Georg Lukas
|
||||
*/
|
||||
public class VersionManager {
|
||||
private static final Map<Connection, VersionManager> instances =
|
||||
Collections.synchronizedMap(new WeakHashMap<Connection, VersionManager>());
|
||||
|
||||
private Version own_version;
|
||||
private WeakReference<Connection> weakRefConnection;
|
||||
|
||||
private VersionManager(final Connection connection) {
|
||||
this.weakRefConnection = new WeakReference<Connection>(connection);
|
||||
instances.put(connection, this);
|
||||
|
||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
sdm.addFeature(Version.NAMESPACE);
|
||||
|
||||
connection.addPacketListener(new PacketListener() {
|
||||
/**
|
||||
* Sends a Version reply on request
|
||||
*/
|
||||
public void processPacket(Packet packet) {
|
||||
if (own_version == null)
|
||||
return;
|
||||
|
||||
Version reply = new Version(own_version);
|
||||
reply.setPacketID(packet.getPacketID());
|
||||
reply.setTo(packet.getFrom());
|
||||
weakRefConnection.get().sendPacket(reply);
|
||||
}
|
||||
}
|
||||
, new AndFilter(new PacketTypeFilter(Version.class), new IQTypeFilter(Type.GET)));
|
||||
}
|
||||
|
||||
public static synchronized VersionManager getInstanceFor(Connection connection) {
|
||||
VersionManager versionManager = instances.get(connection);
|
||||
|
||||
if (versionManager == null) {
|
||||
versionManager = new VersionManager(connection);
|
||||
}
|
||||
|
||||
return versionManager;
|
||||
}
|
||||
|
||||
public void setVersion(Version v) {
|
||||
own_version = v;
|
||||
}
|
||||
}
|
|
@ -17,7 +17,9 @@
|
|||
|
||||
package org.jivesoftware.smackx.iqversion.packet;
|
||||
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A Version IQ packet, which is used by XMPP clients to discover version information
|
||||
|
@ -46,11 +48,30 @@ import org.jivesoftware.smack.packet.IQ;
|
|||
* @author Gaston Dombiak
|
||||
*/
|
||||
public class Version extends IQ {
|
||||
public static final String NAMESPACE = "jabber:iq:version";
|
||||
|
||||
private String name;
|
||||
private String version;
|
||||
private String os;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
this.setType(IQ.Type.RESULT);
|
||||
this.name = name;
|
||||
this.version = version;
|
||||
this.os = os;
|
||||
}
|
||||
|
||||
public Version(Version original) {
|
||||
this(original.name, original.version, original.os);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the natural-language name of the software. This property will always be
|
||||
* present in a result.
|
||||
|
@ -113,15 +134,17 @@ public class Version extends IQ {
|
|||
|
||||
public String getChildElementXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<query xmlns=\"jabber:iq:version\">");
|
||||
buf.append("<query xmlns=\"");
|
||||
buf.append(Version.NAMESPACE);
|
||||
buf.append("\">");
|
||||
if (name != null) {
|
||||
buf.append("<name>").append(name).append("</name>");
|
||||
buf.append("<name>").append(StringUtils.escapeForXML(name)).append("</name>");
|
||||
}
|
||||
if (version != null) {
|
||||
buf.append("<version>").append(version).append("</version>");
|
||||
buf.append("<version>").append(StringUtils.escapeForXML(version)).append("</version>");
|
||||
}
|
||||
if (os != null) {
|
||||
buf.append("<os>").append(os).append("</os>");
|
||||
buf.append("<os>").append(StringUtils.escapeForXML(os)).append("</os>");
|
||||
}
|
||||
buf.append("</query>");
|
||||
return buf.toString();
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2014 Georg Lukas.
|
||||
*
|
||||
* 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.provider;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.provider.IQProvider;
|
||||
import org.jivesoftware.smackx.iqversion.packet.Version;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
public class VersionProvider implements IQProvider {
|
||||
public IQ parseIQ(XmlPullParser parser) throws Exception {
|
||||
String name = null, version = null, os = null;
|
||||
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
String tagName = parser.getName();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (tagName.equals("name")) {
|
||||
name = parser.nextText();
|
||||
}
|
||||
else if (tagName.equals("version")) {
|
||||
version = parser.nextText();
|
||||
}
|
||||
else if (tagName.equals("os")) {
|
||||
os = parser.nextText();
|
||||
}
|
||||
} else if (eventType == XmlPullParser.END_TAG && tagName.equals("query")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
return new Version(name, version, os);
|
||||
}
|
||||
}
|
|
@ -127,7 +127,7 @@
|
|||
<iqProvider>
|
||||
<elementName>query</elementName>
|
||||
<namespace>jabber:iq:version</namespace>
|
||||
<className>org.jivesoftware.smackx.iqversion.packet.Version</className>
|
||||
<className>org.jivesoftware.smackx.iqversion.provider.VersionProvider</className>
|
||||
</iqProvider>
|
||||
|
||||
<!-- VCard -->
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2014 Georg Lukas.
|
||||
*
|
||||
* 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;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.jivesoftware.smack.DummyConnection;
|
||||
import org.jivesoftware.smack.ThreadedDummyConnection;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.test.util.TestUtils;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.jivesoftware.smackx.iqversion.packet.Version;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class VersionTest {
|
||||
private DummyConnection dummyCon;
|
||||
private ThreadedDummyConnection threadedCon;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
dummyCon = new DummyConnection();
|
||||
threadedCon = new ThreadedDummyConnection();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkProvider() throws Exception {
|
||||
// @formatter:off
|
||||
String control = "<iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='s2c1' type='get'>"
|
||||
+ "<query xmlns='jabber:iq:version'/>"
|
||||
+ "</iq>";
|
||||
// @formatter:on
|
||||
DummyConnection con = new DummyConnection();
|
||||
|
||||
// Enable version replys for this connection
|
||||
VersionManager.getInstanceFor(con).setVersion(new Version("Test", "0.23", "DummyOS"));
|
||||
IQ versionRequest = PacketParserUtils.parseIQ(TestUtils.getIQParser(control), con);
|
||||
|
||||
assertTrue(versionRequest instanceof Version);
|
||||
|
||||
con.processPacket(versionRequest);
|
||||
|
||||
Packet replyPacket = con.getSentPacket();
|
||||
assertTrue(replyPacket instanceof Version);
|
||||
|
||||
Version reply = (Version) replyPacket;
|
||||
//getFrom check is pending for SMACK-547
|
||||
//assertEquals("juliet@capulet.lit/balcony", reply.getFrom());
|
||||
assertEquals("capulet.lit", reply.getTo());
|
||||
assertEquals("s2c1", reply.getPacketID());
|
||||
assertEquals(IQ.Type.RESULT, reply.getType());
|
||||
assertEquals("Test", reply.getName());
|
||||
assertEquals("0.23", reply.getVersion());
|
||||
assertEquals("DummyOS", reply.getOs());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue