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 { ...@@ -138,7 +138,6 @@ public class PrivateStorage extends BasicModule {
* @return the data stored under the given key or the data element. * @return the data stored under the given key or the data element.
*/ */
public Element get(String username, Element data) { public Element get(String username, Element data) {
data.clearContent();
if (enabled) { if (enabled) {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
...@@ -149,6 +148,7 @@ public class PrivateStorage extends BasicModule { ...@@ -149,6 +148,7 @@ public class PrivateStorage extends BasicModule {
pstmt.setString(2, data.getNamespaceURI()); pstmt.setString(2, data.getNamespaceURI());
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
if (rs.next()) { if (rs.next()) {
data.clearContent();
String result = rs.getString(1).trim(); String result = rs.getString(1).trim();
Document doc = xmlReader.read(result); Document doc = xmlReader.read(result);
data = doc.getRootElement(); data = doc.getRootElement();
......
...@@ -89,6 +89,7 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo { ...@@ -89,6 +89,7 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo {
String username = query.elementTextTrim("username"); String username = query.elementTextTrim("username");
probeResponse.element("username").setText(username); probeResponse.element("username").setText(username);
response = IQ.createResultIQ(packet); response = IQ.createResultIQ(packet);
probeResponse.setParent(null);
response.setChildElement(probeResponse); response.setChildElement(probeResponse);
} }
// Otherwise set query // Otherwise set query
......
...@@ -62,8 +62,9 @@ public class IQPrivateHandler extends IQHandler implements ServerFeaturesProvide ...@@ -62,8 +62,9 @@ public class IQPrivateHandler extends IQHandler implements ServerFeaturesProvide
if (dataElement != null) { if (dataElement != null) {
if (IQ.Type.get.equals(packet.getType())) { if (IQ.Type.get.equals(packet.getType())) {
replyPacket = IQ.createResultIQ(packet); replyPacket = IQ.createResultIQ(packet);
Element privateData = replyPacket.setChildElement("query", "jabber:iq:private"); Element dataStored = privateStorage.get(packet.getFrom().getNode(), dataElement);
privateData.add(privateStorage.get(packet.getFrom().getNode(), dataElement)); dataStored.setParent(null);
replyPacket.setChildElement(dataStored);
} }
else { else {
privateStorage.add(packet.getFrom().getNode(), dataElement); privateStorage.add(packet.getFrom().getNode(), dataElement);
......
...@@ -18,9 +18,7 @@ import org.jivesoftware.messenger.*; ...@@ -18,9 +18,7 @@ import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.*; import org.jivesoftware.messenger.user.*;
import org.jivesoftware.messenger.user.Roster; import org.jivesoftware.messenger.user.Roster;
import org.jivesoftware.messenger.user.spi.IQRosterItemImpl;
import org.xmpp.packet.*; import org.xmpp.packet.*;
import org.dom4j.Element;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
...@@ -87,7 +85,7 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -87,7 +85,7 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
UnauthorizedException, PacketException { UnauthorizedException, PacketException {
try { try {
IQ returnPacket = null; IQ returnPacket = null;
IQRoster roster = (IQRoster)packet; org.xmpp.packet.Roster roster = (org.xmpp.packet.Roster)packet;
JID recipientJID = packet.getTo(); JID recipientJID = packet.getTo();
...@@ -114,14 +112,12 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -114,14 +112,12 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
* *
* @param packet The packet suspected of containing a roster removal * @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 recipientJID = packet.getTo();
JID senderJID = packet.getFrom(); JID senderJID = packet.getFrom();
try { try {
Iterator itemIter = packet.getRosterItems(); for (org.xmpp.packet.Roster.Item packetItem : packet.getItems()) {
while (itemIter.hasNext()) { if (packetItem.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
RosterItem packetItem = (RosterItem)itemIter.next();
if (packetItem.getSubStatus() == RosterItem.SUB_REMOVE) {
Roster roster = userManager.getUser(recipientJID.getNode()).getRoster(); Roster roster = userManager.getUser(recipientJID.getNode()).getRoster();
RosterItem item = roster.getRosterItem(senderJID); RosterItem item = roster.getRosterItem(senderJID);
roster.deleteRosterItem(senderJID); roster.deleteRosterItem(senderJID);
...@@ -147,12 +143,13 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -147,12 +143,13 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
* @param packet The packet that triggered this update * @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 * @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; IQ returnPacket = null;
Session session = null; Session session = null;
try { try {
sessionManager.getSession(packet.getFrom()); session = sessionManager.getSession(packet.getFrom());
} }
catch (Exception e) { catch (Exception e) {
IQ error = IQ.createResultIQ(packet); IQ error = IQ.createResultIQ(packet);
...@@ -177,16 +174,14 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -177,16 +174,14 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
} }
else if (IQ.Type.set == type) { else if (IQ.Type.set == type) {
Iterator itemIter = packet.getRosterItems(); for (org.xmpp.packet.Roster.Item item : packet.getItems()) {
while (itemIter.hasNext()) { if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
RosterItem item = (RosterItem)itemIter.next();
if (item.getSubStatus() == RosterItem.SUB_REMOVE) {
removeItem(cachedRoster, packet.getFrom(), item); removeItem(cachedRoster, packet.getFrom(), item);
} }
else { else {
if (cachedRoster.isRosterItem(item.getJid())) { if (cachedRoster.isRosterItem(item.getJID())) {
// existing item // existing item
CachedRosterItem cachedItem = (CachedRosterItem)cachedRoster.getRosterItem(item.getJid()); CachedRosterItem cachedItem = (CachedRosterItem)cachedRoster.getRosterItem(item.getJID());
cachedItem.setAsCopyOf(item); cachedItem.setAsCopyOf(item);
cachedRoster.updateRosterItem(cachedItem); cachedRoster.updateRosterItem(cachedItem);
} }
...@@ -215,13 +210,13 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -215,13 +210,13 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
* @param sender The JID of the sender of the removal request * @param sender The JID of the sender of the removal request
* @param item The removal item element * @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 throws UnauthorizedException
{ {
JID recipient = item.getJid(); JID recipient = item.getJID();
// Remove recipient from the sender's roster // Remove recipient from the sender's roster
roster.deleteRosterItem(item.getJid()); roster.deleteRosterItem(item.getJID());
// Forward set packet to the subscriber // Forward set packet to the subscriber
if (localServer.isLocal(recipient)) { // Recipient is local so let's handle it here if (localServer.isLocal(recipient)) { // Recipient is local so let's handle it here
try { try {
...@@ -245,18 +240,11 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -245,18 +240,11 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
* @param to The recipient address to use * @param to The recipient address to use
* @return The forwarded packet generated * @return The forwarded packet generated
*/ */
private Packet createRemoveForward(JID from, JID to) throws UnauthorizedException { private Packet createRemoveForward(JID from, JID to) {
org.xmpp.packet.Roster response = new org.xmpp.packet.Roster(IQ.Type.set);
IQ response = new IQ();
response.setFrom(from); response.setFrom(from);
response.setTo(to); response.setTo(to);
response.setType(IQ.Type.set); response.addItem(from, org.xmpp.packet.Roster.Subscription.remove);
Element query = response.setChildElement("query", "jabber:iq:roster");
IQRosterItem responseItem = new IQRosterItemImpl(from);
responseItem.setSubStatus(RosterItem.SUB_REMOVE);
query.add(responseItem.asXMLElement());
return response; return response;
} }
......
...@@ -32,6 +32,7 @@ import org.xmlpull.v1.XmlPullParserFactory; ...@@ -32,6 +32,7 @@ import org.xmlpull.v1.XmlPullParserFactory;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import org.xmpp.packet.Message; import org.xmpp.packet.Message;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import org.xmpp.packet.Roster;
/** /**
* @author Derek DeMoro * @author Derek DeMoro
...@@ -195,7 +196,7 @@ public class SocketReadThread extends Thread { ...@@ -195,7 +196,7 @@ public class SocketReadThread extends Thread {
clearSignout = (Presence.Type.unavailable == packet.getType() ? true : false); clearSignout = (Presence.Type.unavailable == packet.getType() ? true : false);
} }
else if ("iq".equals(tag)) { else if ("iq".equals(tag)) {
IQ packet = new IQ(doc); IQ packet = getIQ(doc);
packet.setFrom(session.getAddress()); packet.setFrom(session.getAddress());
auditor.audit(packet); auditor.audit(packet);
router.route(packet); router.route(packet);
...@@ -208,6 +209,16 @@ public class SocketReadThread extends Thread { ...@@ -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 * Uses the XPP to grab the opening stream tag and create
* an active session object. In all cases, the method obtains the * an active session object. In all cases, the method obtains the
......
...@@ -99,7 +99,8 @@ public class BasicRoster implements Roster, Cacheable { ...@@ -99,7 +99,8 @@ public class BasicRoster implements Roster, Cacheable {
return createRosterItem(user, null, null); 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); RosterItem item = provideRosterItem(user, nickname, groups);
itemLock.writeLock().lock(); itemLock.writeLock().lock();
try { try {
...@@ -111,12 +112,12 @@ public class BasicRoster implements Roster, Cacheable { ...@@ -111,12 +112,12 @@ public class BasicRoster implements Roster, Cacheable {
} }
} }
public RosterItem createRosterItem(RosterItem item) throws UnauthorizedException, UserAlreadyExistsException { public void createRosterItem(org.xmpp.packet.Roster.Item item)
item = provideRosterItem(item); throws UnauthorizedException, UserAlreadyExistsException {
RosterItem rosterItem = provideRosterItem(item);
itemLock.writeLock().lock(); itemLock.writeLock().lock();
try { try {
rosterItems.put(item.getJid().toBareJID(), item); rosterItems.put(item.getJID().toBareJID(), rosterItem);
return item;
} }
finally { finally {
itemLock.writeLock().unlock(); itemLock.writeLock().unlock();
...@@ -134,7 +135,7 @@ public class BasicRoster implements Roster, Cacheable { ...@@ -134,7 +135,7 @@ public class BasicRoster implements Roster, Cacheable {
* @param groups The groups the item belongs to (or null for none) * @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 * @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 { throws UserAlreadyExistsException, UnauthorizedException {
return new BasicRosterItem(user, nickname, groups); return new BasicRosterItem(user, nickname, groups);
} }
...@@ -148,7 +149,7 @@ public class BasicRoster implements Roster, Cacheable { ...@@ -148,7 +149,7 @@ public class BasicRoster implements Roster, Cacheable {
* @param item The item to copy settings for the new item in this roster * @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 * @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 { throws UserAlreadyExistsException, UnauthorizedException {
return new BasicRosterItem(item); return new BasicRosterItem(item);
} }
......
...@@ -27,7 +27,7 @@ public class BasicRosterItem implements RosterItem { ...@@ -27,7 +27,7 @@ public class BasicRosterItem implements RosterItem {
protected RecvType recvStatus; protected RecvType recvStatus;
protected JID jid; protected JID jid;
protected String nickname; protected String nickname;
protected List groups; protected List<String> groups;
protected SubType subStatus; protected SubType subStatus;
protected AskType askStatus; protected AskType askStatus;
...@@ -37,15 +37,15 @@ public class BasicRosterItem implements RosterItem { ...@@ -37,15 +37,15 @@ public class BasicRosterItem implements RosterItem {
AskType askStatus, AskType askStatus,
RecvType recvStatus, RecvType recvStatus,
String nickname, String nickname,
List groups) { List<String> groups) {
this.jid = jid; this.jid = jid;
this.subStatus = subStatus; this.subStatus = subStatus;
this.askStatus = askStatus; this.askStatus = askStatus;
this.recvStatus = recvStatus; this.recvStatus = recvStatus;
this.nickname = nickname; this.nickname = nickname;
this.groups = new LinkedList(); this.groups = new LinkedList<String>();
if (groups != null) { if (groups != null) {
Iterator groupItr = groups.iterator(); Iterator<String> groupItr = groups.iterator();
while (groupItr.hasNext()) { while (groupItr.hasNext()) {
this.groups.add(groupItr.next()); this.groups.add(groupItr.next());
} }
...@@ -61,7 +61,7 @@ public class BasicRosterItem implements RosterItem { ...@@ -61,7 +61,7 @@ public class BasicRosterItem implements RosterItem {
null); null);
} }
public BasicRosterItem(JID jid, String nickname, List groups) { public BasicRosterItem(JID jid, String nickname, List<String> groups) {
this(jid, this(jid,
RosterItem.SUB_NONE, RosterItem.SUB_NONE,
RosterItem.ASK_NONE, RosterItem.ASK_NONE,
...@@ -75,16 +75,44 @@ public class BasicRosterItem implements RosterItem { ...@@ -75,16 +75,44 @@ public class BasicRosterItem implements RosterItem {
* *
* @param item * @param item
*/ */
public BasicRosterItem(RosterItem item) { public BasicRosterItem(org.xmpp.packet.Roster.Item item) {
this(item.getJid(), this(item.getJID(),
item.getSubStatus(), getSubType(item),
item.getAskStatus(), getAskStatus(item),
item.getRecvStatus(), RosterItem.RECV_NONE,
item.getNickname(), item.getName(),
item.getGroups()); 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() { public SubType getSubStatus() {
return subStatus; return subStatus;
...@@ -122,13 +150,13 @@ public class BasicRosterItem implements RosterItem { ...@@ -122,13 +150,13 @@ public class BasicRosterItem implements RosterItem {
this.nickname = nickname; this.nickname = nickname;
} }
public List getGroups() { public List<String> getGroups() {
return groups; return groups;
} }
public void setGroups(List groups) { public void setGroups(List<String> groups) {
if (groups == null) { if (groups == null) {
this.groups = new LinkedList(); this.groups = new LinkedList<String>();
} }
else { else {
this.groups = groups; this.groups = groups;
......
...@@ -13,7 +13,7 @@ package org.jivesoftware.messenger.user; ...@@ -13,7 +13,7 @@ package org.jivesoftware.messenger.user;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.util.Cacheable; 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> * <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 { ...@@ -35,11 +35,11 @@ public interface CachedRoster extends Roster, Cacheable {
String getUsername(); 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> * <p>Broadcast the presence update to all subscribers of the roter.</p>
......
...@@ -39,5 +39,5 @@ public interface CachedRosterItem extends RosterItem, Cacheable { ...@@ -39,5 +39,5 @@ public interface CachedRosterItem extends RosterItem, Cacheable {
* *
* @param item The item who's settings will be copied into the cached copy * @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; ...@@ -22,14 +22,13 @@ import java.util.List;
* <p>Rosters are similar to buddy groups in popular IM clients. The Roster interface is * <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 * a generic representation of the roster data. There are two primary implementations
* of the Roster in Messenger, the CachedRoster representing a cached, persistently stored * 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). * 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 * It is an important distinction as changes to CachedRosters will be saved to disk while
* IQRosters are transient data objects.</p> * IQRosters are transient data objects.</p>
* *
* @author Iain Shigeoka * @author Iain Shigeoka
* *
* @see IQRoster
* @see CachedRoster * @see CachedRoster
*/ */
public interface Roster { public interface Roster {
...@@ -86,7 +85,8 @@ public interface Roster { ...@@ -86,7 +85,8 @@ public interface Roster {
* @throws UnauthorizedException if not the item or an administrator. * @throws UnauthorizedException if not the item or an administrator.
* @throws UserAlreadyExistsException If a roster item already exists for the given user * @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. * Create a new item to the roster based as a copy of the given item.
...@@ -97,7 +97,8 @@ public interface Roster { ...@@ -97,7 +97,8 @@ public interface Roster {
* @throws UnauthorizedException if not the item or an administrator. * @throws UnauthorizedException if not the item or an administrator.
* @throws UserAlreadyExistsException If a roster item already exists for the given user * @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. * Update an item that is already in the roster.
......
...@@ -184,12 +184,12 @@ public interface RosterItem { ...@@ -184,12 +184,12 @@ public interface RosterItem {
* *
* @return The subscription status of the item * @return The subscription status of the item
*/ */
public List getGroups(); public List<String> getGroups();
/** /**
* <p>Set the current groups for the item.</p> * <p>Set the current groups for the item.</p>
* *
* @param groups The subscription status of the item * @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; ...@@ -24,8 +24,6 @@ import org.jivesoftware.messenger.user.BasicRoster;
import org.jivesoftware.messenger.user.BasicRosterItem; import org.jivesoftware.messenger.user.BasicRosterItem;
import org.jivesoftware.messenger.user.CachedRoster; import org.jivesoftware.messenger.user.CachedRoster;
import org.jivesoftware.messenger.user.CachedRosterItem; 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.RosterItem;
import org.jivesoftware.messenger.user.RosterItemProvider; import org.jivesoftware.messenger.user.RosterItemProvider;
import org.jivesoftware.messenger.user.UserAlreadyExistsException; import org.jivesoftware.messenger.user.UserAlreadyExistsException;
...@@ -37,6 +35,7 @@ import org.jivesoftware.util.Log; ...@@ -37,6 +35,7 @@ import org.jivesoftware.util.Log;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import org.xmpp.packet.Roster;
/** /**
* <p>A roster implemented against a JDBC database.</p> * <p>A roster implemented against a JDBC database.</p>
...@@ -79,18 +78,16 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -79,18 +78,16 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
return username; return username;
} }
public IQRoster getReset() throws UnauthorizedException { public Roster getReset() throws UnauthorizedException {
IQRoster roster = new IQRoster(); Roster roster = new Roster();
Iterator items = getRosterItems(); Iterator items = getRosterItems();
while (items.hasNext()) { while (items.hasNext()) {
RosterItem item = (RosterItem)items.next(); RosterItem item = (RosterItem)items.next();
if (item.getSubStatus() != RosterItem.SUB_NONE || item.getAskStatus() != RosterItem.ASK_NONE) { if (item.getSubStatus() != RosterItem.SUB_NONE || item.getAskStatus() != RosterItem.ASK_NONE) {
try { roster.addItem(item.getJid(), item.getNickname(),
roster.createRosterItem(item); Roster.Ask.valueOf(item.getAskStatus().getName()),
} Roster.Subscription.valueOf(item.getSubStatus().getName()),
catch (UserAlreadyExistsException e) { item.getGroups());
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
} }
} }
return roster; return roster;
...@@ -130,20 +127,17 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -130,20 +127,17 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
} }
} }
protected RosterItem provideRosterItem(JID user, String nickname, List group) throws UserAlreadyExistsException, UnauthorizedException { protected RosterItem provideRosterItem(JID user, String nickname, List<String> group) throws UserAlreadyExistsException, UnauthorizedException {
return provideRosterItem(new BasicRosterItem(user, nickname, group)); 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 { RosterItem rosterItem = rosterItemProvider.createItem(username, new BasicRosterItem(item));
item = rosterItemProvider.createItem(username, new BasicRosterItem(item));
// Broadcast the roster push to the user // Broadcast the roster push to the user
IQRoster roster = new IQRoster();
roster.setType(IQ.Type.set);
roster.createRosterItem(item);
broadcast(roster); broadcast(roster);
return item; return rosterItem;
} }
private PresenceManager presenceManager; private PresenceManager presenceManager;
...@@ -155,8 +149,8 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -155,8 +149,8 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
cachedItem = (CachedRosterItem)item; cachedItem = (CachedRosterItem)item;
} }
else { else {
// This is a different item object, probably an IQRosterItem update for an existing item // This is a different item object, probably an BasicRosterItem update for an existing
// So grab the cached version out of the super to learn the rosterID for the item // 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 // And create a new cached roster item with the new info
cachedItem = (CachedRosterItem)super.getRosterItem(item.getJid()); cachedItem = (CachedRosterItem)super.getRosterItem(item.getJid());
cachedItem = new CachedRosterItemImpl(cachedItem.getID(), item); cachedItem = new CachedRosterItemImpl(cachedItem.getID(), item);
...@@ -170,15 +164,13 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -170,15 +164,13 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
if (!(cachedItem.getSubStatus() == RosterItem.SUB_NONE if (!(cachedItem.getSubStatus() == RosterItem.SUB_NONE
&& cachedItem.getAskStatus() == RosterItem.ASK_NONE)) { && cachedItem.getAskStatus() == RosterItem.ASK_NONE)) {
try { Roster roster = new Roster();
IQRoster roster = new IQRoster(); roster.setType(IQ.Type.set);
roster.setType(IQ.Type.set); roster.addItem(cachedItem.getJid(), cachedItem.getNickname(),
roster.createRosterItem(cachedItem); Roster.Ask.valueOf(cachedItem.getAskStatus().getName()),
broadcast(roster); Roster.Subscription.valueOf(cachedItem.getSubStatus().getName()),
} cachedItem.getGroups());
catch (UserAlreadyExistsException e) { broadcast(roster);
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
} }
if (cachedItem.getSubStatus() == RosterItem.SUB_BOTH if (cachedItem.getSubStatus() == RosterItem.SUB_BOTH
...@@ -198,24 +190,18 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -198,24 +190,18 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
// If removing the user was successful, remove the user from the backend store // If removing the user was successful, remove the user from the backend store
rosterItemProvider.deleteItem(username, item.getID()); rosterItemProvider.deleteItem(username, item.getID());
try { // broadcast the update to the user
// broadcast the update to the user Roster roster = new Roster();
IQRoster roster = new IQRoster(); roster.setType(IQ.Type.set);
roster.setType(IQ.Type.set); roster.addItem(user, Roster.Subscription.remove);
IQRosterItem iqItem = (IQRosterItem)roster.createRosterItem(user); broadcast(roster);
iqItem.setSubStatus(RosterItem.SUB_REMOVE);
broadcast(roster);
}
catch (UserAlreadyExistsException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
} }
return item; return item;
} }
private void broadcast(IQRoster roster) throws UnauthorizedException { private void broadcast(Roster roster) throws UnauthorizedException {
if (server == null) { if (server == null) {
server = BasicServer.getInstance(); server = BasicServer.getInstance();
} }
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
package org.jivesoftware.messenger.user.spi; package org.jivesoftware.messenger.user.spi;
import java.util.List; import java.util.List;
import java.util.LinkedList;
import org.jivesoftware.messenger.user.BasicRosterItem; import org.jivesoftware.messenger.user.BasicRosterItem;
import org.jivesoftware.messenger.user.CachedRosterItem; import org.jivesoftware.messenger.user.CachedRosterItem;
import org.jivesoftware.messenger.user.RosterItem; import org.jivesoftware.messenger.user.RosterItem;
...@@ -31,7 +33,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste ...@@ -31,7 +33,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste
AskType askStatus, AskType askStatus,
RecvType recvStatus, RecvType recvStatus,
String nickname, String nickname,
List groups) { List<String> groups) {
super(jid, subStatus, askStatus, recvStatus, nickname, groups); super(jid, subStatus, askStatus, recvStatus, nickname, groups);
this.rosterID = id; this.rosterID = id;
} }
...@@ -46,7 +48,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste ...@@ -46,7 +48,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste
null); null);
} }
public CachedRosterItemImpl(long id, JID jid, String nickname, List groups) { public CachedRosterItemImpl(long id, JID jid, String nickname, List<String> groups) {
this(id, this(id,
jid, jid,
RosterItem.SUB_NONE, RosterItem.SUB_NONE,
...@@ -82,9 +84,9 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste ...@@ -82,9 +84,9 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste
return rosterID; return rosterID;
} }
public void setAsCopyOf(RosterItem item) { public void setAsCopyOf(org.xmpp.packet.Roster.Item item) {
setNickname(item.getNickname()); setNickname(item.getName());
setGroups(item.getGroups()); setGroups(new LinkedList<String>(item.getGroups()));
} }
public int getCachedSize() { 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