F
F
ForhaxeD2011-12-01 17:53:30
XMPP
ForhaxeD, 2011-12-01 17:53:30

Java: smack xmpp custom extensions (Extension)?

And again for all of the day, very recently I created the question that smack works crookedly.
There I wrote: “Once again I remind you that the first problem exists independently from the second, they are somehow connected.”
Let me remind you once again the rephrased problem (under the cut):
There are self-written custom ( extensions ), I register it for the connection, add a listener, process, again, everything would be fine - but it works every other time.
As I understand it, this is my mistake, incorrect in the use of parsing, as a result of which the overall parsing of packets falls. But why this happens, I still do not understand.
Creating, registering, listening to an extension looks like this:
Initializing and throwing a listener:

ProviderManager pm = ProviderManager.getInstance();
pm.addExtensionProvider(TestEvent.elementname, TestEvent.namespace, new TestEvent.Provider());

...

OnlineListener onlineListener = new OnlineListener();
PacketExtensionFilter onlineFilter = new PacketExtensionFilter(TestEvent.namespace);
connection.addPacketListener(onlineListener, onlineFilter);

OnlineListener implementation :
public static class OnlineListener implements PacketListener
  {

    @Override
    public void processPacket(Packet packet)
    {
       TestEvent e = (TestEvent) packet.getExtension(TestEvent.namespace);
       
             Message message = new Message();
             message.setTo(e.getUser().aid + "@" + SERVER);
             
             OnlineEvent response = new OnlineEvent();
             message.addExtension(response);
             connection.sendPacket(message);
    }

Extension Implementation:
public class TestEvent implements PacketExtension {
    
    private UserRepresentation user;

    
    public static String namespace = "sometext:event:testevent"; 
    public static String elementname = "event";
    
    public ProfileviewEvent()
    {

    }
    
    public void setUser(UserRepresentation value) {
    	user = value;
    }
    
    public UserRepresentation getUser() {
        return user;
    }
 
    public String getElementName() {
        return ProfileviewEvent.elementname;
    }
 
    public String getNamespace() {
        return ProfileviewEvent.namespace;
    }
 
    public String toXML() {
        StringBuffer buf = new StringBuffer();
     
        buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
        if(user != null) buf.append(user.toXML());
        buf.append("</").append(getElementName()).append(">");
        
        return buf.toString();
    }
    
    public static class Provider implements PacketExtensionProvider
    {
 
        public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
        	TestEvent extension = new TestEvent();
            boolean done = false;
            while(!done) {
                int eventType = parser.next();
                breakpoint(parser.getName()); // ###
                if(eventType == XmlPullParser.START_TAG) {
                    if(parser.getName().equals("User")) 
                    {
                    	UserRepresentation user = new UserRepresentation();
                    	
                    	...
                		
                    	extension.setUser(user);
                        done = true;
                    }
                }
            }
            return extension;
        } 
    }
}

Not only does this method break the global parser (in the last question, it breaks the MUC), but tags that do not belong to the extension at all fly into the breakpoint; maybe they should be there, but it seemed illogical to me, because I pass the namespace and the name of the tag, it seems that the tags should be in the Provider parser only those that I passed to it (registered).
________
PS You don't need to attach listeners to the connector, just register the extension:
pm.addExtensionProvider(TestEvent.elementname, TestEvent.namespace, new TestEvent.Provider());

As it breaks the global parser (does not lie, but makes it work crookedly), it seems that it ignores the tegname and namespace.
I can't solve this problem for the second day :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lBen, 2013-12-05
@lBen

So did you solve this problem somehow? Answer here if you don't mind.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question