commit f08098496a2fc3c9d94f8ee1fe193e82beba40af Author: Costantino Perciante Date: Sat Oct 21 20:35:21 2017 +0000 git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/social-networking/social-networking-client-library@157438 82a268e6-3cf1-43bd-a215-b396298e98cf diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..7bd7856 --- /dev/null +++ b/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..57e640d --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + social-networking-client-library + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..f9fe345 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ec4300d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/distro/LICENSE b/distro/LICENSE new file mode 100644 index 0000000..3695e26 --- /dev/null +++ b/distro/LICENSE @@ -0,0 +1 @@ +${gcube.license} diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..edf22f4 --- /dev/null +++ b/distro/README @@ -0,0 +1,61 @@ +The gCube System - ${name} +-------------------------------------------------- + +${description} + + +${gcube.description} + +${gcube.funding} + + +Version +-------------------------------------------------- + +${version} (${buildDate}) + +Please see the file named "changelog.xml" in this directory for the release notes. + + +Authors +-------------------------------------------------- + +* Lucio Lelii (lucio.lelii-AT-isti.cnr.it), CNR, Italy + +MAINTAINERS +-------------------------------------------------- + +* Lucio Lelii (lucio.lelii-AT-isti.cnr.it), CNR, Italy + +Download information +-------------------------------------------------- + +Source code is available from SVN: + ${scm.url} + +Binaries can be downloaded from the gCube website: + ${gcube.website} + + +Installation +-------------------------------------------------- + +Installation documentation is available on-line in the gCube Wiki: + ${gcube.wikiRoot} + +Documentation +-------------------------------------------------- + +Documentation is available on-line in the gCube Wiki: + ${gcube.wikiRoot} +Support +-------------------------------------------------- + +Bugs and support requests can be reported in the gCube issue tracking tool: + ${gcube.issueTracking} + + +Licensing +-------------------------------------------------- + +This software is licensed under the terms you may find in the file named "LICENSE" in this directory. diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..c6f46ad --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,5 @@ + + + first version + + \ No newline at end of file diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..67b0c2c --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,31 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + profile.xml + changelog.xml + + 755 + true + + + + + target/${build.finalName}.jar + /${artifactId} + + + \ No newline at end of file diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..62d5fef --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,29 @@ + + + + Service + + ${description} + DataAccess + ${artifactId} + 1.0.0 + + + ${description} + ${artifactId} + ${version} + + ${groupId} + ${artifactId} + ${version} + + library + + ${build.finalName}.jar + + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..fe7d75c --- /dev/null +++ b/pom.xml @@ -0,0 +1,206 @@ + + + 4.0.0 + + maven-parent + org.gcube.tools + 1.0.0 + + + org.gcube.portal + social-networking-client-library + 1.0.0-SNAPSHOT + social-networking-client-library + jar + The social networking web service client library + + + + + org.gcube.distribution + gcube-bom + LATEST + pom + import + + + + + + 1.7 + 2.25.1 + ${project.basedir}/distro + ${project.build.directory}/${project.build.finalName} + distro + UTF-8 + UTF-8 + + + + scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/portal/${project.artifactId} + scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/portal/${project.artifactId} + http://svn.d4science.research-infrastructures.eu/gcube/trunk/portal/${project.artifactId} + + + + + org.gcube.common + home-library + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + compile + + + org.gcube.common + home-library-jcr + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + compile + + + org.gcube.portal + social-networking-library + [1.0.0-SNAPSHOT,) + compile + + + org.gcube.core + common-generic-clients + compile + + + org.gcube.portal + social-networking-service-model + [1.0.0-SNAPSHOT,) + + + org.glassfish.jersey.ext + jersey-bean-validation + ${version.jersey} + provided + + + org.glassfish.jersey.media + jersey-media-json-jackson + ${version.jersey} + provided + + + org.glassfish.jersey.core + jersey-client + ${version.jersey} + provided + + + org.slf4j + slf4j-api + provided + + + org.slf4j + slf4j-log4j12 + 1.6.4 + provided + + + junit + junit + 4.10 + test + + + + + + + src/main/java + + **/*.* + + + + + + maven-compiler-plugin + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.2 + + + + test-jar + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12 + + true + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + + copy-profile + install + + copy-resources + + + target + + + ${distroDirectory} + true + + profile.xml + + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 2.2 + + + ${distroDirectory}/descriptor.xml + + + + fully.qualified.MainClass + + + + + + + + + servicearchive + install + + single + + + + + + + diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/BaseClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/BaseClient.java new file mode 100644 index 0000000..0dbc18b --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/BaseClient.java @@ -0,0 +1,24 @@ +package org.gcube.portal.social_networking_client_library; + +import org.gcube.portal.social_networking_client_library.utils.ServiceDiscoverer; + +/** + * Base client service. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public abstract class BaseClient { + + private String serviceEndpoint; + + public BaseClient(String subPath) throws Exception { + ServiceDiscoverer discover = new ServiceDiscoverer(); + serviceEndpoint = discover.getEntryPoint(); + serviceEndpoint = serviceEndpoint.endsWith("/") ? serviceEndpoint + subPath : + serviceEndpoint + "/" + subPath; + } + + public String getServiceEndpoint() { + return serviceEndpoint; + } + +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/FullTextSearchClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/FullTextSearchClient.java new file mode 100644 index 0000000..6791d1c --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/FullTextSearchClient.java @@ -0,0 +1,51 @@ +package org.gcube.portal.social_networking_client_library; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.Validate; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.portal.databook.shared.EnhancedFeed; +import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Client for full text search over social content. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class FullTextSearchClient extends BaseClient{ + + private static final String SUB_SERVICE_PATH = "2/full-text-search/"; + private static Logger logger = LoggerFactory.getLogger(FullTextSearchClient.class); + + + public FullTextSearchClient() throws Exception { + super(SUB_SERVICE_PATH); + } + + /** + * Perform a search query over social data + * @param query + * @param from a value greater or equal to zero + * @param quantity a value greater than zero + * @return a list of matching posts + * @throws UnsupportedEncodingException + */ + @SuppressWarnings("unchecked") + public List search(String query, int from, int quantity) throws UnsupportedEncodingException{ + + Validate.isTrue(query != null, "Query cannot be null"); + Validate.isTrue(from >= 0, "From needs to be greater or equal to zero"); + Validate.isTrue(quantity >= 0, "Quantity needs to be greater or equal to zero"); + + logger.debug("Request for query " + query + " and from " + from + " and quantity is " + quantity); + String thisMethodSignature = "search-by-query"; + String request = getServiceEndpoint() + thisMethodSignature + "?" + "query=" + URLEncoder.encode(query, "UTF-8") + "&from=" + from + "&quantity=" + quantity; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/HashTagsClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/HashTagsClient.java new file mode 100644 index 0000000..904154c --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/HashTagsClient.java @@ -0,0 +1,40 @@ +package org.gcube.portal.social_networking_client_library; + +import java.util.HashMap; +import java.util.Map; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * HashTags client. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class HashTagsClient extends BaseClient{ + + private static final String SUB_SERVICE_PATH = "2/hashtags/"; + private static Logger logger = LoggerFactory.getLogger(HashTagsClient.class); + + + public HashTagsClient() throws Exception { + super(SUB_SERVICE_PATH); + } + + /** + * Get hashtags and their count + * @return a map of type hashtag -> number + */ + @SuppressWarnings("unchecked") + public Map getHashtagsCount(){ + + logger.debug("Request for hastags"); + String thisMethodSignature = "get-hashtags-and-occurrences"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(new HashMap(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/MessagesClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/MessagesClient.java new file mode 100644 index 0000000..24e028c --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/MessagesClient.java @@ -0,0 +1,70 @@ +package org.gcube.portal.social_networking_client_library; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.Validate; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.homelibrary.home.workspace.sharing.WorkspaceMessage; +import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.gcube.portal.socialnetworking.model.input.MessageInputBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Messages client. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class MessagesClient extends BaseClient{ + + private static final String SUB_SERVICE_PATH = "2/messages/"; + private static Logger logger = LoggerFactory.getLogger(MessagesClient.class); + + + public MessagesClient() throws Exception { + super(SUB_SERVICE_PATH); + } + + /** + * Get sent messages + * @return + */ + @SuppressWarnings("unchecked") + public List getSentMessages(){ + + logger.debug("Request for sent messages"); + String thisMethodSignature = "get-sent-messages"; + String request = getServiceEndpoint() + thisMethodSignature + "?"; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get received messages + * @return + */ + @SuppressWarnings("unchecked") + public List getReceivedMessages(){ + + logger.debug("Request for received messages"); + String thisMethodSignature = "get-received-messages"; + String request = getServiceEndpoint() + thisMethodSignature + "?"; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Write message + * @return message id + */ + public String writeMessage(MessageInputBean m){ + + Validate.isTrue(m != null, "Message cannot be null"); + + logger.debug("Request for writing new message"); + String thisMethodSignature = "write-message"; + String request = getServiceEndpoint() + thisMethodSignature + "?"; + return HttpClient.post(String.class, request, SecurityTokenProvider.instance.get(), m); + + } +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/NotificationsClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/NotificationsClient.java new file mode 100644 index 0000000..e91751a --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/NotificationsClient.java @@ -0,0 +1,64 @@ +package org.gcube.portal.social_networking_client_library; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.Validate; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.portal.databook.shared.Notification; +import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.gcube.portal.socialnetworking.model.input.JobNotificationBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Notifications client. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class NotificationsClient extends BaseClient{ + + private static final String SUB_SERVICE_PATH = "2/notifications/"; + private static Logger logger = LoggerFactory.getLogger(NotificationsClient.class); + + + public NotificationsClient() throws Exception { + super(SUB_SERVICE_PATH); + } + + /** + * Get range notifications + * @param from + * @param quantity + * @return + */ + @SuppressWarnings("unchecked") + public List getNotifications(int from, int quantity){ + + Validate.isTrue(from >= 0, "From cannot be negative"); + Validate.isTrue(from >= 0, "Quantity cannot be negative"); + + logger.debug("Request for getting notifications"); + String thisMethodSignature = "get-range-notifications"; + String request = getServiceEndpoint() + thisMethodSignature + "?from=" + from + "&quantity=" +quantity; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Notify job status + * @param notification + * @return + */ + @SuppressWarnings("unchecked") + public List sendJobNotification(JobNotificationBean notification){ + + Validate.isTrue(notification != null, "Notification cannot be null"); + + logger.debug("Request for getting notifications"); + String thisMethodSignature = "notify-job-status"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.post(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get(), notification); + + } + +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/PeopleClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/PeopleClient.java new file mode 100644 index 0000000..26d9396 --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/PeopleClient.java @@ -0,0 +1,36 @@ +package org.gcube.portal.social_networking_client_library; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * People client. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class PeopleClient extends BaseClient{ + + private static final String SUB_SERVICE_PATH = "2/people/"; + private static Logger logger = LoggerFactory.getLogger(NotificationsClient.class); + + + public PeopleClient() throws Exception { + super(SUB_SERVICE_PATH); + } + + /** + * Get profile + * @return user's profile + */ + public JSONObject getProfile(){ + + logger.debug("Request for getting profile"); + String thisMethodSignature = "profile"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(JSONObject.class, request, SecurityTokenProvider.instance.get()); + + } + +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/PostsClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/PostsClient.java new file mode 100644 index 0000000..1fce66b --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/PostsClient.java @@ -0,0 +1,178 @@ +package org.gcube.portal.social_networking_client_library; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.Validate; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.portal.databook.shared.Feed; +import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.gcube.portal.socialnetworking.model.input.PostInputBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Posts client. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class PostsClient extends BaseClient{ + + private static final String SUB_SERVICE_PATH = "2/posts/"; + private static Logger logger = LoggerFactory.getLogger(PostsClient.class); + + + public PostsClient() throws Exception { + super(SUB_SERVICE_PATH); + } + + /** + * Get posts since date + * @return + */ + @SuppressWarnings("unchecked") + public List getPostsSinceDate(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; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get all posts + * @return + */ + @SuppressWarnings("unchecked") + public List getAllPosts(long timeInMillis){ + + Validate.isTrue(timeInMillis >= 0, "time cannot be negative"); + + logger.debug("Request for getting posts"); + String thisMethodSignature = "get-posts-user"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get posts quantity + * @return + */ + @SuppressWarnings("unchecked") + public List getPostsQuantity(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; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Write post + * @return + */ + public Feed write(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(Feed.class, request, SecurityTokenProvider.instance.get(), toWrite); + + } + + /** + * Get posts application (token set must belong to the application) + * @return + */ + @SuppressWarnings("unchecked") + public List getAllApplicationPosts(){ + + logger.debug("Request for getting posts"); + String thisMethodSignature = "get-posts-app"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Write post application (token set must belong to the application) + * @return + */ + public Feed 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(Feed.class, request, SecurityTokenProvider.instance.get(), toWrite); + + } + + /** + * Get posts vre + * @return + */ + @SuppressWarnings("unchecked") + public List getPostsVRE(){ + + logger.debug("Request for getting posts vre"); + String thisMethodSignature = "get-posts-vre"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get posts hashtag + * @return + */ + @SuppressWarnings("unchecked") + public List getHashtagPosts(String hashtag){ + + 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; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get liked posts + * @return + */ + @SuppressWarnings("unchecked") + public List getLikedPost(){ + + logger.debug("Request for getting posts liked"); + String thisMethodSignature = "get-liked-posts"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get liked posts ids + * @return + */ + @SuppressWarnings("unchecked") + public List getLikedPostIds(){ + + logger.debug("Request for getting posts liked"); + String thisMethodSignature = "get-id-liked-posts"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/TokensClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/TokensClient.java new file mode 100644 index 0000000..ff481ab --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/TokensClient.java @@ -0,0 +1,39 @@ +package org.gcube.portal.social_networking_client_library; + +import org.apache.commons.lang.Validate; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.gcube.portal.socialnetworking.model.input.ApplicationId; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Tokens client. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class TokensClient extends BaseClient{ + + private static final String SUB_SERVICE_PATH = "2/tokens/"; + private static Logger logger = LoggerFactory.getLogger(TokensClient.class); + + + public TokensClient() throws Exception { + super(SUB_SERVICE_PATH); + } + + /** + * Generate application token + * @return generated token + */ + public String generateApplicationToken(ApplicationId appId){ + + Validate.isTrue(appId != null, "application id cannot be null"); + + logger.debug("Request for writing new message"); + String thisMethodSignature = "generate-application-token"; + String request = getServiceEndpoint() + thisMethodSignature + "?"; + return HttpClient.post(String.class, request, SecurityTokenProvider.instance.get(), appId); + + } + +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/UsersClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/UsersClient.java new file mode 100644 index 0000000..45bd01b --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/UsersClient.java @@ -0,0 +1,158 @@ +package org.gcube.portal.social_networking_client_library; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.Validate; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Users client. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class UsersClient extends BaseClient{ + + private static final String SUB_SERVICE_PATH = "2/users/"; + private static Logger logger = LoggerFactory.getLogger(UsersClient.class); + + + public UsersClient() throws Exception { + super(SUB_SERVICE_PATH); + } + + /** + * Get attribute value + * @return + */ + public String getCustomAttribute(String attributeKey){ + + Validate.isTrue(attributeKey != null, "attribute key cannot be null"); + + logger.debug("Request for attribute value"); + String thisMethodSignature = "get-custom-attribute"; + String request = getServiceEndpoint() + thisMethodSignature + "?attribute=" + attributeKey; + return HttpClient.get(String.class, request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get fullname + * @return user's fullname + */ + public String getFullName(){ + + logger.debug("Request for fullname"); + String thisMethodSignature = "get-fullname"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(String.class, request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get email + * @return users'email + */ + public String getEmail(){ + + logger.debug("Request for email"); + String thisMethodSignature = "get-email"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(String.class, request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get profile + * @return + */ + public JSONObject getProfile(){ + + logger.debug("Request for getting profile"); + String thisMethodSignature = "get-profile"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(JSONObject.class, request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get all usernames in this context + * @return + */ + @SuppressWarnings("unchecked") + public List getAllUsernamesContext(){ + + logger.debug("Request for getting usernames in this context"); + String thisMethodSignature = "get-all-usernames"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get all usernames and fullnames in this context + * @return + */ + @SuppressWarnings("unchecked") + public Map getAllUsernamesFullnamesContext(){ + + logger.debug("Request for getting usernames and fullnames in this context"); + String thisMethodSignature = "get-all-fullnames-and-usernames"; + String request = getServiceEndpoint() + thisMethodSignature; + return HttpClient.get(new HashMap(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get usernames by global role + * @return + */ + @SuppressWarnings("unchecked") + public List getAllUsernamesByGlobalRole(String globalRole){ + + Validate.isTrue(globalRole != null, "role cannot be null"); + + logger.debug("Request for getting usernames with global role"); + String thisMethodSignature = "get-usernames-by-global-role"; + String request = getServiceEndpoint() + thisMethodSignature + "?role-name=" + globalRole; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Get usernames by local role + * @return + */ + @SuppressWarnings("unchecked") + public List getAllUsernamesByLocalRole(String localRole){ + + Validate.isTrue(localRole != null, "role cannot be null"); + + logger.debug("Request for getting usernames with global role"); + String thisMethodSignature = "get-usernames-by-role"; + String request = getServiceEndpoint() + thisMethodSignature + "?role-name=" + localRole; + return HttpClient.get(new ArrayList(0).getClass(), request, SecurityTokenProvider.instance.get()); + + } + + /** + * Check if user exists + * @return + */ + public Boolean userExists(String username){ + + Validate.isTrue(username != null, "username cannot be null"); + + logger.debug("Request for getting usernames with global role"); + String thisMethodSignature = "user-exists"; + String request = getServiceEndpoint() + thisMethodSignature + "?username=" + username; + return HttpClient.get(Boolean.class, request, SecurityTokenProvider.instance.get()); + + } + +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/VREsClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/VREsClient.java new file mode 100644 index 0000000..0371b61 --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/VREsClient.java @@ -0,0 +1,36 @@ +package org.gcube.portal.social_networking_client_library; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.json.JSONArray; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Vres client. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class VREsClient extends BaseClient{ + + private static final String SUB_SERVICE_PATH = "2/vres/"; + private static Logger logger = LoggerFactory.getLogger(VREsClient.class); + + + public VREsClient() throws Exception { + super(SUB_SERVICE_PATH); + } + + /** + * Get my Vres + * @return my vres + */ + public JSONArray getMyVRES(){ + + logger.debug("Request for writing new message"); + String thisMethodSignature = "get-my-vres"; + String request = getServiceEndpoint() + thisMethodSignature + "?"; + return HttpClient.get(JSONArray.class, request, SecurityTokenProvider.instance.get()); + + } + +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/utils/HttpClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/utils/HttpClient.java new file mode 100644 index 0000000..7745b63 --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/utils/HttpClient.java @@ -0,0 +1,84 @@ +package org.gcube.portal.social_networking_client_library.utils; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.gcube.portal.socialnetworking.model.output.ResponseBean; +import org.glassfish.jersey.client.ClientResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Http client with post and get methods (jersey is used) + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +@SuppressWarnings("unchecked") +public class HttpClient { + + private static Logger logger = LoggerFactory.getLogger(HttpClient.class); + + public static R get(Class returnType, String requestUrl, String token){ + + logger.debug("Executing get request at url " + requestUrl + " and token is " + maskedToken(token)); + + // Create Jersey client + Client client = ClientBuilder.newClient(); + WebTarget webResourceGet = client.target(requestUrl).queryParam("gcube-token", token); + ClientResponse response = webResourceGet.request(MediaType.APPLICATION_JSON).get(ClientResponse.class); + + if (response.getStatus() < 200 || response.getStatus() > 205) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatus()); + } + + ResponseBean responseEntity = (ResponseBean) response.getEntity(); + + if (responseEntity.getMessage() != null && !responseEntity.getMessage().isEmpty()) { + throw new WebApplicationException( + "Response has status code " + response.getStatus() + " and message " + + responseEntity.getMessage()); + } + + return (R) responseEntity.getResult(); + } + + public static R post(Class returnType, String requestUrl, String token, Object body){ + + logger.debug("Executing get request at url " + requestUrl + " and token is " + maskedToken(token)); + + // Create Jersey client + Client client = ClientBuilder.newClient(); + WebTarget webResourceGet = client.target(requestUrl).queryParam("gcube-token", token); + Response response = webResourceGet.request(MediaType.APPLICATION_JSON).post(Entity.json(body)); + + if (response.getStatus() < 200 || response.getStatus() > 205) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatus()); + } + + ResponseBean responseEntity = (ResponseBean) response.getEntity(); + + if (responseEntity.getMessage() != null && !responseEntity.getMessage().isEmpty()) { + throw new WebApplicationException( + "Response has status code " + response.getStatus() + " and message " + + responseEntity.getMessage()); + } + + return (R) responseEntity.getResult(); + } + + private static String maskedToken(String token){ + + if(token == null) + return ""; + else + return token.substring(0, 5) + "**************************"; + + } + +} diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/utils/ServiceDiscoverer.java b/src/main/java/org/gcube/portal/social_networking_client_library/utils/ServiceDiscoverer.java new file mode 100644 index 0000000..9660573 --- /dev/null +++ b/src/main/java/org/gcube/portal/social_networking_client_library/utils/ServiceDiscoverer.java @@ -0,0 +1,73 @@ +/** + * + */ +package org.gcube.portal.social_networking_client_library.utils; + +import static org.gcube.resources.discovery.icclient.ICFactory.client; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.List; + +import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ServiceDiscoverer { + + private static final String resource = "jersey-servlet"; + private static final String serviceName = "SocialNetworking"; + private static final String serviceClass = "Portal"; + + private static Logger logger = LoggerFactory.getLogger(ServiceDiscoverer.class); + private String entryPoint; + + /** + * Discover the entry point for the service + * @throws Exception + */ + public ServiceDiscoverer() throws Exception { + + String currentScope = ScopeProvider.instance.get(); + + try{ + + logger.info("set scope "+currentScope); + + SimpleQuery query = queryFor(GCoreEndpoint.class); + query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'",serviceClass)); + query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'"); + query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'",serviceName)); + query.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""+resource+"\"]/text()"); + + logger.debug("submitting quey "+query.toString()); + + DiscoveryClient client = client(); + List endpoints = client.submit(query); + if (endpoints == null || endpoints.isEmpty()) + throw new Exception("Cannot retrieve the GCoreEndpoint serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+currentScope); + + + this.entryPoint = endpoints.get(0); + if(entryPoint==null) + throw new Exception("Endpoint:"+resource+", is null for serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+currentScope); + + logger.info("found entryPoint "+entryPoint+" for social service: "+resource); + + }catch(Exception e){ + String error = "An error occurred during GCoreEndpoint discovery, serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+currentScope +"."; + logger.error(error, e); + throw new Exception(error); + } + } + + /** + * @return the ResourceEntyName + */ + public String getEntryPoint() { + + return entryPoint; + } +}