mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 06:12:05 +01:00
Cleanup carbons, forwarded and a few others API
Adopt to common design patterns in Smack: - getFrom(Packet) in Packetextensions - INSTANCES.put() in getInstanceFor() - ELEMENT instead of ELEMENT_NAME - Use XmlStringBuilder
This commit is contained in:
parent
1ed5c48bcc
commit
49ee058c38
14 changed files with 105 additions and 90 deletions
|
@ -160,6 +160,20 @@ public class XmlStringBuilder implements Appendable, CharSequence {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the given attribute if value => 0
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* @param value
|
||||||
|
* @return a reference to this object
|
||||||
|
*/
|
||||||
|
public XmlStringBuilder optLongAttribute(String name, Long value) {
|
||||||
|
if (value >= 0) {
|
||||||
|
attribute(name, Long.toString(value));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public XmlStringBuilder xmlnsAttribute(String value) {
|
public XmlStringBuilder xmlnsAttribute(String value) {
|
||||||
optAttribute("xmlns", value);
|
optAttribute("xmlns", value);
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2013 Georg Lukas
|
* Copyright 2013-2014 Georg Lukas
|
||||||
*
|
*
|
||||||
* 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.
|
||||||
|
@ -16,7 +16,6 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.carbons;
|
package org.jivesoftware.smackx.carbons;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
|
@ -49,8 +48,7 @@ import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||||
*/
|
*/
|
||||||
public class CarbonManager extends Manager {
|
public class CarbonManager extends Manager {
|
||||||
|
|
||||||
private static Map<XMPPConnection, CarbonManager> instances =
|
private static Map<XMPPConnection, CarbonManager> INSTANCES = new WeakHashMap<XMPPConnection, CarbonManager>();
|
||||||
Collections.synchronizedMap(new WeakHashMap<XMPPConnection, CarbonManager>());
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
||||||
|
@ -66,7 +64,6 @@ public class CarbonManager extends Manager {
|
||||||
super(connection);
|
super(connection);
|
||||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
sdm.addFeature(CarbonExtension.NAMESPACE);
|
sdm.addFeature(CarbonExtension.NAMESPACE);
|
||||||
instances.put(connection, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -77,16 +74,17 @@ public class CarbonManager extends Manager {
|
||||||
* @return a CarbonManager instance
|
* @return a CarbonManager instance
|
||||||
*/
|
*/
|
||||||
public static synchronized CarbonManager getInstanceFor(XMPPConnection connection) {
|
public static synchronized CarbonManager getInstanceFor(XMPPConnection connection) {
|
||||||
CarbonManager carbonManager = instances.get(connection);
|
CarbonManager carbonManager = INSTANCES.get(connection);
|
||||||
|
|
||||||
if (carbonManager == null) {
|
if (carbonManager == null) {
|
||||||
carbonManager = new CarbonManager(connection);
|
carbonManager = new CarbonManager(connection);
|
||||||
|
INSTANCES.put(connection, carbonManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
return carbonManager;
|
return carbonManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IQ carbonsEnabledIQ(final boolean new_state) {
|
private static IQ carbonsEnabledIQ(final boolean new_state) {
|
||||||
IQ setIQ = new IQ() {
|
IQ setIQ = new IQ() {
|
||||||
public String getChildElementXML() {
|
public String getChildElementXML() {
|
||||||
return "<" + (new_state? "enable" : "disable") + " xmlns='" + CarbonExtension.NAMESPACE + "'/>";
|
return "<" + (new_state? "enable" : "disable") + " xmlns='" + CarbonExtension.NAMESPACE + "'/>";
|
||||||
|
@ -100,10 +98,11 @@ public class CarbonManager extends Manager {
|
||||||
* Returns true if XMPP Carbons are supported by the server.
|
* Returns true if XMPP Carbons are supported by the server.
|
||||||
*
|
*
|
||||||
* @return true if supported
|
* @return true if supported
|
||||||
* @throws SmackException if there was no response from the server.
|
* @throws NotConnectedException
|
||||||
* @throws XMPPException
|
* @throws XMPPErrorException
|
||||||
|
* @throws NoResponseException
|
||||||
*/
|
*/
|
||||||
public boolean isSupportedByServer() throws XMPPException, SmackException {
|
public boolean isSupportedByServer() throws NoResponseException, XMPPErrorException, NotConnectedException {
|
||||||
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(
|
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(
|
||||||
connection().getServiceName(), CarbonExtension.NAMESPACE);
|
connection().getServiceName(), CarbonExtension.NAMESPACE);
|
||||||
}
|
}
|
||||||
|
@ -184,20 +183,6 @@ public class CarbonManager extends Manager {
|
||||||
return this.enabled_state;
|
return this.enabled_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Obtain a Carbon from a message, if available.
|
|
||||||
*
|
|
||||||
* @param msg Message object to check for carbons
|
|
||||||
*
|
|
||||||
* @return a Carbon if available, null otherwise.
|
|
||||||
*/
|
|
||||||
public static CarbonExtension getCarbon(Message msg) {
|
|
||||||
CarbonExtension cc = (CarbonExtension)msg.getExtension("received", CarbonExtension.NAMESPACE);
|
|
||||||
if (cc == null)
|
|
||||||
cc = (CarbonExtension)msg.getExtension("sent", CarbonExtension.NAMESPACE);
|
|
||||||
return cc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mark a message as "private", so it will not be carbon-copied.
|
* Mark a message as "private", so it will not be carbon-copied.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2013 Georg Lukas
|
* Copyright 2013-2014 Georg Lukas
|
||||||
*
|
*
|
||||||
* 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.
|
||||||
|
@ -16,7 +16,9 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.carbons.packet;
|
package org.jivesoftware.smackx.carbons.packet;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smack.packet.PacketExtension;
|
import org.jivesoftware.smack.packet.PacketExtension;
|
||||||
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
import org.jivesoftware.smackx.forward.Forwarded;
|
import org.jivesoftware.smackx.forward.Forwarded;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,8 +35,8 @@ import org.jivesoftware.smackx.forward.Forwarded;
|
||||||
public class CarbonExtension implements PacketExtension {
|
public class CarbonExtension implements PacketExtension {
|
||||||
public static final String NAMESPACE = "urn:xmpp:carbons:2";
|
public static final String NAMESPACE = "urn:xmpp:carbons:2";
|
||||||
|
|
||||||
private Direction dir;
|
private final Direction dir;
|
||||||
private Forwarded fwd;
|
private final Forwarded fwd;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a Carbon message extension.
|
* Construct a Carbon message extension.
|
||||||
|
@ -67,7 +69,7 @@ public class CarbonExtension implements PacketExtension {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getElementName() {
|
public String getElementName() {
|
||||||
return dir.toString();
|
return dir.name();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -76,15 +78,29 @@ public class CarbonExtension implements PacketExtension {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toXML() {
|
public XmlStringBuilder toXML() {
|
||||||
StringBuilder buf = new StringBuilder();
|
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||||
buf.append("<").append(getElementName()).append(" xmlns=\"")
|
xml.rightAngelBracket();
|
||||||
.append(getNamespace()).append("\">");
|
xml.append(fwd.toXML());
|
||||||
|
xml.closeElement(this);
|
||||||
|
return xml;
|
||||||
|
}
|
||||||
|
|
||||||
buf.append(fwd.toXML());
|
/**
|
||||||
|
* Obtain a Carbon from a message, if available.
|
||||||
buf.append("</").append(getElementName()).append(">");
|
* <p>
|
||||||
return buf.toString();
|
* Only {@link Message} instances can contain a Carbon extensions.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param msg Message object to check for carbons
|
||||||
|
*
|
||||||
|
* @return a Carbon if available, null otherwise.
|
||||||
|
*/
|
||||||
|
public static CarbonExtension getFrom(Message msg) {
|
||||||
|
CarbonExtension cc = msg.getExtension(Direction.received.name(), CarbonExtension.NAMESPACE);
|
||||||
|
if (cc == null)
|
||||||
|
cc = msg.getExtension(Direction.sent.name(), CarbonExtension.NAMESPACE);
|
||||||
|
return cc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -102,16 +118,19 @@ public class CarbonExtension implements PacketExtension {
|
||||||
public static class Private implements PacketExtension {
|
public static class Private implements PacketExtension {
|
||||||
public static final String ELEMENT = "private";
|
public static final String ELEMENT = "private";
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getElementName() {
|
public String getElementName() {
|
||||||
return ELEMENT;
|
return ELEMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getNamespace() {
|
public String getNamespace() {
|
||||||
return CarbonExtension.NAMESPACE;
|
return CarbonExtension.NAMESPACE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String toXML() {
|
public String toXML() {
|
||||||
return "<" + ELEMENT + " xmlns=\"" + CarbonExtension.NAMESPACE + "\"/>";
|
return "<" + ELEMENT + " xmlns='" + CarbonExtension.NAMESPACE + "'/>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2013 Georg Lukas
|
* Copyright 2013-2014 Georg Lukas
|
||||||
*
|
*
|
||||||
* 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.
|
||||||
|
@ -26,7 +26,7 @@ import org.xmlpull.v1.XmlPullParser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class implements the {@link PacketExtensionProvider} to parse
|
* This class implements the {@link PacketExtensionProvider} to parse
|
||||||
* cabon copied messages from a packet. It will return a {@link CarbonExtension} packet extension.
|
* carbon copied messages from a packet. It will return a {@link CarbonExtension} packet extension.
|
||||||
*
|
*
|
||||||
* @author Georg Lukas
|
* @author Georg Lukas
|
||||||
*
|
*
|
||||||
|
@ -41,7 +41,7 @@ public class CarbonManagerProvider implements PacketExtensionProvider {
|
||||||
while (!done) {
|
while (!done) {
|
||||||
int eventType = parser.next();
|
int eventType = parser.next();
|
||||||
if (eventType == XmlPullParser.START_TAG && parser.getName().equals("forwarded")) {
|
if (eventType == XmlPullParser.START_TAG && parser.getName().equals("forwarded")) {
|
||||||
fwd = (Forwarded) PacketParserUtils.parsePacketExtension(Forwarded.ELEMENT_NAME, Forwarded.NAMESPACE, parser);
|
fwd = (Forwarded) PacketParserUtils.parsePacketExtension(Forwarded.ELEMENT, Forwarded.NAMESPACE, parser);
|
||||||
}
|
}
|
||||||
else if (eventType == XmlPullParser.END_TAG && dir == Direction.valueOf(parser.getName()))
|
else if (eventType == XmlPullParser.END_TAG && dir == Direction.valueOf(parser.getName()))
|
||||||
done = true;
|
done = true;
|
||||||
|
|
|
@ -20,30 +20,23 @@ import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.jivesoftware.smack.provider.ProviderManager;
|
|
||||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
|
import org.jivesoftware.smackx.ExperimentalInitializerTest;
|
||||||
import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
|
import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
|
||||||
import org.jivesoftware.smackx.carbons.provider.CarbonManagerProvider;
|
import org.jivesoftware.smackx.carbons.provider.CarbonManagerProvider;
|
||||||
import org.jivesoftware.smackx.forward.Forwarded;
|
import org.jivesoftware.smackx.forward.Forwarded;
|
||||||
import org.jivesoftware.smackx.forward.provider.ForwardedProvider;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
|
||||||
import com.jamesmurty.utils.XMLBuilder;
|
import com.jamesmurty.utils.XMLBuilder;
|
||||||
|
|
||||||
public class CarbonTest {
|
public class CarbonTest extends ExperimentalInitializerTest {
|
||||||
|
|
||||||
private static Properties outputProperties = new Properties();
|
private static Properties outputProperties = new Properties();
|
||||||
static {
|
static {
|
||||||
outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
|
outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeClass
|
|
||||||
public static void setup() {
|
|
||||||
ProviderManager.addExtensionProvider("forwarded", "urn:xmpp:forward:0", new ForwardedProvider());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void carbonSentTest() throws Exception {
|
public void carbonSentTest() throws Exception {
|
||||||
XmlPullParser parser;
|
XmlPullParser parser;
|
||||||
|
@ -66,7 +59,7 @@ public class CarbonTest {
|
||||||
assertEquals(CarbonExtension.Direction.sent, cc.getDirection());
|
assertEquals(CarbonExtension.Direction.sent, cc.getDirection());
|
||||||
|
|
||||||
// no delay in packet
|
// no delay in packet
|
||||||
assertEquals(null, fwd.getDelayInfo());
|
assertEquals(null, fwd.getDelayInformation());
|
||||||
|
|
||||||
// check message
|
// check message
|
||||||
assertEquals("romeo@montague.com", fwd.getForwardedPacket().getFrom());
|
assertEquals("romeo@montague.com", fwd.getForwardedPacket().getFrom());
|
||||||
|
|
|
@ -20,6 +20,7 @@ package org.jivesoftware.smackx.chatstates.packet;
|
||||||
import org.jivesoftware.smackx.chatstates.ChatState;
|
import org.jivesoftware.smackx.chatstates.ChatState;
|
||||||
import org.jivesoftware.smack.packet.PacketExtension;
|
import org.jivesoftware.smack.packet.PacketExtension;
|
||||||
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
||||||
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,7 +32,9 @@ import org.xmlpull.v1.XmlPullParser;
|
||||||
*/
|
*/
|
||||||
public class ChatStateExtension implements PacketExtension {
|
public class ChatStateExtension implements PacketExtension {
|
||||||
|
|
||||||
private ChatState state;
|
public static final String NAMESPACE = "http://jabber.org/protocol/chatstates";
|
||||||
|
|
||||||
|
private final ChatState state;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor. The argument provided is the state that the extension will represent.
|
* Default constructor. The argument provided is the state that the extension will represent.
|
||||||
|
@ -42,16 +45,21 @@ public class ChatStateExtension implements PacketExtension {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getElementName() {
|
public String getElementName() {
|
||||||
return state.name();
|
return state.name();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getNamespace() {
|
public String getNamespace() {
|
||||||
return "http://jabber.org/protocol/chatstates";
|
return NAMESPACE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toXML() {
|
@Override
|
||||||
return "<" + getElementName() + " xmlns=\"" + getNamespace() + "\" />";
|
public XmlStringBuilder toXML() {
|
||||||
|
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||||
|
xml.closeEmptyElement();
|
||||||
|
return xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Provider implements PacketExtensionProvider {
|
public static class Provider implements PacketExtensionProvider {
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class DelayInformationManager {
|
||||||
* @return the Delayed Delivery information or <code>null</code>
|
* @return the Delayed Delivery information or <code>null</code>
|
||||||
*/
|
*/
|
||||||
public static DelayInformation getXep203DelayInformation(Packet packet) {
|
public static DelayInformation getXep203DelayInformation(Packet packet) {
|
||||||
return (DelayInformation) packet.getExtension(DelayInformation.ELEMENT, DelayInformation.NAMESPACE);
|
return DelayInformation.getFrom(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,7 +56,7 @@ public class DelayInformationManager {
|
||||||
* @return the Delayed Delivery information or <code>null</code>
|
* @return the Delayed Delivery information or <code>null</code>
|
||||||
*/
|
*/
|
||||||
public static DelayInformation getLegacyDelayInformation(Packet packet) {
|
public static DelayInformation getLegacyDelayInformation(Packet packet) {
|
||||||
return (DelayInformation) packet.getExtension(LEGACY_DELAYED_DELIVERY_ELEMENT, LEGACY_DELAYED_DELIVERY_NAMESPACE);
|
return packet.getExtension(LEGACY_DELAYED_DELIVERY_ELEMENT, LEGACY_DELAYED_DELIVERY_NAMESPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -18,6 +18,7 @@ package org.jivesoftware.smackx.delay.packet;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
import org.jivesoftware.smack.packet.PacketExtension;
|
import org.jivesoftware.smack.packet.PacketExtension;
|
||||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
import org.jxmpp.util.XmppDateTime;
|
import org.jxmpp.util.XmppDateTime;
|
||||||
|
@ -96,16 +97,17 @@ public class DelayInformation implements PacketExtension {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CharSequence toXML() {
|
public XmlStringBuilder toXML() {
|
||||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||||
xml.attribute("stamp", XmppDateTime.formatXEP0082Date(stamp));
|
xml.attribute("stamp", XmppDateTime.formatXEP0082Date(stamp));
|
||||||
xml.optAttribute("from", from);
|
xml.optAttribute("from", from);
|
||||||
xml.rightAngelBracket();
|
xml.rightAngelBracket();
|
||||||
if (reason != null) {
|
xml.optAppend(reason);
|
||||||
xml.escape(reason);
|
|
||||||
}
|
|
||||||
xml.closeElement(this);
|
xml.closeElement(this);
|
||||||
return xml;
|
return xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DelayInformation getFrom(Packet packet) {
|
||||||
|
return packet.getExtension(ELEMENT, NAMESPACE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2013 Georg Lukas
|
* Copyright 2013-2014 Georg Lukas
|
||||||
*
|
*
|
||||||
* 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.
|
||||||
|
@ -18,6 +18,7 @@ package org.jivesoftware.smackx.forward;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
import org.jivesoftware.smack.packet.PacketExtension;
|
import org.jivesoftware.smack.packet.PacketExtension;
|
||||||
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
import org.jivesoftware.smackx.delay.packet.DelayInformation;
|
import org.jivesoftware.smackx.delay.packet.DelayInformation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,7 +28,7 @@ import org.jivesoftware.smackx.delay.packet.DelayInformation;
|
||||||
*/
|
*/
|
||||||
public class Forwarded implements PacketExtension {
|
public class Forwarded implements PacketExtension {
|
||||||
public static final String NAMESPACE = "urn:xmpp:forward:0";
|
public static final String NAMESPACE = "urn:xmpp:forward:0";
|
||||||
public static final String ELEMENT_NAME = "forwarded";
|
public static final String ELEMENT = "forwarded";
|
||||||
|
|
||||||
private DelayInformation delay;
|
private DelayInformation delay;
|
||||||
private Packet forwardedPacket;
|
private Packet forwardedPacket;
|
||||||
|
@ -54,7 +55,7 @@ public class Forwarded implements PacketExtension {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getElementName() {
|
public String getElementName() {
|
||||||
return ELEMENT_NAME;
|
return ELEMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -63,17 +64,17 @@ public class Forwarded implements PacketExtension {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toXML() {
|
public XmlStringBuilder toXML() {
|
||||||
StringBuilder buf = new StringBuilder();
|
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||||
buf.append("<").append(getElementName()).append(" xmlns=\"")
|
xml.rightAngelBracket();
|
||||||
.append(getNamespace()).append("\">");
|
xml.optElement(getDelayInformation());
|
||||||
|
xml.append(forwardedPacket.toXML());
|
||||||
|
xml.closeElement(this);
|
||||||
|
return xml;
|
||||||
|
}
|
||||||
|
|
||||||
if (delay != null)
|
public static Forwarded getFrom(Packet packet) {
|
||||||
buf.append(delay.toXML());
|
return packet.getExtension(ELEMENT, NAMESPACE);
|
||||||
buf.append(forwardedPacket.toXML());
|
|
||||||
|
|
||||||
buf.append("</").append(getElementName()).append(">");
|
|
||||||
return buf.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2013 Georg Lukas
|
* Copyright 2013-2014 Georg Lukas
|
||||||
*
|
*
|
||||||
* 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.
|
||||||
|
@ -45,7 +45,7 @@ public class ForwardedProvider implements PacketExtensionProvider {
|
||||||
packet = PacketParserUtils.parseMessage(parser);
|
packet = PacketParserUtils.parseMessage(parser);
|
||||||
else throw new Exception("Unsupported forwarded packet type: " + parser.getName());
|
else throw new Exception("Unsupported forwarded packet type: " + parser.getName());
|
||||||
}
|
}
|
||||||
else if (eventType == XmlPullParser.END_TAG && parser.getName().equals(Forwarded.ELEMENT_NAME))
|
else if (eventType == XmlPullParser.END_TAG && parser.getName().equals(Forwarded.ELEMENT))
|
||||||
done = true;
|
done = true;
|
||||||
}
|
}
|
||||||
if (packet == null)
|
if (packet == null)
|
||||||
|
|
|
@ -54,11 +54,10 @@ public class LastActivity extends IQ {
|
||||||
@Override
|
@Override
|
||||||
public XmlStringBuilder getChildElementXML() {
|
public XmlStringBuilder getChildElementXML() {
|
||||||
XmlStringBuilder xml = new XmlStringBuilder();
|
XmlStringBuilder xml = new XmlStringBuilder();
|
||||||
xml.halfOpenElement("query");
|
xml.halfOpenElement(IQ.QUERY_ELEMENT);
|
||||||
xml.xmlnsAttribute(NAMESPACE);
|
xml.xmlnsAttribute(NAMESPACE);
|
||||||
if (lastActivity != -1) {
|
xml.optLongAttribute("seconds", lastActivity);
|
||||||
xml.attribute("seconds", Long.toString(lastActivity));
|
|
||||||
}
|
|
||||||
// We don't support adding the optional message attribute, because it is usually only added
|
// We don't support adding the optional message attribute, because it is usually only added
|
||||||
// by XMPP servers and not by client entities.
|
// by XMPP servers and not by client entities.
|
||||||
xml.closeEmptyElement();
|
xml.closeEmptyElement();
|
||||||
|
@ -108,10 +107,6 @@ public class LastActivity extends IQ {
|
||||||
}
|
}
|
||||||
|
|
||||||
public IQ parseIQ(XmlPullParser parser) throws SmackException, XmlPullParserException {
|
public IQ parseIQ(XmlPullParser parser) throws SmackException, XmlPullParserException {
|
||||||
if (parser.getEventType() != XmlPullParser.START_TAG) {
|
|
||||||
throw new SmackException("Parser not in proper position, or bad XML.");
|
|
||||||
}
|
|
||||||
|
|
||||||
LastActivity lastActivity = new LastActivity();
|
LastActivity lastActivity = new LastActivity();
|
||||||
String seconds = parser.getAttributeValue("", "seconds");
|
String seconds = parser.getAttributeValue("", "seconds");
|
||||||
if (seconds != null) {
|
if (seconds != null) {
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
|
|
||||||
package org.jivesoftware.smackx.iqversion;
|
package org.jivesoftware.smackx.iqversion;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
|
@ -50,8 +49,7 @@ import org.jivesoftware.smackx.iqversion.packet.Version;
|
||||||
* @author Georg Lukas
|
* @author Georg Lukas
|
||||||
*/
|
*/
|
||||||
public class VersionManager extends Manager {
|
public class VersionManager extends Manager {
|
||||||
private static final Map<XMPPConnection, VersionManager> instances =
|
private static final Map<XMPPConnection, VersionManager> INSTANCES = new WeakHashMap<XMPPConnection, VersionManager>();
|
||||||
Collections.synchronizedMap(new WeakHashMap<XMPPConnection, VersionManager>());
|
|
||||||
|
|
||||||
private static final PacketFilter PACKET_FILTER = new AndFilter(new PacketTypeFilter(Version.class), IQTypeFilter.GET);
|
private static final PacketFilter PACKET_FILTER = new AndFilter(new PacketTypeFilter(Version.class), IQTypeFilter.GET);
|
||||||
|
|
||||||
|
@ -59,7 +57,6 @@ public class VersionManager extends Manager {
|
||||||
|
|
||||||
private VersionManager(final XMPPConnection connection) {
|
private VersionManager(final XMPPConnection connection) {
|
||||||
super(connection);
|
super(connection);
|
||||||
instances.put(connection, this);
|
|
||||||
|
|
||||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
sdm.addFeature(Version.NAMESPACE);
|
sdm.addFeature(Version.NAMESPACE);
|
||||||
|
@ -83,10 +80,11 @@ public class VersionManager extends Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized VersionManager getInstanceFor(XMPPConnection connection) {
|
public static synchronized VersionManager getInstanceFor(XMPPConnection connection) {
|
||||||
VersionManager versionManager = instances.get(connection);
|
VersionManager versionManager = INSTANCES.get(connection);
|
||||||
|
|
||||||
if (versionManager == null) {
|
if (versionManager == null) {
|
||||||
versionManager = new VersionManager(connection);
|
versionManager = new VersionManager(connection);
|
||||||
|
INSTANCES.put(connection, versionManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
return versionManager;
|
return versionManager;
|
||||||
|
|
|
@ -60,6 +60,7 @@ public class EntityTimeManager extends Manager {
|
||||||
EntityTimeManager entityTimeManager = INSTANCES.get(connection);
|
EntityTimeManager entityTimeManager = INSTANCES.get(connection);
|
||||||
if (entityTimeManager == null) {
|
if (entityTimeManager == null) {
|
||||||
entityTimeManager = new EntityTimeManager(connection);
|
entityTimeManager = new EntityTimeManager(connection);
|
||||||
|
INSTANCES.put(connection, entityTimeManager);
|
||||||
}
|
}
|
||||||
return entityTimeManager;
|
return entityTimeManager;
|
||||||
}
|
}
|
||||||
|
@ -68,7 +69,6 @@ public class EntityTimeManager extends Manager {
|
||||||
|
|
||||||
private EntityTimeManager(XMPPConnection connection) {
|
private EntityTimeManager(XMPPConnection connection) {
|
||||||
super(connection);
|
super(connection);
|
||||||
INSTANCES.put(connection, this);
|
|
||||||
if (autoEnable)
|
if (autoEnable)
|
||||||
enable();
|
enable();
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ public class ForwardedTest {
|
||||||
fwd = (Forwarded) new ForwardedProvider().parseExtension(parser);
|
fwd = (Forwarded) new ForwardedProvider().parseExtension(parser);
|
||||||
|
|
||||||
// no delay in packet
|
// no delay in packet
|
||||||
assertEquals(null, fwd.getDelayInfo());
|
assertEquals(null, fwd.getDelayInformation());
|
||||||
|
|
||||||
// check message
|
// check message
|
||||||
assertEquals("romeo@montague.com", fwd.getForwardedPacket().getFrom());
|
assertEquals("romeo@montague.com", fwd.getForwardedPacket().getFrom());
|
||||||
|
|
Loading…
Reference in a new issue