Commit acb37dab authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Refactoring fixes.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@644 b35dd754-fafc-0310-a699-88a17e54d16e
parent ee482ec7
...@@ -592,6 +592,12 @@ public class IQOwnerHandler { ...@@ -592,6 +592,12 @@ public class IQOwnerHandler {
for (String jid : room.getOwners()) { for (String jid : room.getOwners()) {
field.addValue(jid); field.addValue(jid);
} }
// Remove the old element
probeResult.remove(probeResult.element(QName.get("x", "jabber:x:data")));
// Add the new representation of configurationForm as an element
probeResult.add(configurationForm.asXMLElement());
} }
finally { finally {
room.lock.readLock().unlock(); room.lock.readLock().unlock();
......
...@@ -13,6 +13,7 @@ package org.jivesoftware.messenger.muc.spi; ...@@ -13,6 +13,7 @@ package org.jivesoftware.messenger.muc.spi;
import org.dom4j.Element; import org.dom4j.Element;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;
import org.dom4j.QName;
import org.jivesoftware.messenger.PacketRouter; import org.jivesoftware.messenger.PacketRouter;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.muc.MUCRole; import org.jivesoftware.messenger.muc.MUCRole;
...@@ -108,7 +109,8 @@ public class MUCRoleImpl implements MUCRole { ...@@ -108,7 +109,8 @@ public class MUCRoleImpl implements MUCRole {
this.router = packetRouter; this.router = packetRouter;
this.role = role; this.role = role;
this.affiliation = affiliation; this.affiliation = affiliation;
extendedInformation = DocumentHelper.createElement("x").addNamespace("", "http://jabber.org/protocol/muc#user"); extendedInformation =
DocumentHelper.createElement(QName.get("x", "http://jabber.org/protocol/muc#user"));
calculateExtendedInformation(); calculateExtendedInformation();
rJID = new JID(room.getName(), server.getServiceName(), nick); rJID = new JID(room.getName(), server.getServiceName(), nick);
setPresence(room.createPresence(null)); setPresence(room.createPresence(null));
......
...@@ -501,7 +501,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -501,7 +501,7 @@ public class MUCRoomImpl implements MUCRoom {
if (isRoomNew) { if (isRoomNew) {
Element frag = joinPresence.getChildElement( Element frag = joinPresence.getChildElement(
"x", "http://jabber.org/protocol/muc#user"); "x", "http://jabber.org/protocol/muc#user");
frag.element("status").addAttribute("code", "201"); frag.addElement("status").addAttribute("code", "201");
} }
joinPresence.setFrom(joinRole.getRoleAddress()); joinPresence.setFrom(joinRole.getRoleAddress());
broadcastPresence(joinPresence); broadcastPresence(joinPresence);
...@@ -648,11 +648,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -648,11 +648,7 @@ public class MUCRoomImpl implements MUCRoom {
if (destroy == null) { if (destroy == null) {
destroy = fragment.addElement("destroy"); destroy = fragment.addElement("destroy");
} }
Element reasonEl = destroy.element("reason"); destroy.addElement("reason").setText(reason);
if (reasonEl == null) {
reasonEl = destroy.addElement("reason");
}
reasonEl.setText(reason);
} }
router.route(presence); router.route(presence);
...@@ -915,14 +911,12 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -915,14 +911,12 @@ public class MUCRoomImpl implements MUCRoom {
throws NotAllowedException { throws NotAllowedException {
List<Presence> presences = new ArrayList<Presence>(); List<Presence> presences = new ArrayList<Presence>();
// Get all the roles (i.e. occupants) of this user based on his/her bare JID // Get all the roles (i.e. occupants) of this user based on his/her bare JID
List roles = occupantsByBareJID.get(bareJID); List<MUCRole> roles = occupantsByBareJID.get(bareJID);
if (roles == null) { if (roles == null) {
return presences; return presences;
} }
MUCRole role;
// Collect all the updated presences of these roles // Collect all the updated presences of these roles
for (Iterator it = roles.iterator(); it.hasNext();) { for (MUCRole role : roles) {
role = (MUCRole) it.next();
// Update the presence with the new affiliation and role // Update the presence with the new affiliation and role
role.setAffiliation(newAffiliation); role.setAffiliation(newAffiliation);
role.setRole(newRole); role.setRole(newRole);
...@@ -1125,26 +1119,22 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1125,26 +1119,22 @@ public class MUCRoomImpl implements MUCRoom {
bareJID, bareJID,
MUCRole.OUTCAST, MUCRole.OUTCAST,
MUCRole.NONE_ROLE); MUCRole.NONE_ROLE);
if (!updatedPresences.isEmpty()) { Element frag;
Presence presence; // Add the status code and reason why the user was banned to the presences that will
Element frag; // be sent to the room occupants (the banned user will not receive this presences)
// Add the status code and reason why the user was banned to the presences that will for (Presence presence : updatedPresences) {
// be sent to the room occupants (the banned user will not receive this presences) frag = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
for (Iterator it = updatedPresences.iterator(); it.hasNext();) { // Add the status code 301 that indicates that the user was banned
presence = (Presence) it.next(); frag.addElement("status").addAttribute("code", "301");
frag = presence.getChildElement("x", "http://jabber.org/protocol/muc#user"); // Add the reason why the user was banned
// Add the status code 301 that indicates that the user was banned if (reason != null && reason.trim().length() > 0) {
frag.element("status").addAttribute("code", "301"); frag.element("item").addElement("reason").setText(reason);
// Add the reason why the user was banned
if (reason != null && reason.trim().length() > 0) {
frag.element("item").element("reason").setText(reason);
}
// Remove the banned users from the room. If a user has joined the room from
// different client resources, he/she will be kicked from all the client resources
// Effectively kick the occupant from the room
kickPresence(presence, actorJID);
} }
// Remove the banned users from the room. If a user has joined the room from
// different client resources, he/she will be kicked from all the client resources
// Effectively kick the occupant from the room
kickPresence(presence, actorJID);
} }
// Update the affiliation lists // Update the affiliation lists
outcasts.add(bareJID); outcasts.add(bareJID);
...@@ -1216,25 +1206,22 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1216,25 +1206,22 @@ public class MUCRoomImpl implements MUCRoom {
// If the room is members-only, remove the user from the room including a status // If the room is members-only, remove the user from the room including a status
// code of 321 to indicate that the user was removed because of an affiliation // code of 321 to indicate that the user was removed because of an affiliation
// change // change
Presence presence;
Element frag; Element frag;
// Add the status code to the presences that will be sent to the room occupants // Add the status code to the presences that will be sent to the room occupants
for (Iterator it = updatedPresences.iterator(); it.hasNext();) { for (Presence presence : updatedPresences) {
presence = (Presence) it.next();
// Set the presence as an unavailable presence // Set the presence as an unavailable presence
presence.setType(Presence.Type.unavailable); presence.setType(Presence.Type.unavailable);
frag = presence.getChildElement("x", "http://jabber.org/protocol/muc#user"); frag = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
// Add the status code 321 that indicates that the user was removed because of // Add the status code 321 that indicates that the user was removed because of
// an affiliation change // an affiliation change
frag.element("status").addAttribute("code", "321"); frag.addElement("status").addAttribute("code", "321");
// Remove the ex-member from the room. If a user has joined the room from // Remove the ex-member from the room. If a user has joined the room from
// different client resources, he/she will be kicked from all the client // different client resources, he/she will be kicked from all the client
// resources. // resources.
// Effectively kick the occupant from the room // Effectively kick the occupant from the room
MUCUser senderUser = senderRole.getChatUser(); MUCUser senderUser = senderRole.getChatUser();
JID actorJID = (senderUser == null ? JID actorJID = (senderUser == null ? null : senderUser.getAddress());
null : senderUser.getAddress());
kickPresence(presence, actorJID); kickPresence(presence, actorJID);
} }
} }
...@@ -1415,15 +1402,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1415,15 +1402,7 @@ public class MUCRoomImpl implements MUCRoom {
"x", "http://jabber.org/protocol/muc#user"); "x", "http://jabber.org/protocol/muc#user");
// Add the reason why the user was granted voice // Add the reason why the user was granted voice
if (reason != null && reason.trim().length() > 0) { if (reason != null && reason.trim().length() > 0) {
Element item = frag.element("item"); frag.element("item").addElement("reason").setText(reason);
if (item == null) {
item = frag.addElement("item");
}
Element reasonEl = item.element("reason");
if (reasonEl == null) {
reasonEl = item.addElement("reason");
}
reasonEl.setText(reason);
} }
} }
return updatedPresence; return updatedPresence;
...@@ -1446,22 +1425,10 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1446,22 +1425,10 @@ public class MUCRoomImpl implements MUCRoom {
"x", "http://jabber.org/protocol/muc#user"); "x", "http://jabber.org/protocol/muc#user");
// Add the status code 307 that indicates that the user was kicked // Add the status code 307 that indicates that the user was kicked
Element status = frag.element("status"); frag.addElement("status").addAttribute("code", "307");
if (status == null) {
status = frag.addElement("status");
}
status.addAttribute("code", "307");
// Add the reason why the user was kicked // Add the reason why the user was kicked
if (reason != null && reason.trim().length() > 0) { if (reason != null && reason.trim().length() > 0) {
Element item = frag.element("item"); frag.element("item").addElement("reason").setText(reason);
if (item == null) {
item = frag.addElement("item");
}
Element reasonEl = item.element("reason");
if (reasonEl == null) {
reasonEl = item.addElement("reason");
}
reasonEl.setText(reason);
} }
// Effectively kick the occupant from the room // Effectively kick the occupant from the room
...@@ -1489,15 +1456,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1489,15 +1456,7 @@ public class MUCRoomImpl implements MUCRoom {
if (actorJID != null && actorJID.toString().length() > 0) { if (actorJID != null && actorJID.toString().length() > 0) {
Element frag = kickPresence.getChildElement( Element frag = kickPresence.getChildElement(
"x", "http://jabber.org/protocol/muc#user"); "x", "http://jabber.org/protocol/muc#user");
Element item = frag.element("item"); frag.element("item").addElement("actor").addAttribute("jid", actorJID.toString());
if (item == null) {
frag.addElement("item");
}
Element actor = item.element("actor");
if (actor == null) {
actor = item.addElement("actor");
}
actor.addAttribute("jid", actorJID.toString());
} }
// Send the unavailable presence to the banned user // Send the unavailable presence to the banned user
kickedRole.send(kickPresence); kickedRole.send(kickPresence);
......
...@@ -289,14 +289,13 @@ public class MUCUserImpl implements MUCUser { ...@@ -289,14 +289,13 @@ public class MUCUserImpl implements MUCUser {
} }
else { else {
try { try {
if ("query".equals(packet.getElement().getNamespacePrefix()) Element query = packet.getElement().element("query");
&& "http://jabber.org/protocol/muc#owner".equals(packet if (query != null &&
.getElement().getNamespaceURI())) { "http://jabber.org/protocol/muc#owner".equals(query.getNamespaceURI())) {
role.getChatRoom().getIQOwnerHandler().handleIQ(packet, role); role.getChatRoom().getIQOwnerHandler().handleIQ(packet, role);
} }
else if ("query".equals(packet.getElement().getNamespacePrefix()) else if (query != null &&
&& "http://jabber.org/protocol/muc#admin".equals(packet "http://jabber.org/protocol/muc#admin".equals(query.getNamespaceURI())) {
.getElement().getNamespaceURI())) {
role.getChatRoom().getIQAdminHandler().handleIQ(packet, role); role.getChatRoom().getIQAdminHandler().handleIQ(packet, role);
} }
else { else {
......
...@@ -64,7 +64,8 @@ public class ElementUtil { ...@@ -64,7 +64,8 @@ public class ElementUtil {
} }
// Search for this property by traversing down the XML hierarchy. // Search for this property by traversing down the XML hierarchy.
for (int i = 0; i < propName.length; i++) { int i = propName[0].equals(element.getName()) ? 1 : 0;
for (; i < propName.length; i++) {
element = element.element(propName[i]); element = element.element(propName[i]);
if (element == null) { if (element == null) {
break; break;
...@@ -105,7 +106,8 @@ public class ElementUtil { ...@@ -105,7 +106,8 @@ public class ElementUtil {
} }
// Search for this property by traversing down the XML hierarchy. // Search for this property by traversing down the XML hierarchy.
for (int i = 0; i < propName.length; i++) { int i = propName[0].equals(element.getName()) ? 1 : 0;
for (; i < propName.length; i++) {
element = element.element(propName[i]); element = element.element(propName[i]);
if (element == null) { if (element == null) {
break; break;
...@@ -113,8 +115,13 @@ public class ElementUtil { ...@@ -113,8 +115,13 @@ public class ElementUtil {
} }
if (element != null) { if (element != null) {
// The property exists so return true if (attName == null){
return true; // The property exists so return true
return true;
} else {
// The property exists if the attribute exists in the element
return element.attribute(attName) != null;
}
} }
else { else {
// The property does not exist so return false // The property does not exist so return false
...@@ -150,7 +157,8 @@ public class ElementUtil { ...@@ -150,7 +157,8 @@ public class ElementUtil {
String[] propName = parsePropertyName(name); String[] propName = parsePropertyName(name);
// Search for this property by traversing down the XML heirarchy, stopping one short. // Search for this property by traversing down the XML heirarchy, stopping one short.
for (int i = 0; i < propName.length - 1; i++) { int i = propName[0].equals(element.getName()) ? 1 : 0;
for (; i < propName.length - 1; i++) {
element = element.element(propName[i]); element = element.element(propName[i]);
if (element == null) { if (element == null) {
// This node doesn't match this part of the property name which // This node doesn't match this part of the property name which
...@@ -191,9 +199,10 @@ public class ElementUtil { ...@@ -191,9 +199,10 @@ public class ElementUtil {
public static void setProperties(Element element, String name, String[] values) { public static void setProperties(Element element, String name, String[] values) {
String[] propName = parsePropertyName(name); String[] propName = parsePropertyName(name);
setProperty(element, name, values[0]); setProperty(element, name, values[0]);
// Search for this property by traversing down the XML heirarchy, stopping one short.
for (int i = 0; i < propName.length - 1; i++) { // Search for this property by traversing down the XML heirarchy, stopping one short.
int i = propName[0].equals(element.getName()) ? 1 : 0;
for (; i < propName.length - 1; i++) {
element = element.element(propName[i]); element = element.element(propName[i]);
if (element == null) { if (element == null) {
// This node doesn't match this part of the property name which // This node doesn't match this part of the property name which
...@@ -207,9 +216,9 @@ public class ElementUtil { ...@@ -207,9 +216,9 @@ public class ElementUtil {
while (iter.hasNext()) { while (iter.hasNext()) {
((Node) iter.next()).detach(); ((Node) iter.next()).detach();
} }
for (int i = 0; i < values.length; i++) { for (int j = 0; i < values.length; i++) {
if (values[i] != null) { if (values[j] != null) {
element.addElement(childName).setText(values[i]); element.addElement(childName).setText(values[j]);
} }
} }
} }
...@@ -229,7 +238,8 @@ public class ElementUtil { ...@@ -229,7 +238,8 @@ public class ElementUtil {
String[] propName = parsePropertyName(parent); String[] propName = parsePropertyName(parent);
// Search for this property by traversing down the XML heirarchy. // Search for this property by traversing down the XML heirarchy.
for (int i = 0; i < propName.length; i++) { int i = propName[0].equals(element.getName()) ? 1 : 0;
for (; i < propName.length; i++) {
element = element.element(propName[i]); element = element.element(propName[i]);
if (element == null) { if (element == null) {
// This node doesn't match this part of the property name which // This node doesn't match this part of the property name which
...@@ -241,8 +251,8 @@ public class ElementUtil { ...@@ -241,8 +251,8 @@ public class ElementUtil {
List children = element.elements(); List children = element.elements();
int childCount = children.size(); int childCount = children.size();
String[] childrenNames = new String[childCount]; String[] childrenNames = new String[childCount];
for (int i = 0; i < childCount; i++) { for (int j = 0; i < childCount; i++) {
childrenNames[i] = ((Element) children.get(i)).getName(); childrenNames[j] = ((Element) children.get(j)).getName();
} }
return childrenNames; return childrenNames;
} }
...@@ -280,13 +290,14 @@ public class ElementUtil { ...@@ -280,13 +290,14 @@ public class ElementUtil {
* @param value the new value for the property. * @param value the new value for the property.
*/ */
public static void setProperty(Element element, String name, String value) { public static void setProperty(Element element, String name, String value) {
if (name == null) return; if (name == null || name.length() == 0) return;
if (value == null) value = ""; if (value == null) value = "";
String[] propName = parsePropertyName(name); String[] propName = parsePropertyName(name);
// Search for this property by traversing down the XML heirarchy. // Search for this property by traversing down the XML heirarchy.
for (int i = 0; i < propName.length - 1; i++) { int i = propName[0].equals(element.getName()) ? 1 : 0;
for (; i < propName.length - 1; i++) {
// If we don't find this part of the property in the XML heirarchy // If we don't find this part of the property in the XML heirarchy
// we add it as a new node // we add it as a new node
if (element.element(propName[i]) == null) { if (element.element(propName[i]) == null) {
......
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