SMACK XEP-313 Implementation
Asked Answered
T

2

9

It is necessary to attach a 0313 XEP (0.2) http://xmpp.org/extensions/attic/xep-0313-0.2.html#sect-idp616432 in to SMACK/ASMACK. I create PacketExtension for this.

    public class Archive313 implements PacketExtension {
    static final public String NAMESPACE = "urn:xmpp:mam:tmp";
    static final public String ELEMENT = "result";
    private String Id;
    private String Queryid;
    private String Stamp;
    private String To;
    private String From;
    private String Type;
    private String Body;

    public Archive313(String id, String queryid, String stamp, String to, String from, String type, String body) {
        Id = id;
        Queryid = queryid;
        Stamp = stamp;
        To = to;
        From = from;
        Type = type;
        Body = body;
    }

    @Override
    public String getElementName() {
        return ELEMENT;
    }

    @Override
    public String getNamespace() {
        return NAMESPACE;
    }

    @Override
    public String toXML() {
        return "<" + ELEMENT + " xmlns='" + NAMESPACE + "' queryid='" + Queryid + "' id='" + Id + "'>" +
                "<forwarded xmlns='urn:xmpp:forward:0'>" +
                "<delay xmlns='urn:xmpp:delay' stamp='" + Stamp + "'/><message to='"
                + To + "' from='" + From + "' type='" + Type + "'><body>" + Body + "</body></message></forwarded></result>";
    }

    public static class Archive313Provider implements PacketExtensionProvider {

        @Override
        public PacketExtension parseExtension(XmlPullParser xmlPullParser) throws Exception {
            String tag_name = "";
            String id = "";
            String queryid = "";
            String stamp = "";
            String to = "";
            String from = "";
            String type = "";
            String body = "";

            while (xmlPullParser.getEventType() != XmlPullParser.END_DOCUMENT) {
                switch (xmlPullParser.getEventType()) {
                    case XmlPullParser.START_TAG:
                        tag_name = xmlPullParser.getName();
                        for (int i = 0; i < xmlPullParser.getAttributeCount(); i++) {
                            if (tag_name != null && tag_name.equals("result")) {
                                if (xmlPullParser.getAttributeName(i).equals("queryid")) {
                                    queryid = xmlPullParser.getAttributeValue(i);
                                }
                                if (xmlPullParser.getAttributeName(i).equals("id")) {
                                    id = xmlPullParser.getAttributeValue(i);
                                }
                            }

                            if (tag_name != null && tag_name.equals("delay")) {
                                if (xmlPullParser.getAttributeName(i).equals("stamp")) {
                                    stamp = xmlPullParser.getAttributeValue(i);
                                }
                            }

                            if (tag_name != null && tag_name.equals("message") && xmlPullParser.getAttributeCount() > 2) {
                                if (xmlPullParser.getAttributeName(i).equals("to")) {
                                    to = xmlPullParser.getAttributeValue(i);
                                }

                                if (xmlPullParser.getAttributeName(i).equals("from")) {
                                    from = xmlPullParser.getAttributeValue(i);
                                }

                                if (xmlPullParser.getAttributeName(i).equals("type")) {
                                    type = xmlPullParser.getAttributeValue(i);
                                }
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        tag_name = xmlPullParser.getName();
                        break;
                    case XmlPullParser.TEXT:
                        if ("body".equals(tag_name)) {
                            body = xmlPullParser.getText();
                        }
                        break;
                    default:
                        break;
                }
                xmlPullParser.next();
            }

            return new Archive313(id,queryid,stamp,to,from,type,body);
        }
    }
}

ProviderManager pm = ProviderManager.getInstance();

pm.addExtensionProvider("result", "urn:xmpp:mam:tmp", new Archive313.Archive313Provider());

mXMPPConnection.addPacketListener(archiveListener, new PacketExtensionFilter("result", "urn:xmpp:mam:tmp"));

private PacketListener archiveListener = new PacketListener() {

    @Override
    public void processPacket(Packet packet) {
        Log.d("archiveListener", packet.toXML());
    }
};

In this implementation I do not give any Packet to archiveListener.

If i set filter:

PacketFilter filter = new PacketFilter() {
    @Override
    public boolean accept(Packet packet) {
        if (packet.toXML().contains("urn:xmpp:mam:tmp")) {
            return true;
        }
        return false;
    }
};

I got packages but they are not full.

Need to be:

<message id='aeb213' to='[email protected]/chamber'>
  <result xmlns='urn:xmpp:mam:tmp' queryid='f27' id='28482-98726-73623'>
    <forwarded xmlns='urn:xmpp:forward:0'>
      <delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:08:25Z'/>
      <message to='[email protected]/balcony'
        from='[email protected]/orchard'
        type='chat'>
        <body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>
      </message>
    </forwarded>
  </result>
</message>

I got:

<message id='aeb213' to='[email protected]/chamber'>
  <result xmlns='urn:xmpp:mam:tmp' queryid='f27' id='28482-98726-73623'>
  </result>
</message>

In SMACK Logs I got full message.

Tar answered 26/3, 2015 at 15:27 Comment(6)
I suggest to read the smack documentation on providers.Longcloth
hi, I am also stuck at same point. Did you fixed? please helpBonaire
Probably you are using a server that does not support the feature. I developed an openfire plugin to achive itIllbred
By the way: XEP-0313: Message Archive Management - WARNING: Consideration of this document has been Deferred by the XMPP Standards Foundation. Implementation of the protocol described herein is not recommended.Illbred
hi, I am getting the actual response from server as log, as I have enabled debugging. but inside my packet filter, the packet doesn't have that "forwarded" or "delay" extensions/ elements that as shown by the log.Bonaire
Did you figure this out?Cangue
P
3

It looks like your code

ProviderManager pm = ProviderManager.getInstance();
pm.addExtensionProvider("result", "urn:xmpp:mam:tmp", new Archive313.Archive313Provider());

never calls. Check this. Commonly, this code must be placed in static block of main class which working with xmpp lib.

Playtime answered 5/6, 2016 at 14:53 Comment(0)
S
0

To add any extension you have to do three things.

  1. Create a custom extension class extending Extension/ExtensionElement.

  2. Create an extension provider extending ExtensionProvider.

  3. Add custom extension provider to provider manager.

Note : Extension provider will parse your extension from packet (XML) as per your requirement (onParse() method of provider). Every custom extension have to parsed manually and need to add custom provider to provider manager.

Santanasantayana answered 17/8, 2016 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.