Mapping Openfire Custom plugin with aSmack Client
Asked Answered
R

3

1

I'm a newbie to XMPP so forgive me if this question sounds silly. I want to create a custom plugin and map it with my aSmack client on Android. I'm trying to apply my knowledge of Web Services but I'm not winning. So please guide my thinking toward the best approach, an example will be really helpful. Thanx in advance.

Radiotelegraph answered 22/6, 2016 at 10:30 Comment(1)
Please note that aSmack is deprecated and obsolete. Starting with Version 4.1 Smack is able to run without modifications on Android.Merozoite
D
7

There are many types of plugins, let's talk in general pourpose. Igniterealtime Plugin guide

You want to define a brand new IQ Stanza to manage an UserCustomParam. Let's say:

<iq from="user1@myserver" to="myserver" type="get">
 <usercustomparam xmls:"com.records.iq" retrive="favouritecolor">
</iq>

What you have to:

step 1: define a plugin (class that implemements Plugin) that adds a new handler

MyCustomHandler colorshandler;
IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
iqRouter.addHandler(colorshandler);

Step2: implements MyCustomHandler as you need (read on database, write on database, read server side and so on).

public class MyCustomHandler extends IQHandler {
    public static final String NAMESPACE_TICKET_IQ = "com.records.iq";
    public static final String TAG_TICKET_IQ = "usercustomparam ";

Now your server it's ready to manage your custom IQ request.

Time to go client side:

Step3: register to your ProviderManager an IQProvider

ProviderManager.addIQProvider("usercustomparam ","com.records.iq", new IQUserCustomParamProvider());

Step4: implements your IQUserCustomParamProvider as you need

public class IQUserCustomParamProvider extends IQProvider<IQUserCustomParam>

into Provider you'll parse the incoming IQ from server and you'll create a IQUserCustomParam with an instance param like

String favouriteColor

Step5: you need to implement IQUserCustomParam

public class IQUserCustomParam extends IQ
    private final static String childElementName = "usercustomparam";
    private final static String childElementNamespace = "com.records.iq";

public IQUserCustomParam (String color)
    {
        this(childElementName , childElementNamespace );

        this.setType(IQ.Type.result);
        this.setFavouriteColor(color);
    }

Step 6: now set up it's completed, but you haven't defined yet when to accept IQUserCustomParam when it comes from server. So you need a StanzaFilter

public class IQUserCustomParamFilter implements StanzaFilter

Step 7: and you haven't defined yet what to do with IQUserCustomParam when it comes from server. So you need a StanzaListner

public class IQUserCustomParamListner implements StanzaListener

Step 8: finally you'll have to register the combo filter/listner on your connection:

AbstractXMPPConnection connection = ...;
connection.addAsyncStanzaListener(new PersonalConfigListner(this), new IQMUCConfigTicketFIlter();

if that helped, please don't forget to accept the answer!

Dowdy answered 22/6, 2016 at 15:37 Comment(7)
Thanx for taking the time, you summarized everything well.Radiotelegraph
I have a question is there any step we need to do in server side or its all need in android client side.Archdiocese
@DeepakMaurya first and second steps are absolutely SERVER side. You need to deploy your plugin in Openfire, I'm pretty sure the OF's admin panel has an option for it. The docs I've linked describes also how to modify your admin panel to manage the plugin but it's not really needed: for sure, your OpenFire must know your new Plugin, even if the main work it's made to Android client side (from step 3 to end "Time to go client side").Dowdy
@Dowdy I tried as you suggesting. what am trying is to first send custom IQ to let user decide response he/she wants. I have uploaded my project in github github.com/lakadbagha/customIQHandler with the issue github.com/lakadbagha/customIQHandler/issues/1 . Can you please help me to know why this happening.Archdiocese
@DeepakMaurya it happens when server side it's not deployed in right way in most cases. As I see, you had too much: you just need the class that extends (or implements?) Plugin and the related classes, not a new class loader etc. If you copy the jar in the proper directory OpenFire will load the plugin at startup, otherwise you must load it from server interface.Dowdy
@Dowdy am getting response error : <iq type="error" to="[email protected]/Android_User328" id="OA14U-113"> <query xmlns="com:via:call#request"> <call type="set" calltype="video">meet.jit.si/testroom123</call> </query> <error type="cancel"> <service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /> </error> </iq> what this error means?Archdiocese
@DeepakMaurya prolly wrong configuration of namespaceDowdy
B
1

This is a sample of plugin implementation:

First, you should implement the Plugin interface:

public class MotDPlugin implements Plugin

Then, this requires implementation of the intitalizePlugin and destroyPlugin methods, as shown below:

public void initializePlugin(PluginManager manager, File pluginDirectory) {
   serverAddress = new JID(XMPPServer.getInstance().getServerInfo().getName());
   router = XMPPServer.getInstance().getMessageRouter();

   SessionEventDispatcher.addListener(listener);
}

public void destroyPlugin() {
   SessionEventDispatcher.removeListener(listener);

   listener = null;
   serverAddress = null;
   router = null;
}

The more about this sample, you may refer to the Openfire Plugin Development: Message of the Day.

Hope it helps.

Bedroom answered 28/6, 2018 at 5:57 Comment(0)
P
1

There is a simple instance about plugin:

public class TestIQHandle extends IQHandler {
    private static final String MODULE_NAME = "test plugin";
    private static final String NAME_SPACE = "com:test:testplug";
    private IQHandlerInfo info;
    public TestIQHandle(){
        super(MODULE_NAME);
        info = new IQHandlerInfo("query", NAME_SPACE);
    }
    public TestIQHandle(String moduleName) {
        super(moduleName);
        info = new IQHandlerInfo("query", NAME_SPACE);
    }
    @Override
    public IQ handleIQ(IQ packet) throws UnauthorizedException {
        IQ reply = IQ.createResultIQ(packet);
        Element groups = packet.getChildElement();
        if(true){
            System.out.println("=======invalid========");
        }
        if(!IQ.Type.get.equals(packet.getType())){
            reply.setChildElement(groups.createCopy());
            reply.setError(PacketError.Condition.bad_request);
            return reply;
        }
        //StringUtils.substringBefore(packet.getFrom().toString(), "@");
        return reply;
    }
    @Override
    public IQHandlerInfo getInfo() {
        // TODO Auto-generated method stub
        return info;
    }
}
Purine answered 29/6, 2018 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.