implemented set and get

This commit is contained in:
Massimiliano Assante 2022-09-15 14:24:04 +02:00
parent 440dd5236b
commit 3ee604924c
2 changed files with 87 additions and 3 deletions

View File

@ -3,8 +3,12 @@ package org.gcube.portal.social.networking.ws.methods.v2;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import javax.validation.Valid;
import javax.validation.ValidationException;
@ -49,6 +53,7 @@ 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.DistributedCacheClient;
import org.gcube.portal.social.networking.ws.utils.ErrorMessages;
import org.gcube.portal.social.networking.ws.utils.SocialUtils;
import org.gcube.social_networking.socialnetworking.model.beans.JobNotificationBean;
import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEvent;
@ -76,6 +81,7 @@ import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.internal.OperationFuture;
/**
* REST interface for the social networking library (notifications).
@ -168,14 +174,91 @@ public class Notifications {
return Response.status(status).entity(responseBean).build();
}
/**
* Set user notification enabled or disabled
* @param disable true if you want to disable the notifications for this user
* @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
String username, boolean disable) 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(username, disable, opExecutor);
String toReturn = "Notifications have been set ";
if (result) {
toReturn += disable ? " Off ": " On ";
toReturn += "for " + username;
}
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();
}
/**
*
* @param usernameToCheck
* @return true if notification are enabled for this user
*/
private boolean isNotificationEnabled(String usernameToCheck) {
MemcachedClient entries = new DistributedCacheClient().getMemcachedClient();
String key = SocialUtils.DISABLED_USERS_NOTIFICATIONS_NAMESPACE+usernameToCheck;
Boolean userEnabled = false;
if(entries.get(usernameToCheck) == null)
if(entries.get(key) == null)
userEnabled = true;
return userEnabled;
}
/**
*
* @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
*/
private Boolean setUserNotificationsOnOff(String username, boolean disable, String callerId) {
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(username);
}
try {
return result.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return null;
}
/**
* Send a JOB notification to a given recipient

View File

@ -49,7 +49,6 @@ import org.xml.sax.InputSource;
/**
* Utility class.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class SocialUtils {
@ -57,7 +56,9 @@ public class SocialUtils {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SocialUtils.class);
public final static String NO_TEXT_FILE_SHARE = "_N0_73X7_SH4R3_";
public final static int CACHING_TIME_TO_EXPIRATION = 2506000;//29 days 6 minutes 40 seconds
public final static String DISABLED_USERS_NOTIFICATIONS_NAMESPACE = "dun:";
// name of the portlet for vre notification
public static final String NEWS_FEED_PORTLET_CLASSNAME = "org.gcube.portlets.user.newsfeed.server.NewsServiceImpl";