partially implemented worspace notif support

This commit is contained in:
Massimiliano Assante 2022-04-21 17:50:48 +02:00
parent d8a4046eed
commit fd1eb389c3
8 changed files with 172 additions and 101 deletions

24
pom.xml
View File

@ -70,6 +70,11 @@
<scope>provided</scope>
</dependency>
<!-- end enunciate deps -->
<dependency>
<groupId>org.gcube.social-networking</groupId>
<artifactId>social-service-model</artifactId>
<version>[1.1.0-SNAPSHOT, 2.0.0)</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
@ -302,7 +307,6 @@
</dependencies>
<build>
<finalName>${project.name}</finalName>
<plugins>
<!-- Enunciate Maven plugin -->
<plugin>
@ -345,6 +349,24 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
<additionalJOption>-Xdoclint:none</additionalJOption>
</configuration>
<version>3.1.0</version>
<executions>
<execution>
<id>generate-doc</id>
<phase>install</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,57 @@
package org.gcube.portal.social.networking.ws.mappers;
import org.gcube.applicationsupportlayer.social.shared.SocialFileItem;
import org.gcube.applicationsupportlayer.social.shared.SocialSharedFolder;
import org.gcube.social_networking.socialnetworking.model.beans.SharedFolderBean;
import org.gcube.social_networking.socialnetworking.model.beans.WorkspaceFileItemBean;
public class WorkspaceItemMapper {
public WorkspaceItemMapper() {
// TODO Auto-generated constructor stub
}
public static SocialFileItem getFileItem(WorkspaceFileItemBean item) {
String id = null;
String name= null;
String title= null;
String path= null;
SocialSharedFolder parent = null;
try {
id = item.getId();
name = item.getName();
title = item.getTitle();
path = item.getPath();
parent = getSharedFolder(item.getParent());
} catch (Exception e) {
e.printStackTrace();
}
SocialFileItem toReturn = new SocialFileItem(id, name, title, path, parent);
return toReturn;
}
public static SocialSharedFolder getSharedFolder(SharedFolderBean item) {
String id = null;
String name= null;
String title= null;
String displayName= null;
String path= null;
String parentId= null;
boolean vreFolder = false;
try {
id = item.getId();
name = item.getName();
title = item.getTitle();
displayName = item.getDisplayName();
path = item.getPath();
parentId = item.getParentId();
vreFolder = item.isVreFolder();
} catch (Exception e) {
e.printStackTrace();
}
SocialSharedFolder toReturn = new SocialSharedFolder(id, name, title, displayName, path, parentId, vreFolder);
return toReturn;
}
}

View File

