2

Format of result will be this.

<iq from='52@localhost' to='20@localhost/Gajim' id='253' type='result'>
<query xmlns='someName'>
<item subscription='both' jid='1@localhost'/>
</query>
</iq>

I am trying to send a custom iq query with the following format.

 <iq xmlns="Name" type="get" id="253">
    <query xmlns="someName">
    <auth type='token'>asd</auth>
    </query>
    </iq>

From this I understand that I need to send a query with a authorization type token(token id ). Here is my try at that.

final IQ iq = new IQ() {  
  @Override  
  public String getChildElementXML() {  
    return "<query xmlns='someName'auth type="+t_id"+"asd<................'</query>"; // I am confused on how to write here  
  }  
};  
iq.setType(IQ.Type.get);  
connection.sendPacket(iq); // connection is an XMPPTCPConnection object.

I am confused on how to complete this getChildElementXML() and furthermore I get an error when I try to instantiate a new IQ because I need to implement some builder method. Should I create a new class for sending custom IQ queries?Can someone show how to do it?

Note: Constructive feedback is appreciated, I can make the question clearer if someone points out an ambiguity.

Prethia
  • 1,183
  • 3
  • 11
  • 26

1 Answers1

2

This will answer your question but keep in mind in next step you'll need something like this: Mapping Openfire Custom plugin with aSmack Client


Generally speaking, ID it's created by smack API and you don't deserve to assign it manually.

Generally speaking, xmnls but to be assigned to custom tag and not IQ itself.

Our target:

 <iq from="me@domain" to="domain" type="get" id="253">
    <query xmlns="someName">
    <auth type='token'>asd</auth>
    </query>
    </iq>

How your class will look like:

package ....;

import org.jivesoftware.smack.packet.IQ;



public class IQCustomAuth extends IQ
{
public final static String childElementName = "query";
public final static String childElementNamespace = "com:prethia:query#auth";


private final String auth;
private final String typeAuth;

public IQCustomAuth(String userFrom, String server, String typeAuth, String auth)
{

    super( childElementName, childElementNamespace );
    this.setType( IQ.Type.get );
    this.auth = auth;
    this.typeAuth = typeAuth;
    setTo( server );
    setFrom( userFrom );
}



@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder( IQChildElementXmlStringBuilder xml )
{

    xml.rightAngleBracket();
    xml.halfOpenElement( "auth ");
    xml.attribute( "type", this.typeAuth );
    xml.rightAngleBracket();
    xml.append(auth);
    xml.closeElement("auth");
    return xml;
}




}

To test:

IQCustomAuth iq = new IQCustomAuth( "me@domain", "domain", "token", "asd" );
System.out.println(iq.toString());

to Send:

connection.sendPacket(new IQCustomAuth( "me@domain", "domain", "token", "asd" ));
MrPk
  • 2,862
  • 2
  • 20
  • 26
  • I will let you know, I am going to try it now. By the way you failed to format the code – Prethia Dec 28 '16 at 15:51
  • By the way I did not have a to field in header. Is it the server I am using for XMPP connection? – Prethia Dec 28 '16 at 16:03
  • connection.getServiceName() (connection is the object of AbstractXMPPConnection). In theory you can avoid to set the "to" but it's possible to lost the packet under certain conditions – MrPk Dec 28 '16 at 16:17
  • Hi your answer works I am upvoting it and I will accept it as an answer but I could not get the answer with the format you gave in other answer. Can you adjust this answer to that response? I am providing the format of the result – Prethia Dec 29 '16 at 01:14
  • I understaind your trouble but implements a full custom iq it's out of the question and too large topic for a comment. Take a look to first link in my answer. However seems you are tryin to implement somwthing old like https://xmpp.org/extensions/xep-0078.html, maybe there is some api ready to manage this. Kind of request – MrPk Dec 29 '16 at 08:29