Tuesday 2 April 2013

Openfire Custom IQ Handler


XMPP is providing plenty of Rosters. That we can use to develop a full fledged chat application. But Openfire is delighting its developers by providing custom plugins development.

I have been needed to add a plugin that would be helpful to search users from the server. On the application side i needed a list of users with some patterns.

I have developed following plugin code for that. This plugin gets installed on server side. It mainly deals with a custom IQ. Whenever application will request for that particular custom IQ. Server will pass flow to this custom plugin.

For developing custom iq. You need to extend IQHandler class. As well need to implement some of the methods from that class to provide your functionality.

public class SearchIQhanlder extends IQHandler { // extend IQ handler class to create a custom iq request
    private IQHandlerInfo info;
    
    public SearchIQhanlder(String moduleName)
    {
  super(moduleName);
  info = new IQHandlerInfo("query", "custom:iq:Search"); // provide a custom unique query signature for your IQ
    }
    
    public IQHandlerInfo getInfo()
    {
 return info;
    }
    
    public IQ handleIQ(IQ packet) throws UnauthorizedException{ // this method will handle the request. all login will go into this method.
 IQ result = IQ.createResultIQ(packet);
 try {
  // if you want to handle specific query. you need to check for its type. I needed this as i have wrote the plugin to provide some information to application. If you are setting some values on server side. You should use set type.
  IQ.Type type = packet.getType(); 
  result.setChildElement("query", "custom:iq:Search");
  Element query = result.getChildElement();
  // conditionally hanlde get or set type of IQ. I was not using set type. So i have make it as "not acceptable"
  if (type.equals(IQ.Type.get)) {
  } else {
      result.setChildElement(packet.getChildElement().createCopy());
      result.setChildElement("coord", "custom:iq:Search#response");    
      result.setError(PacketError.Condition.not_acceptable);
  }
  return result;
 } catch (Exception e) {
  // if you face any issue while processing. You can response back with "bad request"
  result.setChildElement(packet.getChildElement().createCopy());
  result.setChildElement("coord", "custom:iq:Search#response");    
  result.setError(PacketError.Condition.bad_request);
 } 
 return result;
    }
    
    public void initialize(XMPPServer server) {
 super.initialize(server);
    }    
}

Now you need to create one plugin class. That class would be extending plugin class. In that class you need to create one instance of IQ handler class that you have developed. Assign that handler class to IQ router. IQ router is kind bus of openfire that is managing roster requests.

 IQHandler handler = new SearchIQhanlder("SearchIQhanlder");
 IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
 iqRouter.addHandler(handler);  

On the application side you need to create one new IQ to request for your custom IQHanlder. Following is the example of what i have created.

<iq type='get' id='search' from='"+ Datas.jid.getLittleJid() + "'>
<query xmlns='custom:iq:Search'>
<search>"+ usernamefield.getText() +"</search>
</query>
</iq>

That is all to create a custom IQHandler. It is very easy to create such IQ. As well this would be very helpful for application development.

Let me know if you need any help regarding the same.

11 comments:

  1. Hey :)

    could you maybe send me a sample Java Project of this tutorial (prefer Eclipse) ? I really got problems to create the project structure...

    ReplyDelete
  2. provide me your email address. I will send you one sample.

    ReplyDelete
    Replies
    1. Hie Hardik, Can u provide the sample project ?

      Delete
  3. <3333 aldilaimi.saif@yahoo.de

    ReplyDelete
  4. Hey, just wanted to know if u got my mail.. cuz i didnt got a mail from you :S

    ReplyDelete
  5. dieman.manly@gmail.com
    thank for your help

    ReplyDelete
  6. Is there a way to modify IQ request before delivering to other components? Thanks.

    ReplyDelete
    Replies
    1. Hi Anton,

      Sorry for the late reply. There is some PacketInterceptor those you can use for your purpose i think. I have not developed any of them yet so don't know more details about it.

      Delete
  7. Could you send me the sample project too?
    upbeatdash@gmail.com

    ReplyDelete
    Replies
    1. hii Hardik
      can u provide the sample project

      Delete