Unverified Commit c502b2b0 authored by Dave Cridland's avatar Dave Cridland Committed by GitHub

Merge pull request #910 from surevine/of1335-2

OF-1335 Part Two: Strip namespaces differently
parents 10caae62 61e38756
...@@ -377,11 +377,29 @@ public class XMPPPacketReader { ...@@ -377,11 +377,29 @@ public class XMPPPacketReader {
QName qname = (pp.getPrefix() == null) ? df.createQName(pp.getName(), pp.getNamespace()) : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace()); QName qname = (pp.getPrefix() == null) ? df.createQName(pp.getName(), pp.getNamespace()) : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace());
Element newElement; Element newElement;
// Do not qualify stanza element with certain namespaces. This makes those stanzas re-usable between, // Strip namespace from all default-namespaced elements if
// for example, c2s and s2s. This code prevents such qualification only for elements that have no // all ancestors have the same namespace and it's a content
// default namespace declared // namespace.
final boolean defaultNamespaceDeclared = parent == null || !parent.getNamespaceForPrefix("").getURI().equals(""); boolean dropNamespace = false;
if ( !defaultNamespaceDeclared && IGNORED_NAMESPACE_ON_STANZA.contains( qname.getNamespaceURI() ) ) { if (pp.getPrefix() == null && IGNORED_NAMESPACE_ON_STANZA.contains(qname.getNamespaceURI())) {
// Default namespaced element which is in a content namespace,
// so we'll drop. Example, stanzas, <message><body/></message>
dropNamespace = true;
for (Element el = parent; el != null; el = el.getParent()) {
final String defaultNS = el.getNamespaceForPrefix("").getURI();
if (defaultNS.equals("")) {
// We've cleared this one already, just bail.
break;
}
if (!defaultNS.equals(qname.getNamespaceURI())) {
// But if there's an ancestor element, we shouldn't drop
// after all. Example: forwarded message.
dropNamespace = false;
break;
}
}
}
if ( dropNamespace ) {
newElement = df.createElement(pp.getName()); newElement = df.createElement(pp.getName());
} }
else { else {
...@@ -393,8 +411,10 @@ public class XMPPPacketReader { ...@@ -393,8 +411,10 @@ public class XMPPPacketReader {
final String namespacePrefix = pp.getNamespacePrefix( i ); final String namespacePrefix = pp.getNamespacePrefix( i );
final String namespaceUri = pp.getNamespaceUri( i ); final String namespaceUri = pp.getNamespaceUri( i );
if ( namespacePrefix != null ) { if ( namespacePrefix != null ) {
newElement.addNamespace( namespacePrefix, namespaceUri ); newElement.addNamespace(namespacePrefix, namespaceUri);
} else if ( !( pp.getDepth() <= 2 && IGNORED_NAMESPACE_ON_STANZA.contains(namespaceUri) ) ) { } else if ( parent == null && IGNORED_NAMESPACE_ON_STANZA.contains( namespaceUri ) ) {
// Don't copy.
} else if ( !(dropNamespace && namespaceUri.equals( qname.getNamespaceURI() ) ) ) {
// Do not include certain default namespace on the root-element ('stream') or stanza level. This makes stanzas re-usable between, for example, c2s and s2s. // Do not include certain default namespace on the root-element ('stream') or stanza level. This makes stanzas re-usable between, for example, c2s and s2s.
newElement.addNamespace( "", namespaceUri ); newElement.addNamespace( "", namespaceUri );
} }
......
...@@ -141,4 +141,24 @@ public class XMPPPacketReaderTest ...@@ -141,4 +141,24 @@ public class XMPPPacketReaderTest
Assert.assertFalse( "'jabber:client' should not occur before 'something:else'", result.asXML().substring( 0, result.asXML().indexOf("something:else") ).contains( "jabber:client" ) ); Assert.assertFalse( "'jabber:client' should not occur before 'something:else'", result.asXML().substring( 0, result.asXML().indexOf("something:else") ).contains( "jabber:client" ) );
Assert.assertTrue( "'jabber:client' should occur after 'something:else'", result.asXML().substring( result.asXML().indexOf("something:else") ).contains( "jabber:client" ) ); Assert.assertTrue( "'jabber:client' should occur after 'something:else'", result.asXML().substring( result.asXML().indexOf("something:else") ).contains( "jabber:client" ) );
} }
/**
* Check that a websocket connection woudl also work.
*/
@Test
public void testStripNamespacesForWebsocket() throws Exception
{
final String input_header = "<open xmlns='urn:ietf:params:xml:ns:xmpp-framing' to='example.com' version='1.0' />";
final Document doc_header = packetReader.read( new StringReader( input_header ) );
final String input = " <message xmlns='jabber:client'>" +
" <other xmlns='something:else'>" +
" <message xmlns='jabber:client'/>" +
" </other>" +
" </message>";
final Document result = packetReader.read( new StringReader( input ) );
// Verify result.
Assert.assertFalse( "'jabber:client' should not occur before 'something:else'", result.asXML().substring( 0, result.asXML().indexOf("something:else") ).contains( "jabber:client" ) );
Assert.assertTrue( "'jabber:client' should occur after 'something:else'", result.asXML().substring( result.asXML().indexOf("something:else") ).contains( "jabber:client" ) );
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment