2006-06-17 00:58:24 +02:00
|
|
|
/**
|
|
|
|
*
|
2007-03-08 00:03:48 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2007-03-08 00:03:48 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2006-06-17 00:58:24 +02:00
|
|
|
*/
|
2007-03-08 00:03:48 +01:00
|
|
|
|
2014-02-18 10:24:59 +01:00
|
|
|
package org.jivesoftware.smackx.bookmarks;
|
2006-06-17 00:58:24 +02:00
|
|
|
|
2014-04-04 11:55:06 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
2014-03-19 14:22:20 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2014-03-10 09:45:50 +01:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-02-15 11:35:08 +01:00
|
|
|
import org.jivesoftware.smackx.iqprivate.PrivateDataManager;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-05-27 19:29:51 +02:00
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
2015-04-21 19:05:22 +02:00
|
|
|
import org.jxmpp.jid.parts.Resourcepart;
|
2006-06-17 00:58:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-14 11:45:38 +02:00
|
|
|
* Provides methods to manage bookmarks in accordance with XEP-0048. Methods for managing URLs and
|
2006-06-17 00:58:24 +02:00
|
|
|
* Conferences are provided.
|
2017-12-25 12:51:41 +01:00
|
|
|
*
|
2014-06-14 11:45:38 +02:00
|
|
|
* It should be noted that some extensions have been made to the XEP. There is an attribute on URLs
|
2006-06-17 00:58:24 +02:00
|
|
|
* 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
|
|
|
|
*/
|
2015-04-06 10:45:12 +02:00
|
|
|
public final class BookmarkManager {
|
2017-12-13 23:10:11 +01:00
|
|
|
private static final Map<XMPPConnection, BookmarkManager> bookmarkManagerMap = new WeakHashMap<>();
|
2014-04-04 11:55:06 +02:00
|
|
|
|
2006-06-17 00:58:24 +02:00
|
|
|
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.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws IllegalArgumentException when the connection is null.
|
2006-06-17 00:58:24 +02:00
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public static synchronized BookmarkManager getBookmarkManager(XMPPConnection connection) {
|
2015-03-23 09:27:15 +01:00
|
|
|
BookmarkManager manager = bookmarkManagerMap.get(connection);
|
2014-03-12 11:50:05 +01:00
|
|
|
if (manager == null) {
|
2006-06-17 00:58:24 +02:00
|
|
|
manager = new BookmarkManager(connection);
|
2015-03-24 18:33:47 +01:00
|
|
|
bookmarkManagerMap.put(connection, manager);
|
2006-06-17 00:58:24 +02:00
|
|
|
}
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
2015-03-24 18:33:47 +01:00
|
|
|
private final PrivateDataManager privateDataManager;
|
2006-06-17 00:58:24 +02:00
|
|
|
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.
|
|
|
|
*/
|
2015-03-24 18:33:47 +01:00
|
|
|
private BookmarkManager(XMPPConnection connection) {
|
2014-04-04 11:55:06 +02:00
|
|
|
privateDataManager = PrivateDataManager.getInstanceFor(connection);
|
2006-06-17 00:58:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all currently bookmarked conferences.
|
|
|
|
*
|
|
|
|
* @return returns all currently bookmarked conferences
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws XMPPErrorException
|
|
|
|
* @throws NoResponseException
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2006-06-17 00:58:24 +02:00
|
|
|
* @see BookmarkedConference
|
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
public List<BookmarkedConference> getBookmarkedConferences() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2006-06-17 00:58:24 +02:00
|
|
|
retrieveBookmarks();
|
2014-11-29 13:36:56 +01:00
|
|
|
return Collections.unmodifiableList(bookmarks.getBookmarkedConferences());
|
2006-06-17 00:58:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws XMPPErrorException thrown when there is an issue retrieving the current bookmarks from
|
2006-06-17 00:58:24 +02:00
|
|
|
* the server.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws NoResponseException if there was no response from the server.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2006-06-17 00:58:24 +02:00
|
|
|
*/
|
2015-05-27 19:29:51 +02:00
|
|
|
public void addBookmarkedConference(String name, EntityBareJid jid, boolean isAutoJoin,
|
2018-04-06 10:21:46 +02:00
|
|
|
Resourcepart nickname, String password) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2006-06-17 00:58:24 +02:00
|
|
|
retrieveBookmarks();
|
|
|
|
BookmarkedConference bookmark
|
|
|
|
= new BookmarkedConference(name, jid, isAutoJoin, nickname, password);
|
2009-06-04 11:56:20 +02:00
|
|
|
List<BookmarkedConference> conferences = bookmarks.getBookmarkedConferences();
|
2017-05-23 16:45:04 +02:00
|
|
|
if (conferences.contains(bookmark)) {
|
2009-06-04 11:56:20 +02:00
|
|
|
BookmarkedConference oldConference = conferences.get(conferences.indexOf(bookmark));
|
2017-05-23 16:45:04 +02:00
|
|
|
if (oldConference.isShared()) {
|
2006-06-17 00:58:24 +02:00
|
|
|
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.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws XMPPErrorException thrown when there is a problem with the connection attempting to
|
2006-06-17 00:58:24 +02:00
|
|
|
* retrieve the bookmarks or persist the bookmarks.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws NoResponseException if there was no response from the server.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2006-06-17 00:58:24 +02:00
|
|
|
* @throws IllegalArgumentException thrown when the conference being removed is a shared
|
|
|
|
* conference
|
|
|
|
*/
|
2015-05-27 19:29:51 +02:00
|
|
|
public void removeBookmarkedConference(EntityBareJid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2006-06-17 00:58:24 +02:00
|
|
|
retrieveBookmarks();
|
2009-06-04 11:56:20 +02:00
|
|
|
Iterator<BookmarkedConference> it = bookmarks.getBookmarkedConferences().iterator();
|
2017-05-23 16:45:04 +02:00
|
|
|
while (it.hasNext()) {
|
2009-06-04 11:56:20 +02:00
|
|
|
BookmarkedConference conference = it.next();
|
2017-05-23 16:45:04 +02:00
|
|
|
if (conference.getJid().equals(jid)) {
|
|
|
|
if (conference.isShared()) {
|
2006-06-17 00:58:24 +02:00
|
|
|
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.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws XMPPErrorException thrown when there is a problem retriving bookmarks from the server.
|
|
|
|
* @throws NoResponseException if there was no response from the server.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2006-06-17 00:58:24 +02:00
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
public List<BookmarkedURL> getBookmarkedURLs() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2006-06-17 00:58:24 +02:00
|
|
|
retrieveBookmarks();
|
2014-11-29 13:36:56 +01:00
|
|
|
return Collections.unmodifiableList(bookmarks.getBookmarkedURLS());
|
2006-06-17 00:58:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws XMPPErrorException thrown when there is an error retriving or saving bookmarks from or to
|
2006-06-17 00:58:24 +02:00
|
|
|
* the server
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws NoResponseException if there was no response from the server.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2006-06-17 00:58:24 +02:00
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
public void addBookmarkedURL(String URL, String name, boolean isRSS) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2006-06-17 00:58:24 +02:00
|
|
|
retrieveBookmarks();
|
|
|
|
BookmarkedURL bookmark = new BookmarkedURL(URL, name, isRSS);
|
2009-06-04 11:56:20 +02:00
|
|
|
List<BookmarkedURL> urls = bookmarks.getBookmarkedURLS();
|
2017-05-23 16:45:04 +02:00
|
|
|
if (urls.contains(bookmark)) {
|
2009-06-04 11:56:20 +02:00
|
|
|
BookmarkedURL oldURL = urls.get(urls.indexOf(bookmark));
|
2017-05-23 16:45:04 +02:00
|
|
|
if (oldURL.isShared()) {
|
2006-06-17 00:58:24 +02:00
|
|
|
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
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws XMPPErrorException thrown if there is an error retriving or saving bookmarks from or to
|
2006-06-17 00:58:24 +02:00
|
|
|
* the server.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws NoResponseException if there was no response from the server.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2006-06-17 00:58:24 +02:00
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
public void removeBookmarkedURL(String bookmarkURL) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2006-06-17 00:58:24 +02:00
|
|
|
retrieveBookmarks();
|
2009-06-04 11:56:20 +02:00
|
|
|
Iterator<BookmarkedURL> it = bookmarks.getBookmarkedURLS().iterator();
|
2017-05-23 16:45:04 +02:00
|
|
|
while (it.hasNext()) {
|
2009-06-04 11:56:20 +02:00
|
|
|
BookmarkedURL bookmark = it.next();
|
2017-05-23 16:45:04 +02:00
|
|
|
if (bookmark.getURL().equalsIgnoreCase(bookmarkURL)) {
|
|
|
|
if (bookmark.isShared()) {
|
2006-06-17 00:58:24 +02:00
|
|
|
throw new IllegalArgumentException("Cannot delete a shared bookmark.");
|
|
|
|
}
|
|
|
|
it.remove();
|
|
|
|
privateDataManager.setPrivateData(bookmarks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 19:05:22 +02:00
|
|
|
/**
|
|
|
|
* Check if the service supports bookmarks using private data.
|
|
|
|
*
|
|
|
|
* @return true if the service supports private data, false otherwise.
|
|
|
|
* @throws NoResponseException
|
|
|
|
* @throws NotConnectedException
|
|
|
|
* @throws InterruptedException
|
|
|
|
* @throws XMPPErrorException
|
|
|
|
* @see PrivateDataManager#isSupported()
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public boolean isSupported() throws NoResponseException, NotConnectedException,
|
|
|
|
XMPPErrorException, InterruptedException {
|
|
|
|
return privateDataManager.isSupported();
|
|
|
|
}
|
|
|
|
|
2015-02-14 09:43:44 +01:00
|
|
|
private Bookmarks retrieveBookmarks() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2017-05-23 16:45:04 +02:00
|
|
|
synchronized (bookmarkLock) {
|
|
|
|
if (bookmarks == null) {
|
2006-06-17 00:58:24 +02:00
|
|
|
bookmarks = (Bookmarks) privateDataManager.getPrivateData("storage",
|
|
|
|
"storage:bookmarks");
|
|
|
|
}
|
|
|
|
return bookmarks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|