Project

General

Profile

new plugin question

Raylin lin
Added almost 5 years ago

hi,all:

   I've added a new plugin to store message in db table.and i found that receiver received more that one message while sender send only one. And i know that tigase.xmpp.impl.Message processes message. Do i need to modify this message function instead of writing a new plugin to realize the function? thank you.
public class ChatLogsStore extends XMPPProcessor
implements XMPPProcessorIfc{

    private static final String     ELEM_NAME = tigase.server.Message.ELEM_NAME;
    private static final String[][] ELEMENTS  = {
        { ELEM_NAME }
    };
    private static final Logger log = Logger.getLogger( ChatLogsProcessImpl.class.getName() );
    private static final String   XMLNS  = "jabber:client";
    private static final String[] XMLNSS = { XMLNS };
    private DbLogRepository   dblog_repo         = null;
    private final int PARTREQUEST = 1;
    /** Define the plugin ID **/  
    private static final String ID = "jabber:iq:storeLastMessage";  
    @Override
    public String id() {
        return ID;
    }


@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 ( session == null ){
            if ( log.isLoggable( Level.FINE ) ){
                log.log( Level.FINE, "Session is null, ignoring packet: {0}", packet );
            }

            return;
        }    // end of if (session == null)
        if ( !session.isAuthorized() ){
            if ( log.isLoggable( Level.FINE ) ){
                log.log( Level.FINE, "Session is not authorized, ignoring packet: {0}", packet );
            }

            return;
        }

        // user and some other entity like transport
        JID connectionId = session.getConnectionId();

        if ( connectionId.equals( packet.getPacketFrom() ) ){
            // Packet from the user, let's check where it should go
            if ( ( packet.getStanzaTo() != null ) && !session.isLocalDomain( getDomain(packet.getStanzaTo()
                    .toString()), false ) && !session.isUserId( packet.getStanzaTo().getBareJID() ) ){

                results.offer( packet.copyElementOnly() );

                return;
            }
        } else {

            // Packet probably to the user, let's check where it came from
            if ( session.isUserId( packet.getStanzaTo().getBareJID() ) ){
                if ( packet.getStanzaTo().getResource() != null ){
                    Packet result = packet.copyElementOnly();

                    result.setPacketTo( session.getConnectionId( packet.getStanzaTo() ) );
                    result.setPacketFrom( packet.getTo() );
                    results.offer( result );
                } else {

                }

                return;
            } else {
                // Hm, I do not know what to do here, should not happen
            }
        }

        try {
            if ( ( packet.getStanzaFrom() != null ) && !session.isUserId( packet.getStanzaFrom()
                    .getBareJID() ) ){

                // RFC says: ignore such request
                log.log( Level.WARNING, "un know request ''from'' attribute doesn't match "
                                                                + "session: {0}, request: {1}", new Object[] { session,
                                                                                                                                                             packet } );
                return;
            }    // end of if (packet.getElemFrom() != null

            if(packet instanceof  Message)
            {
                processChatMessage(packet, session, results, settings);
                return;
            }
            else
            {
                //results.offer(packet.copyElementOnly());
                log.log( Level.WARNING, "Unknown XMLNS for the conversation plugin: {0}", packet ); 
                return;
            }


        } catch ( RepositoryAccessException e ) {
            log.log( Level.WARNING,
                             "Problem with repository access: {0} for packet: {1}",
                             new Object[] { e, packet } );
            results.offer( packet.okResult( (String) null, 0 ) );
        } catch ( NotAuthorizedException e ) {
            log.log( Level.WARNING,
                             "Received request but user session is not authorized yet: {0}", packet );
            results.offer( Authorization.NOT_AUTHORIZED.getResponseMessage( packet,
                                                                                                                                            "You must authorize session first.", true ) );
        } catch ( TigaseDBException e ) {
            log.log( Level.WARNING, "Database problem, please contact admin:", e );
            results.offer( Authorization.INTERNAL_SERVER_ERROR.getResponseMessage( packet,                                                                                                                                           "Database access problem, please contact administrator.", true ) );
        }  
        catch (Exception e ) {
                log.log( Level.WARNING, "Unknown retrieving exception: {0} for packet: {1}",
                                 new Object[] { e, packet } );
                results.offer( Authorization.UNDEFINED_CONDITION.getResponseMessage( packet, e
                        .getMessage(), true ) );
        }// end of try-catch
    }

private void processChatMessage(Packet packet, XMPPResourceConnection session,
            Queue<Packet> results, final Map<String, Object> settings) throws XMPPException, NotAuthorizedException, TigaseDBException
     {
        Message message = (Message) packet;
            ChatLogs logs = this.get(packet,session);
            if(null==logs)
            {
                 results.offer(packet.copyElementOnly());
                 return;
            }
            if(message.getElemCData(Message.MESSAGE_SUBJECT_PATH)!=null)
            {
                logs.setSubject(message.getElemCData(Message.MESSAGE_SUBJECT_PATH).toString());
            }
            else
            {
                logs.setSubject("");
            }
            dblog_repo.add(logs);
            dblog_repo.addConversation(logs);
            //results.offer(packet.copyElementOnly());

     }

Replies (1)

Avatar?id=6023&size=32x32

Added by Artur Hefczyc TigaseTeam almost 5 years ago

Writing your own plugin is a good approach. You just need to remove all the logic you took from Message plugin. As you copied Message logic (the process method) you end up with doubled messages. Please read the plugin documentation online and the comments to understand how it works and what it does.

Also, in you custom code, you do not need the condition: if (packet instanceof Message). It is guaranteed by the API that you get only messages for processing in your plugin.

    (1-1/1)