Commit 59162ac0 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Added notification of new packet received to Sessions. JM-249


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1218 b35dd754-fafc-0310-a699-88a17e54d16e
parent 74bfd449
...@@ -379,4 +379,8 @@ public class ClientSession extends Session { ...@@ -379,4 +379,8 @@ public class ClientSession extends Session {
} }
} }
} }
public void packetReceived(Packet packet) {
packet.setFrom(getAddress());
}
} }
...@@ -19,6 +19,7 @@ import org.jivesoftware.messenger.auth.UnauthorizedException; ...@@ -19,6 +19,7 @@ import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.auth.AuthFactory; import org.jivesoftware.messenger.auth.AuthFactory;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.jivesoftware.util.LocaleUtils; import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.JiveGlobals;
import org.dom4j.io.XPPPacketReader; import org.dom4j.io.XPPPacketReader;
import org.dom4j.Element; import org.dom4j.Element;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
...@@ -56,6 +57,13 @@ public class ComponentSession extends Session { ...@@ -56,6 +57,13 @@ public class ComponentSession extends Session {
Session session; Session session;
String domain = xpp.getAttributeValue("", "to"); String domain = xpp.getAttributeValue("", "to");
// Get the requested subdomain
String subdomain = domain;
int index = domain.indexOf(serverName);
if (index > -1) {
subdomain = domain.substring(0, index -1);
}
Writer writer = connection.getWriter(); Writer writer = connection.getWriter();
// Default answer header in case of an error // Default answer header in case of an error
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -96,8 +104,8 @@ public class ComponentSession extends Session { ...@@ -96,8 +104,8 @@ public class ComponentSession extends Session {
connection.close(); connection.close();
return null; return null;
} }
// Check that the requested domain is not already in use // Check that the requested subdomain is not already in use
if (InternalComponentManager.getInstance().getComponent(domain) != null) { if (InternalComponentManager.getInstance().getComponent(subdomain) != null) {
// Domain already occupied so return a conflict error and close the connection // Domain already occupied so return a conflict error and close the connection
// Include the conflict error in the response // Include the conflict error in the response
StreamError error = new StreamError(StreamError.Condition.conflict); StreamError error = new StreamError(StreamError.Condition.conflict);
...@@ -113,8 +121,7 @@ public class ComponentSession extends Session { ...@@ -113,8 +121,7 @@ public class ComponentSession extends Session {
// Create a ComponentSession for the external component // Create a ComponentSession for the external component
session = SessionManager.getInstance().createComponentSession(connection); session = SessionManager.getInstance().createComponentSession(connection);
// Set the bind address as the address of the session // Set the bind address as the address of the session
session.setAddress(new JID(null, session.setAddress(new JID(null, domain , null));
domain + "." + XMPPServer.getInstance().getServerInfo().getName(), null));
try { try {
// Build the start packet response // Build the start packet response
...@@ -160,9 +167,7 @@ public class ComponentSession extends Session { ...@@ -160,9 +167,7 @@ public class ComponentSession extends Session {
writer.flush(); writer.flush();
// Bind the domain to this component // Bind the domain to this component
ExternalComponent component = ((ComponentSession) session).getExternalComponent(); ExternalComponent component = ((ComponentSession) session).getExternalComponent();
InternalComponentManager.getInstance().addComponent(domain, component); InternalComponentManager.getInstance().addComponent(subdomain, component);
// Set the service name to the component
component.setServiceName(domain);
return session; return session;
} }
} }
...@@ -200,8 +205,6 @@ public class ComponentSession extends Session { ...@@ -200,8 +205,6 @@ public class ComponentSession extends Session {
*/ */
public class ExternalComponent implements Component { public class ExternalComponent implements Component {
private String serviceName;
public void processPacket(Packet packet) { public void processPacket(Packet packet) {
if (conn != null && !conn.isClosed()) { if (conn != null && !conn.isClosed()) {
try { try {
...@@ -227,17 +230,9 @@ public class ComponentSession extends Session { ...@@ -227,17 +230,9 @@ public class ComponentSession extends Session {
public void shutdown() { public void shutdown() {
} }
public JID getAddress() {
return ComponentSession.this.getAddress();
} }
public String getServiceName() { public void packetReceived(Packet packet) {
return serviceName; //Do nothing
}
void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
} }
} }
\ No newline at end of file
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
package org.jivesoftware.messenger; package org.jivesoftware.messenger;
import org.dom4j.Element; import org.dom4j.Element;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.xmpp.component.Component; import org.xmpp.component.Component;
import org.xmpp.component.ComponentManager; import org.xmpp.component.ComponentManager;
import org.xmpp.component.ComponentManagerFactory; import org.xmpp.component.ComponentManagerFactory;
...@@ -21,6 +20,7 @@ import org.xmpp.packet.JID; ...@@ -21,6 +20,7 @@ import org.xmpp.packet.JID;
import org.xmpp.packet.Packet; import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.jivesoftware.util.JiveGlobals;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -80,6 +80,8 @@ public class InternalComponentManager implements ComponentManager, RoutableChann ...@@ -80,6 +80,8 @@ public class InternalComponentManager implements ComponentManager, RoutableChann
XMPPServer.getInstance().getRoutingTable().addRoute(componentJID, XMPPServer.getInstance().getRoutingTable().addRoute(componentJID,
new RoutableComponent(componentJID, component)); new RoutableComponent(componentJID, component));
component.initialize(componentJID, this);
// Check for potential interested users. // Check for potential interested users.
checkPresences(); checkPresences();
// Send a disco#info request to the new component. If the component provides information // Send a disco#info request to the new component. If the component provides information
...@@ -119,6 +121,14 @@ public class InternalComponentManager implements ComponentManager, RoutableChann ...@@ -119,6 +121,14 @@ public class InternalComponentManager implements ComponentManager, RoutableChann
//To change body of implemented methods use File | Settings | File Templates. //To change body of implemented methods use File | Settings | File Templates.
} }
public String getServerName() {
return serverDomain;
}
public String getHomeDirectory() {
return JiveGlobals.getHomeDirectory();
}
public boolean isExternalMode() { public boolean isExternalMode() {
return false; //To change body of implemented methods use File | Settings | File Templates. return false; //To change body of implemented methods use File | Settings | File Templates.
} }
...@@ -179,11 +189,11 @@ public class InternalComponentManager implements ComponentManager, RoutableChann ...@@ -179,11 +189,11 @@ public class InternalComponentManager implements ComponentManager, RoutableChann
* Retrieves the <code>Component</code> which is mapped * Retrieves the <code>Component</code> which is mapped
* to the specified JID. * to the specified JID.
* *
* @param jid the jid mapped to the component. * @param componentJID the jid mapped to the component.
* @return the component with the specified id. * @return the component with the specified id.
*/ */
public Component getComponent(String jid) { public Component getComponent(JID componentJID) {
jid = new JID(jid).toBareJID(); String jid = componentJID.toBareJID();
if (components.containsKey(jid)) { if (components.containsKey(jid)) {
return components.get(jid); return components.get(jid);
} }
...@@ -198,6 +208,17 @@ public class InternalComponentManager implements ComponentManager, RoutableChann ...@@ -198,6 +208,17 @@ public class InternalComponentManager implements ComponentManager, RoutableChann
return components.get(jid); return components.get(jid);
} }
/**
* Retrieves the <code>Component</code> which is mapped
* to the specified JID.
*
* @param jid the jid mapped to the component.
* @return the component with the specified id.
*/
public Component getComponent(String jid) {
return getComponent(new JID(jid));
}
/** /**
* Registers Probeers who have not yet been serviced. * Registers Probeers who have not yet been serviced.
* *
......
...@@ -13,6 +13,7 @@ package org.jivesoftware.messenger; ...@@ -13,6 +13,7 @@ package org.jivesoftware.messenger;
import org.jivesoftware.messenger.auth.AuthToken; import org.jivesoftware.messenger.auth.AuthToken;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import java.util.Date; import java.util.Date;
...@@ -208,4 +209,12 @@ public abstract class Session implements RoutableChannelHandler { ...@@ -208,4 +209,12 @@ public abstract class Session implements RoutableChannelHandler {
public long getNumServerPackets() { public long getNumServerPackets() {
return serverPacketCount; return serverPacketCount;
} }
/**
* Notification message that a packet has been received from this session. Client sessions
* will probably want to set the FROM attribute to avoid spoofing.
*
* @param packet the received packet by this session.
*/
public abstract void packetReceived(Packet packet);
} }
\ No newline at end of file
...@@ -192,7 +192,9 @@ public class SocketReadThread extends Thread { ...@@ -192,7 +192,9 @@ public class SocketReadThread extends Thread {
session.process(reply); session.process(reply);
continue; continue;
} }
packet.setFrom(session.getAddress()); // Notify the session that a new packet has been received. This could be useful
// for client sessions for setting the real sender of the packet and avoid spoofing
session.packetReceived(packet);
try { try {
// Invoke the interceptors before we process the read packet // Invoke the interceptors before we process the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true, InterceptorManager.getInstance().invokeInterceptors(packet, session, true,
...@@ -228,7 +230,9 @@ public class SocketReadThread extends Thread { ...@@ -228,7 +230,9 @@ public class SocketReadThread extends Thread {
session.process(reply); session.process(reply);
continue; continue;
} }
packet.setFrom(session.getAddress()); // Notify the session that a new packet has been received. This could be useful
// for client sessions for setting the real sender of the packet and avoid spoofing
session.packetReceived(packet);
try { try {
// Invoke the interceptors before we process the read packet // Invoke the interceptors before we process the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true, InterceptorManager.getInstance().invokeInterceptors(packet, session, true,
...@@ -271,7 +275,9 @@ public class SocketReadThread extends Thread { ...@@ -271,7 +275,9 @@ public class SocketReadThread extends Thread {
session.process(reply); session.process(reply);
continue; continue;
} }
packet.setFrom(session.getAddress()); // Notify the session that a new packet has been received. This could be useful
// for client sessions for setting the real sender of the packet and avoid spoofing
session.packetReceived(packet);
try { try {
// Invoke the interceptors before we process the read packet // Invoke the interceptors before we process the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true, InterceptorManager.getInstance().invokeInterceptors(packet, session, true,
......
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