added partial support for catalogue notifications

This commit is contained in:
Massimiliano Assante 2022-05-06 17:15:24 +02:00
parent 5111439f3e
commit f7d2a1e569
2 changed files with 65 additions and 1 deletions

View File

@ -1,5 +1,6 @@
package org.gcube.portal.social.networking.ws.methods.v2;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@ -30,6 +31,7 @@ import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.portal.databook.shared.Notification;
import org.gcube.portal.databook.shared.NotificationType;
import org.gcube.portal.databook.shared.RunningJob;
import org.gcube.portal.notifications.bean.GenericItemBean;
import org.gcube.portal.notifications.thread.JobStatusNotificationThread;
@ -44,6 +46,8 @@ import org.gcube.portal.social.networking.ws.outputs.ResponseBean;
import org.gcube.portal.social.networking.ws.utils.CassandraConnection;
import org.gcube.portal.social.networking.ws.utils.ErrorMessages;
import org.gcube.social_networking.socialnetworking.model.beans.JobNotificationBean;
import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEvent;
import org.gcube.social_networking.socialnetworking.model.beans.workspace.WorkspaceEvent;
import org.gcube.social_networking.socialnetworking.model.beans.workspace.AddedItemEvent;
import org.gcube.social_networking.socialnetworking.model.beans.workspace.DeletedItemEvent;
@ -180,6 +184,66 @@ public class Notifications {
return Response.status(status).entity(responseBean).build();
}
/**
* Send a Catalogue notification to a given user
* @param event
* @return
* @throws ValidationException
*/
@POST
@Path("catalogue/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@StatusCodes ({
@ResponseCode ( code = 200, condition = "Catalogue Notification is sent correctly"),
@ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT)
})
public Response catalogue(
@NotNull(message="input is missing")
@Valid
CatalogueEvent event) throws ValidationException{
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
String username = caller.getClient().getId();
logger.debug("catalogue notifications from user = " + username);
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
boolean deliveryResult = false;
try {
logger.debug("catalogue notifications type is " + event.getType());
SocialNetworkingSite site = SocialNetworkingSiteFinder.getSocialNetworkingSiteFromScope(context);
GCubeUser senderUser = UserManagerWSBuilder.getInstance().getUserManager().getUserByUsername(username);
SocialNetworkingUser user = new SocialNetworkingUser(senderUser.getUsername(), senderUser.getEmail(), senderUser.getFullname(), senderUser.getUserAvatarURL());
NotificationsManager nm = new ApplicationNotificationsManager(UserManagerWSBuilder.getInstance().getUserManager(), site, context, user);
String[] idsToNotify = event.getIdsToNotify();
if (! event.idsAsGroup()) {
for (int i = 0; i < idsToNotify.length; i++) {
String userIdToNotify = idsToNotify[i];
deliveryResult = nm.notifyCatalogueEvent(NotificationType.CAT_ITEM_PUBLISHED, userIdToNotify, event.getItemId(), event.getNotifyText(), event.getItemURL());
}
}
} catch(Exception e){
logger.error("Unable to send job notification", e);
responseBean.setSuccess(false);
responseBean.setMessage(e.getMessage());
status = Status.INTERNAL_SERVER_ERROR;
}
logger.debug("catalogue notifications should have been sent");
if (deliveryResult) {
responseBean.setSuccess(true);
responseBean.setMessage("catalogue notification delivered correctly");
responseBean.setResult(new Boolean(true));
} else {
responseBean.setSuccess(false);
responseBean.setMessage("An error occurred between this service and Cassandra DB, notification not delivered correctly");
responseBean.setResult(new Boolean(false));
}
return Response.status(status).entity(responseBean).build();
}
/**
* Send a Workspace notification to a given user

View File

@ -280,7 +280,7 @@ public class Users {
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
UserProfileExtendedWithVerifiedEmail userWithEmailVerified = null;
if(! (TokensUtils.isUserTokenDefault(caller) || TokensUtils.isUserTokenQualified(caller))){
if(!TokensUtils.isUserToken(caller)){
status = Status.FORBIDDEN;
responseBean.setMessage(NOT_USER_TOKEN_CONTEXT_USED);
logger.warn("Trying to access users method via a token different than USER is not allowed");