powered by Jive Software

SimplePayload intentions and usage

xep-0060

#1

I’m writing code to publish an item to a pubsub node, with a payload. Something like Example 99 found here.

So I’m using code like this :-

SimplePayload Payload = new SimplePayload( “entry”, “http://www.w3.org/2005/Atom”, “<stuff>a payload</stuff>” );
PayloadItem<SimplePayload> MessageItem = new PayloadItem<>(Payload);
LeafNode Node = m_PubSubManager.getLeafNode( “My Node Name” );
Node.publish( MessageItem );

But, so far as I can see SimplePayload does nothing with the elementName and namespace parameters that are passed to it. The toXML function just returns the payload.

<stuff>a payload</stuff>

So, I was wondering what the intention was for this class. At the moment it does little more than hold on to a CharSequence. But maybe I’m missing the point.

I had been expecting it to generate an XML element with the given name and namespace, and insert the payload as it’s content. i.e.

<entry xmlns=‘http://www.w3.org/2005/Atom’>
<stuff>a payload</stuff>
</entry>

Surely there’s some other XML building class which does this already - I’ll go and have a look…

Have fun !

Tim

Smack version - 4.3.0-beta1


#2

Afaiu you are supposed to hand over valid xml to the SimplePayload class. The name and namespace are supposed to be equal to the name and namespace of the content you pass it. So in your case you would call it like this:

SimplePayload Payload = new SimplePayload(“entry”, “http://www.w3.org/2005/Atom”,
        “<entry xmlns=‘http://www.w3.org/2005/Atom’><stuff>a payload</stuff></entry>”);

Now you have a payload item, which represents

<entry xmlns=‘http://www.w3.org/2005/Atom’>
<stuff>a payload</stuff>
</entry>

and which will return its element name and namespace.


#3

Yes… That strikes me as not being very useful ! Indeed, it’s rather redundant.

Well, no worries :slight_smile:


#4

Right, I’ve created SMACK-816.


#5

Just to be clear, I had expected it to build the XML using the element name and namespace, making the passed “payload” string the internal contents of that element. It would still need to be “correctly fomatted XML” - including being a simple string.

As a client/user of SimplePayload, when constructing a pubsub message to be sent, I know which element I want to use. At least in my use case.

It seems that SimplePayload is used as the default to hold a payload when a pubsub item is received from a node, and that the element and namespace are entered, so I presume they are parsed to this level. But I have yet to track down the source code where this happens.

I don’t think SimplePayload should have to parse any XML itself. That would make it complex :wink: !


#6

Also, I just spotted the SimplePayload usage here at the end of the “Publishing to a node” section - that will need an update, depending on what happens to the code…


#7

After a little bit of digging, I found the parsing code in ItemProvider, where I can see the parser provides the element name and namspace. It then gets the “payload” on line 61

CharSequence payloadText = PacketParserUtils.parseElement(parser, true);

Which returns the whole outer element. This could perhaps be replaced by PacketParserUtils.parseContent().

Then, I think SimplePayload should have

public CharSequence getPayload()
    {
        return payload;
    }

And the toXML() should construct the outer element around the payload. That way you can get the inner and outer XML out.

Nevertheless, this would leave the constuctor as a breaking change for any existing client code !

There’s no easy solution to this, I think…:cry:

I should probably keep quiet now, and go and implement my own ExtensionElementProvider


#8

Thats definitely the correct way to handle custom extensions :slight_smile:


#9

For sure - it’s just that I am very lazy passionate about code re-use, and SimplePayload almost gives me what I want (an element with some text in).