Search in sources :

Example 1 with PacketInterceptor

use of org.jivesoftware.openfire.interceptor.PacketInterceptor in project Openfire by igniterealtime.

the class DiscoIQRegisteredProcessor method process.

@Override
public void process(Packet packet, final String subdomain, String to, String from) throws PacketRejectedException {
    Log.debug("Processing packet in DiscoIQRegisteredProcessor for " + subdomain);
    // Check if the jabber:iq:register is enabled in admin panel
    boolean isFeatureEnabled = JiveGlobals.getBooleanProperty("plugin.remoteroster.sparkDiscoInfo", false);
    if (!isFeatureEnabled) {
        Log.debug("Spark extension is deactivated. Won't change the disco#info");
        return;
    }
    final InterceptorManager interceptorManager = InterceptorManager.getInstance();
    final PacketInterceptor interceptor = new PacketInterceptor() {

        public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
            if (!processed && incoming) {
                if (packet instanceof IQ) {
                    IQ iqPacket = (IQ) packet;
                    Element packetElement = iqPacket.getChildElement();
                    if (packetElement == null)
                        return;
                    String ns = iqPacket.getChildElement().getNamespace().getURI();
                    if (iqPacket.getType().equals(IQ.Type.result) && ns.equals("jabber:iq:register") && iqPacket.getFrom().toString().equals(subdomain)) {
                        // Check if we are already registered
                        setRegistered(iqPacket.toString().contains("<registered/>"));
                        throw new PacketRejectedException();
                    } else if (iqPacket.getType().equals(IQ.Type.result) && ns.equals("http://jabber.org/protocol/disco#info") && iqPacket.getFrom().toString().equals(subdomain)) {
                        /*
							 * This is the answer of the disco#info from spark
							 * to our component. add the jabber:iq:register
							 * feature if we are registered
							 */
                        if (isRegistered()) {
                            Log.debug("Modifying disco#info packge to send registered iq feature to Spark user " + iqPacket.getTo().toString());
                            Attribute attribut = new DefaultAttribute("var", "jabber:iq:registered");
                            iqPacket.getChildElement().addElement("feature").add(attribut);
                        }
                    }
                }
            }
        }
    };
    Log.debug("Creating my own listener for jabber:iq:register result to external component " + subdomain);
    interceptorManager.addInterceptor(interceptor);
    IQ askComponent = new IQ();
    askComponent.setTo(to);
    askComponent.setFrom(from);
    askComponent.setType(IQ.Type.get);
    Element query = new DefaultElement(QName.get("query", "jabber:iq:register"));
    askComponent.setChildElement(query);
    // Remove the package intercepter in 1sec
    TimerTask removeInterceptorTask = new TimerTask() {

        @Override
        public void run() {
            Log.debug("Removing my created listener for jabber:iq:register. Component " + subdomain);
            interceptorManager.removeInterceptor(interceptor);
        }
    };
    Timer timer = new Timer();
    timer.schedule(removeInterceptorTask, 1000);
    // Send the register query to component
    dispatchPacket(askComponent);
}
Also used : Packet(org.xmpp.packet.Packet) DefaultAttribute(org.dom4j.tree.DefaultAttribute) Attribute(org.dom4j.Attribute) DefaultElement(org.dom4j.tree.DefaultElement) InterceptorManager(org.jivesoftware.openfire.interceptor.InterceptorManager) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) PacketInterceptor(org.jivesoftware.openfire.interceptor.PacketInterceptor) TimerTask(java.util.TimerTask) Timer(java.util.Timer) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) DefaultAttribute(org.dom4j.tree.DefaultAttribute) Session(org.jivesoftware.openfire.session.Session)

Example 2 with PacketInterceptor

use of org.jivesoftware.openfire.interceptor.PacketInterceptor in project Openfire by igniterealtime.

the class PacketCounter method start.

/**
     * Resets all counters, and starts counting.
     */
public void start() {
    // Register a packet listener so that we can track packet traffic.
    interceptor = new PacketInterceptor() {

        public void interceptPacket(final Packet packet, final Session session, final boolean incoming, final boolean processed) {
            if (!processed) {
                // don't count packets twice!
                return;
            }
            stanza.incrementAndGet();
            if (packet instanceof Message) {
                message.incrementAndGet();
            }
            if (packet instanceof Presence) {
                presence.incrementAndGet();
            }
            if (packet instanceof IQ) {
                iq.incrementAndGet();
                switch(((IQ) packet).getType()) {
                    case get:
                        iqGet.incrementAndGet();
                        break;
                    case set:
                        iqSet.incrementAndGet();
                        break;
                    case result:
                        iqResult.incrementAndGet();
                        break;
                    case error:
                        iqError.incrementAndGet();
                        break;
                }
            }
        }
    };
    // reset counters
    stanza.set(0);
    message.set(0);
    presence.set(0);
    iq.set(0);
    iqGet.set(0);
    iqSet.set(0);
    iqResult.set(0);
    iqError.set(0);
    // register listener
    InterceptorManager.getInstance().addInterceptor(interceptor);
}
Also used : Packet(org.xmpp.packet.Packet) Message(org.xmpp.packet.Message) IQ(org.xmpp.packet.IQ) Presence(org.xmpp.packet.Presence) PacketInterceptor(org.jivesoftware.openfire.interceptor.PacketInterceptor) Session(org.jivesoftware.openfire.session.Session)

