1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49: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:
Florian Schmaus 2014-07-16 11:46:30 +02:00
parent 1ed5c48bcc
commit 49ee058c38
14 changed files with 105 additions and 90 deletions

View file

@ -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.
@ -16,7 +16,6 @@
*/
package org.jivesoftware.smackx.carbons;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
@ -49,8 +48,7 @@ import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
*/
public class CarbonManager extends Manager {
private static Map<XMPPConnection, CarbonManager> instances =
Collections.synchronizedMap(new WeakHashMap<XMPPConnection, CarbonManager>());
private static Map<XMPPConnection, CarbonManager> INSTANCES = new WeakHashMap<XMPPConnection, CarbonManager>();
static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@ -66,7 +64,6 @@ public class CarbonManager extends Manager {
super(connection);
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
sdm.addFeature(CarbonExtension.NAMESPACE);
instances.put(connection, this);
}
/**
@ -77,16 +74,17 @@ public class CarbonManager extends Manager {
* @return a CarbonManager instance
*/
public static synchronized CarbonManager getInstanceFor(XMPPConnection connection) {
CarbonManager carbonManager = instances.get(connection);
CarbonManager carbonManager = INSTANCES.get(connection);
if (carbonManager == null) {
carbonManager = new CarbonManager(connection);
INSTANCES.put(connection, carbonManager);
}
return carbonManager;
}
private IQ carbonsEnabledIQ(final boolean new_state) {
private static IQ carbonsEnabledIQ(final boolean new_state) {
IQ setIQ = new IQ() {
public String getChildElementXML() {
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.
*
* @return true if supported
* @throws SmackException if there was no response from the server.
* @throws XMPPException
* @throws NotConnectedException
* @throws XMPPErrorException
* @throws NoResponseException
*/
public boolean isSupportedByServer() throws XMPPException, SmackException {
public boolean isSupportedByServer() throws NoResponseException, XMPPErrorException, NotConnectedException {
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(
connection().getServiceName(), CarbonExtension.NAMESPACE);
}
@ -184,20 +183,6 @@ public class CarbonManager extends Manager {
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.
*

View file

@ -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.
@ -16,7 +16,9 @@
*/
package org.jivesoftware.smackx.carbons.packet;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.forward.Forwarded;
/**
@ -33,8 +35,8 @@ import org.jivesoftware.smackx.forward.Forwarded;
public class CarbonExtension implements PacketExtension {
public static final String NAMESPACE = "urn:xmpp:carbons:2";
private Direction dir;
private Forwarded fwd;
private final Direction dir;
private final Forwarded fwd;
/**
* Construct a Carbon message extension.
@ -67,7 +69,7 @@ public class CarbonExtension implements PacketExtension {
@Override
public String getElementName() {
return dir.toString();
return dir.name();
}
@Override
@ -76,15 +78,29 @@ public class CarbonExtension 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.append(fwd.toXML());
xml.closeElement(this);
return xml;
}
buf.append(fwd.toXML());
buf.append("</").append(getElementName()).append(">");
return buf.toString();
/**
* Obtain a Carbon from a message, if available.
* <p>
* 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 final String ELEMENT = "private";
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public String getNamespace() {
return CarbonExtension.NAMESPACE;
}
@Override
public String toXML() {
return "<" + ELEMENT + " xmlns=\"" + CarbonExtension.NAMESPACE + "\"/>";
return "<" + ELEMENT + " xmlns='" + CarbonExtension.NAMESPACE + "'/>";
}
}
}

View file

@ -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.
@ -26,7 +26,7 @@ import org.xmlpull.v1.XmlPullParser;
/**
* 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
*
@ -41,7 +41,7 @@ public class CarbonManagerProvider implements PacketExtensionProvider {
while (!done) {
int eventType = parser.next();
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()))
done = true;

View file

@ -20,30 +20,23 @@ import static org.junit.Assert.assertEquals;
import java.util.Properties;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.ExperimentalInitializerTest;
import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
import org.jivesoftware.smackx.carbons.provider.CarbonManagerProvider;
import org.jivesoftware.smackx.forward.Forwarded;
import org.jivesoftware.smackx.forward.provider.ForwardedProvider;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import com.jamesmurty.utils.XMLBuilder;
public class CarbonTest {
public class CarbonTest extends ExperimentalInitializerTest {
private static Properties outputProperties = new Properties();
static {
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
public void carbonSentTest() throws Exception {
XmlPullParser parser;
@ -66,7 +59,7 @@ public class CarbonTest {
assertEquals(CarbonExtension.Direction.sent, cc.getDirection());
// no delay in packet
assertEquals(null, fwd.getDelayInfo());
assertEquals(null, fwd.getDelayInformation());
// check message
assertEquals("romeo@montague.com", fwd.getForwardedPacket().getFrom());