mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 01:29:38 +02: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
|
@ -20,6 +20,7 @@ package org.jivesoftware.smackx.chatstates.packet;
|
|||
import org.jivesoftware.smackx.chatstates.ChatState;
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
/**
|
||||
|
@ -31,7 +32,9 @@ import org.xmlpull.v1.XmlPullParser;
|
|||
*/
|
||||
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.
|
||||
|
@ -42,16 +45,21 @@ public class ChatStateExtension implements PacketExtension {
|
|||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return state.name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
return "http://jabber.org/protocol/chatstates";
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
return "<" + getElementName() + " xmlns=\"" + getNamespace() + "\" />";
|
||||
@Override
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
}
|
||||
|
||||
public static class Provider implements PacketExtensionProvider {
|
||||
|
|
|
@ -44,7 +44,7 @@ public class DelayInformationManager {
|
|||
* @return the Delayed Delivery information or <code>null</code>
|
||||
*/
|
||||
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>
|
||||
*/
|
||||
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 org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
import org.jxmpp.util.XmppDateTime;
|
||||
|
@ -96,16 +97,17 @@ public class DelayInformation implements PacketExtension {
|
|||
}
|
||||
|
||||
@Override
|
||||
public CharSequence toXML() {
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.attribute("stamp", XmppDateTime.formatXEP0082Date(stamp));
|
||||
xml.optAttribute("from", from);
|
||||
xml.rightAngelBracket();
|
||||
if (reason != null) {
|
||||
xml.escape(reason);
|
||||
}
|
||||
xml.optAppend(reason);
|
||||
xml.closeElement(this);
|
||||
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");
|
||||
* 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.PacketExtension;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
import org.jivesoftware.smackx.delay.packet.DelayInformation;
|
||||
|
||||
/**
|
||||
|
@ -27,7 +28,7 @@ import org.jivesoftware.smackx.delay.packet.DelayInformation;
|
|||
*/
|
||||
public class Forwarded implements PacketExtension {
|
||||
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 Packet forwardedPacket;
|
||||
|
@ -54,7 +55,7 @@ public class Forwarded implements PacketExtension {
|
|||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT_NAME;
|
||||
return ELEMENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -63,17 +64,17 @@ public class Forwarded implements PacketExtension {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<").append(getElementName()).append(" xmlns=\"")
|
||||
.append(getNamespace()).append("\">");
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.rightAngelBracket();
|
||||
xml.optElement(getDelayInformation());
|
||||
xml.append(forwardedPacket.toXML());
|
||||
xml.closeElement(this);
|
||||
return xml;
|
||||
}
|
||||
|
||||
if (delay != null)
|
||||
buf.append(delay.toXML());
|
||||
buf.append(forwardedPacket.toXML());
|
||||
|
||||
buf.append("</").append(getElementName()).append(">");
|
||||
return buf.toString();
|
||||
public static Forwarded 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");
|
||||
* 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);
|
||||
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;
|
||||
}
|
||||
if (packet == null)
|
||||
|
|
|
@ -54,11 +54,10 @@ public class LastActivity extends IQ {
|
|||
@Override
|
||||
public XmlStringBuilder getChildElementXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder();
|
||||
xml.halfOpenElement("query");
|
||||
xml.halfOpenElement(IQ.QUERY_ELEMENT);
|
||||
xml.xmlnsAttribute(NAMESPACE);
|
||||
if (lastActivity != -1) {
|
||||
xml.attribute("seconds", Long.toString(lastActivity));
|
||||
}
|
||||
xml.optLongAttribute("seconds", lastActivity);
|
||||
|
||||
// We don't support adding the optional message attribute, because it is usually only added
|
||||
// by XMPP servers and not by client entities.
|
||||
xml.closeEmptyElement();
|
||||
|
@ -108,10 +107,6 @@ public class LastActivity extends IQ {
|
|||
}
|
||||
|
||||
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();
|
||||
String seconds = parser.getAttributeValue("", "seconds");
|
||||
if (seconds != null) {
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
package org.jivesoftware.smackx.iqversion;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
|
@ -50,8 +49,7 @@ import org.jivesoftware.smackx.iqversion.packet.Version;
|
|||
* @author Georg Lukas
|
||||
*/
|
||||
public class VersionManager extends Manager {
|
||||
private static final Map<XMPPConnection, VersionManager> instances =
|
||||
Collections.synchronizedMap(new WeakHashMap<XMPPConnection, VersionManager>());
|
||||
private static final Map<XMPPConnection, VersionManager> INSTANCES = new WeakHashMap<XMPPConnection, VersionManager>();
|
||||
|
||||
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) {
|
||||
super(connection);
|
||||
instances.put(connection, this);
|
||||
|
||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
sdm.addFeature(Version.NAMESPACE);
|
||||
|
@ -83,10 +80,11 @@ public class VersionManager extends Manager {
|
|||
}
|
||||
|
||||
public static synchronized VersionManager getInstanceFor(XMPPConnection connection) {
|
||||
VersionManager versionManager = instances.get(connection);
|
||||
VersionManager versionManager = INSTANCES.get(connection);
|
||||
|
||||
if (versionManager == null) {
|
||||
versionManager = new VersionManager(connection);
|
||||
INSTANCES.put(connection, versionManager);
|
||||
}
|
||||
|
||||
return versionManager;
|
||||
|
|
|
@ -60,6 +60,7 @@ public class EntityTimeManager extends Manager {
|
|||
EntityTimeManager entityTimeManager = INSTANCES.get(connection);
|
||||
if (entityTimeManager == null) {
|
||||
entityTimeManager = new EntityTimeManager(connection);
|
||||
INSTANCES.put(connection, entityTimeManager);
|
||||
}
|
||||
return entityTimeManager;
|
||||
}
|
||||
|
@ -68,7 +69,6 @@ public class EntityTimeManager extends Manager {
|
|||
|
||||
private EntityTimeManager(XMPPConnection connection) {
|
||||
super(connection);
|
||||
INSTANCES.put(connection, this);
|
||||
if (autoEnable)
|
||||
enable();
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ public class ForwardedTest {
|
|||
fwd = (Forwarded) new ForwardedProvider().parseExtension(parser);
|
||||
|
||||
// no delay in packet
|
||||
assertEquals(null, fwd.getDelayInfo());
|
||||
assertEquals(null, fwd.getDelayInformation());
|
||||
|
||||
// check message
|
||||
assertEquals("romeo@montague.com", fwd.getForwardedPacket().getFrom());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue