2019-04-05 16:39:46 +02:00
package org.gcube.portal.social.networking.ws.methods.v2 ;
2023-09-29 11:32:16 +02:00
import java.io.IOException ;
2023-11-21 18:38:57 +01:00
import java.util.* ;
2019-04-05 16:39:46 +02:00
import javax.validation.Valid ;
import javax.validation.ValidationException ;
import javax.validation.constraints.Min ;
import javax.validation.constraints.NotNull ;
import javax.ws.rs.Consumes ;
import javax.ws.rs.DefaultValue ;
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 ;
2023-11-21 18:38:57 +01:00
import javax.ws.rs.core.GenericType ;
2019-04-05 16:39:46 +02:00
import javax.ws.rs.core.MediaType ;
import javax.ws.rs.core.Response ;
import javax.ws.rs.core.Response.Status ;
2023-11-21 18:38:57 +01:00
import org.apache.commons.lang.Validate ;
2019-04-05 16:39:46 +02:00
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 ;
2022-09-14 16:26:17 +02:00
import org.gcube.common.authorization.control.annotations.AuthorizationControl ;
2019-04-05 16:39:46 +02:00
import org.gcube.common.authorization.library.provider.AuthorizationProvider ;
2022-10-19 15:54:45 +02:00
import org.gcube.common.authorization.library.provider.SecurityTokenProvider ;
2019-04-05 16:39:46 +02:00
import org.gcube.common.authorization.library.utils.Caller ;
import org.gcube.common.scope.api.ScopeProvider ;
2022-05-05 11:04:31 +02:00
import org.gcube.common.scope.impl.ScopeBean ;
2023-11-21 18:38:57 +01:00
import org.gcube.portal.databook.shared.* ;
2019-04-05 16:39:46 +02:00
import org.gcube.portal.notifications.bean.GenericItemBean ;
import org.gcube.portal.notifications.thread.JobStatusNotificationThread ;
import org.gcube.portal.social.networking.caches.SocialNetworkingSiteFinder ;
2022-05-05 11:04:31 +02:00
import org.gcube.portal.social.networking.caches.UsersCache ;
import org.gcube.portal.social.networking.liferay.ws.GroupManagerWSBuilder ;
2019-04-05 16:39:46 +02:00
import org.gcube.portal.social.networking.liferay.ws.LiferayJSONWsCredentials ;
import org.gcube.portal.social.networking.liferay.ws.UserManagerWSBuilder ;
2022-09-14 16:26:17 +02:00
import org.gcube.portal.social.networking.ws.ex.AuthException ;
2022-05-09 10:31:20 +02:00
import org.gcube.portal.social.networking.ws.mappers.CatalogueEventTypeMapper ;
2022-04-27 18:29:33 +02:00
import org.gcube.portal.social.networking.ws.mappers.JobMapper ;
2022-04-21 17:50:48 +02:00
import org.gcube.portal.social.networking.ws.mappers.WorkspaceItemMapper ;
2019-04-05 16:39:46 +02:00
import org.gcube.portal.social.networking.ws.outputs.ResponseBean ;
import org.gcube.portal.social.networking.ws.utils.CassandraConnection ;
2020-12-01 18:10:15 +01:00
import org.gcube.portal.social.networking.ws.utils.ErrorMessages ;
2022-10-19 15:54:45 +02:00
import org.gcube.portal.social.networking.ws.utils.TokensUtils ;
2022-04-27 18:29:33 +02:00
import org.gcube.social_networking.socialnetworking.model.beans.JobNotificationBean ;
2022-05-06 17:15:24 +02:00
import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEvent ;
2022-05-04 11:28:38 +02:00
import org.gcube.social_networking.socialnetworking.model.beans.workspace.AddedItemEvent ;
2022-05-05 13:38:47 +02:00
import org.gcube.social_networking.socialnetworking.model.beans.workspace.DeletedItemEvent ;
import org.gcube.social_networking.socialnetworking.model.beans.workspace.FolderAddedUserEvent ;
2022-05-05 14:32:25 +02:00
import org.gcube.social_networking.socialnetworking.model.beans.workspace.FolderAdminDowngradeEvent ;
import org.gcube.social_networking.socialnetworking.model.beans.workspace.FolderAdminUpgradeEvent ;
2022-05-05 13:38:47 +02:00
import org.gcube.social_networking.socialnetworking.model.beans.workspace.FolderRemovedUserEvent ;
2022-05-04 16:32:48 +02:00
import org.gcube.social_networking.socialnetworking.model.beans.workspace.RenamedFolderEvent ;
2022-05-04 11:28:38 +02:00
import org.gcube.social_networking.socialnetworking.model.beans.workspace.SharedFolderEvent ;
import org.gcube.social_networking.socialnetworking.model.beans.workspace.UnsharedFolderEvent ;
2022-05-05 13:38:47 +02:00
import org.gcube.social_networking.socialnetworking.model.beans.workspace.UpdatedItemEvent ;
2023-09-29 18:07:47 +02:00
import org.gcube.social_networking.socialnetworking.model.beans.workspace.WorkspaceEvent ;
2022-05-05 11:04:31 +02:00
import org.gcube.vomanagement.usermanagement.GroupManager ;
import org.gcube.vomanagement.usermanagement.UserManager ;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException ;
2022-10-19 15:54:45 +02:00
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault ;
2019-04-05 16:39:46 +02:00
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2022-09-16 14:33:54 +02:00
import org.mortbay.log.Log ;
2019-04-05 16:39:46 +02:00
import org.slf4j.LoggerFactory ;
2022-04-07 18:11:56 +02:00
import com.webcohesion.enunciate.metadata.rs.RequestHeader ;
import com.webcohesion.enunciate.metadata.rs.RequestHeaders ;
2022-04-07 16:25:26 +02:00
import com.webcohesion.enunciate.metadata.rs.ResponseCode ;
import com.webcohesion.enunciate.metadata.rs.StatusCodes ;
2019-04-05 16:39:46 +02:00
/ * *
* REST interface for the social networking library ( notifications ) .
* /
@Path ( " 2/notifications " )
2022-04-07 18:11:56 +02:00
@RequestHeaders ( {
2022-04-21 17:50:48 +02:00
@RequestHeader ( name = " Authorization " , description = " Bearer token, see https://dev.d4science.org/how-to-access-resources " ) ,
@RequestHeader ( name = " Content-Type " , description = " application/json " )
} )
2019-04-05 16:39:46 +02:00
public class Notifications {
// Logger
private static final org . slf4j . Logger logger = LoggerFactory . getLogger ( Notifications . class ) ;
2022-09-14 16:26:17 +02:00
private static final String INFRASTRUCTURE_MANAGER_ROLE = " Infrastructure-Manager " ;
2019-04-05 16:39:46 +02:00
2022-04-07 16:25:26 +02:00
/ * *
2022-10-25 18:12:29 +02:00
* Retrieve notifications of the token ' s owner
2022-04-07 16:25:26 +02:00
* @param from must be greater or equal to 1 , range [ 0 , infinity ]
* @param quantity quantity must be greater or equal to 0
* @return notifications up to quantity
* @throws ValidationException
* /
2022-10-25 18:12:29 +02:00
@GET
@Path ( " get-range-notifications/ " )
@Produces ( MediaType . APPLICATION_JSON )
2022-04-07 16:25:26 +02:00
@StatusCodes ( {
@ResponseCode ( code = 200 , condition = " Notifications retrieved and reported in the 'result' field of the returned object " ) ,
@ResponseCode ( code = 500 , condition = ErrorMessages . ERROR_IN_API_RESULT )
} )
2019-04-05 16:39:46 +02:00
public Response getRangeNotifications (
@DefaultValue ( " 1 " ) @QueryParam ( " from " ) @Min ( value = 1 , message = " from must be greater or equal to 1 " )
int from ,
@DefaultValue ( " 10 " ) @QueryParam ( " quantity " ) @Min ( value = 0 , message = " quantity must be greater or equal to 0 " )
int quantity
) throws ValidationException {
Caller caller = AuthorizationProvider . instance . get ( ) ;
String username = caller . getClient ( ) . getId ( ) ;
logger . debug ( " Retrieving " + quantity + " notifications of user = " + username + " from " + from ) ;
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
List < Notification > notifications = null ;
try {
notifications = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . getRangeNotificationsByUser ( username , from , quantity ) ;
responseBean . setResult ( notifications ) ;
responseBean . setSuccess ( true ) ;
logger . debug ( " List of notifications retrieved " ) ;
} catch ( Exception e ) {
logger . error ( " Unable to retrieve such notifications. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
2022-09-16 14:33:54 +02:00
2022-09-16 12:14:11 +02:00
/ * *
2022-10-25 18:12:29 +02:00
* Return whether the notifications for this user are enabled or not
2022-09-16 12:14:11 +02:00
* @pathExample / is - user - disabled ? username = john . smith
* @responseExample application / json { " success " : true , " message " : null " result " : true }
2022-09-16 11:46:41 +02:00
* @param username the username you want to check
* @return true if the notification for the user are disabled ( Catalogue and Workspace ones )
2022-09-16 12:14:11 +02:00
*
2022-09-14 16:26:17 +02:00
* /
2022-10-25 18:12:29 +02:00
@GET
@Path ( " is-user-disabled/ " )
2022-09-16 12:14:11 +02:00
@Produces ( MediaType . APPLICATION_JSON )
2022-09-14 16:26:17 +02:00
@StatusCodes ( {
2022-09-16 12:14:11 +02:00
@ResponseCode ( code = 200 , condition = " true if the notification for the username given as query param are disabled (Catalogue and Workspace ones), false otherwise " ) ,
2022-09-14 16:26:17 +02:00
@ResponseCode ( code = 500 , condition = ErrorMessages . ERROR_IN_API_RESULT )
} )
@AuthorizationControl ( allowedRoles = { INFRASTRUCTURE_MANAGER_ROLE } , exception = AuthException . class )
2022-09-16 08:56:55 +02:00
public Response isUserDisabled (
2022-09-16 12:14:11 +02:00
@QueryParam ( " username " ) @NotNull ( message = " username cannot be null " )
2022-09-16 11:46:41 +02:00
String username ) throws ValidationException {
2022-09-14 16:26:17 +02:00
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
2022-09-16 11:46:41 +02:00
Boolean userDisabled = ! isNotificationEnabled ( username ) ;
2022-09-14 16:26:17 +02:00
responseBean . setResult ( userDisabled ) ;
responseBean . setSuccess ( true ) ;
2022-09-16 11:46:41 +02:00
logger . debug ( " are User " + username + " Notifications Disabled? " + userDisabled ) ;
2022-09-14 16:26:17 +02:00
} catch ( Exception e ) {
logger . error ( " Unable to read whether the notifications for this user are enabled or not. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
2022-09-16 14:33:54 +02:00
2023-09-29 18:07:24 +02:00
// /**
// * Set user notification enabled or disabled
// * @param disable true if you want to disable the notifications for this user, false if you want to enable them
// * @return the result of the operation
// * @throws ValidationException
// */
// @POST
// @Path("set-user-notifications/")
// @Consumes(MediaType.APPLICATION_JSON)
// @Produces(MediaType.APPLICATION_JSON)
// @StatusCodes ({
// @ResponseCode ( code = 200, condition = "Notification set Off or On correctly executed"),
// @ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT)
// })
// @AuthorizationControl(allowedRoles={INFRASTRUCTURE_MANAGER_ROLE}, exception=AuthException.class)
// public Response setUserNotifications(
// @NotNull(message="input is missing")
// @Valid
// UserSetNotificationBean setting) throws ValidationException{
//
// Caller caller = AuthorizationProvider.instance.get();
// String context = ScopeProvider.instance.get();
// ResponseBean responseBean = new ResponseBean();
// Status status = Status.OK;
//
//
// try{
// String opExecutor = caller.getClient().getId();
// Boolean result = setUserNotificationsOnOff(setting.getUsername(), setting.isDisableNotification(), opExecutor);
// String toReturn = "Could not set notifications";
// if (result) {
// toReturn = "Notifications have been set";
// toReturn += setting.isDisableNotification() ? " OFF (for 29 days unless re-enabled manually) ": " ON ";
// toReturn += "for username=" + setting.getUsername();
// }
// responseBean.setSuccess(true);
// responseBean.setResult(toReturn);
//
// } catch(Exception e){
// logger.error("Unable to set user notification", e);
// responseBean.setSuccess(false);
// responseBean.setMessage(e.getMessage());
// status = Status.INTERNAL_SERVER_ERROR;
// }
//
//
// return Response.status(status).entity(responseBean).build();
// }
2022-09-16 14:33:54 +02:00
2022-09-15 14:24:04 +02:00
/ * *
2023-09-29 18:07:24 +02:00
* @deprecated
2022-09-15 14:24:04 +02:00
* @param usernameToCheck
* @return true if notification are enabled for this user
2023-09-29 11:32:16 +02:00
* @throws IOException
2022-09-15 14:24:04 +02:00
* /
2023-09-29 11:32:16 +02:00
private boolean isNotificationEnabled ( String usernameToCheck ) throws IOException {
2023-09-29 18:07:24 +02:00
// MemcachedClient entries = new DistributedCacheClient().getMemcachedClient();
// String key = SocialUtils.DISABLED_USERS_NOTIFICATIONS_NAMESPACE+usernameToCheck;
// Boolean userEnabled = false;
// if(entries.get(key) == null)
// userEnabled = true;
// entries.getConnection().shutdown();
// return userEnabled;
return true ;
2022-09-15 14:24:04 +02:00
}
2023-09-29 18:07:24 +02:00
// /**
// *
// * @param username the user you want to disable or enable notifications (max 29 days)
// * @param callerId the username or clientid of the operation executor
// * @param disable true if you want to disable the notifications for this user
// * @return true if the operation was performed
// * @throws IOException
// */
// private Boolean setUserNotificationsOnOff(String username, boolean disable, String callerId) throws IOException {
// MemcachedClient entries = new DistributedCacheClient().getMemcachedClient();
// String key = SocialUtils.DISABLED_USERS_NOTIFICATIONS_NAMESPACE+username;
// OperationFuture<Boolean> result = null;
// if (disable) {
// result = entries.set(key, SocialUtils.CACHING_TIME_TO_EXPIRATION, "op.ex:" + callerId); //operator executor is who silenced the user
// } else {
// result = entries.delete(key);
// }
// try {
// boolean res = result.getStatus().isSuccess();
// entries.getConnection().shutdown();
// return res;
// } catch (Exception e) {
// entries.getConnection().shutdown();
// e.printStackTrace();
// }
// return null;
// }
2022-09-14 16:26:17 +02:00
2022-04-07 16:25:26 +02:00
/ * *
* Send a JOB notification to a given recipient
* @param job The job bean
* @return
* @throws ValidationException
* /
2019-04-05 16:39:46 +02:00
@POST
@Path ( " notify-job-status/ " )
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . APPLICATION_JSON )
2022-04-07 16:25:26 +02:00
@StatusCodes ( {
@ResponseCode ( code = 200 , condition = " Notification is sent correctly " ) ,
@ResponseCode ( code = 500 , condition = ErrorMessages . ERROR_IN_API_RESULT )
} )
2019-04-05 16:39:46 +02:00
public Response notifyJobStatus (
@NotNull ( message = " input is missing " )
2022-04-07 16:25:26 +02:00
@Valid
JobNotificationBean job ) throws ValidationException {
2019-04-05 16:39:46 +02:00
Caller caller = AuthorizationProvider . instance . get ( ) ;
String context = ScopeProvider . instance . get ( ) ;
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
String appQualifier = caller . getClient ( ) . getId ( ) ;
logger . info ( " Received request from app " + appQualifier + " to notify job status described by bean " + job ) ;
try {
String recipient = job . getRecipient ( ) ;
GCubeUser userRecipient = UserManagerWSBuilder . getInstance ( ) . getUserManager ( ) . getUserByUsername ( recipient ) ;
GenericItemBean recipientBean = new GenericItemBean ( userRecipient . getUsername ( ) , userRecipient . getUsername ( ) , userRecipient . getFullname ( ) , userRecipient . getUserAvatarURL ( ) ) ;
// notifications are sent by using the user allowed to use liferay's json apis
SocialNetworkingSite site = SocialNetworkingSiteFinder . getSocialNetworkingSiteFromScope ( context ) ;
GCubeUser senderUser = UserManagerWSBuilder . getInstance ( ) . getUserManager ( ) . getUserByEmail ( LiferayJSONWsCredentials . getSingleton ( ) . getUser ( ) ) ;
SocialNetworkingUser user = new SocialNetworkingUser ( senderUser . getUsername ( ) , senderUser . getEmail ( ) , senderUser . getFullname ( ) , senderUser . getUserAvatarURL ( ) ) ;
NotificationsManager nm = new ApplicationNotificationsManager ( UserManagerWSBuilder . getInstance ( ) . getUserManager ( ) , site , context , user ) ;
2022-04-27 18:29:33 +02:00
RunningJob theJob = JobMapper . getJob ( job ) ;
2022-05-04 16:32:48 +02:00
2022-04-27 18:29:33 +02:00
new Thread ( new JobStatusNotificationThread ( theJob , Arrays . asList ( recipientBean ) , nm ) ) . start ( ) ;
2019-04-05 16:39:46 +02:00
responseBean . setSuccess ( true ) ;
responseBean . setResult ( " Notification thread started " ) ;
} catch ( Exception e ) {
logger . error ( " Unable to send job notification " , e ) ;
responseBean . setSuccess ( false ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
2022-05-09 12:19:42 +02:00
2022-05-06 17:15:24 +02:00
/ * *
* 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
2022-10-19 15:54:45 +02:00
CatalogueEvent event ) throws ValidationException , UserManagementSystemException , UserRetrievalFault {
2022-05-06 17:15:24 +02:00
Caller caller = AuthorizationProvider . instance . get ( ) ;
2022-10-19 15:54:45 +02:00
UserManager um = UserManagerWSBuilder . getInstance ( ) . getUserManager ( ) ;
GCubeUser senderUser = null ;
SocialNetworkingUser user = null ;
// check if the token belongs to an application token. In this case use J.A.R.V.I.S (the username used to communicate with Liferay)
String username = null ;
String fullName = " " ;
2022-10-19 16:28:03 +02:00
logger . debug ( " Catalogue Notification called by " + caller . getClient ( ) . getId ( ) + " isUser? " + TokensUtils . isUserToken ( caller ) ) ;
2022-10-19 15:54:45 +02:00
if ( ! TokensUtils . isUserToken ( caller ) ) {
GCubeUser jarvis = UserManagerWSBuilder . getInstance ( ) . getUserManager ( ) . getUserByEmail ( LiferayJSONWsCredentials . getSingleton ( ) . getUser ( ) ) ;
SecurityTokenProvider . instance . set ( LiferayJSONWsCredentials . getSingleton ( ) . getNotifierUserToken ( ) ) ;
username = jarvis . getUsername ( ) ;
2022-10-19 17:12:56 +02:00
fullName = caller . getClient ( ) . getId ( ) . replace ( " service-account- " , " " ) ; // the actual name of the IAM Client
2022-10-19 15:54:45 +02:00
senderUser = um . getUserByUsername ( username ) ;
user = new SocialNetworkingUser ( senderUser . getUsername ( ) , senderUser . getEmail ( ) , fullName , senderUser . getUserAvatarURL ( ) ) ;
} else {
username = caller . getClient ( ) . getId ( ) ;
senderUser = um . getUserByUsername ( username ) ;
user = new SocialNetworkingUser ( senderUser . getUsername ( ) , senderUser . getEmail ( ) , senderUser . getFullname ( ) , senderUser . getUserAvatarURL ( ) ) ;
}
2022-05-06 17:15:24 +02:00
String context = ScopeProvider . instance . get ( ) ;
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 ) ;
2022-10-19 15:54:45 +02:00
2022-05-06 17:15:24 +02:00
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 ] ;
2022-09-14 16:26:17 +02:00
if ( isNotificationEnabled ( userIdToNotify ) ) {
String username2Notify = null ;
try {
username2Notify = um . getUserByUsername ( userIdToNotify ) . getUsername ( ) ;
}
catch ( Exception e ) {
status = Status . BAD_REQUEST ;
logger . error ( " Username not found " , e ) ;
responseBean . setSuccess ( false ) ;
responseBean . setMessage ( " Username not found, got: " + userIdToNotify ) ;
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
deliveryResult =
2022-05-09 12:19:42 +02:00
nm . notifyCatalogueEvent (
CatalogueEventTypeMapper . getType ( event . getType ( ) ) ,
2022-05-09 12:27:51 +02:00
username2Notify ,
2022-05-09 12:19:42 +02:00
event . getItemId ( ) ,
event . getNotifyText ( ) ,
event . getItemURL ( ) ) ;
2022-09-14 16:26:17 +02:00
}
2022-09-16 14:33:54 +02:00
else {
Log . info ( " Notification disabled (admin) for user " + userIdToNotify + " will not notify " ) ;
}
2022-09-14 16:26:17 +02:00
2022-05-09 10:31:20 +02:00
}
} else { //the ids are contexts
for ( int i = 0 ; i < idsToNotify . length ; i + + ) {
String contextId = idsToNotify [ i ] ;
try {
ScopeBean scope = new ScopeBean ( contextId ) ;
if ( scope . type ( ) ! = ScopeBean . Type . VRE ) {
logger . error ( " Context not a VRE " ) ;
status = Status . BAD_REQUEST ;
responseBean . setSuccess ( false ) ;
responseBean . setMessage ( " Not a VRE Context, only VREs are supported " ) ;
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
} else { // it is a context and it is a valid VRE
String [ ] userIdsToNotify = getUsernamesByContext ( scope ) . toArray ( new String [ 0 ] ) ; //resolve the members
for ( int j = 0 ; j < userIdsToNotify . length ; j + + ) {
String userIdToNotify = userIdsToNotify [ j ] ;
2022-09-14 16:26:17 +02:00
if ( isNotificationEnabled ( userIdToNotify ) ) {
deliveryResult =
nm . notifyCatalogueEvent (
CatalogueEventTypeMapper . getType ( event . getType ( ) ) ,
userIdToNotify ,
event . getItemId ( ) ,
event . getNotifyText ( ) ,
event . getItemURL ( ) ) ;
2022-09-16 14:33:54 +02:00
} else {
Log . info ( " Notification disabled (admin) for user " + userIdToNotify + " will not notify " ) ;
2022-09-14 16:26:17 +02:00
}
2022-05-09 10:31:20 +02:00
}
}
}
catch ( IllegalArgumentException e ) {
status = Status . BAD_REQUEST ;
logger . error ( " Context not valid " , e ) ;
responseBean . setSuccess ( false ) ;
responseBean . setMessage ( " Context not valid, must start with / " + e . getMessage ( ) ) ;
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
2022-05-06 17:15:24 +02:00
}
}
} 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 ( ) ;
}
2019-04-05 16:39:46 +02:00
2022-04-21 13:20:33 +02:00
/ * *
2022-04-21 17:50:48 +02:00
* Send a Workspace notification to a given user
2022-05-04 11:28:38 +02:00
* @param event
2022-04-21 13:20:33 +02:00
* @return
* @throws ValidationException
* /
@POST
2022-04-27 17:54:51 +02:00
@Path ( " workspace/ " )
2022-04-21 13:20:33 +02:00
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . APPLICATION_JSON )
@StatusCodes ( {
2022-04-21 17:50:48 +02:00
@ResponseCode ( code = 200 , condition = " Workspace Notification is sent correctly " ) ,
2022-04-21 13:20:33 +02:00
@ResponseCode ( code = 500 , condition = ErrorMessages . ERROR_IN_API_RESULT )
} )
2022-04-27 17:54:51 +02:00
public Response workspace (
2022-04-21 13:20:33 +02:00
@NotNull ( message = " input is missing " )
@Valid
2022-05-04 11:28:38 +02:00
WorkspaceEvent event ) throws ValidationException {
2022-04-21 13:20:33 +02:00
Caller caller = AuthorizationProvider . instance . get ( ) ;
String context = ScopeProvider . instance . get ( ) ;
2022-04-29 14:57:15 +02:00
String username = caller . getClient ( ) . getId ( ) ;
2022-05-04 16:32:48 +02:00
2022-10-19 17:23:40 +02:00
logger . debug ( " workspace notifications from = " + username ) ;
2022-04-21 17:50:48 +02:00
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
2022-05-04 16:32:48 +02:00
boolean deliveryResult = false ;
2022-05-03 11:02:17 +02:00
try {
2022-10-19 17:20:52 +02:00
GCubeUser senderUser = null ;
SocialNetworkingUser user = null ;
String fullName = " " ;
2022-05-04 11:28:38 +02:00
logger . debug ( " workspace notifications type is " + event . getType ( ) ) ;
2022-04-27 17:54:51 +02:00
SocialNetworkingSite site = SocialNetworkingSiteFinder . getSocialNetworkingSiteFromScope ( context ) ;
2022-05-09 11:45:09 +02:00
UserManager um = UserManagerWSBuilder . getInstance ( ) . getUserManager ( ) ;
2022-10-19 17:20:52 +02:00
logger . debug ( " Workspace Notification called by " + caller . getClient ( ) . getId ( ) + " isUser? " + TokensUtils . isUserToken ( caller ) ) ;
if ( ! TokensUtils . isUserToken ( caller ) ) {
GCubeUser jarvis = UserManagerWSBuilder . getInstance ( ) . getUserManager ( ) . getUserByEmail ( LiferayJSONWsCredentials . getSingleton ( ) . getUser ( ) ) ;
SecurityTokenProvider . instance . set ( LiferayJSONWsCredentials . getSingleton ( ) . getNotifierUserToken ( ) ) ;
username = jarvis . getUsername ( ) ;
fullName = caller . getClient ( ) . getId ( ) . replace ( " service-account- " , " " ) ; // the actual name of the IAM Client
senderUser = um . getUserByUsername ( username ) ;
user = new SocialNetworkingUser ( senderUser . getUsername ( ) , senderUser . getEmail ( ) , fullName , senderUser . getUserAvatarURL ( ) ) ;
} else {
username = caller . getClient ( ) . getId ( ) ;
senderUser = um . getUserByUsername ( username ) ;
user = new SocialNetworkingUser ( senderUser . getUsername ( ) , senderUser . getEmail ( ) , senderUser . getFullname ( ) , senderUser . getUserAvatarURL ( ) ) ;
}
2022-05-03 11:02:17 +02:00
NotificationsManager nm = new ApplicationNotificationsManager ( UserManagerWSBuilder . getInstance ( ) . getUserManager ( ) , site , context , user ) ;
2022-05-04 16:32:48 +02:00
2022-05-04 18:16:22 +02:00
String [ ] idsToNotify = event . getIdsToNotify ( ) ;
if ( ! event . idsAsGroup ( ) ) {
for ( int i = 0 ; i < idsToNotify . length ; i + + ) {
String userIdToNotify = idsToNotify [ i ] ;
2022-09-14 16:26:17 +02:00
if ( isNotificationEnabled ( userIdToNotify ) ) {
String username2Notify = " " ;
try {
username2Notify = um . getUserByUsername ( userIdToNotify ) . getUsername ( ) ;
}
catch ( Exception e ) {
status = Status . NOT_ACCEPTABLE ;
logger . error ( " Username not found " , e ) ;
responseBean . setSuccess ( false ) ;
responseBean . setMessage ( " Username not found, received: " + userIdToNotify ) ;
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
deliveryResult = notifyWorkspaceEvent ( event , nm , username2Notify ) ;
2022-05-09 11:45:09 +02:00
}
2022-09-16 14:33:54 +02:00
else {
Log . info ( " Notification disabled (admin) for user " + userIdToNotify + " will not notify " ) ;
}
2022-05-04 16:32:48 +02:00
}
2022-05-05 11:04:31 +02:00
} else { //the ids are contexts
for ( int i = 0 ; i < idsToNotify . length ; i + + ) {
String contextId = idsToNotify [ i ] ;
try {
ScopeBean scope = new ScopeBean ( contextId ) ;
if ( scope . type ( ) ! = ScopeBean . Type . VRE ) {
logger . error ( " Context not a VRE " ) ;
status = Status . BAD_REQUEST ;
responseBean . setSuccess ( false ) ;
2022-05-05 13:38:47 +02:00
responseBean . setMessage ( " Not a VRE Context, only VREs are supported " ) ;
2022-05-05 11:04:31 +02:00
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
} else { // it is a context and it is a valid VRE
String [ ] userIdsToNotify = getUsernamesByContext ( scope ) . toArray ( new String [ 0 ] ) ; //resolve the members
for ( int j = 0 ; j < userIdsToNotify . length ; j + + ) {
String userIdToNotify = userIdsToNotify [ j ] ;
2022-09-14 16:26:17 +02:00
if ( isNotificationEnabled ( userIdToNotify ) )
deliveryResult = notifyWorkspaceEvent ( event , nm , userIdToNotify ) ;
2022-09-16 14:33:54 +02:00
else {
Log . info ( " Notification disabled (admin) for user " + userIdToNotify + " will not notify " ) ;
}
2022-05-05 11:04:31 +02:00
}
}
}
catch ( IllegalArgumentException e ) {
status = Status . BAD_REQUEST ;
logger . error ( " Context not valid " , e ) ;
responseBean . setSuccess ( false ) ;
responseBean . setMessage ( " Context not valid, must start with / " + e . getMessage ( ) ) ;
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
}
2022-04-21 17:50:48 +02:00
}
} catch ( Exception e ) {
logger . error ( " Unable to send job notification " , e ) ;
responseBean . setSuccess ( false ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
2022-05-03 11:02:17 +02:00
logger . debug ( " workspace notifications should have been sent " ) ;
2022-05-04 16:32:48 +02:00
if ( deliveryResult ) {
responseBean . setSuccess ( true ) ;
responseBean . setMessage ( " Workspace notification delivered correctly " ) ;
responseBean . setResult ( new Boolean ( true ) ) ;
} else {
responseBean . setSuccess ( false ) ;
2022-05-05 11:04:31 +02:00
responseBean . setMessage ( " An error occurred between this service and Cassandra DB, notification not delivered correctly " ) ;
2022-05-04 16:32:48 +02:00
responseBean . setResult ( new Boolean ( false ) ) ;
}
2022-04-21 17:50:48 +02:00
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
2022-05-04 18:16:22 +02:00
/ * *
*
* @param event
* @param nm
* @param idToNotify
* @return
* /
private boolean notifyWorkspaceEvent ( WorkspaceEvent event , NotificationsManager nm , String idToNotify ) {
try {
switch ( event . getType ( ) ) {
case ITEM_NEW : {
AddedItemEvent itemBean = ( AddedItemEvent ) event ;
return nm . notifyAddedItem ( idToNotify , WorkspaceItemMapper . getFileItem ( itemBean . getItem ( ) ) , WorkspaceItemMapper . getSharedFolder ( itemBean . getItem ( ) . getParent ( ) ) ) ;
}
2022-05-05 13:38:47 +02:00
case ITEM_UPDATE : {
UpdatedItemEvent itemBean = ( UpdatedItemEvent ) event ;
return nm . notifyUpdatedItem ( idToNotify , WorkspaceItemMapper . getFileItem ( itemBean . getItem ( ) ) , WorkspaceItemMapper . getSharedFolder ( itemBean . getItem ( ) . getParent ( ) ) ) ;
}
case ITEM_DELETE : {
DeletedItemEvent itemBean = ( DeletedItemEvent ) event ;
return nm . notifyRemovedItem ( idToNotify , itemBean . getItemName ( ) , WorkspaceItemMapper . getSharedFolder ( itemBean . getFolder ( ) ) ) ;
}
2022-05-04 18:16:22 +02:00
case FOLDER_SHARE : {
SharedFolderEvent itemBean = ( SharedFolderEvent ) event ;
return nm . notifyFolderSharing ( idToNotify , WorkspaceItemMapper . getSharedFolder ( itemBean . getFolder ( ) ) ) ;
}
case FOLDER_UNSHARE : {
UnsharedFolderEvent itemBean = ( UnsharedFolderEvent ) event ;
return nm . notifyFolderUnsharing ( idToNotify , itemBean . getUnsharedFolderId ( ) , itemBean . getUnsharedFolderName ( ) ) ;
}
case FOLDER_RENAME : {
RenamedFolderEvent itemBean = ( RenamedFolderEvent ) event ;
return nm . notifyFolderRenaming ( idToNotify , itemBean . getPreviousName ( ) , itemBean . getNewName ( ) , itemBean . getRenamedFolderId ( ) ) ;
}
2022-05-05 14:32:25 +02:00
case FOLDER_ADMIN_UPGRADE : {
FolderAdminUpgradeEvent itemBean = ( FolderAdminUpgradeEvent ) event ;
return nm . notifyAdministratorUpgrade ( idToNotify , WorkspaceItemMapper . getSharedFolder ( itemBean . getFolder ( ) ) ) ;
}
case FOLDER_ADMIN_DOWNGRADE : {
FolderAdminDowngradeEvent itemBean = ( FolderAdminDowngradeEvent ) event ;
return nm . notifyAdministratorDowngrade ( idToNotify , WorkspaceItemMapper . getSharedFolder ( itemBean . getFolder ( ) ) ) ;
}
2022-05-05 13:38:47 +02:00
case FOLDER_ADDEDUSER : {
FolderAddedUserEvent itemBean = ( FolderAddedUserEvent ) event ;
2022-05-05 14:13:51 +02:00
UserManager userManager = UserManagerWSBuilder . getInstance ( ) . getUserManager ( ) ;
return nm . notifyFolderAddedUsers ( idToNotify , WorkspaceItemMapper . getSharedFolder ( itemBean . getFolder ( ) ) , itemBean . getNewAddedUserIds ( ) , userManager ) ;
2022-05-05 13:38:47 +02:00
}
case FOLDER_REMOVEDUSER : {
FolderRemovedUserEvent itemBean = ( FolderRemovedUserEvent ) event ;
return nm . notifyFolderRemovedUser ( idToNotify , WorkspaceItemMapper . getSharedFolder ( itemBean . getFolder ( ) ) ) ;
}
2022-05-04 18:16:22 +02:00
default :
break ;
}
} catch ( Exception e ) {
logger . error ( " Unable to send job notification " , e ) ;
return false ;
}
return false ;
}
2022-05-05 11:04:31 +02:00
/ * *
*
* @param context
* @return
* @throws Exception
* /
private List < String > getUsernamesByContext ( ScopeBean context ) throws Exception {
List < String > usernames = new ArrayList < String > ( ) ;
GroupManager groupManager = GroupManagerWSBuilder . getInstance ( ) . getGroupManager ( ) ;
UserManager userManager = UserManagerWSBuilder . getInstance ( ) . getUserManager ( ) ;
long groupId = groupManager . getGroupIdFromInfrastructureScope ( context . toString ( ) ) ;
// first retrieve ids
List < Long > userIds = userManager . getUserIdsByGroup ( groupId ) ;
// check info in cache when available
UsersCache cache = UsersCache . getSingleton ( ) ;
for ( Long userId : userIds ) {
if ( cache . getUser ( userId ) = = null ) {
GCubeUser theUser = userManager . getUserById ( userId ) ;
if ( theUser ! = null ) {
usernames . add ( theUser . getUsername ( ) ) ;
cache . pushEntry ( userId , theUser ) ;
}
} else
usernames . add ( cache . getUser ( userId ) . getUsername ( ) ) ;
}
return usernames ;
}
2022-04-21 17:50:48 +02:00
2023-11-21 18:38:57 +01:00
//library api
@POST
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " save-notification-lib " )
public Response saveNotificationLib (
@NotNull ( message = " notification to add is missing " )
@Valid
Notification not
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
boolean result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . saveNotification ( not ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( result ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@GET
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " read-notification-lib " )
public Response readNotificationLib (
@QueryParam ( " notid " )
String notid
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
Notification result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . readNotification ( notid ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( true ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@POST
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " set-notification-read-lib " )
public Response setNotificationReadLib (
@QueryParam ( " notid " )
String notid
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
boolean result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . setNotificationRead ( notid ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( result ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@GET
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " get-all-notifications-user " )
public Response getAllNotificationByUserLib (
@QueryParam ( " userid " )
String userid ,
@QueryParam ( " limit " )
int limit
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
List < Notification > result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . getAllNotificationByUser ( userid , limit ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( true ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@GET
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " get-unread-notifications-user " )
public Response getUnreadNotificationsByUserLib (
@QueryParam ( " userid " )
String userid
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
List < Notification > result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . getUnreadNotificationsByUser ( userid ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( true ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@GET
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " get-range-notifications-user " )
public Response getRangeNotificationsByUserLib (
@QueryParam ( " userid " )
String userid ,
@QueryParam ( " from " )
int from ,
@QueryParam ( " quantity " )
int quantity
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
List < Notification > result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . getRangeNotificationsByUser ( userid , from , quantity ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( true ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@POST
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " set-all-notification-read-lib " )
public Response setAllNotificationReadByUserLib (
@QueryParam ( " userid " )
String userid
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
boolean result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . setAllNotificationReadByUser ( userid ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( result ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@POST
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " check-unread-notification-lib " )
public Response checkUnreadNotificationsLib (
@QueryParam ( " userid " )
String userid
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
boolean result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . checkUnreadNotifications ( userid ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( true ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@POST
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " check-unread-messages-notification-lib " )
public Response checkUnreadMessagesNotificationsLib (
@QueryParam ( " userid " )
String userid
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
boolean result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . checkUnreadMessagesNotifications ( userid ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( true ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@POST
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " get-user-notification-channels-lib " )
public Response getUserNotificationChannelsLib (
@QueryParam ( " userid " )
String userid ,
@NotNull ( message = " attachment to add is missing " )
@Valid
NotificationType notificationType
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
List < NotificationChannelType > result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . getUserNotificationChannels ( userid , notificationType ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( true ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@POST
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " set-notification-preference-lib " )
public Response setUserNotificationPreferencesLib (
@QueryParam ( " userid " )
String userid ,
@NotNull ( message = " attachment to add is missing " )
@Valid
Map < NotificationType , NotificationChannelType [ ] > enabledChannels
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
boolean result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . setUserNotificationPreferences ( userid , enabledChannels ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( true ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
@GET
@Produces ( MediaType . APPLICATION_JSON )
@Path ( " get-notification-preference-lib " )
public Response getUserNotificationPreferencesLib (
@QueryParam ( " userid " )
String userid
) throws ValidationException {
ResponseBean responseBean = new ResponseBean ( ) ;
Status status = Status . OK ;
try {
Map < NotificationType , NotificationChannelType [ ] > result = CassandraConnection . getInstance ( ) . getDatabookStore ( ) . getUserNotificationPreferences ( userid ) ;
responseBean . setResult ( result ) ;
responseBean . setMessage ( " " ) ;
responseBean . setSuccess ( true ) ;
} catch ( Exception e ) {
logger . error ( " Unable to write comment. " , e ) ;
responseBean . setMessage ( e . getMessage ( ) ) ;
responseBean . setSuccess ( false ) ;
status = Status . INTERNAL_SERVER_ERROR ;
}
return Response . status ( status ) . entity ( responseBean ) . build ( ) ;
}
2019-04-05 16:39:46 +02:00
}