How to send a XMPP IQ Package to a PHP JAXL-Script?

643 views Asked by At

ive a problem. Im using asmack for connecting and sending Data between Android and OpenFire.

Now i'ld like to get this data with PHP and Jaxl (which is working fine).

The problem is that i modify the sent message (to JSON and XML) and add a HEADER to the XML/JSON. This seems to work well but its not a proper solution.

Now i'm thinking about sending IQ Packages which hold the Header information already.

This is the Part in Android/Java.

      CommandIQPacket iq1 = new CustomIQPacket();
            iq1.setTo(to);
            iq1.setType(IQ.Type.GET);
            iq1.setCommand("test");
            iq1.setPriority(1);
            iq1.setJid(message.getMessage());
            iq1.setDate(new Date(System.currentTimeMillis()));
        mConnection.sendPacket(iq1);

The CustomIQPacket class extends IQ and add the required data by overriding getChildElementXML().

Sample:

public class CommandIQPacket extends IQ {


private int priority;
private String jid, command;
//some getter and setter

@Override
public CharSequence getChildElementXML() {

    StringBuilder builder = new StringBuilder("<query xmlns=\"urn:xmpp:ts\">");
    builder.append("<jid command=\"");
    builder.append(command).append("\"");
    builder.append(" date=\"");
    builder.append(date).append("\"");
    builder.append(" priority=\"");
    builder.append(priority).append("\"");
    builder.append(">");
    builder.append(jid);
    builder.append("</jid>");
    builder.append("</query>");
    return builder.toString();

}

}

Now i've my PHP CLI Script with JAXL. This looks like that.

 <?php

  // configs, stuff, etc. 

$client = new JAXL(array(
'jid' => "myopenfireuser",
'pass' => "myopenfirepass",
'bosh_url' => "http://url-to-bosh.com:7070/http-bind/",
'log_level' => JAXL_INFO,
'strict' => TRUE
    ));

$client->require_xep(array(
'0115', // Entity Capabilities
'0203', // Delayed Delivery
'0114', // jabber component protocol
'1337'
));

ive added 1337 here, which should be the XEP for parsing the IQ package.

$client->add_cb('on_chat_message', function($stanza) {
global $client;

$iq = $client->xeps['1337']->get_raw_iq_pkt();
  // some more stuff
}

Now it seems to be that $iq does not hold any rawdata. I cant access the XML which means that i cant parse the IQ Package.

The 1337 XEP looks like that.

class XEP_1337 extends XMPPXep {

public function init() {
    return array(
        'on_get_iq' => 'on_xmpp_iq'
    );
}

public function get_raw_iq_pkt() {

    $attrs = array(
        'type' => 'get',
        'from' => $this->jaxl->full_jid->to_string(),
        'to' => $this->jaxl->full_jid->domain
    );

    return $this->jaxl->get_iq_pkt(
                    $attrs, new JAXLXml('query', "ts")
    );
} 
}

I couldnt find any good tutorial about JAXL and IQ Packages. This is the only readme ive found about it (https://media.readthedocs.org/pdf/jaxl/latest/jaxl.pdf)

The sent data is for example

 <iq id='qEfml-14' to='[email protected]/Ressource' type='get'><query xmlns="ts"><jid command="test" date="Mon Jul 28 12:36:15 MESZ 2014" priority="1">...</query></iq>

the return value from openfire (jaxl) is

<iq type="error" id="NP3HP-12" from="[email protected]" to="[email protected]/Ressource"><query xmlns="ts" action="create"><jid command="test" date="Mon Jul 28 23:04:22 MESZ 2014" priority="1">...</jid></query><error code="503" type="cancel"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>

OpenFire error=

     at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.run(ExecutorFilter.java:283) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51) 
at java.lang.Thread.run(Thread.java:701) 
2014.07.29 15:56:33 org.jivesoftware.openfire.nio.ConnectionHandler - Closing connection due to error while processing message: <iq id="769-83772" to="ts.com" from="ts.com/4140f14a" type="result"></iq> 
java.lang.NullPointerException 
at org.jivesoftware.openfire.IQRouter.route(IQRouter.java:115) 
at org.jivesoftware.openfire.spi.PacketRouterImpl.route(PacketRouterImpl.java:76) 
at org.jivesoftware.openfire.net.StanzaHandler.processIQ(StanzaHandler.java:330) 
at org.jivesoftware.openfire.net.ClientStanzaHandler.processIQ(ClientStanzaHandler.java:93) 
at org.jivesoftware.openfire.net.StanzaHandler.process(StanzaHandler.java:295) 
at org.jivesoftware.openfire.net.StanzaHandler.process(StanzaHandler.java:187) 
at org.jivesoftware.openfire.nio.ConnectionHandler.messageReceived(ConnectionHandler.java:181) 
at org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.messageReceived(AbstractIoFilterChain.java:570) 
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299) 
at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(AbstractIoFilterChain.java:53) 
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageReceived(AbstractIoFilterChain.java:648) 
at org.apache.mina.common.IoFilterAdapter.messageReceived(IoFilterAdapter.java:80) 
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299) 
at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(AbstractIoFilterChain.java:53) 
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageReceived(AbstractIoFilterChain.java:648) 
at org.apache.mina.filter.codec.support.SimpleProtocolDecoderOutput.flush(SimpleProtocolDecoderOutput.java:58) 
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:185) 
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299) 
at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(AbstractIoFilterChain.java:53) 
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageReceived(AbstractIoFilterChain.java:648) 
at org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorFilter.java:239) 
at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.run(ExecutorFilter.java:283) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51) 
at java.lang.Thread.run(Thread.java:701) 
1

There are 1 answers

1
Emanuel On

This is what ive found out now:

Its important that to has the fullname with ressource.  user@domain/Ressource

And.. the Receiving client must be online. Offline messages dont get accepted for IQ PAckets from OpenFire (by default)

And.. regarding this article it must be registered somehow. How can this be done in jaxl? i thought adding the XEP and using init() is enough. Sending and Receiving custom XMPP IQs with aSmack on Android

And.. ive modified the PAcketListener but still same result.

        PacketFilter filter = new PacketFilter() {
            @Override
            public boolean accept(Packet packet) {
                return true;
            }
        };

        mConnection.addPacketListener(mPacketListener, filter);
        ProviderManager.addIQProvider("query", "ts", new CommandIQProvider());

while mPacketListener is a class which extends from PacketListener.

 public class ChatPacketListener implements PacketListener {

public void processPacket(Packet packet) {
    if (packet instanceof  Message) {
        Message message = (Message) packet;
        String from = message.getFrom();
        if (message.getBody() != null) {

            Log.d("ChatPacketLsitener", "XMPP packet received - sending Intent: " + XmppService.ACTION_XMPP_MESSAGE_RECEIVED);
            XmppService.maybeAcquireWakeLock();
            XmppTools.startSvcXMPPMsg(mCtx, message.getBody(), from);
        }
    } else {
        Log.e("Packet", "Packet is not from type Message. Its from type " + packet.getClass());

    }
}