Commit a26c4225 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Refactoring wortk. JM-924

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@6544 b35dd754-fafc-0310-a699-88a17e54d16e
parent 61224df9
......@@ -3,7 +3,7 @@
* $Revision: 3125 $
* $Date: 2005-11-30 15:14:14 -0300 (Wed, 30 Nov 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
......@@ -20,6 +20,8 @@ import org.jivesoftware.wildfire.container.BasicModule;
import org.jivesoftware.wildfire.roster.Roster;
import org.jivesoftware.wildfire.roster.RosterItem;
import org.jivesoftware.wildfire.roster.RosterManager;
import org.jivesoftware.wildfire.session.ClientSession;
import org.jivesoftware.wildfire.session.Session;
import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.xmpp.packet.*;
......@@ -82,10 +84,12 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
directedPresences = new ConcurrentHashMap<String, WeakHashMap<ChannelHandler, Set<String>>>();
}
public void process(Packet xmppPacket) throws UnauthorizedException, PacketException {
Presence presence = (Presence)xmppPacket;
public void process(Packet packet) throws UnauthorizedException, PacketException {
process((Presence) packet, sessionManager.getSession(packet.getFrom()));
}
public void process(Presence presence, ClientSession session) throws UnauthorizedException, PacketException {
try {
ClientSession session = sessionManager.getSession(presence.getFrom());
Presence.Type type = presence.getType();
// Available
if (type == null) {
......@@ -108,11 +112,6 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
else if (Presence.Type.unavailable == type) {
broadcastUpdate(presence.createCopy());
broadcastUnavailableForDirectedPresences(presence);
if (session == null) {
// Recovery logic. Check if a session can be found in the routing table.
session = (ClientSession) XMPPServer.getInstance().getRoutingTable()
.getRoute(presence.getFrom());
}
if (session != null) {
session.setPresence(presence);
}
......@@ -138,8 +137,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error") + ". Triggered by packet: " +
xmppPacket, e);
Log.error(LocaleUtils.getLocalizedString("admin.error") + ". Triggered by packet: " + presence, e);
}
}
......
......@@ -3,7 +3,7 @@
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
......@@ -15,19 +15,21 @@ import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.ClientSession;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.SessionPacketRouter;
import org.xmpp.packet.*;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.session.ClientSession;
import org.jivesoftware.wildfire.session.ConnectionMultiplexerSession;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
* IQ packets sent from Connection Managers themselves to the server will be handled by
* instances of this class. Each instance of
* {@link org.jivesoftware.wildfire.net.ConnectionMultiplexerSocketReader} will have an instance
* of this class so that IQ packets can be routed to this handler.<p>
* instances of this class.<p>
* <p/>
* This class will interact with {@link ConnectionMultiplexerManager} to create, close or
* get client sessions.
......@@ -157,6 +159,8 @@ public class MultiplexerPacketHandler {
}
SessionPacketRouter router = new SessionPacketRouter(session);
// Connection Manager already validate JIDs so just skip this expensive operation
router.setSkipJIDValidation(true);
try {
router.route(route.getChildElement());
}
......@@ -228,7 +232,7 @@ public class MultiplexerPacketHandler {
ConnectionMultiplexerSession session =
multiplexerManager.getMultiplexerSession(connectionManagerDomain);
if (session != null) {
session.deliver(reply);
session.process(reply);
}
else {
Log.warn("No multiplexer session found. Packet not delivered: " + reply.toXML());
......
......@@ -3,7 +3,7 @@
* $Revision: 3187 $
* $Date: 2005-12-11 13:34:34 -0300 (Sun, 11 Dec 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
......@@ -16,11 +16,13 @@ import com.jcraft.jzlib.ZOutputStream;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.wildfire.Connection;
import org.jivesoftware.wildfire.ConnectionCloseListener;
import org.jivesoftware.wildfire.PacketDeliverer;
import org.jivesoftware.wildfire.PacketException;
import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.wildfire.interceptor.InterceptorManager;
import org.jivesoftware.wildfire.interceptor.PacketRejectedException;
import org.jivesoftware.wildfire.server.IncomingServerSession;
import org.jivesoftware.wildfire.session.IncomingServerSession;
import org.jivesoftware.wildfire.session.Session;
import org.xmpp.packet.Packet;
import javax.net.ssl.SSLSession;
......@@ -113,7 +115,7 @@ public class SocketConnection implements Connection {
* @param backupDeliverer the packet deliverer this connection will use when socket is closed.
* @param socket the socket to represent.
* @param isSecure true if this is a secure connection.
* @throws NullPointerException if the socket is null.
* @throws java.io.IOException if there was a socket error while sending the packet.
*/
public SocketConnection(PacketDeliverer backupDeliverer, Socket socket, boolean isSecure)
throws IOException {
......@@ -149,17 +151,6 @@ public class SocketConnection implements Connection {
return tlsStreamHandler;
}
/**
* Secures the plain connection by negotiating TLS with the client. When connecting
* to a remote server then <tt>clientMode</tt> will be <code>true</code> and
* <tt>remoteServer</tt> is the server name of the remote server. Otherwise <tt>clientMode</tt>
* will be <code>false</code> and <tt>remoteServer</tt> null.
*
* @param clientMode boolean indicating if this entity is a client or a server.
* @param remoteServer server name of the remote server we are connecting to or <tt>null</tt>
* when not in client mode.
* @throws IOException if an error occured while securing the connection.
*/
public void startTLS(boolean clientMode, String remoteServer) throws IOException {
if (!secure) {
secure = true;
......@@ -177,29 +168,28 @@ public class SocketConnection implements Connection {
}
}
/**
* Start using compression for this connection. Compression will only be available after TLS
* has been negotiated. This means that a connection can never be using compression before
* TLS. However, it is possible to use compression without TLS.
*
* @throws IOException if an error occured while starting compression.
*/
public void startCompression() throws IOException {
public void startCompression() {
compressed = true;
if (tlsStreamHandler == null) {
ZOutputStream out = new ZOutputStream(
ServerTrafficCounter.wrapOutputStream(socket.getOutputStream()),
JZlib.Z_BEST_COMPRESSION);
out.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
writer = new BufferedWriter(new OutputStreamWriter(out, CHARSET));
xmlSerializer = new XMLSocketWriter(writer, this);
}
else {
ZOutputStream out = new ZOutputStream(tlsStreamHandler.getOutputStream(), JZlib.Z_BEST_COMPRESSION);
out.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
writer = new BufferedWriter(new OutputStreamWriter(out, CHARSET));
xmlSerializer = new XMLSocketWriter(writer, this);
try {
if (tlsStreamHandler == null) {
ZOutputStream out = new ZOutputStream(
ServerTrafficCounter.wrapOutputStream(socket.getOutputStream()),
JZlib.Z_BEST_COMPRESSION);
out.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
writer = new BufferedWriter(new OutputStreamWriter(out, CHARSET));
xmlSerializer = new XMLSocketWriter(writer, this);
}
else {
ZOutputStream out = new ZOutputStream(tlsStreamHandler.getOutputStream(), JZlib.Z_BEST_COMPRESSION);
out.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
writer = new BufferedWriter(new OutputStreamWriter(out, CHARSET));
xmlSerializer = new XMLSocketWriter(writer, this);
}
} catch (IOException e) {
// TODO Would be nice to still be able to throw the exception and not catch it here
Log.error("Error while starting compression", e);
compressed = false;
}
}
......@@ -234,19 +224,17 @@ public class SocketConnection implements Connection {
session = owner;
}
public Object registerCloseListener(ConnectionCloseListener listener, Object handbackMessage) {
Object status = null;
public void registerCloseListener(ConnectionCloseListener listener, Object handbackMessage) {
if (isClosed()) {
listener.onConnectionClose(handbackMessage);
}
else {
status = listeners.put(listener, handbackMessage);
listeners.put(listener, handbackMessage);
}
return status;
}
public Object removeCloseListener(ConnectionCloseListener listener) {
return listeners.remove(listener);
public void removeCloseListener(ConnectionCloseListener listener) {
listeners.remove(listener);
}
public InetAddress getInetAddress() {
......@@ -399,13 +387,6 @@ public class SocketConnection implements Connection {
return null;
}
/**
* Returns the packet deliverer to use when delivering a packet over the socket fails. The
* packet deliverer will retry to send the packet using some other connection, will store
* the packet offline for later retrieval or will just drop it.
*
* @return the packet deliverer to use when delivering a packet over the socket fails.
*/
public PacketDeliverer getPacketDeliverer() {
return backupDeliverer;
}
......@@ -553,43 +534,34 @@ public class SocketConnection implements Connection {
backupDeliverer.deliver(packet);
}
else {
boolean errorDelivering = false;
boolean allowedToWrite = false;
try {
// Invoke the interceptors before we send the packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, false, false);
boolean errorDelivering = false;
boolean allowedToWrite = false;
try {
requestWriting();
allowedToWrite = true;
xmlSerializer.write(packet.getElement());
if (flashClient) {
writer.write('\0');
}
xmlSerializer.flush();
}
catch (Exception e) {
Log.debug("Error delivering packet" + "\n" + this.toString(), e);
errorDelivering = true;
}
finally {
if (allowedToWrite) {
releaseWriting();
}
}
if (errorDelivering) {
close();
// Retry sending the packet again. Most probably if the packet is a
// Message it will be stored offline
backupDeliverer.deliver(packet);
requestWriting();
allowedToWrite = true;
xmlSerializer.write(packet.getElement());
if (flashClient) {
writer.write('\0');
}
else {
// Invoke the interceptors after we have sent the packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, false, true);
session.incrementServerPacketCount();
xmlSerializer.flush();
}
catch (Exception e) {
Log.debug("Error delivering packet" + "\n" + this.toString(), e);
errorDelivering = true;
}
finally {
if (allowedToWrite) {
releaseWriting();
}
}
catch (PacketRejectedException e) {
// An interceptor rejected the packet so do nothing
if (errorDelivering) {
close();
// Retry sending the packet again. Most probably if the packet is a
// Message it will be stored offline
backupDeliverer.deliver(packet);
}
else {
session.incrementServerPacketCount();
}
}
}
......
......@@ -3,7 +3,7 @@
* $Revision: 3137 $
* $Date: 2005-12-01 02:11:05 -0300 (Thu, 01 Dec 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
......@@ -11,10 +11,12 @@
package org.jivesoftware.wildfire.net;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.wildfire.session.ClientSession;
import org.jivesoftware.wildfire.session.Session;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
......@@ -61,9 +63,9 @@ public class SocketPacketWriteHandler implements ChannelHandler {
// The target domain belongs to the local server
if (recipient == null || (recipient.getNode() == null && recipient.getResource() == null)) {
// no TO was found so send back the packet to the sender
Session senderSession = sessionManager.getSession(packet.getFrom());
if (senderSession != null && !senderSession.getConnection().isClosed()) {
senderSession.getConnection().deliver(packet);
ClientSession senderSession = sessionManager.getSession(packet.getFrom());
if (senderSession != null) {
senderSession.process(packet);
}
else {
// The sender is no longer available so drop the packet
......@@ -77,7 +79,7 @@ public class SocketPacketWriteHandler implements ChannelHandler {
}
else {
try {
session.getConnection().deliver(packet);
session.process(packet);
}
catch (Exception e) {
// do nothing
......
......@@ -3,7 +3,7 @@
* $Revision: 3187 $
* $Date: 2005-12-11 13:34:34 -0300 (Sun, 11 Dec 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
......@@ -16,11 +16,13 @@ import org.dom4j.io.XMPPPacketReader;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.StringUtils;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.wildfire.Connection;
import org.jivesoftware.wildfire.PacketRouter;
import org.jivesoftware.wildfire.RoutableChannelHandler;
import org.jivesoftware.wildfire.RoutingTable;
import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.wildfire.interceptor.InterceptorManager;
import org.jivesoftware.wildfire.interceptor.PacketRejectedException;
import org.jivesoftware.wildfire.server.OutgoingSessionPromise;
import org.jivesoftware.wildfire.session.Session;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
......@@ -107,12 +109,7 @@ public abstract class SocketReader implements Runnable {
reader.setXPPFactory(factory);
// Set the blocking reading mode to use
if (useBlockingMode) {
readingMode = new BlockingReadingMode(socket, this);
}
else {
readingMode = new NonBlockingReadingMode(socket, this);
}
readingMode = new BlockingReadingMode(socket, this);
}
/**
......@@ -243,35 +240,8 @@ public abstract class SocketReader implements Runnable {
closeNeverSecuredConnection();
return;
}
try {
// Invoke the interceptors before we process the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true,
false);
router.route(packet);
// Invoke the interceptors after we have processed the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true,
true);
session.incrementClientPacketCount();
}
catch (PacketRejectedException e) {
// An interceptor rejected this packet so answer a not_allowed error
IQ reply = new IQ();
reply.setChildElement(packet.getChildElement().createCopy());
reply.setID(packet.getID());
reply.setTo(session.getAddress());
reply.setFrom(packet.getTo());
reply.setError(PacketError.Condition.not_allowed);
session.process(reply);
// Check if a message notifying the rejection should be sent
if (e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
// A message for the rejection will be sent to the sender of the rejected packet
Message notification = new Message();
notification.setTo(session.getAddress());
notification.setFrom(packet.getTo());
notification.setBody(e.getRejectionMessage());
session.process(notification);
}
}
router.route(packet);
session.incrementClientPacketCount();
}
/**
......@@ -292,34 +262,8 @@ public abstract class SocketReader implements Runnable {
closeNeverSecuredConnection();
return;
}
try {
// Invoke the interceptors before we process the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true,
false);
router.route(packet);
// Invoke the interceptors after we have processed the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true,
true);
session.incrementClientPacketCount();
}
catch (PacketRejectedException e) {
// An interceptor rejected this packet so answer a not_allowed error
Presence reply = new Presence();
reply.setID(packet.getID());
reply.setTo(session.getAddress());
reply.setFrom(packet.getTo());
reply.setError(PacketError.Condition.not_allowed);
session.process(reply);
// Check if a message notifying the rejection should be sent
if (e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
// A message for the rejection will be sent to the sender of the rejected packet
Message notification = new Message();
notification.setTo(session.getAddress());
notification.setFrom(packet.getTo());
notification.setBody(e.getRejectionMessage());
session.process(notification);
}
}
router.route(packet);
session.incrementClientPacketCount();
}
/**
......@@ -340,30 +284,8 @@ public abstract class SocketReader implements Runnable {
closeNeverSecuredConnection();
return;
}
try {
// Invoke the interceptors before we process the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true,
false);
router.route(packet);
// Invoke the interceptors after we have processed the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true,
true);
session.incrementClientPacketCount();
}
catch (PacketRejectedException e) {
// An interceptor rejected this packet
if (e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
// A message for the rejection will be sent to the sender of the rejected packet
Message reply = new Message();
reply.setID(packet.getID());
reply.setTo(session.getAddress());
reply.setFrom(packet.getTo());
reply.setType(packet.getType());
reply.setThread(packet.getThread());
reply.setBody(e.getRejectionMessage());
session.process(reply);
}
}
router.route(packet);
session.incrementClientPacketCount();
}
/**
......@@ -538,7 +460,7 @@ public abstract class SocketReader implements Runnable {
}
/**
* Creates the appropriate {@link Session} subclass based on the specified namespace.
* Creates the appropriate {@link org.jivesoftware.wildfire.session.Session} subclass based on the specified namespace.
*
* @param namespace the namespace sent in the stream element. eg. jabber:client.
* @return the created session or null.
......
......@@ -3,7 +3,7 @@
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
......@@ -15,7 +15,7 @@ import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.Connection;
import org.jivesoftware.wildfire.Session;
import org.jivesoftware.wildfire.session.Session;
import org.xmlpull.v1.XmlPullParserException;
import org.xmpp.packet.StreamError;
......@@ -23,7 +23,7 @@ import java.io.IOException;
import java.net.Socket;
/**
* Abstract class for {@link BlockingReadingMode} and {@link NonBlockingReadingMode}.
* Abstract class for {@link BlockingReadingMode}.
*
* @author Gaston Dombiak
*/
......
......@@ -3,7 +3,7 @@
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
......@@ -15,7 +15,8 @@ import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.Connection;
import org.jivesoftware.wildfire.ConnectionCloseListener;
import org.jivesoftware.wildfire.Session;
import org.jivesoftware.wildfire.PacketDeliverer;
import org.jivesoftware.wildfire.session.Session;
import java.util.HashMap;
import java.util.Map;
......@@ -81,6 +82,39 @@ public abstract class VirtualConnection implements Connection {
return false;
}
public void setFlashClient(boolean flashClient) {
//Ignore
}
public void setXMPPVersion(int majorVersion, int minorVersion) {
//Ignore
}
public void setLanaguage(String language) {
//Ignore
}
public void setCompressionPolicy(CompressionPolicy compressionPolicy) {
//Ignore
}
public void setTlsPolicy(TLSPolicy tlsPolicy) {
//Ignore
}
public PacketDeliverer getPacketDeliverer() {
//Ignore
return null;
}
public void startTLS(boolean clientMode, String remoteServer) throws Exception {
//Ignore
}
public void startCompression() {
//Ignore
}
public boolean isSecure() {
// Return false since TLS is not used for virtual connections
return false;
......@@ -122,19 +156,17 @@ public abstract class VirtualConnection implements Connection {
}
}
public Object registerCloseListener(ConnectionCloseListener listener, Object handbackMessage) {
Object status = null;
public void registerCloseListener(ConnectionCloseListener listener, Object handbackMessage) {
if (isClosed()) {
listener.onConnectionClose(handbackMessage);
}
else {
status = listeners.put(listener, handbackMessage);
listeners.put(listener, handbackMessage);
}
return status;
}
public Object removeCloseListener(ConnectionCloseListener listener) {
return listeners.remove(listener);
public void removeCloseListener(ConnectionCloseListener listener) {
listeners.remove(listener);
}
/**
......
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