Commit 9b91fa9c authored by Matt Tucker's avatar Matt Tucker Committed by matt

Refactoring work.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@588 b35dd754-fafc-0310-a699-88a17e54d16e
parent bcbd1c3d
...@@ -22,7 +22,6 @@ import org.jivesoftware.util.Log; ...@@ -22,7 +22,6 @@ import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*; import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.Permissions; import org.jivesoftware.messenger.auth.Permissions;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.spi.PresenceImpl;
import org.jivesoftware.messenger.user.*; import org.jivesoftware.messenger.user.*;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -34,6 +33,10 @@ import javax.xml.stream.XMLStreamException; ...@@ -34,6 +33,10 @@ import javax.xml.stream.XMLStreamException;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;
import org.dom4j.Element; import org.dom4j.Element;
import org.dom4j.QName; import org.dom4j.QName;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
import org.xmpp.packet.Presence;
/** /**
* Implements the TYPE_IQ jabber:iq:register protocol (plain only). Clients * Implements the TYPE_IQ jabber:iq:register protocol (plain only). Clients
...@@ -49,12 +52,7 @@ import org.dom4j.QName; ...@@ -49,12 +52,7 @@ import org.dom4j.QName;
* one to route TYPE_IQ requests not addressed to the server to * one to route TYPE_IQ requests not addressed to the server to
* another channel (probably for direct delivery to the recipient). * another channel (probably for direct delivery to the recipient).
* <p/> * <p/>
* <h2>Warning</h2>
* There should be a way of determining whether a session has
* authorization to access this feature. I'm not sure it is a good
* idea to do authorization in each handler. It would be nice if
* the framework could assert authorization policies across channels.
* <p/>
* <h2>Compatibility</h2> * <h2>Compatibility</h2>
* The current behavior is designed to emulate jabberd1.4. However * The current behavior is designed to emulate jabberd1.4. However
* this behavior differs significantly from JEP-0078 (non-SASL registration). * this behavior differs significantly from JEP-0078 (non-SASL registration).
...@@ -67,7 +65,7 @@ import org.dom4j.QName; ...@@ -67,7 +65,7 @@ import org.dom4j.QName;
*/ */
public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvider { public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvider {
private static MetaDataFragment probeResult; private static Element probeResult;
public UserManager userManager; public UserManager userManager;
public RosterManager rosterManager; public RosterManager rosterManager;
...@@ -91,10 +89,10 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid ...@@ -91,10 +89,10 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
if (probeResult == null) { if (probeResult == null) {
// Create the basic element of the probeResult which contains the basic registration // Create the basic element of the probeResult which contains the basic registration
// information (e.g. username, passoword and email) // information (e.g. username, passoword and email)
Element element = DocumentHelper.createElement(QName.get("query", "jabber:iq:register")); probeResult = DocumentHelper.createElement(QName.get("query", "jabber:iq:register"));
element.addElement("username"); probeResult.addElement("username");
element.addElement("password"); probeResult.addElement("password");
element.addElement("email"); probeResult.addElement("email");
// Create the registration form to include in the probeResult. The form will include // Create the registration form to include in the probeResult. The form will include
// the basic information plus name and visibility of name and email. // the basic information plus name and visibility of name and email.
...@@ -130,9 +128,8 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid ...@@ -130,9 +128,8 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
field.setRequired(true); field.setRequired(true);
registrationForm.addField(field); registrationForm.addField(field);
// Create the probeResult and add the basic info together with the registration form // Add the registration form to the probe result.
probeResult = new MetaDataFragment(element); probeResult.add(registrationForm.asXMLElement());
probeResult.addFragment(registrationForm);
} }
// Check for the default case where no inband property is set and // Check for the default case where no inband property is set and
// make the default true (allowing inband registration) // make the default true (allowing inband registration)
...@@ -148,7 +145,7 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid ...@@ -148,7 +145,7 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
public synchronized IQ handleIQ(IQ packet) throws public synchronized IQ handleIQ(IQ packet) throws
PacketException, UnauthorizedException, XMLStreamException { PacketException, UnauthorizedException, XMLStreamException {
// Look for a delegate for this packet // Look for a delegate for this packet
IQHandler delegate = getDelegate(packet.getRecipient()); IQHandler delegate = getDelegate(packet.getTo());
// We assume that the registration packet was meant to the server if delegate is // We assume that the registration packet was meant to the server if delegate is
// null // null
if (delegate != null) { if (delegate != null) {
...@@ -156,63 +153,63 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid ...@@ -156,63 +153,63 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
return delegate.handleIQ(packet); return delegate.handleIQ(packet);
} }
Session session = packet.getOriginatingSession(); Session session = null;
try {
session = SessionManager.getInstance().getSession(packet.getFrom());
}
catch (Exception e) {
}
IQ reply = null; IQ reply = null;
if (!enabled) { if (!enabled) {
reply = packet.createResult(); reply = IQ.createResultIQ(packet);
reply.setError(XMPPError.Code.FORBIDDEN); reply.setError(PacketError.Condition.forbidden);
} }
else if (IQ.GET.equals(packet.getType())) { else if (IQ.Type.get.equals(packet.getType())) {
reply = packet.createResult(); reply = IQ.createResultIQ(packet);
if (session.getStatus() == Session.STATUS_AUTHENTICATED) { if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
try { try {
User user = userManager.getUser(session.getUsername()); User user = userManager.getUser(session.getUsername());
MetaDataFragment currentRegistration = (MetaDataFragment) probeResult Element currentRegistration = probeResult.createCopy();
.createDeepCopy(); currentRegistration.element("registered").setText(null);
currentRegistration.setProperty("query.registered", null); currentRegistration.element("username").setText(user.getUsername());
currentRegistration.setProperty("query.username", user.getUsername()); currentRegistration.element("password").setText(null);
currentRegistration.setProperty("query.password", null); currentRegistration.element("email").setText(user.getInfo().getEmail());
currentRegistration.setProperty("query.email", user.getInfo().getEmail());
Element form = currentRegistration.element(QName.get("x", "jabber:x:data"));
XDataFormImpl form = (XDataFormImpl) currentRegistration.getFragment( form.element("username").setText(user.getUsername());
"x", form.element("name").setText(user.getInfo().getName());
"jabber:x:data"); form.element("email").setText(user.getInfo().getEmail());
form.getField("username").addValue(user.getUsername()); reply.setChildElement(currentRegistration);
form.getField("name").addValue(user.getInfo().getName());
form.getField("email").addValue(user.getInfo().getEmail());
reply.setChildFragment(currentRegistration);
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
reply.setChildFragment(probeResult); reply.setChildElement(probeResult);
} }
catch (UnauthorizedException e) { catch (UnauthorizedException e) {
reply.setChildFragment(probeResult); reply.setChildElement(probeResult);
} }
} }
else { else {
reply.setChildFragment(probeResult); reply.setChildElement(probeResult);
} }
} }
else if (IQ.SET.equals(packet.getType())) { else if (IQ.Type.set.equals(packet.getType())) {
try { try {
XMPPFragment iq = packet.getChildFragment(); Element iqElement = packet.getChildElement();
MetaDataFragment metaData = MetaDataFragment.convertToMetaData(iq); if (iqElement.element("remove") != null) {
if (metaData.includesProperty("query.remove")) {
if (session.getStatus() == Session.STATUS_AUTHENTICATED) { if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
// Send an unavailable presence to the user's subscribers // Send an unavailable presence to the user's subscribers
// Note: This gives us a chance to send an unavailable presence to the // Note: This gives us a chance to send an unavailable presence to the
// entities that the user sent directed presences // entities that the user sent directed presences
Presence presence = new PresenceImpl(); Presence presence = new Presence();
presence.setAvailable(false); presence.setType(Presence.Type.unavailable);
presence.setVisible(false); presence.setFrom(packet.getFrom());
presence.setSender(packet.getSender());
presenceHandler.process(presence); presenceHandler.process(presence);
// Delete the user // Delete the user
userManager.deleteUser(userManager.getUser(session.getUsername())); userManager.deleteUser(userManager.getUser(session.getUsername()));
// Delete the roster of the user // Delete the roster of the user
rosterManager.deleteRoster(session.getAddress()); rosterManager.deleteRoster(session.getAddress());
reply = packet.createResult(); reply = IQ.createResultIQ(packet);
session.getConnection().deliver(reply); session.getConnection().deliver(reply);
// Close the user's connection // Close the user's connection
session.getConnection().close(); session.getConnection().close();
...@@ -229,10 +226,7 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid ...@@ -229,10 +226,7 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
XDataFormImpl registrationForm = null; XDataFormImpl registrationForm = null;
FormField field; FormField field;
// We prefer to assume that iq is an XMPPDOMFragment for performance reasons. Element formElement = iqElement.element("x");
// The other choice is to use metaData.convertToDOMFragment() which creates new
// objects.
Element formElement = ((XMPPDOMFragment)iq).getRootElement().element("x");
// Check if a form was used to provide the registration info // Check if a form was used to provide the registration info
if (formElement != null) { if (formElement != null) {
// Get the sent form // Get the sent form
...@@ -256,9 +250,9 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid ...@@ -256,9 +250,9 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
} }
else { else {
// Get the registration info from the query elements // Get the registration info from the query elements
username = metaData.getProperty("query.username"); username = iqElement.elementText("username");
password = metaData.getProperty("query.password"); password = iqElement.elementText("password");
email = metaData.getProperty("query.email"); email = iqElement.elementText("email");
} }
if (email == null || "".equals(email)) { if (email == null || "".equals(email)) {
email = " "; email = " ";
...@@ -267,8 +261,8 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid ...@@ -267,8 +261,8 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
// Inform the entity of failed registration if some required information was // Inform the entity of failed registration if some required information was
// not provided // not provided
if (password == null || password.trim().length() == 0) { if (password == null || password.trim().length() == 0) {
reply = packet.createResult(); reply = IQ.createResultIQ(packet);
reply.setError(XMPPError.Code.NOT_ACCEPTABLE); reply.setError(PacketError.Condition.not_acceptable);
return reply; return reply;
} }
...@@ -310,16 +304,16 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid ...@@ -310,16 +304,16 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
newUser.saveInfo(); newUser.saveInfo();
} }
reply = packet.createResult(); reply = IQ.createResultIQ(packet);
} }
} }
catch (UserAlreadyExistsException e) { catch (UserAlreadyExistsException e) {
reply = packet.createResult(); reply = IQ.createResultIQ(packet);
reply.setError(XMPPError.Code.CONFLICT); reply.setError(PacketError.Condition.conflict);
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
reply = packet.createResult(); reply = IQ.createResultIQ(packet);
reply.setError(XMPPError.Code.BAD_REQUEST); reply.setError(PacketError.Condition.bad_request);
} }
catch (Exception e) { catch (Exception e) {
Log.error(e); Log.error(e);
...@@ -341,11 +335,11 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid ...@@ -341,11 +335,11 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
JiveGlobals.setProperty("register.inband", enabled ? "true" : "false"); JiveGlobals.setProperty("register.inband", enabled ? "true" : "false");
} }
private IQHandler getDelegate(XMPPAddress recipientJID) { private IQHandler getDelegate(JID recipientJID) {
if (recipientJID == null) { if (recipientJID == null) {
return null; return null;
} }
return (IQHandler) delegates.get(recipientJID.getHostPrep()); return (IQHandler) delegates.get(recipientJID.getDomain());
} }
public void addDelegate(String serviceName, IQHandler delegate) { public void addDelegate(String serviceName, IQHandler delegate) {
......
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