1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 14:02:06 +01:00

1) Created bookmark manager to facilitate the management of bookmarks. SMACK-144

2) Added RSS attribute to URLs. SMACK-136

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@4007 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2006-06-16 22:58:24 +00:00 committed by alex
parent ae97f5fd6a
commit 8fe7e46330
5 changed files with 363 additions and 41 deletions

View file

@ -0,0 +1,214 @@
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.smackx.bookmark;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.PrivateDataManager;
import java.util.*;
/**
* Provides methods to manage bookmarks in accordance with JEP-0048. Methods for managing URLs and
* Conferences are provided.
* </p>
* It should be noted that some extensions have been made to the JEP. There is an attribute on URLs
* that marks a url as a news feed and also a sub-element can be added to either a URL or conference
* indicated that it is shared amongst all users on a server.
*
* @author Alexander Wenckus
*/
public class BookmarkManager {
private static final Map bookmarkManagerMap = new HashMap();
static {
PrivateDataManager.addPrivateDataProvider("storage", "storage:bookmarks",
new Bookmarks.Provider());
}
/**
* Returns the <i>BookmarkManager</i> for a connection, if it doesn't exist it is created.
*
* @param connection the connection for which the manager is desired.
* @return Returns the <i>BookmarkManager</i> for a connection, if it doesn't
* exist it is created.
* @throws XMPPException Thrown if the connection is null or has not yet been authenticated.
*/
public synchronized static BookmarkManager getBookmarkManager(XMPPConnection connection)
throws XMPPException
{
BookmarkManager manager = (BookmarkManager) bookmarkManagerMap.get(connection);
if(manager == null) {
manager = new BookmarkManager(connection);
bookmarkManagerMap.put(connection, manager);
}
return manager;
}
private PrivateDataManager privateDataManager;
private Bookmarks bookmarks;
private final Object bookmarkLock = new Object();
/**
* Default constructor. Registers the data provider with the private data manager in the
* storage:bookmarks namespace.
*
* @param connection the connection for persisting and retrieving bookmarks.
* @throws XMPPException thrown when the connection is null or has not been authenticated.
*/
private BookmarkManager(XMPPConnection connection) throws XMPPException {
if(connection == null || !connection.isAuthenticated()) {
throw new XMPPException("Invalid connection.");
}
this.privateDataManager = new PrivateDataManager(connection);
}
/**
* Returns all currently bookmarked conferences.
*
* @return returns all currently bookmarked conferences
* @throws XMPPException thrown when there was an error retrieving the current bookmarks from
* the server.
* @see BookmarkedConference
*/
public Collection getBookmarkedConferences() throws XMPPException {
retrieveBookmarks();
return Collections.unmodifiableCollection(bookmarks.getBookmarkedConferences());
}
/**
* Adds or updates a conference in the bookmarks.
*
* @param name the name of the conference
* @param jid the jid of the conference
* @param isAutoJoin whether or not to join this conference automatically on login
* @param nickname the nickname to use for the user when joining the conference
* @param password the password to use for the user when joining the conference
* @throws XMPPException thrown when there is an issue retrieving the current bookmarks from
* the server.
*/
public void addBookmarkedConference(String name, String jid, boolean isAutoJoin,
String nickname, String password) throws XMPPException
{
retrieveBookmarks();
BookmarkedConference bookmark
= new BookmarkedConference(name, jid, isAutoJoin, nickname, password);
List conferences = bookmarks.getBookmarkedConferences();
if(conferences.contains(bookmark)) {
BookmarkedConference oldConference = (BookmarkedConference)
conferences.get(conferences.indexOf(bookmark));
if(oldConference.isShared()) {
throw new IllegalArgumentException("Cannot modify shared bookmark");
}
oldConference.setAutoJoin(isAutoJoin);
oldConference.setName(name);
oldConference.setNickname(nickname);
oldConference.setPassword(password);
}
else {
bookmarks.addBookmarkedConference(bookmark);
}
privateDataManager.setPrivateData(bookmarks);
}
/**
* Removes a conference from the bookmarks.
*
* @param jid the jid of the conference to be removed.
* @throws XMPPException thrown when there is a problem with the connection attempting to
* retrieve the bookmarks or persist the bookmarks.
* @throws IllegalArgumentException thrown when the conference being removed is a shared
* conference
*/
public void removeBookmarkedConference(String jid) throws XMPPException {
retrieveBookmarks();
Iterator it = bookmarks.getBookmarkedConferences().iterator();
while(it.hasNext()) {
BookmarkedConference conference = (BookmarkedConference) it.next();
if(conference.getJid().equalsIgnoreCase(jid)) {
if(conference.isShared()) {
throw new IllegalArgumentException("Conference is shared and can't be removed");
}
it.remove();
privateDataManager.setPrivateData(bookmarks);
return;
}
}
}
/**
* Returns an unmodifiable collection of all bookmarked urls.
*
* @return returns an unmodifiable collection of all bookmarked urls.
* @throws XMPPException thrown when there is a problem retriving bookmarks from the server.
*/
public Collection getBookmarkedURLs() throws XMPPException {
retrieveBookmarks();
return Collections.unmodifiableCollection(bookmarks.getBookmarkedURLS());
}
/**
* Adds a new url or updates an already existing url in the bookmarks.
*
* @param URL the url of the bookmark
* @param name the name of the bookmark
* @param isRSS whether or not the url is an rss feed
* @throws XMPPException thrown when there is an error retriving or saving bookmarks from or to
* the server
*/
public void addBookmarkedURL(String URL, String name, boolean isRSS) throws XMPPException {
retrieveBookmarks();
BookmarkedURL bookmark = new BookmarkedURL(URL, name, isRSS);
List urls = bookmarks.getBookmarkedURLS();
if(urls.contains(bookmark)) {
BookmarkedURL oldURL = (BookmarkedURL) urls.get(urls.indexOf(bookmark));
if(oldURL.isShared()) {
throw new IllegalArgumentException("Cannot modify shared bookmarks");
}
oldURL.setName(name);
oldURL.setRss(isRSS);
}
else {
bookmarks.addBookmarkedURL(bookmark);
}
privateDataManager.setPrivateData(bookmarks);
}
/**
* Removes a url from the bookmarks.
*
* @param bookmarkURL the url of the bookmark to remove
* @throws XMPPException thrown if there is an error retriving or saving bookmarks from or to
* the server.
*/
public void removeBookmarkedURL(String bookmarkURL) throws XMPPException {
retrieveBookmarks();
Iterator it = bookmarks.getBookmarkedURLS().iterator();
while(it.hasNext()) {
BookmarkedURL bookmark = (BookmarkedURL) it.next();
if(bookmark.getURL().equalsIgnoreCase(bookmarkURL)) {
if(bookmark.isShared()) {
throw new IllegalArgumentException("Cannot delete a shared bookmark.");
}
it.remove();
privateDataManager.setPrivateData(bookmarks);
return;
}
}
}
private Bookmarks retrieveBookmarks() throws XMPPException {
synchronized(bookmarkLock) {
if(bookmarks == null) {
bookmarks = (Bookmarks) privateDataManager.getPrivateData("storage",
"storage:bookmarks");
}
return bookmarks;
}
}
}

