I recently added my delicious bookmarks to my website. Mostly so I could easily get at them when away from my main workstation. It turned out to be really easy.
I used the delicious-java library – API docs currently here. This is library has a bunch of functionality that I‘m not using. However it ticked the two requirements I had:
- It has a good API that hides/abstracts the actual HTTP API.
- It has support for caching bookmarks. This means my website‘s functionality is less dependent on the availability of delicious.com.
Solution
I‘m using Maven to build my project so first I add the dependency to my POM, like so:
<groupId>net.sf.delicious-java</groupId>
<artifactId>delicious</artifactId>
<version>1.14</version>
</dependency>
Then I wait a split-second and IDEA 8 asks me if I want to reload dependencies. Then the JAR is fetched from the remote maven repository, is added as a project dependency in IDEA and is automatically included whenever I package the project up for deployment. Using Maven with IDEA is just seamless.
For un-cached and therefore current access to bookmarks I could use the following code in the filter that runs before each action (it‘s a SeemoreJ app). This uses getRecentPosts to get an up–to–date list.
public void loadDelicious() {
try {
//Load delicious bookmarks
Delicious del = new Delicious("name", "pass");
getRequest().setAttribute("delicious", del.getRecentPosts(null, 10));
} catch (Exception e) {
// Don't want my site broken by inability to connect to delicious
log.error("Failed to connect to delicious", e);
}
}
However, I don‘t post to delicious all that often and I therefore don‘t want to check delicious every time a page on my site is accessed. Well, they‘ve thought of that as well. I reckon every half hour is often enough. So the following code does the trick:
public void loadDelicious() {
try {
//Load delicious bookmarks
DeliciousCache del = DeliciousCache.getInstance();
getRequest().setAttribute("delicious", del.getRecentPosts("name", "password", 10, null, 1800000));
} catch (Exception e) {
// Don't want my site broken by inability to connect to delicious
log.error("Failed to connect to delicious", e);
}
}
Then all I have to do is add some JSP/JSTL in my views to reference the attribute in the request scope.
Next I'm going to look at including my even less frequent twitterings
First published on Nov 28, 2008. Last updated on: Nov 28, 2008.