package org.gcube.social_networking.social_networking_client_library; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import javax.ws.rs.core.GenericType; import org.apache.commons.lang.Validate; import org.gcube.portal.databook.shared.*; import org.gcube.social_networking.social_networking_client_library.utils.HttpClient; import org.gcube.social_networking.socialnetworking.model.beans.PostInputBean; import org.gcube.social_networking.socialnetworking.model.output.ResponseBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Posts client. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ public class PostClient extends BaseClient{ private static final String SUB_SERVICE_PATH = "2/posts/"; private static Logger logger = LoggerFactory.getLogger(PostClient.class); public PostClient() throws Exception { super(SUB_SERVICE_PATH); } /** * Get posts since date * @return */ public List getUserPostsSinceDate(long timeInMillis){ Validate.isTrue(timeInMillis >= 0, "time cannot be negative"); logger.debug("Request for getting posts"); String thisMethodSignature = "get-posts-user-since"; String request = getServiceEndpoint() + thisMethodSignature + "?time=" + timeInMillis; return HttpClient.get(new GenericType>>(){}, request); } /** * Get all posts * @return */ public List getAllUserPosts(){ logger.debug("Request for getting posts"); String thisMethodSignature = "get-posts-user"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.get(new GenericType>>(){}, request); } /** * Get posts quantity * @return */ public List getUserPostsQuantity(int quantity){ Validate.isTrue(quantity >= 0, "quantity cannot be negative"); logger.debug("Request for getting posts"); String thisMethodSignature = "get-posts-user-quantity"; String request = getServiceEndpoint() + thisMethodSignature + "?quantity=" + quantity; return HttpClient.get(new GenericType>>(){}, request); } /** * Write post * @return */ public Post writeUserPost(PostInputBean toWrite){ Validate.isTrue(toWrite != null, "Post to write cannot be null"); logger.debug("Request for writing post"); String thisMethodSignature = "write-post-user"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.post(new GenericType>(){}, request, toWrite); } /** * Get posts application (token set must belong to the application) * @return */ public List getAllApplicationPosts(){ logger.debug("Request for getting posts"); String thisMethodSignature = "get-posts-app"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.get(new GenericType>>(){}, request); } /** * Write post application (token set must belong to the application) * @return */ public Post writeApplicationPost(PostInputBean toWrite){ Validate.isTrue(toWrite != null, "Post to write cannot be null"); logger.debug("Request for writing application post"); String thisMethodSignature = "write-post-app"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.post(new GenericType>(){}, request, toWrite); } /** * @deprecated use List getVREPosts() * Get posts vre * @return */ public List getPostsVRE(){ logger.debug("Request for getting posts vre"); String thisMethodSignature = "get-posts-vre"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.get(new GenericType>>(){}, request); } /** * Get posts vre * @return */ public List getVREPosts(){ logger.debug("Request for getting posts vre"); String thisMethodSignature = "get-posts-vre"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.get(new GenericType>>(){}, request); } /** * Get posts hashtag * @return * @throws UnsupportedEncodingException */ public List getHashtagPosts(String hashtag) throws UnsupportedEncodingException{ Validate.isTrue(hashtag != null, "hashtag cannot be null"); logger.debug("Request for getting posts with hashtag " + hashtag); String thisMethodSignature = "get-posts-by-hashtag"; String request = getServiceEndpoint() + thisMethodSignature + "?hashtag=" + URLEncoder.encode(hashtag, "UTF-8"); return HttpClient.get(new GenericType>>(){}, request); } /** * Get liked posts * @return */ public List getUserLikedPost(){ logger.debug("Request for getting posts liked"); String thisMethodSignature = "get-liked-posts"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.get(new GenericType>>(){}, request); } /** * Get liked posts ids * @return */ public List getUserLikedPostIds(){ logger.debug("Request for getting posts liked"); String thisMethodSignature = "get-id-liked-posts"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.get(new GenericType>>(){}, request); } //library api calls public boolean saveUserPostLib(Post post){ Validate.isTrue(post != null, "Post to write cannot be null"); logger.debug("Request for save user post"); String thisMethodSignature = "save-user-post-lib"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.post(new GenericType>(){}, request, post); } //bool public boolean saveAttachmentEntryLib(String key, Attachment attachment){ Validate.isTrue(attachment != null, "attachment to write cannot be null"); logger.debug("Request for save attachment"); String thisMethodSignature = "save-attachment-entry-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?postid=" + key; return HttpClient.post(new GenericType>(){}, request, attachment); } public boolean saveAppPostLib(Post post){ Validate.isTrue(post != null, "Post to write cannot be null"); logger.debug("Request for saving application post"); String thisMethodSignature = "save-app-post-lib"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.post(new GenericType>(){}, request, post); } //bool public boolean savePostToVRETimelineLib(String postkey, String vreid){ Validate.isTrue(postkey != null, "Post to write cannot be null"); logger.debug("Request for writing post to vretimeline"); String thisMethodSignature = "save-post-to-vretimeline-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?postid=" + postkey + "&vreid=" + vreid; return HttpClient.post(new GenericType>(){}, request, postkey); } //bool public Post readPostLib(String postid){ logger.debug("Request for getting post with id"); String thisMethodSignature = "read-post-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?postid=" + postid; return HttpClient.get(new GenericType>(){}, request); } //post public List getRecentPostsByUserAndDateLib(String userid, long timeinmillis){ logger.debug("Request for getting recent posts by user"); String thisMethodSignature = "get-recent-posts-by-user-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?userid=" + userid + "&time=" + timeinmillis; return HttpClient.get(new GenericType>>(){}, request); } //list public boolean deletePostLib(String postid){ Validate.isTrue(postid != null, "feed to delete cannot be null"); logger.debug("Request for delete user post"); String thisMethodSignature = "delete-post-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?postid=" + postid; return HttpClient.post(new GenericType>(){}, request, postid); } //bool public List getAllPostsByUserLib(String userid) { logger.debug("Request for getting all posts by user"); String thisMethodSignature = "get-all-posts-by-user-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?userid=" + userid ; return HttpClient.get(new GenericType>>(){}, request); } public List getAllPostsByAppLib(String appid) { logger.debug("Request for getting all posts by app"); String thisMethodSignature = "get-all-posts-by-app-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?appid=" + appid ; return HttpClient.get(new GenericType>>(){}, request); } public List getRecentCommentedPostsByUserAndDateLib(String userid, long timeInMillis) { logger.debug("Request for getting recent commented posts by user and date"); String thisMethodSignature = "get-recent-commented-posts-by-user-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?userid=" + userid + "&time=" + timeInMillis; return HttpClient.get(new GenericType>>(){}, request); } public List getAllPortalPrivacyLevelPostsLib(){ logger.debug("Request for getting portal privacy level posts"); String thisMethodSignature = "get-portal-privacy-level-posts-lib"; String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.get(new GenericType>>(){}, request); } //list public List getRecentPostsByUserLib(String userid, int quantity) { logger.debug("Request for getting recent posts by user"); String thisMethodSignature = "get-recent-posts-by-user-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?userid=" + userid + "&quantity=" + quantity ; return HttpClient.get(new GenericType>>(){}, request); } public List getAllPostsByVRELib(String vreid) { logger.debug("Request for getting all posts by vre"); String thisMethodSignature = "get-all-posts-by-vre-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?vreid=" + vreid ; return HttpClient.get(new GenericType>>(){}, request); } public List getRecentPostsByVRELib(String vreid, int quantity) { logger.debug("Request for getting recent posts by vre"); String thisMethodSignature = "get-recent-posts-by-vre-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?vreid=" + vreid + "&quantity=" + quantity ; return HttpClient.get(new GenericType>>(){}, request); } public RangePosts getRecentPostsByVREAndRangeLib(String vreid, int from, int quantity) { logger.debug("Request for getting recent posts by vre and range"); String thisMethodSignature = "get-recent-posts-by-vre-range-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?vreid=" + vreid + "&from=" + from + "&quantity=" + quantity; return HttpClient.get(new GenericType>(){}, request); } public List getAttachmentsByFeedIdLib(String feedId) { logger.debug("Request for getting attachment by postid"); String thisMethodSignature = "get-attachment-by-postid-lib"; String request = getServiceEndpoint() + thisMethodSignature + "?postid=" + feedId ; return HttpClient.get(new GenericType>>(){}, request); } public List getAllVREIdsLib(){ logger.debug("Request for getting all vre ids"); String thisMethodSignature = "get-all-vre-ids-lib"; String request = getServiceEndpoint() + thisMethodSignature ; return HttpClient.get(new GenericType>>(){}, request); } }