From 61fd3c9dd0971323cc5624362336e06136044d5f Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 14 Apr 2014 09:07:42 +0200 Subject: [PATCH] Add FromMode regarding 'from' attribute of outgoing stanzas XMPP Servers should ignore the 'from' attribute of outgoing stanzas. Makes the behavior how Smack populates the 'from' attribute of outgoing stanzas configurable. Fixes SMACK-547 --- .../jivesoftware/smack/XMPPConnection.java | 52 +++++++++++++++++++ .../jivesoftware/smackx/ping/PingTest.java | 1 - 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java b/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java index 517d1c4a6..a6783d932 100644 --- a/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java +++ b/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java @@ -184,6 +184,11 @@ public abstract class XMPPConnection { */ private String serviceCapsNode; + /** + * Defines how the from attribute of outgoing stanzas should be handled. + */ + private FromMode fromMode = FromMode.OMITTED; + /** * Stores whether the server supports rosterVersioning */ @@ -426,6 +431,17 @@ public abstract class XMPPConnection { if (packet == null) { throw new NullPointerException("Packet is null."); } + switch (fromMode) { + case OMITTED: + packet.setFrom(null); + break; + case USER: + packet.setFrom(getUser()); + break; + case UNCHANGED: + default: + break; + } // Invoke interceptors for the new packet that is about to be sent. Interceptors may modify // the content of the packet. firePacketInterceptors(packet); @@ -1104,4 +1120,40 @@ public abstract class XMPPConnection { public int getConnectionCounter() { return connectionCounterValue; } + + public static enum FromMode { + /** + * Leave the 'from' attribute unchanged. This is the behavior of Smack < 4.0 + */ + UNCHANGED, + /** + * Omit the 'from' attribute. According to RFC 6120 8.1.2.1 1. XMPP servers "MUST (...) + * override the 'from' attribute specified by the client". It is therefore safe to specify + * FromMode.OMITTED here. + */ + OMITTED, + /** + * Set the from to the clients full JID. This is usually not required. + */ + USER + } + + /** + * Set the FromMode for this connection instance. Defines how the 'from' attribute of outgoing + * stanzas should be populated by Smack. + * + * @param fromMode + */ + public void setFromMode(FromMode fromMode) { + this.fromMode = fromMode; + } + + /** + * Get the currently active FromMode. + * + * @return the currently active {@link FromMode} + */ + public FromMode getFromMode() { + return this.fromMode; + } } diff --git a/extensions/src/test/java/org/jivesoftware/smackx/ping/PingTest.java b/extensions/src/test/java/org/jivesoftware/smackx/ping/PingTest.java index 1b98422f2..941eb8fbd 100644 --- a/extensions/src/test/java/org/jivesoftware/smackx/ping/PingTest.java +++ b/extensions/src/test/java/org/jivesoftware/smackx/ping/PingTest.java @@ -67,7 +67,6 @@ public class PingTest extends InitExtensions { assertTrue(pongPacket instanceof IQ); IQ pong = (IQ) pongPacket; - assertEquals("juliet@capulet.lit/balcony", pong.getFrom()); assertEquals("capulet.lit", pong.getTo()); assertEquals("s2c1", pong.getPacketID()); assertEquals(IQ.Type.RESULT, pong.getType());