1   /*
2    * BibSonomyExporter
3    * Copyright (C) 2007 Christian Schenk
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18   */
19  package org.christianschenk.bibsonomy.export;
20  
21  import java.util.List;
22  
23  import org.bibsonomy.model.Bookmark;
24  import org.bibsonomy.model.Post;
25  import org.bibsonomy.model.Resource;
26  import org.bibsonomy.rest.client.Bibsonomy;
27  import org.bibsonomy.rest.client.exception.ErrorPerformingRequestException;
28  import org.bibsonomy.rest.client.queries.get.GetPostsQuery;
29  
30  /**
31   * Simple wrapper for the BibSonomy client API.
32   * 
33   * @author Christian Schenk
34   */
35  public class BibSonomyClient {
36  
37  	private final String username;
38  	private final String apikey;
39  	private final int MAX_POSTS = Integer.MAX_VALUE;
40  
41  	public BibSonomyClient(final String username, final String apikey) {
42  		this.username = username;
43  		this.apikey = apikey;
44  	}
45  
46  	public List<Post<? extends Resource>> getAllBookmarksFromBibSonomy() {
47  		final Bibsonomy client = new Bibsonomy(this.username, this.apikey);
48  		client.setApiURL("http://www.bibsonomy.org/api/users/" + this.username);
49  
50  		final GetPostsQuery gpq = new GetPostsQuery(0, this.MAX_POSTS);
51  		gpq.setResourceType(Bookmark.class);
52  
53  		try {
54  			client.executeQuery(gpq);
55  
56  			if (gpq.getHttpStatusCode() != 200) {
57  				throw new ErrorPerformingRequestException("HTTP status code: " + gpq.getHttpStatusCode() + " " + gpq.getError());
58  			}
59  
60  			return gpq.getResult();
61  		} catch (final ErrorPerformingRequestException ex) {
62  			throw new RuntimeException("Couldn't fetch bookmarks from BibSonomy", ex);
63  		}
64  	}
65  }