Costantino Perciante 2017-10-21 20:35:21 +00:00
commit f08098496a
23 changed files with 1248 additions and 0 deletions

26
.classpath Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>social-networking-client-library</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8

View File

@ -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

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

1
distro/LICENSE Normal file
View File

@ -0,0 +1 @@
${gcube.license}

61
distro/README Normal file
View File

@ -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.

5
distro/changelog.xml Normal file
View File

@ -0,0 +1,5 @@
<ReleaseNotes>
<Changeset component="org.gcube.portal.social-networking-client-library.1-0-0" date="2017-02-01">
<Change>first version</Change>
</Changeset>
</ReleaseNotes>

31
distro/descriptor.xml Normal file
View File

@ -0,0 +1,31 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>servicearchive</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>${distroDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>README</include>
<include>LICENSE</include>
<include>profile.xml</include>
<include>changelog.xml</include>
</includes>
<fileMode>755</fileMode>
<filtered>true</filtered>
</fileSet>
</fileSets>
<files>
<file>
<source>target/${build.finalName}.jar</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
</files>
</assembly>

29
distro/profile.xml Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource>
<ID></ID>
<Type>Service</Type>
<Profile>
<Description>${description}</Description>
<Class>DataAccess</Class>
<Name>${artifactId}</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>${description}</Description>
<Name>${artifactId}</Name>
<Version>${version}</Version>
<MavenCoordinates>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</MavenCoordinates>
<Type>library</Type>
<Files>
<File>${build.finalName}.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

206
pom.xml Normal file
View File

@ -0,0 +1,206 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>maven-parent</artifactId>
<groupId>org.gcube.tools</groupId>
<version>1.0.0</version>
</parent>
<groupId>org.gcube.portal</groupId>
<artifactId>social-networking-client-library</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>social-networking-client-library</name>
<packaging>jar</packaging>
<description>The social networking web service client library</description>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<java-version>1.7</java-version>
<version.jersey>2.25.1</version.jersey>
<distroDirectory>${project.basedir}/distro</distroDirectory>
<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
<distroDirectory>distro</distroDirectory>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<scm>
<connection>scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/portal/${project.artifactId}</connection>
<developerConnection>scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/portal/${project.artifactId}</developerConnection>
<url>http://svn.d4science.research-infrastructures.eu/gcube/trunk/portal/${project.artifactId}</url>
</scm>
<dependencies>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>home-library</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>home-library-jcr</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.gcube.portal</groupId>
<artifactId>social-networking-library</artifactId>
<version>[1.0.0-SNAPSHOT,)</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-generic-clients</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.gcube.portal</groupId>
<artifactId>social-networking-service-model</artifactId>
<version>[1.0.0-SNAPSHOT,)</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>${version.jersey}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${version.jersey}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${version.jersey}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-profile</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target</outputDirectory>
<resources>
<resource>
<directory>${distroDirectory}</directory>
<filtering>true</filtering>
<includes>
<include>profile.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptors>
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<!-- <descriptorRefs> -->
<!-- <descriptorRef>jar-with-dependencies</descriptorRef> -->
<!-- </descriptorRefs> -->
</configuration>
<executions>
<execution>
<id>servicearchive</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -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;
}
}

View File

@ -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<EnhancedFeed> 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<EnhancedFeed>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
}

View File

@ -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<String, Integer> getHashtagsCount(){
logger.debug("Request for hastags");
String thisMethodSignature = "get-hashtags-and-occurrences";
String request = getServiceEndpoint() + thisMethodSignature;
return HttpClient.get(new HashMap<String, Integer>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
}

View File

@ -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<WorkspaceMessage> getSentMessages(){
logger.debug("Request for sent messages");
String thisMethodSignature = "get-sent-messages";
String request = getServiceEndpoint() + thisMethodSignature + "?";
return HttpClient.get(new ArrayList<WorkspaceMessage>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
/**
* Get received messages
* @return
*/
@SuppressWarnings("unchecked")
public List<WorkspaceMessage> getReceivedMessages(){
logger.debug("Request for received messages");
String thisMethodSignature = "get-received-messages";
String request = getServiceEndpoint() + thisMethodSignature + "?";
return HttpClient.get(new ArrayList<WorkspaceMessage>(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);
}
}

View File

@ -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<Notification> 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<Notification>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
/**
* Notify job status
* @param notification
* @return
*/
@SuppressWarnings("unchecked")
public List<Notification> 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<Notification>(0).getClass(), request, SecurityTokenProvider.instance.get(), notification);
}
}

View File

@ -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());
}
}

View File

@ -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<Feed> 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<Feed>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
/**
* Get all posts
* @return
*/
@SuppressWarnings("unchecked")
public List<Feed> 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<Feed>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
/**
* Get posts quantity
* @return
*/
@SuppressWarnings("unchecked")
public List<Feed> 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<Feed>(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<Feed> getAllApplicationPosts(){
logger.debug("Request for getting posts");
String thisMethodSignature = "get-posts-app";
String request = getServiceEndpoint() + thisMethodSignature;
return HttpClient.get(new ArrayList<Feed>(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<Feed> getPostsVRE(){
logger.debug("Request for getting posts vre");
String thisMethodSignature = "get-posts-vre";
String request = getServiceEndpoint() + thisMethodSignature;
return HttpClient.get(new ArrayList<Feed>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
/**
* Get posts hashtag
* @return
*/
@SuppressWarnings("unchecked")
public List<Feed> 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<Feed>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
/**
* Get liked posts
* @return
*/
@SuppressWarnings("unchecked")
public List<Feed> getLikedPost(){
logger.debug("Request for getting posts liked");
String thisMethodSignature = "get-liked-posts";
String request = getServiceEndpoint() + thisMethodSignature;
return HttpClient.get(new ArrayList<String>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
/**
* Get liked posts ids
* @return
*/
@SuppressWarnings("unchecked")
public List<String> getLikedPostIds(){
logger.debug("Request for getting posts liked");
String thisMethodSignature = "get-id-liked-posts";
String request = getServiceEndpoint() + thisMethodSignature;
return HttpClient.get(new ArrayList<String>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
}

View File

@ -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);
}
}

View File

@ -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<String> getAllUsernamesContext(){
logger.debug("Request for getting usernames in this context");
String thisMethodSignature = "get-all-usernames";
String request = getServiceEndpoint() + thisMethodSignature;
return HttpClient.get(new ArrayList<String>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
/**
* Get all usernames and fullnames in this context
* @return
*/
@SuppressWarnings("unchecked")
public Map<String, String> 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<String, String>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
/**
* Get usernames by global role
* @return
*/
@SuppressWarnings("unchecked")
public List<String> 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<String>(0).getClass(), request, SecurityTokenProvider.instance.get());
}
/**
* Get usernames by local role
* @return
*/
@SuppressWarnings("unchecked")
public List<String> 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<String>(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());
}
}

View File

@ -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());
}
}

View File

@ -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 extends Object> R get(Class<R> 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 extends Object> R post(Class<R> 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) + "**************************";
}
}

View File

@ -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<String> client = client();
List<String> 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;
}
}