Search in sources :

Example 11 with Connection

use of org.jivesoftware.openfire.Connection in project Openfire by igniterealtime.

the class ClientConnectionHandler method sessionIdle.

/**
 * In addition to the functionality provided by the parent class, this
 * method will send XMPP ping requests to the remote entity on every first
 * invocation of this method (which will occur after a period of half the
 * allowed connection idle time has passed, without any IO).
 *
 * XMPP entities must respond with either an IQ result or an IQ error
 * (feature-unavailable) stanza upon receiving the XMPP ping stanza. Both
 * responses will be received by Openfire and will cause the connection idle
 * count to be reset.
 *
 * Entities that do not respond to the IQ Ping stanzas can be considered
 * dead, and their connection will be closed by the parent class
 * implementation on the second invocation of this method.
 *
 * Note that whitespace pings that are sent by XMPP entities will also cause
 * the connection idle count to be reset.
 *
 * @see ConnectionHandler#sessionIdle(IoSession, IdleStatus)
 */
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
    super.sessionIdle(session, status);
    final boolean doPing = ConnectionSettings.Client.KEEP_ALIVE_PING_PROPERTY.getValue();
    if (doPing && session.getIdleCount(status) == 1) {
        final ClientStanzaHandler handler = (ClientStanzaHandler) session.getAttribute(HANDLER);
        final JID entity = handler.getAddress();
        if (entity != null) {
            // Ping the connection to see if it is alive.
            final IQ pingRequest = new IQ(Type.get);
            pingRequest.setChildElement("ping", IQPingHandler.NAMESPACE);
            pingRequest.setFrom(XMPPServer.getInstance().getServerInfo().getXMPPDomain());
            pingRequest.setTo(entity);
            // Get the connection for this session
            final Connection connection = (Connection) session.getAttribute(CONNECTION);
            if (Log.isDebugEnabled()) {
                Log.debug("ConnectionHandler: Pinging connection that has been idle: " + connection);
            }
            // OF-1497: Ensure that data sent to the client is processed through LocalClientSession, to avoid
            // synchronisation issues with stanza counts related to Stream Management (XEP-0198)!
            LocalClientSession ofSession = (LocalClientSession) SessionManager.getInstance().getSession(entity);
            if (ofSession == null) {
                Log.warn("Trying to ping a MINA connection that's idle, but has no corresponding Openfire session. MINA Connection: " + connection);
            } else {
                ofSession.deliver(pingRequest);
            }
        }
    }
}
Also used : LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) JID(org.xmpp.packet.JID) IQ(org.xmpp.packet.IQ) Connection(org.jivesoftware.openfire.Connection) ClientStanzaHandler(org.jivesoftware.openfire.net.ClientStanzaHandler)

Example 12 with Connection

use of org.jivesoftware.openfire.Connection in project Openfire by igniterealtime.

the class ConnectionHandler method messageReceived.

@Override
public void messageReceived(IoSession session, Object message) throws Exception {
    // Get the stanza handler for this session
    StanzaHandler handler = (StanzaHandler) session.getAttribute(HANDLER);
    // Get the parser to use to process stanza. For optimization there is going
    // to be a parser for each running thread. Each Filter will be executed
    // by the Executor placed as the first Filter. So we can have a parser associated
    // to each Thread
    final XMPPPacketReader parser = PARSER_CACHE.get();
    // Update counter of read btyes
    updateReadBytesCounter(session);
    // Let the stanza handler process the received stanza
    try {
        handler.process((String) message, parser);
    } catch (Throwable e) {
        // Make sure to catch Throwable, not (only) Exception! See OF-2367
        Log.error("Closing connection due to error while processing message: {}", message, e);
        final Connection connection = (Connection) session.getAttribute(CONNECTION);
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : XMPPPacketReader(org.dom4j.io.XMPPPacketReader) StanzaHandler(org.jivesoftware.openfire.net.StanzaHandler) Connection(org.jivesoftware.openfire.Connection)

Example 13 with Connection

use of org.jivesoftware.openfire.Connection in project Openfire by igniterealtime.

the class ConnectionHandler method exceptionCaught.

@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
    Log.warn("Closing connection due to exception in session: " + session, cause);
    try {
        // OF-524: Determine stream:error message.
        final StreamError error;
        if (cause != null && (cause instanceof XMLNotWellFormedException || (cause.getCause() != null && cause.getCause() instanceof XMLNotWellFormedException))) {
            error = new StreamError(StreamError.Condition.not_well_formed);
        } else {
            error = new StreamError(StreamError.Condition.internal_server_error);
        }
        final Connection connection = (Connection) session.getAttribute(CONNECTION);
        // OF-1784: Don't write an error when the source problem is an issue with writing data.
        if (JiveGlobals.getBooleanProperty("xmpp.skip-error-delivery-on-write-error.disable", false) || !(cause instanceof WriteException)) {
            connection.deliverRawText(error.toXML());
        }
    } finally {
        final Connection connection = (Connection) session.getAttribute(CONNECTION);
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : WriteException(org.apache.mina.core.write.WriteException) StreamError(org.xmpp.packet.StreamError) Connection(org.jivesoftware.openfire.Connection)

Aggregations

Connection (org.jivesoftware.openfire.Connection)13 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)4 SocketConnection (org.jivesoftware.openfire.net.SocketConnection)4 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)4 UnknownHostException (java.net.UnknownHostException)3 StreamID (org.jivesoftware.openfire.StreamID)3 TrustStore (org.jivesoftware.openfire.keystore.TrustStore)3 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)3 IOException (java.io.IOException)2 X509Certificate (java.security.cert.X509Certificate)2 Element (org.dom4j.Element)2 Namespace (org.dom4j.Namespace)2 QName (org.dom4j.QName)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2 KeyStoreException (java.security.KeyStoreException)1 Certificate (java.security.cert.Certificate)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 SaslException (javax.security.sasl.SaslException)1 WriteException (org.apache.mina.core.write.WriteException)1