removed some deprecated methods
This commit is contained in:
parent
4982250c26
commit
5111439f3e
|
@ -1,8 +1,5 @@
|
||||||
package org.gcube.portal.social.networking.ws.methods.v1;
|
package org.gcube.portal.social.networking.ws.methods.v1;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.ws.rs.FormParam;
|
import javax.ws.rs.FormParam;
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
|
@ -11,22 +8,9 @@ import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.Response.Status;
|
import javax.ws.rs.core.Response.Status;
|
||||||
|
|
||||||
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager;
|
|
||||||
import org.gcube.applicationsupportlayer.social.NotificationsManager;
|
|
||||||
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite;
|
|
||||||
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser;
|
|
||||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
|
||||||
import org.gcube.common.authorization.library.utils.Caller;
|
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
|
||||||
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
|
|
||||||
import org.gcube.common.storagehub.client.proxies.MessageManagerClient;
|
|
||||||
import org.gcube.portal.notifications.bean.GenericItemBean;
|
|
||||||
import org.gcube.portal.notifications.thread.MessageNotificationsThread;
|
|
||||||
import org.gcube.portal.social.networking.caches.SocialNetworkingSiteFinder;
|
|
||||||
import org.gcube.portal.social.networking.liferay.ws.UserManagerWSBuilder;
|
import org.gcube.portal.social.networking.liferay.ws.UserManagerWSBuilder;
|
||||||
import org.gcube.portal.social.networking.ws.utils.ErrorMessages;
|
import org.gcube.portal.social.networking.ws.utils.ErrorMessages;
|
||||||
import org.gcube.vomanagement.usermanagement.UserManager;
|
import org.gcube.vomanagement.usermanagement.UserManager;
|
||||||
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,22 +21,10 @@ import org.slf4j.LoggerFactory;
|
||||||
@Path("/messages")
|
@Path("/messages")
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public class Messages {
|
public class Messages {
|
||||||
|
|
||||||
// Logger
|
// Logger
|
||||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Messages.class);
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Messages.class);
|
||||||
|
|
||||||
// recipients separator
|
public Messages() {
|
||||||
private static final String RECIPIENTS_ID_SEPARATOR = ",";
|
|
||||||
|
|
||||||
// user manager
|
|
||||||
private UserManager uManager = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Build usermanager
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public Messages() throws Exception {
|
|
||||||
uManager = UserManagerWSBuilder.getInstance().getUserManager();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
|
@ -64,6 +36,7 @@ public class Messages {
|
||||||
* @param subject
|
* @param subject
|
||||||
* @return ok on success, error otherwise
|
* @return ok on success, error otherwise
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public Response writeMessageToUsers(
|
public Response writeMessageToUsers(
|
||||||
@FormParam("sender") String sender, // the optional sender, if missing the sender will be the token's owner.
|
@FormParam("sender") String sender, // the optional sender, if missing the sender will be the token's owner.
|
||||||
@FormParam("body") String body,
|
@FormParam("body") String body,
|
||||||
|
@ -75,71 +48,6 @@ public class Messages {
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.MISSING_PARAMETERS).build();
|
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.MISSING_PARAMETERS).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
return Response.status(Status.METHOD_NOT_ALLOWED).entity(ErrorMessages.DEPRECATED_METHOD).build();
|
||||||
String senderId = caller.getClient().getId();
|
|
||||||
String scope = ScopeProvider.instance.get();
|
|
||||||
|
|
||||||
// check on sender id
|
|
||||||
if(sender == null || sender.isEmpty())
|
|
||||||
logger.info("Sender is going to be the token's owner [" + senderId + "]");
|
|
||||||
else{
|
|
||||||
logger.info("Sender is going to be " + sender);
|
|
||||||
senderId = sender;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the recipients ids (simple check, trim)
|
|
||||||
List<String> recipientsListFiltered = new ArrayList<String>();
|
|
||||||
String[] splittedRecipientsIds = recipientsIds.split(RECIPIENTS_ID_SEPARATOR);
|
|
||||||
List<GenericItemBean> recipientsBeans = new ArrayList<GenericItemBean>();
|
|
||||||
for (String recipientId : splittedRecipientsIds) {
|
|
||||||
try{
|
|
||||||
String tempId = recipientId.trim();
|
|
||||||
if(tempId.isEmpty())
|
|
||||||
continue;
|
|
||||||
GCubeUser userRecipient = uManager.getUserByUsername(tempId);
|
|
||||||
GenericItemBean beanUser = new GenericItemBean(userRecipient.getUsername(), userRecipient.getUsername(), userRecipient.getFullname(), userRecipient.getUserAvatarURL());
|
|
||||||
recipientsBeans.add(beanUser);
|
|
||||||
recipientsListFiltered.add(tempId);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to retrieve recipient information for recipient with id " + recipientId, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(recipientsListFiltered.isEmpty()){
|
|
||||||
logger.error("Missing/wrong request parameters");
|
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.BAD_REQUEST).build();
|
|
||||||
}
|
|
||||||
try{
|
|
||||||
|
|
||||||
logger.info("Trying to send message with body " + body + " subject " + subject + " to user " + recipientsIds + " from " + senderId);
|
|
||||||
|
|
||||||
// sender info
|
|
||||||
GCubeUser senderUser = uManager.getUserByUsername(senderId);
|
|
||||||
|
|
||||||
MessageManagerClient client = AbstractPlugin.messages().build();
|
|
||||||
|
|
||||||
// send message
|
|
||||||
logger.debug("Sending message to " + recipientsListFiltered);
|
|
||||||
String messageId = client.sendMessage(recipientsListFiltered, subject, body, null);
|
|
||||||
|
|
||||||
// send notification
|
|
||||||
logger.debug("Message sent to " + recipientsIds + ". Sending message notification to: " + recipientsIds);
|
|
||||||
SocialNetworkingSite site = SocialNetworkingSiteFinder.getSocialNetworkingSiteFromScope(scope);
|
|
||||||
SocialNetworkingUser user = new SocialNetworkingUser(
|
|
||||||
senderUser.getUsername(), senderUser.getEmail(),
|
|
||||||
senderUser.getFullname(), senderUser.getUserAvatarURL());
|
|
||||||
|
|
||||||
logger.info("SocialNetworkingUser is " + user);
|
|
||||||
|
|
||||||
NotificationsManager nm = new ApplicationNotificationsManager(UserManagerWSBuilder.getInstance().getUserManager(), site, scope, user);
|
|
||||||
new Thread(new MessageNotificationsThread(recipientsBeans, messageId, subject, body, nm)).start();
|
|
||||||
|
|
||||||
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to send message.", e);
|
|
||||||
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response.status(Status.CREATED).build();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,67 +0,0 @@
|
||||||
package org.gcube.portal.social.networking.ws.methods.v1;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.ws.rs.GET;
|
|
||||||
import javax.ws.rs.Path;
|
|
||||||
import javax.ws.rs.Produces;
|
|
||||||
import javax.ws.rs.QueryParam;
|
|
||||||
import javax.ws.rs.core.MediaType;
|
|
||||||
import javax.ws.rs.core.Response;
|
|
||||||
import javax.ws.rs.core.Response.Status;
|
|
||||||
|
|
||||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
|
||||||
import org.gcube.common.authorization.library.utils.Caller;
|
|
||||||
import org.gcube.portal.databook.shared.Notification;
|
|
||||||
import org.gcube.portal.social.networking.ws.utils.CassandraConnection;
|
|
||||||
import org.gcube.portal.social.networking.ws.utils.ErrorMessages;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* REST interface for the social networking library (notifications).
|
|
||||||
* @author Costantino Perciante at ISTI-CNR
|
|
||||||
*/
|
|
||||||
@Path("/notifications")
|
|
||||||
@Deprecated
|
|
||||||
public class Notifications {
|
|
||||||
|
|
||||||
// Logger
|
|
||||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Notifications.class);
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("getRangeNotificationsByUser/")
|
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
|
|
||||||
/**
|
|
||||||
* Retrieves notifications from from up to a given quantity for a given user (to whom the token belongs)
|
|
||||||
* @param from
|
|
||||||
* @param quantity
|
|
||||||
* @param token
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Response getRangeNotificationsByUser(
|
|
||||||
@QueryParam("from") int from,
|
|
||||||
@QueryParam("quantity") int quantity) {
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String username = caller.getClient().getId();
|
|
||||||
|
|
||||||
logger.info("Retrieving " + quantity + " notifications of user = " + username + " from " + from);
|
|
||||||
|
|
||||||
if(from <= 0 || quantity <= 0){
|
|
||||||
logger.error("Missing/wrong request parameters");
|
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.BAD_REQUEST).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Notification> notifications = null;
|
|
||||||
try{
|
|
||||||
notifications = CassandraConnection.getInstance().getDatabookStore().getRangeNotificationsByUser(username, from, quantity);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to retrieve such notifications.", e);
|
|
||||||
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("List of notifications retrieved");
|
|
||||||
return Response.status(Status.OK).entity(notifications).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,436 +0,0 @@
|
||||||
package org.gcube.portal.social.networking.ws.methods.v1;
|
|
||||||
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.ws.rs.Consumes;
|
|
||||||
import javax.ws.rs.DefaultValue;
|
|
||||||
import javax.ws.rs.FormParam;
|
|
||||||
import javax.ws.rs.GET;
|
|
||||||
import javax.ws.rs.POST;
|
|
||||||
import javax.ws.rs.Path;
|
|
||||||
import javax.ws.rs.Produces;
|
|
||||||
import javax.ws.rs.QueryParam;
|
|
||||||
import javax.ws.rs.core.MediaType;
|
|
||||||
import javax.ws.rs.core.Response;
|
|
||||||
import javax.ws.rs.core.Response.Status;
|
|
||||||
|
|
||||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
|
||||||
import org.gcube.common.authorization.library.utils.Caller;
|
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
|
||||||
import org.gcube.portal.databook.shared.ApplicationProfile;
|
|
||||||
import org.gcube.portal.databook.shared.Feed;
|
|
||||||
import org.gcube.portal.databook.shared.FeedType;
|
|
||||||
import org.gcube.portal.social.networking.liferay.ws.GroupManagerWSBuilder;
|
|
||||||
import org.gcube.portal.social.networking.ws.utils.CassandraConnection;
|
|
||||||
import org.gcube.portal.social.networking.ws.utils.ErrorMessages;
|
|
||||||
import org.gcube.portal.social.networking.ws.utils.Filters;
|
|
||||||
import org.gcube.portal.social.networking.ws.utils.SocialUtils;
|
|
||||||
import org.gcube.vomanagement.usermanagement.GroupManager;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* REST interface for the social networking library (feeds).
|
|
||||||
* @author Costantino Perciante at ISTI-CNR
|
|
||||||
*/
|
|
||||||
@Path("/posts")
|
|
||||||
@Deprecated
|
|
||||||
public class Posts {
|
|
||||||
|
|
||||||
// Logger
|
|
||||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Posts.class);
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("getRecentPostsByUserAndDate/")
|
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
|
|
||||||
/**
|
|
||||||
* Retrieves the most recent posts of the token's owner (starting from timeInMillis)
|
|
||||||
* @param timeInMillis
|
|
||||||
* @return Response (OK, BAD REQUEST, ...)
|
|
||||||
*/
|
|
||||||
public Response getRecentPostsByUserAndDate(
|
|
||||||
@QueryParam("time") long timeInMillis) {
|
|
||||||
|
|
||||||
if(timeInMillis < 0){
|
|
||||||
logger.error("Missing/wrong request parameters");
|
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.MISSING_PARAMETERS).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String username = caller.getClient().getId();
|
|
||||||
String context = ScopeProvider.instance.get();
|
|
||||||
List<Feed> feeds = null;
|
|
||||||
|
|
||||||
try{
|
|
||||||
logger.info("Retrieving feeds for user id " + username + " and reference time " + timeInMillis);
|
|
||||||
feeds = CassandraConnection.getInstance().getDatabookStore().getRecentFeedsByUserAndDate(username, timeInMillis);
|
|
||||||
Filters.filterFeedsPerContext(feeds, context);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to retrieve such feeds.", e);
|
|
||||||
return Response.status(Status.NOT_FOUND).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("List retrieved");
|
|
||||||
return Response.status(Status.OK).entity(feeds).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("getAllPostsByUser/")
|
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
|
|
||||||
/**
|
|
||||||
* Retrieves all posts belong to token's owner
|
|
||||||
* @return Response (OK, BAD REQUEST, ...)
|
|
||||||
*/
|
|
||||||
public Response getAllPostsByUser() {
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String username = caller.getClient().getId();
|
|
||||||
String context = ScopeProvider.instance.get();
|
|
||||||
|
|
||||||
List<Feed> feeds = null;
|
|
||||||
try{
|
|
||||||
logger.info("Retrieving feeds for user with id " + username);
|
|
||||||
feeds = CassandraConnection.getInstance().getDatabookStore().getAllFeedsByUser(username);
|
|
||||||
Filters.filterFeedsPerContext(feeds, context);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to retrieve such feeds.", e);
|
|
||||||
return Response.status(Status.NOT_FOUND).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("List retrieved");
|
|
||||||
return Response.status(Status.OK).entity(feeds).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("getRecentPostsByUser/")
|
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
|
|
||||||
/**
|
|
||||||
* Retrieves the last quantity posts belonging to token's owner
|
|
||||||
* @param quantity
|
|
||||||
* @param token
|
|
||||||
* @return Response (OK, BAD REQUEST, ...)
|
|
||||||
*/
|
|
||||||
public Response getRecentPostsByUser(
|
|
||||||
@DefaultValue("10") @QueryParam("quantity") int quantity) {
|
|
||||||
|
|
||||||
if(quantity < 0){
|
|
||||||
logger.error("Missing/wrong request parameters");
|
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.MISSING_PARAMETERS).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String username = caller.getClient().getId();
|
|
||||||
String context = ScopeProvider.instance.get();
|
|
||||||
List<Feed> feeds = null;
|
|
||||||
|
|
||||||
// if quantity is zero, just return an empty list
|
|
||||||
if(quantity == 0){
|
|
||||||
return Response.status(Status.OK).entity(feeds).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
try{
|
|
||||||
logger.info("Retrieving last " + quantity + " feeds made by user " + username);
|
|
||||||
feeds = CassandraConnection.getInstance().getDatabookStore().getRecentFeedsByUser(username, -1);
|
|
||||||
Filters.filterFeedsPerContext(feeds, context);
|
|
||||||
feeds = feeds.subList(0, quantity);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to retrieve such feeds.", e);
|
|
||||||
return Response.status(Status.NOT_FOUND).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("List retrieved");
|
|
||||||
return Response.status(Status.OK).entity(feeds).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@POST
|
|
||||||
@Consumes("application/x-www-form-urlencoded")
|
|
||||||
@Path("writePostUser")
|
|
||||||
@Produces(MediaType.TEXT_PLAIN)
|
|
||||||
/**
|
|
||||||
* Allows a user to write post
|
|
||||||
* @param feedText
|
|
||||||
* @param previewTitle
|
|
||||||
* @param previewDescription
|
|
||||||
* @param previewHost
|
|
||||||
* @param previewUrl
|
|
||||||
* @param httpImageUrl
|
|
||||||
* @param enableNotification
|
|
||||||
* @param token
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Response writePostUser(
|
|
||||||
@FormParam("text") String feedText,
|
|
||||||
@FormParam("previewtitle")String previewTitle,
|
|
||||||
@FormParam("previewdescription") String previewDescription,
|
|
||||||
@FormParam("previewhost") String previewHost,
|
|
||||||
@FormParam("previewurl") String previewUrl,
|
|
||||||
@FormParam("httpimageurl") String httpImageUrl,
|
|
||||||
@DefaultValue("false") @FormParam("enablenotification") String enableNotification
|
|
||||||
){
|
|
||||||
|
|
||||||
logger.info("Request of writing a feed coming from user");
|
|
||||||
|
|
||||||
// at least the feedText is necessary to write a feed (the token is needed anyway)
|
|
||||||
if(feedText == null || feedText.isEmpty()){
|
|
||||||
|
|
||||||
logger.error("Missing request parameters");
|
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.MISSING_PARAMETERS).build();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String username = caller.getClient().getId();
|
|
||||||
String context = ScopeProvider.instance.get();
|
|
||||||
|
|
||||||
try{
|
|
||||||
// check it is a VRE
|
|
||||||
GroupManager groupManager = GroupManagerWSBuilder.getInstance().getGroupManager();
|
|
||||||
long groupId = groupManager.getGroupIdFromInfrastructureScope(context);
|
|
||||||
boolean isVRE = groupManager.isVRE(groupId);
|
|
||||||
if(!isVRE){
|
|
||||||
logger.error("A post cannot be written into a context that is not a VRE");
|
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.POST_OUTSIDE_VRE).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert enablenotification parameter
|
|
||||||
boolean notifyGroup = enableNotification.equals("true");
|
|
||||||
if(notifyGroup)
|
|
||||||
logger.info("Enable notification for this user post.");
|
|
||||||
else
|
|
||||||
logger.info("Disable notification for this user post.");
|
|
||||||
|
|
||||||
|
|
||||||
// try to share
|
|
||||||
logger.debug("Trying to share user feed...");
|
|
||||||
Feed res = SocialUtils.shareUserUpdate(
|
|
||||||
username,
|
|
||||||
feedText,
|
|
||||||
ScopeProvider.instance.get(),
|
|
||||||
previewTitle,
|
|
||||||
previewDescription,
|
|
||||||
previewHost,
|
|
||||||
previewUrl,
|
|
||||||
httpImageUrl,
|
|
||||||
notifyGroup
|
|
||||||
);
|
|
||||||
|
|
||||||
if(res != null){
|
|
||||||
logger.info("Feed correctly written by user " + username);
|
|
||||||
return Response.status(Status.CREATED).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Feed not written by user " + username, e);
|
|
||||||
}
|
|
||||||
logger.info("Feed not written by user " + username);
|
|
||||||
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("getAllPostsByApp/")
|
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
|
|
||||||
/**
|
|
||||||
* Retrieves all application's posts.
|
|
||||||
* @param token
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Response getAllPostsByApp() {
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String appId = caller.getClient().getId();
|
|
||||||
String context = ScopeProvider.instance.get();
|
|
||||||
// check if the token actually matches an application
|
|
||||||
ApplicationProfile appProfile = SocialUtils.getProfileFromInfrastrucure(appId, ScopeProvider.instance.get());
|
|
||||||
if(appProfile == null){
|
|
||||||
logger.error("The given token is not belonging to an application!!!");
|
|
||||||
return Response.status(Status.FORBIDDEN).entity(ErrorMessages.NOT_APP_TOKEN).build();
|
|
||||||
}
|
|
||||||
List<Feed> feeds = null;
|
|
||||||
try{
|
|
||||||
logger.info("Retrieving feeds for app with id " + appId);
|
|
||||||
feeds = CassandraConnection.getInstance().getDatabookStore().getAllFeedsByApp(appId);
|
|
||||||
Filters.filterFeedsPerContext(feeds, context);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to retrieve such feeds.", e);
|
|
||||||
return Response.status(Status.NOT_FOUND).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("List retrieved");
|
|
||||||
return Response.status(Status.OK).entity(feeds).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@POST
|
|
||||||
@Consumes("application/x-www-form-urlencoded")
|
|
||||||
@Path("writePostApplication")
|
|
||||||
@Produces(MediaType.TEXT_PLAIN)
|
|
||||||
/**
|
|
||||||
* Allows an application to write a post.
|
|
||||||
* @param feedText
|
|
||||||
* @param uriParams
|
|
||||||
* @param previewTitle
|
|
||||||
* @param previewDescription
|
|
||||||
* @param httpImageUrl
|
|
||||||
* @param enableNotification
|
|
||||||
* @param token
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Response writePostApp(
|
|
||||||
@FormParam("text") String feedText,
|
|
||||||
@FormParam("params") String uriParams,
|
|
||||||
@FormParam("previewtitle")String previewTitle,
|
|
||||||
@FormParam("previewdescription") String previewDescription,
|
|
||||||
@FormParam("httpimageurl") String httpImageUrl,
|
|
||||||
@DefaultValue("false") @FormParam("enablenotification") String enableNotification
|
|
||||||
){
|
|
||||||
|
|
||||||
logger.info("Request of writing a feed coming from an application.");
|
|
||||||
|
|
||||||
// at least the feedText is necessary to write a feed (the token is needed anyway)
|
|
||||||
if(feedText == null || feedText.isEmpty()){
|
|
||||||
logger.error("Missing request parameters");
|
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.MISSING_PARAMETERS).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String appId = caller.getClient().getId();
|
|
||||||
String context = ScopeProvider.instance.get();
|
|
||||||
try{
|
|
||||||
// check it is a VRE
|
|
||||||
GroupManager groupManager = GroupManagerWSBuilder.getInstance().getGroupManager();
|
|
||||||
long groupId = groupManager.getGroupIdFromInfrastructureScope(context);
|
|
||||||
boolean isVRE = groupManager.isVRE(groupId);
|
|
||||||
if(!isVRE){
|
|
||||||
logger.error("A post cannot be written into a context that is not a VRE");
|
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.POST_OUTSIDE_VRE).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if the token actually matches an application profile
|
|
||||||
ApplicationProfile appProfile = SocialUtils.getProfileFromInfrastrucure(appId, ScopeProvider.instance.get());
|
|
||||||
if(appProfile == null){
|
|
||||||
logger.error("The given token doesn't belong to an application!!!");
|
|
||||||
return Response.status(Status.FORBIDDEN).entity(ErrorMessages.NOT_APP_TOKEN).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert enablenotification parameter
|
|
||||||
boolean notifyGroup = enableNotification.equals("true");
|
|
||||||
if(notifyGroup)
|
|
||||||
logger.info("Enable notification for this application post.");
|
|
||||||
else
|
|
||||||
logger.info("Disable notification for this application post.");
|
|
||||||
|
|
||||||
// write feed + notification if it is the case
|
|
||||||
Feed written = SocialUtils.shareApplicationUpdate(
|
|
||||||
feedText,
|
|
||||||
uriParams,
|
|
||||||
previewTitle,
|
|
||||||
previewDescription,
|
|
||||||
httpImageUrl,
|
|
||||||
appProfile,
|
|
||||||
caller,
|
|
||||||
notifyGroup
|
|
||||||
);
|
|
||||||
if(written != null){
|
|
||||||
return Response.status(Status.CREATED).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Error while writing a post", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("Feed not written by application " + appId);
|
|
||||||
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("getAllPostsByVRE/")
|
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
|
|
||||||
/**
|
|
||||||
* Retrieve all posts for this vre
|
|
||||||
* @param token a user token associated with a given vre(scope)
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Response getAllPostsByVRE() {
|
|
||||||
|
|
||||||
String scope = ScopeProvider.instance.get();
|
|
||||||
|
|
||||||
logger.info("Retrieving all posts coming from vre = " + scope);
|
|
||||||
|
|
||||||
List<Feed> feeds = null;
|
|
||||||
try{
|
|
||||||
feeds = CassandraConnection.getInstance().getDatabookStore().getAllFeedsByVRE(scope);
|
|
||||||
Iterator<Feed> it = feeds.iterator();
|
|
||||||
|
|
||||||
// remove disabled feeds
|
|
||||||
while (it.hasNext()) {
|
|
||||||
Feed f = it.next();
|
|
||||||
|
|
||||||
if(f.getType() == FeedType.DISABLED)
|
|
||||||
it.remove();
|
|
||||||
|
|
||||||
}
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to retrieve feeds for vre = " + scope, e);
|
|
||||||
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("List of feeds of vre = " + scope + " retrieved");
|
|
||||||
return Response.status(Status.OK).entity(feeds).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("getAllLikedPostIdsByUser/")
|
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
|
|
||||||
/**
|
|
||||||
* Retrieves all liked posts ids relates to token's owner
|
|
||||||
* @param token
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Response getAllLikedPostIdsByUser() {
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String username = caller.getClient().getId();
|
|
||||||
|
|
||||||
logger.info("Retrieving all liked feeds IDS for user with id " + username);
|
|
||||||
|
|
||||||
List<String> retrievedLikedFeeds = null;
|
|
||||||
try{
|
|
||||||
retrievedLikedFeeds = CassandraConnection.getInstance().getDatabookStore().getAllLikedFeedIdsByUser(username);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to read such ids of liked feeds.", e);
|
|
||||||
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("Ids of liked feeds by " + username + " retrieved");
|
|
||||||
return Response.status(Status.OK).entity(retrievedLikedFeeds).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("getAllLikedPostsByUser/")
|
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
|
|
||||||
/**
|
|
||||||
* Returns all liked feeds of token's owner
|
|
||||||
* @param limit
|
|
||||||
* @param token
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Response getAllLikedPostsByUser(@DefaultValue("10") @QueryParam("limit") int limit) {
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String username = caller.getClient().getId();
|
|
||||||
|
|
||||||
logger.info("Retrieving " + limit + " liked feeds for user with id " + username);
|
|
||||||
|
|
||||||
List<Feed> retrievedLikedFeeds = null;
|
|
||||||
try{
|
|
||||||
retrievedLikedFeeds = CassandraConnection.getInstance().getDatabookStore().getAllLikedFeedsByUser(username, limit);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to read such liked feeds.", e);
|
|
||||||
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("Liked feeds by " + username + " retrieved");
|
|
||||||
return Response.status(Status.OK).entity(retrievedLikedFeeds).build();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,78 +0,0 @@
|
||||||
package org.gcube.portal.social.networking.ws.methods.v1;
|
|
||||||
|
|
||||||
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.ws.rs.FormParam;
|
|
||||||
import javax.ws.rs.POST;
|
|
||||||
import javax.ws.rs.Path;
|
|
||||||
import javax.ws.rs.Produces;
|
|
||||||
import javax.ws.rs.core.MediaType;
|
|
||||||
import javax.ws.rs.core.Response;
|
|
||||||
import javax.ws.rs.core.Response.Status;
|
|
||||||
|
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
|
||||||
import org.gcube.portal.databook.shared.ApplicationProfile;
|
|
||||||
import org.gcube.portal.social.networking.ws.utils.ErrorMessages;
|
|
||||||
import org.gcube.portal.social.networking.ws.utils.SocialUtils;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* REST interface for the social networking library (tokens).
|
|
||||||
* @author Costantino Perciante at ISTI-CNR
|
|
||||||
*/
|
|
||||||
@Path("/tokens")
|
|
||||||
@Deprecated
|
|
||||||
public class Tokens {
|
|
||||||
|
|
||||||
private final static String DEFAULT_ROLE = "OrganizationMember";
|
|
||||||
|
|
||||||
// Logger
|
|
||||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Tokens.class);
|
|
||||||
|
|
||||||
@POST
|
|
||||||
@Path("generateApplicationToken/")
|
|
||||||
@Produces(MediaType.TEXT_PLAIN)
|
|
||||||
/**
|
|
||||||
* Allows the owner of the token token to generate a new token for the couple (appId, vre)
|
|
||||||
* where vre is the same associated to the original user token.
|
|
||||||
* @param appId
|
|
||||||
* @param token
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Response generateApplicationToken(@FormParam("appid") String appId){
|
|
||||||
|
|
||||||
logger.info("Incoming request for app token generation. Appid id is " + appId);
|
|
||||||
|
|
||||||
//user scope
|
|
||||||
String userScope = ScopeProvider.instance.get();
|
|
||||||
|
|
||||||
// check if an application profile exists for this appId/scope (the discovery of the service will be made on the root)
|
|
||||||
ApplicationProfile appProfile = SocialUtils.getProfileFromInfrastrucure(appId, userScope);
|
|
||||||
|
|
||||||
if(appProfile == null){
|
|
||||||
logger.error("There is no application profile for this application id and scope!");
|
|
||||||
return Response.status(Status.FORBIDDEN).entity(ErrorMessages.NO_APP_PROFILE_FOUND).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("Generating token for the application with id " + appId);
|
|
||||||
|
|
||||||
List<String> roles = new ArrayList<>();
|
|
||||||
roles.add(DEFAULT_ROLE);
|
|
||||||
|
|
||||||
String appToken = null;
|
|
||||||
try {
|
|
||||||
// each token is related to an identifier and the context
|
|
||||||
appToken = authorizationService().generateExternalServiceToken(appId);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Unable to generate token for app " + appId + " and scope " + userScope);
|
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.TOKEN_GENERATION_APP_FAILED).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response.status(Status.CREATED).entity(appToken).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -18,146 +18,23 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST interface for the social networking library (users).
|
* REST interface for the social networking library (users).
|
||||||
* @author Costantino Perciante at ISTI-CNR
|
|
||||||
*/
|
*/
|
||||||
@Path("/users")
|
@Path("/users")
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public class Users {
|
public class Users {
|
||||||
|
|
||||||
// Logger
|
|
||||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Users.class);
|
|
||||||
|
|
||||||
private static final String NOT_USER_TOKEN_CONTEXT_USED = "User's information can only be retrieved through a user token (not qualified)";
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("readCustomAttr/")
|
|
||||||
@Produces(MediaType.TEXT_PLAIN)
|
|
||||||
/**
|
|
||||||
* A wrapper for the user management library 's readCustomAttr method
|
|
||||||
* @return Response (OK, BAD REQUEST, ...)
|
|
||||||
*/
|
|
||||||
public Response readCustomAttr(
|
|
||||||
@QueryParam("attribute") String attributeKey
|
|
||||||
) {
|
|
||||||
|
|
||||||
if(attributeKey == null || attributeKey.isEmpty()){
|
|
||||||
logger.error("Missing/wrong request parameters");
|
|
||||||
return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.MISSING_PARAMETERS).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String username = caller.getClient().getId();
|
|
||||||
String toReturn;
|
|
||||||
if(!TokensUtils.isUserTokenDefault(caller)){
|
|
||||||
logger.warn(NOT_USER_TOKEN_CONTEXT_USED);
|
|
||||||
return Response.status(Status.FORBIDDEN).entity("User's information can only be retrieved through a user token").build();
|
|
||||||
}else{
|
|
||||||
try{
|
|
||||||
GCubeUser user = UserManagerWSBuilder.getInstance().getUserManager().getUserByUsername(username);
|
|
||||||
toReturn = (String)UserManagerWSBuilder.getInstance().getUserManager().readCustomAttr(user.getUserId(), attributeKey);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to retrieve attribute for user.", e);
|
|
||||||
return Response.status(Status.NOT_FOUND).build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
logger.info("Attribute " + attributeKey + " retrieved for user " + username);
|
|
||||||
return Response.status(Status.OK).entity(toReturn).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
// @PUT
|
|
||||||
// @Path("updateCustomAttr")
|
|
||||||
// @Produces(MediaType.TEXT_PLAIN)
|
|
||||||
// /**
|
|
||||||
// * A wrapper for the user management library 's saveCustomAttr method
|
|
||||||
// * @return
|
|
||||||
// */
|
|
||||||
// public Response updateCustomAttr(
|
|
||||||
// @FormParam("attribute") String attributeKey,
|
|
||||||
// @FormParam("value") String newValue
|
|
||||||
// ){
|
|
||||||
//
|
|
||||||
// if(attributeKey == null || attributeKey.isEmpty() || newValue == null){
|
|
||||||
//
|
|
||||||
// logger.error("Missing/wrong request parameters");
|
|
||||||
// return Response.status(Status.BAD_REQUEST).entity(ErrorMessages.missingParameters).build();
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
// String username = caller.getClient().getId();
|
|
||||||
//
|
|
||||||
// try{
|
|
||||||
//
|
|
||||||
// GCubeUser user = userManager.getUserByUsername(username);
|
|
||||||
// userManager.saveCustomAttr(user.getUserId(), attributeKey, newValue);
|
|
||||||
//
|
|
||||||
// }catch(Exception e){
|
|
||||||
//
|
|
||||||
// logger.error("Unable to set attribute for user.", e);
|
|
||||||
// return Response.status(Status.NOT_MODIFIED).build();
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return Response.status(Status.OK).build();
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("getUserFullname")
|
@Path("getUserFullname")
|
||||||
@Produces(MediaType.TEXT_PLAIN)
|
@Produces(MediaType.TEXT_PLAIN)
|
||||||
/**
|
@Deprecated
|
||||||
* Retrieve user's fullname
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Response getUserUsername(){
|
public Response getUserUsername(){
|
||||||
|
return Response.status(Status.METHOD_NOT_ALLOWED).entity(ErrorMessages.DEPRECATED_METHOD).build();
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String username = caller.getClient().getId();
|
|
||||||
String toReturn = null;
|
|
||||||
if(!TokensUtils.isUserTokenDefault(caller)){
|
|
||||||
logger.warn(NOT_USER_TOKEN_CONTEXT_USED);
|
|
||||||
return Response.status(Status.FORBIDDEN).entity("User's information can only be retrieved through a user token").build();
|
|
||||||
}else{
|
|
||||||
try{
|
|
||||||
GCubeUser user = UserManagerWSBuilder.getInstance().getUserManager().getUserByUsername(username);
|
|
||||||
toReturn = user.getFullname();
|
|
||||||
logger.info("Found fullname " + toReturn + " for user " + username);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Unable to retrieve attribute for user.", e);
|
|
||||||
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.toString()).build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Response.status(Status.OK).entity(toReturn).build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("getUserEmail")
|
@Path("getUserEmail")
|
||||||
@Produces(MediaType.TEXT_PLAIN)
|
@Produces(MediaType.TEXT_PLAIN)
|
||||||
/**
|
@Deprecated
|
||||||
* Retrieve user's email
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Response getUserEmail(){
|
public Response getUserEmail(){
|
||||||
|
return Response.status(Status.METHOD_NOT_ALLOWED).entity(ErrorMessages.DEPRECATED_METHOD).build();
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
|
||||||
String username = caller.getClient().getId();
|
|
||||||
String toReturn = null;
|
|
||||||
if(!TokensUtils.isUserTokenDefault(caller)){
|
|
||||||
logger.warn(NOT_USER_TOKEN_CONTEXT_USED);
|
|
||||||
return Response.status(Status.FORBIDDEN).entity("User's information can only be retrieved through a user token").build();
|
|
||||||
}else{
|
|
||||||
try{
|
|
||||||
|
|
||||||
GCubeUser user = UserManagerWSBuilder.getInstance().getUserManager().getUserByUsername(username);
|
|
||||||
toReturn = user.getEmail();
|
|
||||||
logger.info("Found email " + toReturn + " for user " + username);
|
|
||||||
|
|
||||||
}catch(Exception e){
|
|
||||||
|
|
||||||
logger.error("Unable to retrieve attribute for user.", e);
|
|
||||||
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.toString()).build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Response.status(Status.OK).entity(toReturn).build();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -211,10 +211,10 @@ public class Users {
|
||||||
ResponseBean responseBean = new ResponseBean();
|
ResponseBean responseBean = new ResponseBean();
|
||||||
Status status = Status.OK;
|
Status status = Status.OK;
|
||||||
|
|
||||||
if(!TokensUtils.isUserTokenDefault(caller)){
|
if(!TokensUtils.isUserToken(caller)){
|
||||||
status = Status.FORBIDDEN;
|
status = Status.FORBIDDEN;
|
||||||
responseBean.setMessage(NOT_USER_TOKEN_CONTEXT_USED);
|
responseBean.setMessage("User's information can only be retrieved through a user Jtoken");
|
||||||
logger.warn("Trying to access users method via a token different than USER is not allowed");
|
logger.warn("Trying to access users method via a token different than 'user-token' is not allowed");
|
||||||
}else{
|
}else{
|
||||||
try{
|
try{
|
||||||
UserManager userManager = UserManagerWSBuilder.getInstance().getUserManager();
|
UserManager userManager = UserManagerWSBuilder.getInstance().getUserManager();
|
||||||
|
|
|
@ -17,4 +17,5 @@ public class ErrorMessages {
|
||||||
public static final String BAD_REQUEST = "Please check the parameter you passed, it seems a bad request";
|
public static final String BAD_REQUEST = "Please check the parameter you passed, it seems a bad request";
|
||||||
public static final String ERROR_IN_API_RESULT = "The error is reported into the 'message' field of the returned object";
|
public static final String ERROR_IN_API_RESULT = "The error is reported into the 'message' field of the returned object";
|
||||||
public static final String POST_OUTSIDE_VRE = "A post cannot be written into a context that is not a VRE";
|
public static final String POST_OUTSIDE_VRE = "A post cannot be written into a context that is not a VRE";
|
||||||
|
public static final String DEPRECATED_METHOD = "This method is deprecated, must use version 2";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue