Commit bac23fdc authored by Axel Brand's avatar Axel Brand Committed by daeva

Work on the upcoming Refactoring on GoJara

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/branches/plugins@13540 b35dd754-fafc-0310-a699-88a17e54d16e
parent af5853ae
package org.jivesoftware.openfire.plugin.gojara.base;
import java.util.Set;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.plugin.gojara.database.DatabaseManager;
import org.jivesoftware.openfire.plugin.gojara.messagefilter.remoteroster.RemoteRosterInterceptor;
import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.util.ConcurrentHashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.Packet;
public class MainInterceptor implements PacketInterceptor {
private static final Logger Log = LoggerFactory
.getLogger(RemoteRosterInterceptor.class);
private Set<String> activeTransports = new ConcurrentHashSet<String>();
private DatabaseManager _db;
private RemoteRosterInterceptor interceptor;
public MainInterceptor() {
Log.debug("Started MainInterceptor");
_db = DatabaseManager.getInstance();
interceptor = new RemoteRosterInterceptor();
}
public void addTransport(String subDomain) {
this.activeTransports.add(subDomain);
Log.debug("Added interceptor for subdomain " + subDomain
+ " to MainInterceptor");
}
public boolean removeTransport(String subDomain) {
if (this.activeTransports.contains(subDomain)) {
this.activeTransports.remove(subDomain);
return true;
}
return false;
}
@Override
public void interceptPacket(Packet packet, Session session,
boolean incoming, boolean processed) throws PacketRejectedException {
String from = packet.getFrom().toString();
String to = packet.getTo().toString();
//TODO: Not intercepting Messages & Presences
if (activeTransports.contains(to) || activeTransports.contains(from)) {
String subdomain = activeTransports.contains(to) ? to : from;
interceptor.interceptPacket(packet, session, incoming, processed, subdomain);
logForStatistic(packet, incoming, processed, to);
}
// // PacketInterceptor interceptor = this.activeTransports.get(to);
// if (interceptor != null) {
// return;
// }
//
// // interceptor = this.activeTransports.get(from);
// if (interceptor != null) {
// interceptor.interceptPacket(packet, session, incoming, processed);
// logForStatistic(packet, incoming, processed, from);
// return;
// }
}
private void logForStatistic(Packet packet, boolean incoming,
boolean processed, String subdomain) {
String from = packet.getFrom().toString();
String to = packet.getTo().toString();
if (from != null && to != null && processed && incoming) {
/*
* Spectrum sends a Ping to itself through the server to check if
* the server is alive. We ignore that for statistics
*/
if (to.toString().equals(from.toString())
&& to.toString().equals(subdomain))
return;
String type = packet.getClass().getName();
_db.addNewLogEntry(subdomain, type, from.toString(), to.toString());
}
}
}
package org.jivesoftware.openfire.plugin.gojara.base;
import java.io.File;
import java.util.HashMap;
//import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
//import java.util.Map;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.jivesoftware.openfire.SessionManager;
//import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.component.ComponentEventListener;
import org.jivesoftware.openfire.component.InternalComponentManager;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.plugin.gojara.messagefilter.handler.AbstractInterceptorHandler;
import org.jivesoftware.openfire.plugin.gojara.messagefilter.handler.GatewayInterceptorHandler;
import org.jivesoftware.openfire.plugin.gojara.messagefilter.MainInterceptor;
//import org.jivesoftware.openfire.plugin.gojara.messagefilter.handler.AbstractInterceptorHandler;
//import org.jivesoftware.openfire.plugin.gojara.messagefilter.handler.GatewayInterceptorHandler;
import org.jivesoftware.openfire.plugin.gojara.utils.XpathHelper;
import org.jivesoftware.openfire.session.ComponentSession;
//import org.jivesoftware.openfire.session.ComponentSession;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.PropertyEventDispatcher;
import org.jivesoftware.util.PropertyEventListener;
......@@ -46,7 +47,7 @@ public class RemoteRosterPlugin implements Plugin {
private static final Logger Log = LoggerFactory.getLogger(RemoteRosterPlugin.class);
private static PluginManager pluginManager;
private final SessionManager _sessionManager = SessionManager.getInstance();
// private final SessionManager _sessionManager = SessionManager.getInstance();
// private Map<String, AbstractInterceptorHandler> _interceptors = new HashMap<String, AbstractInterceptorHandler>();
private Set<String> _waitingForIQResponse = new HashSet<String>();
private PropertyEventListener _settingsObserver;
......@@ -75,13 +76,13 @@ public class RemoteRosterPlugin implements Plugin {
* Check if the unregistered component contains to one of our
* package interceptors
*/
@Override
public void componentUnregistered(JID componentJID) {
// ComponentSession session = _sessionManager.getComponentSession(componentJID.getDomain());
// if (session != null && _interceptors.containsKey(session.getExternalComponent().getInitialSubdomain())) {
// String initialSubdomain = session.getExternalComponent().getInitialSubdomain();
// Remove it from Map & ComponentManager
removeInterceptor(componentJID.toString());
mainInterceptor.removeTransport(componentJID.toString());
// }
}
......@@ -89,12 +90,12 @@ public class RemoteRosterPlugin implements Plugin {
* If there is a new external Component, check if it is a gateway
* and add create a package interceptor if it is enabled
*/
@Override
public void componentRegistered(JID componentJID) {
_waitingForIQResponse.add(componentJID.getDomain());
}
@Override
public void componentInfoReceived(IQ iq) {
String from = iq.getFrom().getDomain();
// Waiting for this external component sending an IQ response to
......@@ -165,25 +166,25 @@ public class RemoteRosterPlugin implements Plugin {
return pluginManager;
}
private void removeInterceptor(String initialSubdomain) {
mainInterceptor.removeTransport(initialSubdomain);
// AbstractInterceptorHandler interceptor = _interceptors.get(initialSubdomain);
// if (interceptor != null) {
// _interceptors.remove(initialSubdomain);
// interceptor.stop();
// }
}
/*
* We are handling all our gateways in a Map with subdomain, like
* icq.myserver.com, to ensure that there is only one interceptor for a
* specified gateway
*/
private void createNewPackageIntercetor(String initialSubdomain) {
mainInterceptor.addTransport(initialSubdomain);
// AbstractInterceptorHandler interceptor = new GatewayInterceptorHandler(initialSubdomain);
// _interceptors.put(initialSubdomain, interceptor);
// interceptor.start();
}
// private void removeInterceptor(String initialSubdomain) {
// mainInterceptor.removeTransport(initialSubdomain);
//// AbstractInterceptorHandler interceptor = _interceptors.get(initialSubdomain);
//// if (interceptor != null) {
//// _interceptors.remove(initialSubdomain);
//// interceptor.stop();
//// }
// }
//
// /*
// * We are handling all our gateways in a Map with subdomain, like
// * icq.myserver.com, to ensure that there is only one interceptor for a
// * specified gateway
// */
// private void createNewPackageIntercetor(String initialSubdomain) {
// mainInterceptor.addTransport(initialSubdomain);
//// AbstractInterceptorHandler interceptor = new GatewayInterceptorHandler(initialSubdomain);
//// _interceptors.put(initialSubdomain, interceptor);
//// interceptor.start();
// }
}
......@@ -14,22 +14,18 @@ import org.jivesoftware.util.PropertyEventListener;
*/
public abstract class RemoteRosterPropertyListener implements PropertyEventListener {
@Override
public void xmlPropertySet(String property, Map<String, Object> params) {
}
@Override
public void xmlPropertyDeleted(String property, Map<String, Object> params) {
}
@Override
public void propertySet(String property, Map<String, Object> params) {
if (property.contains("plugin.remoteroster.jids.")) {
changedProperty(property.replace("plugin.remoteroster.jids.", ""));
}
}
@Override
public void propertyDeleted(String property, Map<String, Object> params) {
String hostname = XMPPServer.getInstance().getServerInfo().getXMPPDomain();
property += "." + hostname;
......
package org.jivesoftware.openfire.plugin.gojara.messagefilter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
//import org.jivesoftware.openfire.plugin.gojara.database.DatabaseManager;
//import org.jivesoftware.openfire.plugin.gojara.messagefilter.remoteroster.RemoteRosterInterceptor;
import org.jivesoftware.openfire.plugin.gojara.messagefilter.remoteroster.processors.*;
import org.jivesoftware.openfire.roster.RosterManager;
import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.util.ConcurrentHashSet;
import org.jivesoftware.util.JiveGlobals;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.Packet;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Presence;
public class MainInterceptor implements PacketInterceptor {
// private DatabaseManager _db;
private static final Logger Log = LoggerFactory.getLogger(MainInterceptor.class);
private Set<String> activeTransports = new ConcurrentHashSet<String>();
private Map<String, AbstractRemoteRosterProcessor> packetProcessors = new HashMap<String, AbstractRemoteRosterProcessor>();
private String serverDomain;
//we dont need this anymore
// private RemoteRosterInterceptor interceptor;
public MainInterceptor() {
Log.debug("Started MainInterceptor for GoJara Plugin.");
XMPPServer server = XMPPServer.getInstance();
serverDomain = server.getServerInfo().getXMPPDomain();
// _db = DatabaseManager.getInstance();
// interceptor = new RemoteRosterInterceptor();
//Now we need to add the PacketProcessors.
RosterManager rosterMananger = server.getRosterManager();
AbstractRemoteRosterProcessor sendroster = new SendRosterProcessor(rosterMananger);
AbstractRemoteRosterProcessor receiveChanges = new ReceiveComponentUpdatesProcessor(rosterMananger);
AbstractRemoteRosterProcessor iqRegistered = new DiscoIQRegisteredProcessor();
AbstractRemoteRosterProcessor nonPersistant = new NonPersistantRosterProcessor(rosterMananger);
AbstractRemoteRosterProcessor updateToComponent = new ClientToComponentUpdateProcessor();
packetProcessors.put("sendRoster", sendroster);
packetProcessors.put("receiveChanges", receiveChanges);
packetProcessors.put("sparkIQRegistered", iqRegistered);
packetProcessors.put("handleNonPersistant", nonPersistant);
packetProcessors.put("clientToComponentUpdate", updateToComponent);
}
//These get called from our RemoteRosterPlugin
public boolean addTransport(String subDomain) {
Log.debug("Trying to add " +subDomain + "to Set of watched Transports.");
return this.activeTransports.add(subDomain);
}
public boolean removeTransport(String subDomain) {
Log.debug("Trying to remove " + subDomain + " from Set of watched Transports.");
return this.activeTransports.remove(subDomain);
// if (this.activeTransports.contains(subDomain)) {
// this.activeTransports.remove(subDomain);
// return true;
// }
// return false;
}
//evtl noch auf bool refactoren wenni ch merke das ich das fr nichts brauche(siehe to)
private String searchJIDforSubdomain(String jid) {
//As our Set of Subdomains is a Hash of Strings like icq.domain.tld, if we
//want to check if a jid CONTAINS a watched subdomain we need to iterate over the set.
// We also return the subdomain as a string so we can use it if we find it.
for (String subdomain : activeTransports) {
if (subdomain.contains(jid))
return subdomain;
}
return "";
}
/*
* This Interceptor tests if GoJara needs to process this package. We decided to do one global Interceptor
* so we would'nt redundantly test for cases we already checked in previous Interceptors.
* @see org.jivesoftware.openfire.interceptor.PacketInterceptor#interceptPacket(org.xmpp.packet.Packet, org.jivesoftware.openfire.session.Session, boolean, boolean)
*/
public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed)
throws PacketRejectedException {
//We have to watch out for null else this will throw Exceptions
String from = (packet.getFrom() != null) ? packet.getFrom().toString() : "";
String to = (packet.getTo() != null) ? packet.getTo().toString() : "";
//We dont want this to get too packed, so here we test only for stuff we can test on PACKET
//if to is Empty, this might be a Client to Component Update we have to forward.
if (to.isEmpty()) {
packetProcessors.get("clientToComponentUpdate").process(packet);
}
//This might be a Disco IQ from the SERVER itself, so we have to check for Acess-Restriction
else if (from.isEmpty() || from.equals(serverDomain)) {
packetProcessors.get("WhiteListProcessor").process(packet);
}
//At this Point, TO is NOT EMPTY
String to_subdomain = searchJIDforSubdomain(to);
if (!to_subdomain.isEmpty()){
// To can now be EQUAL to the subdomain or a string containing a subdomain
packetProcessors.get("IQLastProcessor").process(packet,to_subdomain);
//If TO EQUALS a watched subdomain, we test for IQRegistered Process
if (activeTransports.contains(to))
packetProcessors.get("sparkIQRegistered").process(packet,to);
}
//If FROM EQUALS a watched subdomain, we test for IQ:ROSTER Payload
else if (!from.isEmpty() && activeTransports.contains(from)) {
if (packet instanceof IQ)
packetProcessors.get("iqRosterPayload").process(packet,from);
//it could also be a presence from Transport, so we test for Non-Persistancy
if (!JiveGlobals.getBooleanProperty("plugin.remoteroster.persistent", false) && (packet instanceof Presence)) {
packetProcessors.get("handleNonPersistant").process(packet,from);
}
}
}
}
package org.jivesoftware.openfire.plugin.gojara.messagefilter.handler;
import java.util.HashSet;
import java.util.Set;
import org.apache.log4j.Logger;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
/**
*
* A gateway interceptor should extend this class. It supports the child class
* handling their interceptors.
*
* @author Holger Bergunde
*
*/
public abstract class AbstractInterceptorHandler {
private static Logger Log = Logger.getLogger(AbstractInterceptorHandler.class);
private String _subdomain;
private boolean _isRunning = false;
private Set<PacketInterceptor> _interceptors = new HashSet<PacketInterceptor>();
private InterceptorManager _iManager;
public AbstractInterceptorHandler(String subdomain) {
_subdomain = subdomain;
_iManager = InterceptorManager.getInstance();
}
/**
* Add a interceptor to the let it handled by the abstract implementation
* The handle must not be started. You have to stop() until you could add
* new interceptors.
*
* @param interceptor
* @return true if it successfully added, otherwise false
*/
protected boolean addInterceptor(PacketInterceptor interceptor) {
if (_isRunning) {
return false;
}
return _interceptors.add(interceptor);
}
/**
* Remove a interceptor from abstract implementation The handle must not be
* started. You have to stop() until you could remove interceptors.
*
* @param interceptor
* @return true if it successfully added, otherwise false
*/
protected boolean removeInterceptor(PacketInterceptor interceptor) {
if (_isRunning) {
return false;
}
return _interceptors.remove(interceptor);
}
/**
* Start handling the added interceptors.
* If it is started you could not remove or add interceptors
*/
public void start() {
Log.debug("Start handling message interceptors for gateway " + _subdomain);
_isRunning = true;
for (PacketInterceptor interceptor : _interceptors) {
_iManager.addInterceptor(interceptor);
}
}
/**
* Stop handling the added interceptors.
*/
public void stop() {
Log.debug("Stop handling message interceptors for gateway " + _subdomain);
if (!_isRunning)
return;
_isRunning = false;
for (PacketInterceptor interceptor : _interceptors) {
_iManager.removeInterceptor(interceptor);
}
}
}
package org.jivesoftware.openfire.plugin.gojara.messagefilter.handler;
import org.jivesoftware.openfire.plugin.gojara.messagefilter.interceptors.DiscoPackageInterceptorHandler;
import org.jivesoftware.openfire.plugin.gojara.messagefilter.interceptors.IQLastInterceptor;
import org.jivesoftware.openfire.plugin.gojara.messagefilter.interceptors.StatisticPackageInterceptor;
import org.jivesoftware.openfire.plugin.gojara.messagefilter.remoteroster.RemoteRosterInterceptor;
/**
*
* This is the main handler for our gateways. It initializes all needed
* interceptors with the component subdomain this handler is bind to You have to
* start and stop this handler manually.
*
* @author Holger Bergunde
*
*/
public class GatewayInterceptorHandler extends AbstractInterceptorHandler {
public GatewayInterceptorHandler(String subdomain) {
super(subdomain);
DiscoPackageInterceptorHandler discoInterceptor = new DiscoPackageInterceptorHandler(subdomain);
RemoteRosterInterceptor remoteRosterInterceptor = new RemoteRosterInterceptor(subdomain);
StatisticPackageInterceptor statisticInterceptor = new StatisticPackageInterceptor(subdomain);
IQLastInterceptor iqLastInterceptor = new IQLastInterceptor(subdomain);
addInterceptor(remoteRosterInterceptor);
addInterceptor(discoInterceptor);
addInterceptor(statisticInterceptor);
addInterceptor(iqLastInterceptor);
}
}
......@@ -40,7 +40,7 @@ abstract public class AbstractRemoteRosterProcessor {
* @param packet
* @throws PacketRejectedException
*/
abstract public void process(Packet packet, String subdomain) throws PacketRejectedException;
abstract public void process(Packet packet) throws PacketRejectedException;
/**
* Use this method if you want to send your own packets through openfire
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment