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

Refactoring work.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@636 b35dd754-fafc-0310-a699-88a17e54d16e
parent dd13bdb5
......@@ -138,7 +138,6 @@ public class PrivateStorage extends BasicModule {
* @return the data stored under the given key or the data element.
*/
public Element get(String username, Element data) {
data.clearContent();
if (enabled) {
Connection con = null;
PreparedStatement pstmt = null;
......@@ -149,6 +148,7 @@ public class PrivateStorage extends BasicModule {
pstmt.setString(2, data.getNamespaceURI());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
data.clearContent();
String result = rs.getString(1).trim();
Document doc = xmlReader.read(result);
data = doc.getRootElement();
......
......@@ -89,6 +89,7 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo {
String username = query.elementTextTrim("username");
probeResponse.element("username").setText(username);
response = IQ.createResultIQ(packet);
probeResponse.setParent(null);
response.setChildElement(probeResponse);
}
// Otherwise set query
......
......@@ -62,8 +62,9 @@ public class IQPrivateHandler extends IQHandler implements ServerFeaturesProvide
if (dataElement != null) {
if (IQ.Type.get.equals(packet.getType())) {
replyPacket = IQ.createResultIQ(packet);
Element privateData = replyPacket.setChildElement("query", "jabber:iq:private");
privateData.add(privateStorage.get(packet.getFrom().getNode(), dataElement));
Element dataStored = privateStorage.get(packet.getFrom().getNode(), dataElement);
dataStored.setParent(null);
replyPacket.setChildElement(dataStored);
}
else {
privateStorage.add(packet.getFrom().getNode(), dataElement);
......
......@@ -18,9 +18,7 @@ import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.*;
import org.jivesoftware.messenger.user.Roster;
import org.jivesoftware.messenger.user.spi.IQRosterItemImpl;
import org.xmpp.packet.*;
import org.dom4j.Element;
import java.util.ArrayList;
import java.util.Iterator;
......@@ -87,7 +85,7 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
UnauthorizedException, PacketException {
try {
IQ returnPacket = null;
IQRoster roster = (IQRoster)packet;
org.xmpp.packet.Roster roster = (org.xmpp.packet.Roster)packet;
JID recipientJID = packet.getTo();
......@@ -114,14 +112,12 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
*
* @param packet The packet suspected of containing a roster removal
*/
private void removeRosterItem(IQRoster packet) throws UnauthorizedException {
private void removeRosterItem(org.xmpp.packet.Roster packet) throws UnauthorizedException {
JID recipientJID = packet.getTo();
JID senderJID = packet.getFrom();
try {
Iterator itemIter = packet.getRosterItems();
while (itemIter.hasNext()) {
RosterItem packetItem = (RosterItem)itemIter.next();
if (packetItem.getSubStatus() == RosterItem.SUB_REMOVE) {
for (org.xmpp.packet.Roster.Item packetItem : packet.getItems()) {
if (packetItem.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
Roster roster = userManager.getUser(recipientJID.getNode()).getRoster();
RosterItem item = roster.getRosterItem(senderJID);
roster.deleteRosterItem(senderJID);
......@@ -147,12 +143,13 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
* @param packet The packet that triggered this update
* @return Either a response to the roster update or null if the packet is corrupt and the session was closed down
*/
private IQ manageRoster(IQRoster packet) throws UnauthorizedException, UserAlreadyExistsException {
private IQ manageRoster(org.xmpp.packet.Roster packet) throws UnauthorizedException,
UserAlreadyExistsException {
IQ returnPacket = null;
Session session = null;
try {
sessionManager.getSession(packet.getFrom());
session = sessionManager.getSession(packet.getFrom());
}
catch (Exception e) {
IQ error = IQ.createResultIQ(packet);
......@@ -177,16 +174,14 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
}
else if (IQ.Type.set == type) {
Iterator itemIter = packet.getRosterItems();
while (itemIter.hasNext()) {
RosterItem item = (RosterItem)itemIter.next();
if (item.getSubStatus() == RosterItem.SUB_REMOVE) {
for (org.xmpp.packet.Roster.Item item : packet.getItems()) {
if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
removeItem(cachedRoster, packet.getFrom(), item);
}
else {
if (cachedRoster.isRosterItem(item.getJid())) {
if (cachedRoster.isRosterItem(item.getJID())) {
// existing item
CachedRosterItem cachedItem = (CachedRosterItem)cachedRoster.getRosterItem(item.getJid());
CachedRosterItem cachedItem = (CachedRosterItem)cachedRoster.getRosterItem(item.getJID());
cachedItem.setAsCopyOf(item);
cachedRoster.updateRosterItem(cachedItem);
}
......@@ -215,13 +210,13 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
* @param sender The JID of the sender of the removal request
* @param item The removal item element
*/
private void removeItem(Roster roster, JID sender, RosterItem item)
private void removeItem(Roster roster, JID sender, org.xmpp.packet.Roster.Item item)
throws UnauthorizedException
{
JID recipient = item.getJid();
JID recipient = item.getJID();
// Remove recipient from the sender's roster
roster.deleteRosterItem(item.getJid());
roster.deleteRosterItem(item.getJID());
// Forward set packet to the subscriber
if (localServer.isLocal(recipient)) { // Recipient is local so let's handle it here
try {
......@@ -245,18 +240,11 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
* @param to The recipient address to use
* @return The forwarded packet generated
*/
private Packet createRemoveForward(JID from, JID to) throws UnauthorizedException {
IQ response = new IQ();
private Packet createRemoveForward(JID from, JID to) {
org.xmpp.packet.Roster response = new org.xmpp.packet.Roster(IQ.Type.set);
response.setFrom(from);
response.setTo(to);
response.setType(IQ.Type.set);
Element query = response.setChildElement("query", "jabber:iq:roster");
IQRosterItem responseItem = new IQRosterItemImpl(from);
responseItem.setSubStatus(RosterItem.SUB_REMOVE);
query.add(responseItem.asXMLElement());
response.addItem(from, org.xmpp.packet.Roster.Subscription.remove);
return response;
}
......
......@@ -32,6 +32,7 @@ import org.xmlpull.v1.XmlPullParserFactory;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Message;
import org.xmpp.packet.Presence;
import org.xmpp.packet.Roster;
/**
* @author Derek DeMoro
......@@ -195,7 +196,7 @@ public class SocketReadThread extends Thread {
clearSignout = (Presence.Type.unavailable == packet.getType() ? true : false);
}
else if ("iq".equals(tag)) {
IQ packet = new IQ(doc);
IQ packet = getIQ(doc);
packet.setFrom(session.getAddress());
auditor.audit(packet);
router.route(packet);
......@@ -208,6 +209,16 @@ public class SocketReadThread extends Thread {
}
}
private IQ getIQ(Element doc) {
Element query = doc.element("query");
if (query != null && "jabber:iq:roster".equals(query.getNamespaceURI())) {
return new Roster(doc);
}
else {
return new IQ(doc);
}
}
/**
* Uses the XPP to grab the opening stream tag and create
* an active session object. In all cases, the method obtains the
......
......@@ -99,7 +99,8 @@ public class BasicRoster implements Roster, Cacheable {
return createRosterItem(user, null, null);
}
public RosterItem createRosterItem(JID user, String nickname, List groups) throws UnauthorizedException, UserAlreadyExistsException {
public RosterItem createRosterItem(JID user, String nickname, List<String> groups)
throws UnauthorizedException, UserAlreadyExistsException {
RosterItem item = provideRosterItem(user, nickname, groups);
itemLock.writeLock().lock();
try {
......@@ -111,12 +112,12 @@ public class BasicRoster implements Roster, Cacheable {
}
}
public RosterItem createRosterItem(RosterItem item) throws UnauthorizedException, UserAlreadyExistsException {
item = provideRosterItem(item);
public void createRosterItem(org.xmpp.packet.Roster.Item item)
throws UnauthorizedException, UserAlreadyExistsException {
RosterItem rosterItem = provideRosterItem(item);
itemLock.writeLock().lock();
try {
rosterItems.put(item.getJid().toBareJID(), item);
return item;
rosterItems.put(item.getJID().toBareJID(), rosterItem);
}
finally {
itemLock.writeLock().unlock();
......@@ -134,7 +135,7 @@ public class BasicRoster implements Roster, Cacheable {
* @param groups The groups the item belongs to (or null for none)
* @return The newly created roster items ready to be stored by the BasicRoster item's hash table
*/
protected RosterItem provideRosterItem(JID user, String nickname, List groups)
protected RosterItem provideRosterItem(JID user, String nickname, List<String> groups)
throws UserAlreadyExistsException, UnauthorizedException {
return new BasicRosterItem(user, nickname, groups);
}
......@@ -148,7 +149,7 @@ public class BasicRoster implements Roster, Cacheable {
* @param item The item to copy settings for the new item in this roster
* @return The newly created roster items ready to be stored by the BasicRoster item's hash table
*/
protected RosterItem provideRosterItem(RosterItem item)
protected RosterItem provideRosterItem(org.xmpp.packet.Roster.Item item)
throws UserAlreadyExistsException, UnauthorizedException {
return new BasicRosterItem(item);
}
......
......@@ -27,7 +27,7 @@ public class BasicRosterItem implements RosterItem {
protected RecvType recvStatus;
protected JID jid;
protected String nickname;
protected List groups;
protected List<String> groups;
protected SubType subStatus;
protected AskType askStatus;
......@@ -37,15 +37,15 @@ public class BasicRosterItem implements RosterItem {
AskType askStatus,
RecvType recvStatus,
String nickname,
List groups) {
List<String> groups) {
this.jid = jid;
this.subStatus = subStatus;
this.askStatus = askStatus;
this.recvStatus = recvStatus;
this.nickname = nickname;
this.groups = new LinkedList();
this.groups = new LinkedList<String>();
if (groups != null) {
Iterator groupItr = groups.iterator();
Iterator<String> groupItr = groups.iterator();
while (groupItr.hasNext()) {
this.groups.add(groupItr.next());
}
......@@ -61,7 +61,7 @@ public class BasicRosterItem implements RosterItem {
null);
}
public BasicRosterItem(JID jid, String nickname, List groups) {
public BasicRosterItem(JID jid, String nickname, List<String> groups) {
this(jid,
RosterItem.SUB_NONE,
RosterItem.ASK_NONE,
......@@ -75,16 +75,44 @@ public class BasicRosterItem implements RosterItem {
*
* @param item
*/
public BasicRosterItem(RosterItem item) {
this(item.getJid(),
item.getSubStatus(),
item.getAskStatus(),
item.getRecvStatus(),
item.getNickname(),
item.getGroups());
public BasicRosterItem(org.xmpp.packet.Roster.Item item) {
this(item.getJID(),
getSubType(item),
getAskStatus(item),
RosterItem.RECV_NONE,
item.getName(),
new LinkedList<String>(item.getGroups()));
}
private static RosterItem.AskType getAskStatus(org.xmpp.packet.Roster.Item item) {
if (item.getAsk() == org.xmpp.packet.Roster.Ask.subscribe) {
return RosterItem.ASK_SUBSCRIBE;
}
else if (item.getAsk() == org.xmpp.packet.Roster.Ask.unsubscribe) {
return RosterItem.ASK_UNSUBSCRIBE;
}
else {
return RosterItem.ASK_NONE;
}
}
private static RosterItem.SubType getSubType(org.xmpp.packet.Roster.Item item) {
if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.to) {
return RosterItem.SUB_TO;
}
else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.from) {
return RosterItem.SUB_FROM;
}
else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.both) {
return RosterItem.SUB_BOTH;
}
else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
return RosterItem.SUB_REMOVE;
}
else {
return RosterItem.SUB_NONE;
}
}
public SubType getSubStatus() {
return subStatus;
......@@ -122,13 +150,13 @@ public class BasicRosterItem implements RosterItem {
this.nickname = nickname;
}
public List getGroups() {
public List<String> getGroups() {
return groups;
}
public void setGroups(List groups) {
public void setGroups(List<String> groups) {
if (groups == null) {
this.groups = new LinkedList();
this.groups = new LinkedList<String>();
}
else {
this.groups = groups;
......
......@@ -13,7 +13,7 @@ package org.jivesoftware.messenger.user;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.util.Cacheable;
import org.xmpp.packet.Presence;
import org.xmpp.packet.*;
/**
* <p>A Roster that is cached in memory and persisted to some backend storage system.</p>
......@@ -35,11 +35,11 @@ public interface CachedRoster extends Roster, Cacheable {
String getUsername();
/**
* <p>Obtain a 'roster reset', a snapshot of the full cached roster as an IQRoster.</p>
* <p>Obtain a 'roster reset', a snapshot of the full cached roster as an Roster.</p>
*
* @return The roster reset (snapshot) as an IQRoster
* @return The roster reset (snapshot) as an Roster
*/
IQRoster getReset() throws UnauthorizedException;
org.xmpp.packet.Roster getReset() throws UnauthorizedException;
/**
* <p>Broadcast the presence update to all subscribers of the roter.</p>
......
......@@ -39,5 +39,5 @@ public interface CachedRosterItem extends RosterItem, Cacheable {
*
* @param item The item who's settings will be copied into the cached copy
*/
void setAsCopyOf(RosterItem item);
void setAsCopyOf(org.xmpp.packet.Roster.Item item);
}
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.user;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.spi.IQRosterItemImpl;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
/**
* A roster implemented against a JDBC database
*
* @author Iain Shigeoka
*/
public class IQRoster extends IQ implements Roster {
private BasicIQRoster basicRoster = new BasicIQRoster();
/**
* <p>Create an empty iq roster packet.</p>
*/
public IQRoster() {
}
public String getChildNamespace() {
return "jabber:iq:roster";
}
public String getChildName() {
return "query";
}
public void parse(Document doc) {
Element root = doc.getRootElement();
setTo(new JID(root.attributeValue("to")));
setType(Type.valueOf(root.attributeValue("type")));
setID(root.attributeValue("id"));
Iterator elements = root.elements().iterator();
while (elements.hasNext()) {
Element element = (Element)elements.next();
if ("query".equals(element.getName())) {
Iterator items = element.elementIterator("item");
while (items.hasNext()) {
try {
Element item = (Element)items.next();
RosterItem rosterItem = basicRoster.createRosterItem(new JID(item.attributeValue("jid")),
item.attributeValue("name"),
null);
rosterItem.setSubStatus("remove".equals(item.attributeValue("subscription")) ?
RosterItem.SUB_REMOVE : RosterItem.SUB_NONE);
Iterator groupElements = item.elementIterator("group");
while (groupElements.hasNext()) {
rosterItem.getGroups().add(((Element)groupElements.next()).getTextTrim());
}
}
catch (UnauthorizedException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
catch (UserAlreadyExistsException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
}
}
}
// ##################################################################################
// Basic Roster usage - the downside of single inheritance
// ##################################################################################
private class BasicIQRoster extends BasicRoster {
protected RosterItem provideRosterItem(RosterItem item) throws UserAlreadyExistsException, UnauthorizedException {
return new IQRosterItemImpl(item);
}
protected RosterItem provideRosterItem(JID user, String nickname, List groups) throws UserAlreadyExistsException, UnauthorizedException {
return new IQRosterItemImpl(user, nickname, groups);
}
}
public boolean isRosterItem(JID user) {
return basicRoster.isRosterItem(user);
}
public Iterator getRosterItems() throws UnauthorizedException {
return basicRoster.getRosterItems();
}
public int getTotalRosterItemCount() throws UnauthorizedException {
return basicRoster.getTotalRosterItemCount();
}
public RosterItem getRosterItem(JID user)
throws UnauthorizedException, UserNotFoundException {
return basicRoster.getRosterItem(user);
}
public RosterItem createRosterItem(JID user)
throws UnauthorizedException, UserAlreadyExistsException {
return basicRoster.createRosterItem(user);
}
public RosterItem createRosterItem(JID user, String nickname, List groups)
throws UnauthorizedException, UserAlreadyExistsException {
return basicRoster.createRosterItem(user, nickname, groups);
}
public RosterItem createRosterItem(RosterItem item) throws UnauthorizedException, UserAlreadyExistsException {
return basicRoster.createRosterItem(item);
}
public void updateRosterItem(RosterItem item)
throws UnauthorizedException, UserNotFoundException {
basicRoster.updateRosterItem(item);
}
public RosterItem deleteRosterItem(JID user) throws UnauthorizedException {
return basicRoster.deleteRosterItem(user);
}
}
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.user;
import org.dom4j.Element;
/**
* <p>The standard XMPP IQ packet representing a roster item entry.</p>
* <p/>
* <p>IQRosterItems can read and write the standard XML representation of a roster item.
* Roster items are of the form:</p>
* <p><code><pre>
* &lt;item jid='jid' subscription='none|both|to|from' ask='subscribe|unsubscribe' name='nickname'&gt;
* &lt;group&gt;Friends&lt;/group&gt;
* &lt;group&gt;Co-workers&lt;/group&gt;
* &lt;/item&gt;
* </pre></code></p>
*
* @author Iain Shigeoka
*/
public interface IQRosterItem extends RosterItem {
/**
* <p>Obtain the roster item as an XML DOM element.</p>
*
* @return The item as an XML DOM element
*/
public Element asXMLElement();
}
\ No newline at end of file
......@@ -22,14 +22,13 @@ import java.util.List;
* <p>Rosters are similar to buddy groups in popular IM clients. The Roster interface is
* a generic representation of the roster data. There are two primary implementations
* of the Roster in Messenger, the CachedRoster representing a cached, persistently stored
* Roster attached to a user/chatbot account, and an IQRoster containing a roster as XML
* Roster attached to a user/chatbot account, and an Roster containing a roster as XML
* data (usually as it enters and exists Messenger over an XMPP c2s or s2s connection).
* It is an important distinction as changes to CachedRosters will be saved to disk while
* IQRosters are transient data objects.</p>
*
* @author Iain Shigeoka
*
* @see IQRoster
* @see CachedRoster
*/
public interface Roster {
......@@ -86,7 +85,8 @@ public interface Roster {
* @throws UnauthorizedException if not the item or an administrator.
* @throws UserAlreadyExistsException If a roster item already exists for the given user
*/
public RosterItem createRosterItem(JID user, String nickname, List groups) throws UnauthorizedException, UserAlreadyExistsException;
public RosterItem createRosterItem(JID user, String nickname, List<String> groups)
throws UnauthorizedException, UserAlreadyExistsException;
/**
* Create a new item to the roster based as a copy of the given item.
......@@ -97,7 +97,8 @@ public interface Roster {
* @throws UnauthorizedException if not the item or an administrator.
* @throws UserAlreadyExistsException If a roster item already exists for the given user
*/
public RosterItem createRosterItem(RosterItem item) throws UnauthorizedException, UserAlreadyExistsException;
public void createRosterItem(org.xmpp.packet.Roster.Item item)
throws UnauthorizedException, UserAlreadyExistsException;
/**
* Update an item that is already in the roster.
......
......@@ -184,12 +184,12 @@ public interface RosterItem {
*
* @return The subscription status of the item
*/
public List getGroups();
public List<String> getGroups();
/**
* <p>Set the current groups for the item.</p>
*
* @param groups The subscription status of the item
*/
public void setGroups(List groups) throws UnauthorizedException;
public void setGroups(List<String> groups) throws UnauthorizedException;
}
\ No newline at end of file
......@@ -24,8 +24,6 @@ import org.jivesoftware.messenger.user.BasicRoster;
import org.jivesoftware.messenger.user.BasicRosterItem;
import org.jivesoftware.messenger.user.CachedRoster;
import org.jivesoftware.messenger.user.CachedRosterItem;
import org.jivesoftware.messenger.user.IQRoster;
import org.jivesoftware.messenger.user.IQRosterItem;
import org.jivesoftware.messenger.user.RosterItem;
import org.jivesoftware.messenger.user.RosterItemProvider;
import org.jivesoftware.messenger.user.UserAlreadyExistsException;
......@@ -37,6 +35,7 @@ import org.jivesoftware.util.Log;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
import org.xmpp.packet.Roster;
/**
* <p>A roster implemented against a JDBC database.</p>
......@@ -79,18 +78,16 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
return username;
}
public IQRoster getReset() throws UnauthorizedException {
IQRoster roster = new IQRoster();
public Roster getReset() throws UnauthorizedException {
Roster roster = new Roster();
Iterator items = getRosterItems();
while (items.hasNext()) {
RosterItem item = (RosterItem)items.next();
if (item.getSubStatus() != RosterItem.SUB_NONE || item.getAskStatus() != RosterItem.ASK_NONE) {
try {
roster.createRosterItem(item);
}
catch (UserAlreadyExistsException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
roster.addItem(item.getJid(), item.getNickname(),
Roster.Ask.valueOf(item.getAskStatus().getName()),
Roster.Subscription.valueOf(item.getSubStatus().getName()),
item.getGroups());
}
}
return roster;
......@@ -130,20 +127,17 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
}
}
protected RosterItem provideRosterItem(JID user, String nickname, List group) throws UserAlreadyExistsException, UnauthorizedException {
return provideRosterItem(new BasicRosterItem(user, nickname, group));
}
protected RosterItem provideRosterItem(JID user, String nickname, List<String> group) throws UserAlreadyExistsException, UnauthorizedException {
Roster roster = new Roster();
roster.setType(IQ.Type.set);
Roster.Item item = roster.addItem(user, nickname, null, Roster.Subscription.none, group);
protected RosterItem provideRosterItem(RosterItem item) throws UserAlreadyExistsException, UnauthorizedException {
item = rosterItemProvider.createItem(username, new BasicRosterItem(item));
RosterItem rosterItem = rosterItemProvider.createItem(username, new BasicRosterItem(item));
// Broadcast the roster push to the user
IQRoster roster = new IQRoster();
roster.setType(IQ.Type.set);
roster.createRosterItem(item);
broadcast(roster);
return item;
return rosterItem;
}
private PresenceManager presenceManager;
......@@ -155,8 +149,8 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
cachedItem = (CachedRosterItem)item;
}
else {
// This is a different item object, probably an IQRosterItem update for an existing item
// So grab the cached version out of the super to learn the rosterID for the item
// This is a different item object, probably an BasicRosterItem update for an existing
// item. So grab the cached version out of the super to learn the rosterID for the item
// And create a new cached roster item with the new info
cachedItem = (CachedRosterItem)super.getRosterItem(item.getJid());
cachedItem = new CachedRosterItemImpl(cachedItem.getID(), item);
......@@ -170,15 +164,13 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
if (!(cachedItem.getSubStatus() == RosterItem.SUB_NONE
&& cachedItem.getAskStatus() == RosterItem.ASK_NONE)) {
try {
IQRoster roster = new IQRoster();
roster.setType(IQ.Type.set);
roster.createRosterItem(cachedItem);
broadcast(roster);
}
catch (UserAlreadyExistsException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
Roster roster = new Roster();
roster.setType(IQ.Type.set);
roster.addItem(cachedItem.getJid(), cachedItem.getNickname(),
Roster.Ask.valueOf(cachedItem.getAskStatus().getName()),
Roster.Subscription.valueOf(cachedItem.getSubStatus().getName()),
cachedItem.getGroups());
broadcast(roster);
}
if (cachedItem.getSubStatus() == RosterItem.SUB_BOTH
......@@ -198,24 +190,18 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
// If removing the user was successful, remove the user from the backend store
rosterItemProvider.deleteItem(username, item.getID());
try {
// broadcast the update to the user
IQRoster roster = new IQRoster();
roster.setType(IQ.Type.set);
IQRosterItem iqItem = (IQRosterItem)roster.createRosterItem(user);
iqItem.setSubStatus(RosterItem.SUB_REMOVE);
broadcast(roster);
}
catch (UserAlreadyExistsException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
// broadcast the update to the user
Roster roster = new Roster();
roster.setType(IQ.Type.set);
roster.addItem(user, Roster.Subscription.remove);
broadcast(roster);
}
return item;
}
private void broadcast(IQRoster roster) throws UnauthorizedException {
private void broadcast(Roster roster) throws UnauthorizedException {
if (server == null) {
server = BasicServer.getInstance();
}
......
......@@ -12,6 +12,8 @@
package org.jivesoftware.messenger.user.spi;
import java.util.List;
import java.util.LinkedList;
import org.jivesoftware.messenger.user.BasicRosterItem;
import org.jivesoftware.messenger.user.CachedRosterItem;
import org.jivesoftware.messenger.user.RosterItem;
......@@ -31,7 +33,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste
AskType askStatus,
RecvType recvStatus,
String nickname,
List groups) {
List<String> groups) {
super(jid, subStatus, askStatus, recvStatus, nickname, groups);
this.rosterID = id;
}
......@@ -46,7 +48,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste
null);
}
public CachedRosterItemImpl(long id, JID jid, String nickname, List groups) {
public CachedRosterItemImpl(long id, JID jid, String nickname, List<String> groups) {
this(id,
jid,
RosterItem.SUB_NONE,
......@@ -82,9 +84,9 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste
return rosterID;
}
public void setAsCopyOf(RosterItem item) {
setNickname(item.getNickname());
setGroups(item.getGroups());
public void setAsCopyOf(org.xmpp.packet.Roster.Item item) {
setNickname(item.getName());
setGroups(new LinkedList<String>(item.getGroups()));
}
public int getCachedSize() {
......
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.user.spi;
import java.util.Iterator;
import java.util.List;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jivesoftware.messenger.user.BasicRosterItem;
import org.jivesoftware.messenger.user.IQRosterItem;
import org.jivesoftware.messenger.user.RosterItem;
import org.jivesoftware.util.ConcurrentHashSet;
import org.xmpp.packet.JID;
public class IQRosterItemImpl extends BasicRosterItem implements IQRosterItem {
public IQRosterItemImpl(JID jid) {
super(jid);
}
public IQRosterItemImpl(JID jid, String nickname, List groups) {
super(jid, nickname, groups);
}
/**
* <p>Create a copy of the given roster item.</p>
*
* @param item
*/
public IQRosterItemImpl(RosterItem item) {
super(item);
if (item instanceof IQRosterItemImpl) {
fragments = (ConcurrentHashSet)((IQRosterItemImpl)item).fragments.clone();
}
}
public Element asXMLElement() {
Element item = DocumentHelper.createElement("item");
item.addAttribute("jid", jid.toBareJID());
item.addAttribute("subscription", subStatus.getName());
if (askStatus != ASK_NONE) {
item.addAttribute("ask", askStatus.getName());
}
if (nickname != null) {
if (nickname.trim().length() > 0) {
item.addAttribute("name", nickname.trim());
}
}
if (groups != null) {
Iterator groupsItr = groups.iterator();
while (groupsItr.hasNext()) {
item.addElement("group").addText((String)groupsItr.next());
}
}
return item;
}
public String getNamespace() {
return "jabber:iq:roster";
}
public void setNamespace(String namespace) {
// do nothing
}
public String getName() {
return "item";
}
public void setName(String name) {
// do nothing
}
private ConcurrentHashSet fragments = new ConcurrentHashSet();
public void addFragment(Element fragment) {
fragments.add(fragment);
}
public Iterator getFragments() {
return fragments.iterator();
}
public Element getFragment(String name, String namespace) {
if (fragments == null) {
return null;
}
Element frag;
for (Iterator frags = fragments.iterator(); frags.hasNext();) {
frag = (Element)frags.next();
if (name.equals(frag.getName()) && namespace.equals(frag.getNamespace())) {
return frag;
}
}
return null;
}
public void clearFragments() {
fragments.clear();
}
public int getSize() {
return fragments.size();
}
}
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