1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 06:12:05 +01:00

Make PubSub's SimplePayload infer the XML Element name and namespace

Fixes SMACK-816.
This commit is contained in:
Florian Schmaus 2018-04-23 22:00:12 +02:00
parent a3e365bfb9
commit 6c4a02691e
5 changed files with 94 additions and 8 deletions

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2017 Florian Schmaus
* Copyright © 2014-2018 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,6 +23,9 @@ import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import org.jivesoftware.smack.SmackException;
import org.jxmpp.jid.EntityBareJid;
@ -250,4 +253,14 @@ public class ParserUtils {
public static String getXmlLang(XmlPullParser parser) {
return parser.getAttributeValue("http://www.w3.org/XML/1998/namespace", "lang");
}
public static QName getQName(XmlPullParser parser) {
String elementName = parser.getName();
String prefix = parser.getPrefix();
if (prefix == null) {
prefix = XMLConstants.DEFAULT_NS_PREFIX;
}
String namespace = parser.getNamespace();
return new QName(namespace, elementName, prefix);
}
}

View file

@ -16,7 +16,17 @@
*/
package org.jivesoftware.smackx.pubsub;
import java.io.IOException;
import javax.xml.namespace.QName;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
/**
* The default payload representation for {@link PayloadItem#getPayload()}. It simply
@ -27,7 +37,29 @@ import org.jivesoftware.smack.packet.ExtensionElement;
public class SimplePayload implements ExtensionElement {
private final String elemName;
private final String ns;
private final CharSequence payload;
private final String payload;
/**
* Construct a <tt>SimplePayload</tt> object with the specified element name,
* namespace and content. The content must be well formed XML.
*
* @param xmlPayload The payload data
*/
public SimplePayload(String xmlPayload) {
XmlPullParser parser;
try {
parser = PacketParserUtils.getParserFor(xmlPayload);
}
catch (XmlPullParserException | IOException e) {
throw new AssertionError(e);
}
QName qname = ParserUtils.getQName(parser);
payload = xmlPayload;
elemName = StringUtils.requireNotNullOrEmpty(qname.getLocalPart(), "Could not determine element name from XML payload");
ns = StringUtils.requireNotNullOrEmpty(qname.getNamespaceURI(), "Could not determine namespace from XML payload");
}
/**
* Construct a <tt>SimplePayload</tt> object with the specified element name,
@ -36,11 +68,18 @@ public class SimplePayload implements ExtensionElement {
* @param elementName The root element name (of the payload)
* @param namespace The namespace of the payload, null if there is none
* @param xmlPayload The payload data
* @deprecated use {@link #SimplePayload(String)} insteas.
*/
// TODO: Remove in Smack 4.5
@Deprecated
public SimplePayload(String elementName, String namespace, CharSequence xmlPayload) {
elemName = elementName;
payload = xmlPayload;
ns = namespace;
this(xmlPayload.toString());
if (!elementName.equals(this.elemName)) {
throw new IllegalArgumentException();
}
if (!namespace.equals(this.ns)) {
throw new IllegalArgumentException();
}
}
@Override
@ -54,7 +93,7 @@ public class SimplePayload implements ExtensionElement {
}
@Override
public CharSequence toXML() {
public String toXML() {
return payload;
}

View file

@ -59,7 +59,7 @@ public class ItemProvider extends ExtensionElementProvider<Item> {
if (extensionProvider == null) {
// TODO: Should we use StandardExtensionElement in this case? And probably remove SimplePayload all together.
CharSequence payloadText = PacketParserUtils.parseElement(parser, true);
return new PayloadItem<>(itemNamespace, id, node, new SimplePayload(payloadElemName, payloadNS, payloadText));
return new PayloadItem<>(itemNamespace, id, node, new SimplePayload(payloadText.toString()));
}
else {
return new PayloadItem<>(itemNamespace, id, node, extensionProvider.parse(parser));

View file

@ -75,7 +75,7 @@ public class ItemValidationTest extends InitExtensions {
@Test
public void verifyPayloadItem() throws Exception {
SimplePayload payload = new SimplePayload(null, null, "<data>This is the payload</data>");
SimplePayload payload = new SimplePayload("<data xmlns='https://example.org'>This is the payload</data>");
PayloadItem<SimplePayload> simpleItem = new PayloadItem<>(payload);
String simpleCtrl = "<item xmlns='http://jabber.org/protocol/pubsub'>" + payload.toXML() + "</item>";

View file

@ -0,0 +1,34 @@
/**
*
* Copyright 2018 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class SimplePayloadTest {
@Test
public void simplePayloadTest() {
String xmlPayload = "<element xmlns='https://example.org'><foo>Test</foo><bar/></element>";
SimplePayload simplePayload = new SimplePayload(xmlPayload);
assertEquals("element", simplePayload.getElementName());
assertEquals("https://example.org", simplePayload.getNamespace());
assertEquals(xmlPayload, simplePayload.toXML());
}
}