View file

@ -12,14 +12,29 @@ package org.jivesoftware.smackx.bookmark;
*
* @author Derek DeMoro
*/
public class BookmarkedConference {
public class BookmarkedConference implements SharedBookmark {
private String name;
private boolean autoJoin;
private String jid;
private final String jid;
private String nickname;
private String password;
private boolean isShared;
protected BookmarkedConference(String jid) {
this.jid = jid;
}
protected BookmarkedConference(String name, String jid, boolean autoJoin, String nickname,
String password)
{
this.name = name;
this.jid = jid;
this.autoJoin = autoJoin;
this.nickname = nickname;
this.password = password;
}
/**
@ -31,7 +46,7 @@ public class BookmarkedConference {
return name;
}
public void setName(String name) {
protected void setName(String name) {
this.name = name;
}
@ -44,7 +59,7 @@ public class BookmarkedConference {
return autoJoin;
}
public void setAutoJoin(boolean autoJoin) {
protected void setAutoJoin(boolean autoJoin) {
this.autoJoin = autoJoin;
}
@ -57,10 +72,6 @@ public class BookmarkedConference {
return jid;
}
public void setJid(String jid) {
this.jid = jid;
}
/**
* Returns the nickname to use when joining this conference room. This is an optional
* value and may return null.
@ -71,7 +82,7 @@ public class BookmarkedConference {
return nickname;
}
public void setNickname(String nickname) {
protected void setNickname(String nickname) {
this.nickname = nickname;
}
@ -85,7 +96,23 @@ public class BookmarkedConference {
return password;
}
public void setPassword(String password) {
protected void setPassword(String password) {
this.password = password;
}
public boolean equals(Object obj) {
if(obj == null || !(obj instanceof BookmarkedConference)) {
return false;
}
BookmarkedConference conference = (BookmarkedConference)obj;
return conference.getJid().equalsIgnoreCase(jid);
}
protected void setShared(boolean isShared) {
this.isShared = isShared;
}
public boolean isShared() {
return isShared;
}
}

View file

@ -12,10 +12,22 @@ package org.jivesoftware.smackx.bookmark;
*
* @author Derek DeMoro
*/
public class BookmarkedURL {
public class BookmarkedURL implements SharedBookmark {
private String name;
private String URL;
private final String URL;
private boolean isRss;
private boolean isShared;
protected BookmarkedURL(String URL) {
this.URL = URL;
}
protected BookmarkedURL(String URL, String name, boolean isRss) {
this.URL = URL;
this.name = name;
this.isRss = isRss;
}
/**
* Returns the name representing the URL (eg. Jive Software). This can be used in as a label, or
@ -32,7 +44,7 @@ public class BookmarkedURL {
*
* @param name the name.
*/
public void setName(String name) {
protected void setName(String name) {
this.name = name;
}
@ -44,14 +56,37 @@ public class BookmarkedURL {
public String getURL() {
return URL;
}
/**
* Set to true if this URL is an RSS or news feed.
*
* @param isRss True if the URL is a news feed and false if it is not.
*/
protected void setRss(boolean isRss) {
this.isRss = isRss;
}
/**
* Sets the URL.
* Returns true if this URL is a news feed.
*
* @param URL the url.
* @return Returns true if this URL is a news feed.
*/
public void setURL(String URL) {
this.URL = URL;
public boolean isRss() {
return isRss;
}
public boolean equals(Object obj) {
if(!(obj instanceof BookmarkedURL)) {
return false;
}
BookmarkedURL url = (BookmarkedURL)obj;
return url.getURL().equalsIgnoreCase(URL);
}
protected void setShared(boolean shared) {
this.isShared = shared;
}
public boolean isShared() {
return isShared;
}
}

View file

@ -7,15 +7,15 @@
*/
package org.jivesoftware.smackx.bookmark;
import org.jivesoftware.smackx.PrivateDataManager;
import org.jivesoftware.smackx.packet.PrivateData;
import org.jivesoftware.smackx.provider.PrivateDataProvider;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;
/**
* Bookmarks is used for storing and retrieving URLS and Conference rooms.
@ -58,9 +58,6 @@ public class Bookmarks implements PrivateData {
* Required Empty Constructor to use Bookmarks.
*/
public Bookmarks() {
// Register own provider for simpler implementation.
PrivateDataManager.addPrivateDataProvider("storage", "storage:bookmarks", new Bookmarks.Provider());
bookmarkedURLS = new ArrayList();
bookmarkedConferences = new ArrayList();
}
@ -120,7 +117,7 @@ public class Bookmarks implements PrivateData {
*
* @return a collection of all Bookmarked URLs.
*/
public Collection getBookmarkedURLS() {
public List getBookmarkedURLS() {
return bookmarkedURLS;
}
@ -129,7 +126,7 @@ public class Bookmarks implements PrivateData {
*
* @return a collection of all Bookmarked Conferences.
*/
public Collection getBookmarkedConferences() {
public List getBookmarkedConferences() {
return bookmarkedConferences;
}
@ -164,13 +161,24 @@ public class Bookmarks implements PrivateData {
final Iterator urls = getBookmarkedURLS().iterator();
while (urls.hasNext()) {
BookmarkedURL urlStorage = (BookmarkedURL) urls.next();
buf.append("<url name=\"").append(urlStorage.getName()).append("\" url=\"").append(urlStorage.getURL()).append("\" />");
if(urlStorage.isShared()) {
continue;
}
buf.append("<url name=\"").append(urlStorage.getName()).
append("\" url=\"").append(urlStorage.getURL()).append("\"");
if(urlStorage.isRss()) {
buf.append(" rss=\"").append(true).append("\"");
}
buf.append(" />");
}
// Add Conference additions
final Iterator conferences = getBookmarkedConferences().iterator();
while (conferences.hasNext()) {
BookmarkedConference conference = (BookmarkedConference) conferences.next();
if(conference.isShared()) {
continue;
}
buf.append("<conference ");
buf.append("name=\"").append(conference.getName()).append("\" ");
buf.append("autojoin=\"").append(conference.isAutoJoin()).append("\" ");
@ -219,40 +227,52 @@ public class Bookmarks implements PrivateData {
storage.addBookmarkedURL(urlStorage);
}
}
else if (eventType == XmlPullParser.START_TAG && "conference".equals(parser.getName())) {
else if (eventType == XmlPullParser.START_TAG &&
"conference".equals(parser.getName()))
{
final BookmarkedConference conference = getConferenceStorage(parser);
storage.addBookmarkedConference(conference);
}
else if (eventType == XmlPullParser.END_TAG) {
if ("storage".equals(parser.getName())) {
else if (eventType == XmlPullParser.END_TAG && "storage".equals(parser.getName()))
{
done = true;
}
}
}
return storage;
}
}
private static BookmarkedURL getURLStorage(XmlPullParser parser) {
private static BookmarkedURL getURLStorage(XmlPullParser parser) throws IOException, XmlPullParserException {
String name = parser.getAttributeValue("", "name");
String url = parser.getAttributeValue("", "url");
BookmarkedURL urlStore = new BookmarkedURL();
urlStore.setName(name);
urlStore.setURL(url);
String rssString = parser.getAttributeValue("", "rss");
boolean rss = rssString != null && "true".equals(rssString);
BookmarkedURL urlStore = new BookmarkedURL(url, name, rss);
boolean done = false;
while (!done) {
int eventType = parser.next();
if(eventType == XmlPullParser.START_TAG
&& "shared_bookmark".equals(parser.getName())) {
urlStore.setShared(true);
}
else if (eventType == XmlPullParser.END_TAG && "url".equals(parser.getName())) {
done = true;
}
}
return urlStore;
}
private static BookmarkedConference getConferenceStorage(XmlPullParser parser) throws Exception {
BookmarkedConference conf = new BookmarkedConference();
String name = parser.getAttributeValue("", "name");
String autojoin = parser.getAttributeValue("", "autojoin");
String jid = parser.getAttributeValue("", "jid");
BookmarkedConference conf = new BookmarkedConference(jid);
conf.setName(name);
conf.setAutoJoin(Boolean.valueOf(autojoin).booleanValue());
conf.setJid(jid);
// Check for nickname
boolean done = false;
@ -264,10 +284,12 @@ public class Bookmarks implements PrivateData {
else if (eventType == XmlPullParser.START_TAG && "password".equals(parser.getName())) {
conf.setPassword(parser.nextText());
}
else if (eventType == XmlPullParser.END_TAG) {
if ("conference".equals(parser.getName())) {
done = true;
else if(eventType == XmlPullParser.START_TAG
&& "shared_bookmark".equals(parser.getName())) {
conf.setShared(true);
}
else if (eventType == XmlPullParser.END_TAG && "conference".equals(parser.getName())) {
done = true;
}
}

View file

@ -0,0 +1,24 @@
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.smackx.bookmark;
/**
* Interface to indicate if a bookmark is shared across the server.
*
* @author Alexander Wenckus
*/
public interface SharedBookmark {
/**
* Returns true if this bookmark is shared.
*
* @return returns true if this bookmark is shared.
*/
public boolean isShared();
}