@ -20,8 +20,10 @@ 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.SocialFileItem;
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite;
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser;
import org.gcube.applicationsupportlayer.social.shared.SocialSharedFolder;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
@ -33,12 +35,15 @@ import org.gcube.portal.social.networking.caches.SocialNetworkingSiteFinder;
import org.gcube.portal.social.networking.liferay.ws.LiferayJSONWsCredentials;
import org.gcube.portal.social.networking.liferay.ws.UserManagerWSBuilder;
import org.gcube.portal.social.networking.ws.inputs.JobNotificationBean;
import org.gcube.portal.social.networking.ws.model.AddFileNotificationBean;
import org.gcube.portal.social.networking.ws.model.AddFolderNotificationBean;
import org.gcube.portal.social.networking.ws.model.NotificationMessage;
import org.gcube.portal.social.networking.ws.mappers.WorkspaceItemMapper;
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.SharedFolderBean;
import org.gcube.social_networking.socialnetworking.model.beans.WSAddedItemNotificationBean;
import org.gcube.social_networking.socialnetworking.model.beans.WSSharedFolderNotificationBean;
import org.gcube.social_networking.socialnetworking.model.beans.WorkspaceFileItemBean;
import org.gcube.social_networking.socialnetworking.model.beans.WorkspaceNotificationMessage;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.LoggerFactory;
@ -52,9 +57,9 @@ import com.webcohesion.enunciate.metadata.rs.StatusCodes;
*/
@Path("2/notifications")
@RequestHeaders ({
@RequestHeader( name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"),
@RequestHeader( name = "Content-Type", description = "application/json")
})
@RequestHeader( name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"),
@RequestHeader( name = "Content-Type", description = "application/json")
})
public class Notifications {
// Logger
@ -160,23 +165,23 @@ public class Notifications {
}
/**
* Send a JOB notification to a given recipient
* @param job The job bean
* Send a Workspace notification to a given user
* @param message
* @return
* @throws ValidationException
*/
@POST
@Path("notify/")
@Path("notify-ws/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@StatusCodes ({
@ResponseCode ( code = 200, condition = "Notification is sent correctly"),
@ResponseCode ( code = 200, condition = "Workspace Notification is sent correctly"),
@ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT)
})
public Response notify(
public Response notifyWs(
@NotNull(message="input is missing")
@Valid
NotificationMessage message) throws ValidationException{
WorkspaceNotificationMessage message) throws ValidationException{
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
@ -185,12 +190,80 @@ public class Notifications {
//invalid call
return Response.status(Status.BAD_REQUEST).build();
}
//TODO: call notification method
return Response.ok().build();
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
try{
switch (message.getType()) {
case WP_ITEM_NEW:
WSAddedItemNotificationBean wpItemBean = (WSAddedItemNotificationBean) message.getBean();
GCubeUser userRecipient = UserManagerWSBuilder.getInstance().getUserManager().getUserByUsername(wpItemBean.getUserIdToNotify());
// 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);
nm.notifyAddedItem(context, WorkspaceItemMapper.getFileItem(wpItemBean.getItem()), WorkspaceItemMapper.getSharedFolder(wpItemBean.getFolder()));
break;
default:
break;
}
} 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();
}
/**
* Send a Workspace notification to a given user
* @param userIdToNotify the user you want to notify
* @param newItem the new shared {@link SocialFileItem} instance
* @param sharedFolder the shared folder {@link SocialSharedFolder} instance
* @return
* @throws ValidationException
*/
@POST
@Path("notify-ws-item-new/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@StatusCodes ({
@ResponseCode ( code = 200, condition = "Workspace Notification is sent correctly"),
@ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT)
})
public Response notifyWsItemNew(
@NotNull(message="input is missing")
@Valid
String userIdToNotify,
@NotNull(message="input is missing")
@Valid
WorkspaceFileItemBean fileItem) throws ValidationException{
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
try{
// 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);
nm.notifyAddedItem(context, WorkspaceItemMapper.getFileItem(fileItem), WorkspaceItemMapper.getSharedFolder(fileItem.getParent()));
} 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();
}
}

View File

@ -1,16 +0,0 @@
package org.gcube.portal.social.networking.ws.model;
public class AddFileNotificationBean implements NotificationBean {
private String filename;
public AddFileNotificationBean(String filename) {
this.filename = filename;
}
public String getFilename() {
return filename;
}
}

View File

@ -1,15 +0,0 @@
package org.gcube.portal.social.networking.ws.model;
public class AddFolderNotificationBean implements NotificationBean {
private String foldername;
public AddFolderNotificationBean(String foldername) {
this.foldername = foldername;
}
public String getFoldername() {
return foldername;
}
}

View File

@ -1,5 +0,0 @@
package org.gcube.portal.social.networking.ws.model;
interface NotificationBean {
}

View File

@ -1,25 +0,0 @@
package org.gcube.portal.social.networking.ws.model;
public class NotificationMessage {
private NotificationType type;
private NotificationBean bean;
public NotificationMessage(NotificationType type, NotificationBean bean) {
super();
this.type = type;
this.bean = bean;
}
public NotificationType getType() {
return type;
}
public NotificationBean getBean() {
return bean;
}
}

View File

@ -1,20 +0,0 @@
package org.gcube.portal.social.networking.ws.model;
public enum NotificationType {
AddFileToWS(AddFileNotificationBean.class),
AddFolderToWS(AddFolderNotificationBean.class);
Class<? extends NotificationBean> beanClass;
NotificationType(Class<? extends NotificationBean> beanClass){
this.beanClass = beanClass;
}
public Class<? extends NotificationBean> getNotificationClass(){
return this.beanClass;
}
}