Example 3 with PacketInterceptor

use of org.jivesoftware.openfire.interceptor.PacketInterceptor in project Openfire by igniterealtime.

the class StatisticsModule method start.

public void start() {
    // Retrieve instance of StatisticsManager
    statisticsManager = StatisticsManager.getInstance();
    // Register a packet listener so that we can track packet traffic.
    packetInterceptor = new PacketInterceptor() {

        public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) {
            // Only track processed packets so that we don't count them twice.
            if (processed) {
                packetCount.incrementAndGet();
            }
        }
    };
    InterceptorManager.getInstance().addInterceptor(packetInterceptor);
    // Register all statistics.
    addServerToServerStatistic();
    addActiveSessionsStatistic();
    addPacketStatistic();
}
Also used : Packet(org.xmpp.packet.Packet) PacketInterceptor(org.jivesoftware.openfire.interceptor.PacketInterceptor) Session(org.jivesoftware.openfire.session.Session)

Example 4 with PacketInterceptor

use of org.jivesoftware.openfire.interceptor.PacketInterceptor in project Openfire by igniterealtime.

the class S2STestService method run.

/**
 * Run a test against the domain.
 * @return K-V pairs of debug information.
 * @throws Exception On error.
 */
public Map<String, String> run() throws Exception {
    waitUntil = new Semaphore(0);
    Map<String, String> results = new HashMap<>();
    final DomainPair pair = new DomainPair(XMPPServer.getInstance().getServerInfo().getXMPPDomain(), domain);
    // Tear down existing routes.
    final SessionManager sessionManager = SessionManager.getInstance();
    for (final Session incomingServerSession : sessionManager.getIncomingServerSessions(domain)) {
        incomingServerSession.close();
    }
    final Session outgoingServerSession = sessionManager.getOutgoingServerSession(pair);
    if (outgoingServerSession != null) {
        outgoingServerSession.close();
    }
    final IQ pingRequest = new IQ(Type.get);
    pingRequest.setChildElement("ping", IQPingHandler.NAMESPACE);
    pingRequest.setFrom(pair.getLocal());
    pingRequest.setTo(domain);
    // Intercept logging.
    final Writer logs = new StringWriter();
    final String appenderName = addAppender(logs);
    // Intercept packets.
    final PacketInterceptor interceptor = new S2SInterceptor(pingRequest);
    InterceptorManager.getInstance().addInterceptor(interceptor);
    // Send ping.
    try {
        Log.info("Sending server to server ping request to " + domain);
        XMPPServer.getInstance().getIQRouter().route(pingRequest);
        // Wait for success or exceed socket timeout.
        waitUntil.tryAcquire(RemoteServerManager.getSocketTimeout(), TimeUnit.MILLISECONDS);
        // Check on the connection status.
        logSessionStatus();
        // Prepare response.
        results.put("certs", getCertificates());
        results.put("stanzas", interceptor.toString());
        results.put("logs", logs.toString());
        return results;
    } finally {
        // Cleanup
        InterceptorManager.getInstance().removeInterceptor(interceptor);
        removeAppender(appenderName);
    }
}
Also used : StringWriter(java.io.StringWriter) DomainPair(org.jivesoftware.openfire.session.DomainPair) SessionManager(org.jivesoftware.openfire.SessionManager) IQ(org.xmpp.packet.IQ) Semaphore(java.util.concurrent.Semaphore) PacketInterceptor(org.jivesoftware.openfire.interceptor.PacketInterceptor) StringWriter(java.io.StringWriter) Writer(java.io.Writer) OutgoingServerSession(org.jivesoftware.openfire.session.OutgoingServerSession) Session(org.jivesoftware.openfire.session.Session)

Aggregations

PacketInterceptor (org.jivesoftware.openfire.interceptor.PacketInterceptor)4 Session (org.jivesoftware.openfire.session.Session)4 IQ (org.xmpp.packet.IQ)3 Packet (org.xmpp.packet.Packet)3 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 Semaphore (java.util.concurrent.Semaphore)1 Attribute (org.dom4j.Attribute)1 Element (org.dom4j.Element)1 DefaultAttribute (org.dom4j.tree.DefaultAttribute)1 DefaultElement (org.dom4j.tree.DefaultElement)1 SessionManager (org.jivesoftware.openfire.SessionManager)1 InterceptorManager (org.jivesoftware.openfire.interceptor.InterceptorManager)1 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)1 DomainPair (org.jivesoftware.openfire.session.DomainPair)1 OutgoingServerSession (org.jivesoftware.openfire.session.OutgoingServerSession)1 Message (org.xmpp.packet.Message)1 Presence (org.xmpp.packet.Presence)1