As i told you i don’t know if it’s the right way to do it or not …
i will show you a quick example for IQs … but you may try to search more about using IQs :
ok first you have to implement the IQ in spark ( let’s say IQ request a class that extends IQ )
/*
*/
package org.jivesoftware.fastpath.iq;
//~— non-JDK imports --------------------------------------------------------
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.ProviderManager;
//~— JDK imports ------------------------------------------------------------
import java.io.IOException;
import org.jivesoftware.fastpath.provider.IQRelanceProvider;
import org.jivesoftware.spark.SparkManager;
/**
*/
public class IQRelance extends IQ {
private String chatID;
private XMPPConnection con;
private boolean isOk;
private String agentName;
public IQRelance(XMPPConnection connection, String chatid) {
con = connection;
this.chatID = chatid;
agentName = SparkManager.getUserManager().getNickname();
}
public boolean getResultOfRelance() {
return isOk;
}
public synchronized void SendQuery() throws XMPPException, IOException {
ProviderManager.getInstance().addIQProvider("relance", "[http://jabber.org/protocol/relance](http://jabber.org/protocol/relance)",
new IQRelanceProvider());
this.setType(IQ.Type.GET);
this.setTo(con.getServiceName());
PacketFilter filter = new AndFilter(new PacketIDFilter(this.getPacketID()));
PacketCollector collector = con.createPacketCollector(filter); // add the filter for handling the response from the server
con.sendPacket(this); // send the packet to the openfire server
IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
collector.cancel();
if (result == null) {
throw new XMPPException(“No response from server.”);
} else if (result.getType() == IQ.Type.ERROR) {
throw new XMPPException(result.getError());
} else {
if (result instanceof IQRelanceAnswer) { // note that the IQRelanceAnswer is the IQ response from the server it’s a class implemented in the server and in spark ( a stuffed class with some properties, getters and setters
IQRelanceAnswer Resultat = (IQRelanceAnswer) result;
isOk = Resultat.isIsOk();
}
}
}
@Override
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<relance xmlns=\"[http://jabber.org/protocol/relance](http://jabber.org/protocol/relance)\" agent=\""+agentName+"\" chatID=\"" + chatID + "\" >");
buf.append("");
return buf.toString();
}
}
then in Openfire you must implement the IQ handler for this request
/*
*/
package org.jivesoftware.fastpath.iq;
//~— non-JDK imports --------------------------------------------------------
import org.jivesoftware.smack.packet.IQ;
/**
*/
public class IQRelanceAnswer extends IQ {
private boolean IsOk;
public boolean isIsOk() {
return IsOk;
}
public void setIsOk(boolean IsOk) {
this.IsOk = IsOk;
}
public void setIsOk(String IsOk) {
this.IsOk = Boolean.getBoolean(IsOk);
}
@Override
public String getChildElementXML() {
return null;
}
}
IN OPENFIRE :
/*
*/
package com.oceanys.smsplugin.iqhandlers;
//~— non-JDK imports --------------------------------------------------------
import com.oceanys.smsplugin.handlers.zong.ZongSMSReceptionHandler;
import org.dom4j.Element;
import org.jivesoftware.openfire.IQHandlerInfo;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.handler.IQHandler;
import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError.Condition;
/**
*/
public class IQRelanceHandler extends IQHandler {
private IQHandlerInfo info;
private String serverName;
public IQRelanceHandler() {
super(“relance”);
info = new IQHandlerInfo("relance", "[http://jabber.org/protocol/relance](http://jabber.org/protocol/relance)");
}
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
IQ result = IQ.createResultIQ(packet);
Element iqChatId = packet.getChildElement();
iqChatId.getData();
String chatID = iqChatId.attribute(“chatID”).getValue();
String agentName = iqChatId.attribute(“agent”).getValue();
if (chatID != null) {
try {
String HistoryXml = “”;
ZongSMSReceptionHandler zongReceptionHandler = new ZongSMSReceptionHandler();
zongReceptionHandler.Relance(chatID,agentName);
result.setChildElement("relance", "[http://jabber.org/protocol/relance](http://jabber.org/protocol/relance)").setText(HistoryXml); // sending the response in XML ( i think you don't need to send response in your case may be you can send a simple ACK.
} catch (Exception ex) {
result.setError(Condition.bad_request);
}
} else {
result.setError(Condition.bad_request);
}
return result;
}
public IQHandlerInfo getInfo() {
return info;
}
@Override
public void initialize(XMPPServer server) {
super.initialize(server);
serverName = server.getServerInfo().getXMPPDomain();
}
}
FINALLY YOU HAVE TO REGISTER YOUR IQ HANDLER IN YOUR SERVER : SOMEWHERE IN PLUGIN INITIALISATION :
package com.oceanys.smsplugin;
//~— non-JDK imports --------------------------------------------------------
import com.oceanys.smsplugin.iqhandlers.IQAgentHistoryHandler;
import com.oceanys.smsplugin.iqhandlers.IQChatRoomHistoryHandler;
import com.oceanys.smsplugin.iqhandlers.IQRelanceHandler;
import com.oceanys.smsplugin.managers.SendingSMSQueue;
import org.jivesoftware.admin.AuthCheckFilter;
import org.jivesoftware.openfire.IQRouter;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.smsplugin.webchat.SMSChatManager;
//~— JDK imports ------------------------------------------------------------
import java.io.File;
/**
- A sample plugin for Openfire.
*/
public class SMSPlugin implements Plugin { // the main class of the plugin in openfire
IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
SMSChatManager chatManager = SMSChatManager.getInstance();
IQRelanceHandler iqRelanceHandler;
public void initializePlugin(PluginManager manager, File pluginDirectory) {
System.out.println(“Starting SMS Plugin.”);
AuthCheckFilter.addExclude(“smsplugin/smstest”);
iqRelanceHandler = new IQRelanceHandler();
iqRouter.addHandler(iqRelanceHandler);
}
public void destroyPlugin() {
System.out.println(“Shutting down SMS Plugin.”);
AuthCheckFilter.removeExclude(“smsplugin/smstest”);
iqRouter.removeHandler(iqRelanceHandler);
}
}
i did it quickly hope that you will understand that