Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMACK XEP-313 Implementation

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.

like image 432
user3840728 Avatar asked Mar 26 '15 15:03

user3840728


1 Answers

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.

like image 161
Nik Avatar answered Nov 02 '22 23:11

Nik