Project

General

Profile

how to write plugin in tigase service 7.1.0

lin jiafu
Added over 1 year ago

my code is :

public class TalkKingRoster extends XMPPProcessor implements XMPPProcessorIfc, XMPPPreprocessorIfc{  
    private static Logger log = Logger.getLogger(TalkKingRoster.class.getName());  
    public static final String ID = "talkKing:" + RosterAbstract.XMLNS;  
    public static final String XMLNS = "talkKing:roster";  
    private static final String[] XMLNSS = {XMLNS};  
    private static final String[][] ELEMENTS = {Iq.IQ_QUERY_PATH};  
     private static final Element[] FEATURES = { new Element("roster", new String[] { "xmlns" }, new String[] { XMLNS }) };  

    @Override  
    public String id() {  
        // TODO Auto-generated method stub  
        return ID;  
    }  

    @Override  
    public boolean preProcess(Packet packet, XMPPResourceConnection session,  
            NonAuthUserRepository repo, Queue<Packet> results,  
            Map<String, Object> settings) {  
        // TODO Auto-generated method stub  
        return false;  
    }  

    @Override  
    public void process(Packet packet, XMPPResourceConnection session,  
            NonAuthUserRepository repo, Queue<Packet> results,  
            Map<String, Object> settings) throws XMPPException {  
        // TODO Auto-generated method stub  
        if (log.isLoggable(Level.FINEST)) {  
            log.finest("Processing packet: " + packet.toString());  
        }  
        if(session==null){  
            if(log.isLoggable(Level.FINE)){  
                log.log(Level.FINE,"Session is null, ignoring packet: {0}",packet);  
                return;  
            }  
        }  
        if (packet.getStanzaFrom()!= null && session.isUserId(packet.getStanzaFrom().getBareJID())&& !session.isAuthorized()) {  
            if ( log.isLoggable( Level.FINE ) ){  
                log.log( Level.FINE, "Session is not authorized, ignoring packet: {0}", packet );  
            }  
            return;  
        }  

        try {  
            if (!session.isServerSession() && (packet.getStanzaFrom() != null ) && !session.isUserId(packet.getStanzaFrom().getBareJID())) {  
                // RFC says: ignore such request  
                log.log( Level.WARNING, "Roster request ''from'' attribute doesn't match "  
                    + "session: {0}, request: {1}", new Object[] { session, packet } );  
                return;  
            }  

            StanzaType type = packet.getType();  
            String xmlns = packet.getElement().getXMLNSStaticStr( Iq.IQ_QUERY_PATH );  

            if (xmlns == XMLNS && type == StanzaType.get){  
                List<Element> items = packet.getElemChildrenStaticStr(Iq.IQ_QUERY_PATH);  

                if (items!=null) {  
                    String uri = System.getProperty( "user-db-uri" );  
                    UserRepository userRepository = RepositoryFactory.getUserRepository( null, uri, null );  
                    String serverDomain = session.getDomainAsJID().getDomain();  
                    Set<BareJID> found = new HashSet<BareJID>();  
                    for (Element item : items) {  
                        if (!item.getName().equals("item")) {  
                            continue;  
                        }  

                        BareJID jid = BareJID.bareJIDInstance(item.getAttributeStaticStr("jid"));  
                        String domain = jid.getDomain();  
                        BareJID localJid = BareJID.bareJIDInstance(jid.getLocalpart(),serverDomain);  


                        boolean isLocalJid = domain.equals(serverDomain);  
                        if (isLocalJid) {  
                            if (userRepository.userExists(localJid)) {  
                                found.add(jid);  
                            }  
                        }  
                    }  

                    Element query = new Element(Iq.QUERY_NAME);  
                    query.setXMLNS(XMLNS);  

                    for (BareJID bareJID : found) {  
                        Element item = new Element("item");  
                        item.setAttribute("jid", bareJID.toString());  
                        query.addChild(item);  
                    }  

                    results.offer(packet.okResult(query, 0));  

                    packet.processedBy(ID);  
                }
            }
        } catch (NotAuthorizedException e){  
             log.log( Level.WARNING, "Received roster request but user session is not authorized yet: {0}", packet );  
                try {  
                    results.offer( Authorization.NOT_AUTHORIZED.getResponseMessage( packet,  
                        "You must authorize session first.", true ) );  
                }  
                catch (PacketErrorTypeException pe) {  
                    // ignored  
                }  

        } catch (TigaseStringprepException e) {  
            // TODO Auto-generated catch block  

        } catch (DBInitException e) {  
            // TODO Auto-generated catch block  
            log.log( Level.WARNING, "Database problem, please contact admin:", e );  
            try {  
                results.offer( Authorization.INTERNAL_SERVER_ERROR.getResponseMessage( packet,  
                    "Database access problem, please contact administrator.", true ) );  
            }  
            catch (PacketErrorTypeException pe) {  
                // ignored  
            }  
        } catch (ClassNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (InstantiationException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IllegalAccessException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }   
    }  

    @Override  
    public Set<StanzaType> supTypes() {  
        return new HashSet<StanzaType>(Arrays.asList(StanzaType.get));  
    }  

    @Override  
    public String[][] supElementNamePaths() {  
        // TODO Auto-generated method stub  
        return ELEMENTS;  
    }  

    @Override  
    public String[] supNamespaces() {  
        // TODO Auto-generated method stub  
        return XMLNSS;  
    }  

    @Override  
    public Element[] supStreamFeatures(XMPPResourceConnection session) {  
        if (log.isLoggable(Level.FINEST) && (session != null)) {  
            log.finest("VHostItem: " + session.getDomain());  
        }  
        if (session != null) {  
            return FEATURES;  
        }  
        else {  
            return null;  
        }  
    }     

} 

i put this class in the path of src\main\java\tigase\xmpp\impl and i set a config --sm-plugins=talkKing:talkKing:roster in the init.properties, when the service start up , it seems don't load the plugin, any wrong about my mehtod and config, how can i do? thanks.


Replies (3)

Added by Wojciech Kapcia TigaseTeam over 1 year ago

You have following code:

public class TalkKingRoster extends XMPPProcessor implements XMPPProcessorIfc, XMPPPreprocessorIfc{  
…
    public static final String ID = "talkKing:" + RosterAbstract.XMLNS;  
    public static final String XMLNS = "talkKing:roster";

You assign to your ID constant a concatenation of "talkKing:" and constant from different class - namely RosterAbstract.XMLNS; therefore the plugin ID IS NOT talkKing:talkKing:roster but rather @talkKing:jabber:iq:roster@.

You probably wanted following (without @RosterAbstract.@):

public class TalkKingRoster extends XMPPProcessor implements XMPPProcessorIfc, XMPPPreprocessorIfc{  
…
    public static final String ID = "talkKing:" + XMLNS;  
    public static final String XMLNS = "talkKing:roster";

Added by lin jiafu over 1 year ago

thanks Wojciech Kapcia,i replace the code:

public static final String ID = "talkKing:" + RosterAbstract.XMLNS;

public static final String XMLNS = "talkKing:roster";

by public static final String XMLNS = "jabber:iq:search";

public static final String ID = XMLNS;

and it works. but i can't get what realy want.

i send a packet to the service :

<iq type="get"

id="getuser_02"

to="ljf@linjiafu-PC">

and it return :

it seems the process method does not work.please help.

Added by Andrzej Wójcik IoT 1 CloudTigaseTeam over 1 year ago

Process method works fine as you get a response with @type=result@. If it was not working you would not receive a response at all.

Most likely your code is not able to find any results for parameters you provided and responds with an empty query, so you need to fix your code.

    (1-3/3)