mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Mahoney <mike.maho...@thingworx.com>
Subject Vysper stanza filtering
Date Tue, 03 Jan 2012 18:58:16 GMT
Hey Everyone,

I recently wrote a simple stanza filtering framework for Vysper and was wondering if there
would be interest in merging it in.  Here is how it works:

There is one key interface, a StanzaFilter:

//
// StanzaFilter interface
//
package org.apache.vysper.xmpp.filter;

import org.apache.vysper.xmpp.server.ServerRuntimeContext;
import org.apache.vysper.xmpp.server.SessionContext;
import org.apache.vysper.xmpp.stanza.Stanza;

public interface StanzaFilter {
    public Stanza accept(ServerRuntimeContext context, SessionContext session, Stanza stanza);
}

A filter implementation's accept method should return null if the filter fails, or a stanza
in the case of success.
There is also a FIlterChain, which implements StanzaFilter, but also holds a collection of
filters.  This allows you to create a single chain that executes a number of filters.

I added a FilterChain to both XMPPServer and ServerRuntimeContext.  This allows a user to
add Filters via a XMPPServer.addStanzaFilter() method prior to starting the server.  The Server
puts the filters into its FilterChain and then passes the chain into the ServerRuntimeContext
when the server is started.
 
Then I added the following to ProtocolWorker to actually execute the filters.  Keep in mind
that serverRuntimeContext.getFilter() returns a chain and therefore may actually run several
filters.  The default filter (if none are added) simply returns the original stanza.

public void processStanza(ServerRuntimeContext serverRuntimeContext, SessionContext sessionContext,
Stanza stanza,
            SessionStateHolder sessionStateHolder) {
        if (stanza == null)
            throw new RuntimeException("cannot process NULL stanzas");
        
        // Only process the filters for a session once it has authenticated
        if (sessionContext != null && sessionContext.getState() == SessionState.AUTHENTICATED)
{
            stanza = serverRuntimeContext.getFilter().accept(serverRuntimeContext, sessionContext,
stanza);
            if (stanza == null) return;    
        }

	<snip>

I also wrote some classes that extend FilterChain for specific stanza types:  IQFilterChain,
PresenceFilterChain, and MessageFilterChain.  These will only run their filters on stanzas
that match their type.

Any thoughts/concerns about this implementation?  If there is interest I will put together
a patch and submit it.

Thanks,
Mike

----------------------------------------
Mike Mahoney
OEM Application Specialist
ThingWorx
Office:   (610) 594-6200 x817
Mobile: (585) 314-8592


Mime
View raw message