improved exception handling
This commit is contained in:
parent
90f77b1fe5
commit
7b3b7bd8b2
|
@ -1,6 +1,5 @@
|
|||
package org.gcube.portlets.user.vremanagement;
|
||||
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
@ -32,8 +31,13 @@ import org.gcube.common.storagehubwrapper.server.tohl.Workspace;
|
|||
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceFolder;
|
||||
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItem;
|
||||
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItemType;
|
||||
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.InsufficientPrivilegesException;
|
||||
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.InternalErrorException;
|
||||
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.ItemAlreadyExistException;
|
||||
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.ItemNotFoundException;
|
||||
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.WorkspaceFolderNotFoundException;
|
||||
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.WrongDestinationException;
|
||||
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.WrongItemTypeException;
|
||||
import org.gcube.portal.notifications.bean.GenericItemBean;
|
||||
import org.gcube.portal.notifications.thread.MessageNotificationsThread;
|
||||
import org.gcube.vomanagement.usermanagement.exception.RoleRetrievalFault;
|
||||
|
@ -61,22 +65,26 @@ import com.liferay.util.bridges.mvc.MVCPortlet;
|
|||
|
||||
/**
|
||||
*
|
||||
- Request a VRE form
|
||||
* Portlet implementation class RequestVRE
|
||||
* - Request a VRE form Portlet implementation class RequestVRE
|
||||
*/
|
||||
public class RequestVRE extends MVCPortlet {
|
||||
|
||||
private static Log _log = LogFactoryUtil.getLog(RequestVRE.class);
|
||||
|
||||
|
||||
private static String REQUEST_VRE_ROLE_MANAGER_NAME = "Infrastructure-Manager";
|
||||
private final static String UPLOADED_FILE_ATTR_NAME = "myfile";
|
||||
//make sure in the jsp there's <portlet:resourceURL var="uploadFileURL" id="uploadFiles" />
|
||||
// make sure in the jsp there's <portlet:resourceURL var="uploadFileURL"
|
||||
// id="uploadFiles" />
|
||||
private final static String RESOURCE_URL_ID = "uploadFiles";
|
||||
public static final String ATTACHMENT_FOLDER ="_shared attachments";
|
||||
public static final String ATTACHMENT_FOLDER = "_shared attachments";
|
||||
|
||||
private LiferayRoleManager roleManager = new LiferayRoleManager();
|
||||
private LiferayUserManager userManager = new LiferayUserManager();
|
||||
|
||||
public void addIdea(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
|
||||
try{
|
||||
public void addIdea(ActionRequest actionRequest, ActionResponse actionResponse)
|
||||
throws IOException, PortletException {
|
||||
try {
|
||||
User currentUser = (User) actionRequest.getAttribute(WebKeys.USER);
|
||||
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
|
||||
long groupId = themeDisplay.getLayout().getGroupId();
|
||||
|
@ -86,7 +94,7 @@ public class RequestVRE extends MVCPortlet {
|
|||
String contact_email = ParamUtil.getString(actionRequest, "email");
|
||||
String[] attachmentIds = ParamUtil.getParameterValues(actionRequest, "selected-attachment");
|
||||
for (int i = 0; i < attachmentIds.length; i++) {
|
||||
_log.debug(i+"id="+attachmentIds[i]);
|
||||
_log.debug(i + "id=" + attachmentIds[i]);
|
||||
}
|
||||
|
||||
SessionMessages.add(actionRequest.getPortletSession(), "method-form-success");
|
||||
|
@ -94,45 +102,44 @@ public class RequestVRE extends MVCPortlet {
|
|||
_log.debug("new VRE Request: " + vre_name);
|
||||
SessionMessages.add(actionRequest.getPortletSession(), "experiment-form-success");
|
||||
|
||||
|
||||
|
||||
long roleId = roleManager.getRole(REQUEST_VRE_ROLE_MANAGER_NAME, groupId).getRoleId();
|
||||
long roleId = roleManager.getRole(REQUEST_VRE_ROLE_MANAGER_NAME, groupId).getRoleId();
|
||||
List<GCubeUser> theManagers = userManager.listUsersByGroupAndRole(groupId, roleId);
|
||||
long infraManagerRoleId = -1;
|
||||
boolean result = false;
|
||||
if (theManagers == null || theManagers.isEmpty()) {
|
||||
if (theManagers == null || theManagers.isEmpty()) {
|
||||
String rootVoName = PortalContext.getConfiguration().getInfrastructureName();
|
||||
long therootGroupId = new LiferayGroupManager().getGroupIdFromInfrastructureScope("/"+rootVoName);
|
||||
long therootGroupId = new LiferayGroupManager().getGroupIdFromInfrastructureScope("/" + rootVoName);
|
||||
try {
|
||||
infraManagerRoleId = roleManager.getRoleIdByName(GatewayRolesNames.INFRASTRUCTURE_MANAGER.getRoleName());
|
||||
theManagers = userManager.listUsersByGroupAndRole(therootGroupId, infraManagerRoleId);
|
||||
}
|
||||
catch (RoleRetrievalFault e) {
|
||||
_log.warn("There are no users with (Site) Role " + infraManagerRoleId + " on " + rootVoName + " in this portal. Will not notify about this vre request");
|
||||
infraManagerRoleId = roleManager
|
||||
.getRoleIdByName(GatewayRolesNames.INFRASTRUCTURE_MANAGER.getRoleName());
|
||||
theManagers = userManager.listUsersByGroupAndRole(therootGroupId, infraManagerRoleId);
|
||||
} catch (RoleRetrievalFault e) {
|
||||
_log.warn("There are no users with (Site) Role " + infraManagerRoleId + " on " + rootVoName
|
||||
+ " in this portal. Will not notify about this vre request");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (theManagers == null || theManagers.isEmpty()) {
|
||||
_log.warn("There are no users with (Site) Role " + infraManagerRoleId + " on this portal. Will not notify about this vre request.");
|
||||
}
|
||||
else {
|
||||
_log.warn("There are no users with (Site) Role " + infraManagerRoleId
|
||||
+ " on this portal. Will not notify about this vre request.");
|
||||
} else {
|
||||
HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
|
||||
result = sendNotificationToUser(request, groupId, currentUser, theManagers,
|
||||
vre_name, theAbstract, vreManagerName, contact_email, attachmentIds);
|
||||
result = sendNotificationToUser(request, groupId, currentUser, theManagers, vre_name, theAbstract,
|
||||
vreManagerName, contact_email, attachmentIds);
|
||||
}
|
||||
if (result)
|
||||
actionResponse.setRenderParameter("mvcPath", "/html/requestvre/view-experiment-inserted.jsp");
|
||||
else
|
||||
actionResponse.setRenderParameter("mvcPath", "/html/requestvre/view-experiment-error.jsp");
|
||||
} catch(Exception e){
|
||||
SessionErrors.add(actionRequest.getPortletSession(),"experiment-form-error");
|
||||
} catch (Exception e) {
|
||||
SessionErrors.add(actionRequest.getPortletSession(), "experiment-form-error");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws PortletException, IOException {
|
||||
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
|
||||
throws PortletException, IOException {
|
||||
String resourceID = resourceRequest.getResourceID();
|
||||
if (resourceID.equals(RESOURCE_URL_ID)) {
|
||||
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(resourceRequest);
|
||||
|
@ -150,19 +157,20 @@ public class RequestVRE extends MVCPortlet {
|
|||
JSONObject fileObject = JSONFactoryUtil.createJSONObject();
|
||||
try {
|
||||
Workspace workspace = getWorkspace(uploadRequest);
|
||||
String wsItemId = uploadToUserWorkspace(workspace, fileName, contentType, new ByteArrayInputStream(FileUtil.getBytes(multipartFile)));
|
||||
String wsItemId = uploadToUserWorkspace(workspace, fileName, contentType,
|
||||
new ByteArrayInputStream(FileUtil.getBytes(multipartFile)));
|
||||
fileObject.put("itemId", wsItemId);
|
||||
fileObject.put("fileName", fileName);
|
||||
} catch (Exception e) {
|
||||
_log.error("There was an error uploading the file " + fileName, e);
|
||||
}
|
||||
|
||||
resourceResponse.getWriter().println(fileObject);
|
||||
resourceResponse.getWriter().println(fileObject);
|
||||
} else {
|
||||
_log.debug("nothing to do");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param uploadRequest is the XHR http servlet request
|
||||
|
@ -174,11 +182,11 @@ public class RequestVRE extends MVCPortlet {
|
|||
String username = null;
|
||||
try {
|
||||
userId = PortalUtil.getUser(uploadRequest).getUserId();
|
||||
username = new LiferayUserManager().getUserById(userId).getUsername();
|
||||
} catch (Exception e) {
|
||||
username = new LiferayUserManager().getUserById(userId).getUsername();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
PortalContext pContext = PortalContext.getConfiguration();
|
||||
String context = pContext.getCurrentScope(uploadRequest);
|
||||
ScopeProvider.instance.set(context);
|
||||
|
@ -188,12 +196,12 @@ public class RequestVRE extends MVCPortlet {
|
|||
try {
|
||||
StorageHubWrapper wrap = new StorageHubWrapper(context, authorizationToken);
|
||||
return wrap.getWorkspace();
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param currentUsername
|
||||
|
@ -208,37 +216,53 @@ public class RequestVRE extends MVCPortlet {
|
|||
try {
|
||||
StorageHubWrapper wrap = new StorageHubWrapper(context, authorizationToken);
|
||||
return wrap.getWorkspace();
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private String uploadToUserWorkspace(Workspace ownerWS, String fileName, String mimeType,InputStream fileData) throws Exception {
|
||||
/**
|
||||
* @return the id of the uploaded file
|
||||
*/
|
||||
private String uploadToUserWorkspace(Workspace ownerWS, String fileName, String mimeType, InputStream fileData) {
|
||||
WorkspaceFolder attachment = null;
|
||||
if (!ownerWS.exists(ATTACHMENT_FOLDER, ownerWS.getRoot().getId())) {
|
||||
_log.info(ATTACHMENT_FOLDER + " Workspace Folder does not exists, creating it ");
|
||||
attachment = ownerWS.createFolder(ATTACHMENT_FOLDER, "Folder created automatically by the System", ownerWS.getRoot().getId());
|
||||
try {
|
||||
if (!ownerWS.exists(ATTACHMENT_FOLDER, ownerWS.getRoot().getId())) {
|
||||
_log.info(ATTACHMENT_FOLDER + " Workspace Folder does not exists, creating it ");
|
||||
attachment = ownerWS.createFolder(ATTACHMENT_FOLDER, "Folder created automatically by the System",
|
||||
ownerWS.getRoot().getId());
|
||||
} else {
|
||||
attachment = (WorkspaceFolder) ownerWS.find(ATTACHMENT_FOLDER, ownerWS.getRoot().getId()).get(0);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
_log.error(
|
||||
"uploadToUserWorkspace FAILED while trying to create or check folder existence " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
else {
|
||||
attachment = (WorkspaceFolder) ownerWS.find(ATTACHMENT_FOLDER, ownerWS.getRoot().getId()).get(0);
|
||||
|
||||
WorkspaceItem item = null;
|
||||
try {
|
||||
item = ownerWS.uploadFile(attachment.getId(), fileData, fileName,
|
||||
"File created automatically by the System on " + new Date(), null);
|
||||
} catch (Exception e) {
|
||||
_log.error(
|
||||
"uploadToUserWorkspace FAILED while trying to uploadFile " + fileName);
|
||||
e.printStackTrace();
|
||||
}
|
||||
WorkspaceItem item = ownerWS.uploadFile(attachment.getId(), fileData, fileName, "File created automatically by the System on " + new Date(), null);
|
||||
String toReturn = item.getId();
|
||||
_log.debug("Uploaded " + item.getName() + " - Returned Workspace id=" +toReturn);
|
||||
String toReturn = item.getId();
|
||||
_log.info("Uploaded " + item.getName() + " - Returned Workspace id=" + toReturn);
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
|
||||
private boolean sendNotificationToUser(HttpServletRequest request, long groupId, User currentUser, List<GCubeUser> theManagers
|
||||
, String vre_name, String theAbstract, String vreManagerName, String email, String[] attachmentIds) throws Exception {
|
||||
String context = PortalContext.getConfiguration().getCurrentScope(""+groupId);
|
||||
private boolean sendNotificationToUser(HttpServletRequest request, long groupId, User currentUser,
|
||||
List<GCubeUser> theManagers, String vre_name, String theAbstract, String vreManagerName, String email,
|
||||
String[] attachmentIds) throws Exception {
|
||||
String context = PortalContext.getConfiguration().getCurrentScope("" + groupId);
|
||||
_log.info("sendNotificationToUser context: " + context);
|
||||
|
||||
String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(context, currentUser.getScreenName());
|
||||
String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(context,
|
||||
currentUser.getScreenName());
|
||||
ScopeProvider.instance.set(context);
|
||||
SecurityTokenProvider.instance.set(authorizationToken);
|
||||
Workspace workspace = getWorkspace(currentUser.getScreenName(), context);
|
||||
|
@ -246,36 +270,36 @@ public class RequestVRE extends MVCPortlet {
|
|||
List<String> recipientIds = new ArrayList<String>();
|
||||
List<GenericItemBean> recipients = new ArrayList<GenericItemBean>();
|
||||
for (GCubeUser addressee : theManagers) {
|
||||
recipients.add(new GenericItemBean(addressee.getUsername(), addressee.getUsername(), addressee.getFullname(), ""));
|
||||
recipients.add(
|
||||
new GenericItemBean(addressee.getUsername(), addressee.getUsername(), addressee.getFullname(), ""));
|
||||
recipientIds.add(addressee.getUsername());
|
||||
}
|
||||
|
||||
String subject = "A new VRE has been requested";
|
||||
StringBuilder sb = new StringBuilder("Hi, the following VRE was requested by ").append(currentUser.getFullName())
|
||||
.append(" (").append(currentUser.getEmailAddress()).append(").")
|
||||
.append("\n\n").append("VRE name: ").append(vre_name).append("\n")
|
||||
.append("\n").append("Manager(s): ").append(vreManagerName).append("\n")
|
||||
.append("\n").append("Abstract:\n").append(theAbstract).append("\n")
|
||||
StringBuilder sb = new StringBuilder("Hi, the following VRE was requested by ")
|
||||
.append(currentUser.getFullName()).append(" (").append(currentUser.getEmailAddress()).append(").")
|
||||
.append("\n\n").append("VRE name: ").append(vre_name).append("\n").append("\n").append("Manager(s): ")
|
||||
.append(vreManagerName).append("\n").append("\n").append("Abstract:\n").append(theAbstract).append("\n")
|
||||
.append("\n").append("Contact email: ").append(email).append("\n");
|
||||
|
||||
List<String> attachmentsList = Arrays.asList(attachmentIds);
|
||||
String body = sb.toString();
|
||||
try {
|
||||
body += getPublicLinksForAttachs(workspace, attachmentsList);
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
_log.error("Ops, could not generate publick link for some of the attachments");
|
||||
}
|
||||
|
||||
|
||||
MessageManagerClient client = AbstractPlugin.messages().build();
|
||||
String messageId = client.sendMessage(recipientIds, subject, body, attachmentsList);
|
||||
_log.info("Sending new VRE Request message notification to: " + recipientIds.toString());
|
||||
NotificationsManager nm = new ApplicationNotificationsManager(new SocialNetworkingSite(request), context, new SocialNetworkingUser(
|
||||
currentUser.getScreenName(), currentUser.getEmailAddress(), currentUser.getFullName(), ""));
|
||||
NotificationsManager nm = new ApplicationNotificationsManager(new SocialNetworkingSite(request), context,
|
||||
new SocialNetworkingUser(currentUser.getScreenName(), currentUser.getEmailAddress(),
|
||||
currentUser.getFullName(), ""));
|
||||
Thread thread = new Thread(new MessageNotificationsThread(recipients, messageId, subject, body, nm));
|
||||
thread.start();
|
||||
|
||||
return (messageId != null);
|
||||
return (messageId != null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -283,51 +307,58 @@ public class RequestVRE extends MVCPortlet {
|
|||
* @param workspace
|
||||
* @param listAttachmentsId
|
||||
* @return
|
||||
* @throws Exception
|
||||
* @throws ItemNotFoundException
|
||||
* @throws InternalErrorException
|
||||
* @throws Exception
|
||||
* @throws ItemNotFoundException
|
||||
* @throws InternalErrorException
|
||||
*/
|
||||
private String getPublicLinksForAttachs(Workspace workspace, List<String> listAttachmentsId) throws ItemNotFoundException, InternalErrorException, Exception {
|
||||
private String getPublicLinksForAttachs(Workspace workspace, List<String> listAttachmentsId)
|
||||
throws ItemNotFoundException, InternalErrorException {
|
||||
|
||||
if (listAttachmentsId != null && (!listAttachmentsId.isEmpty()) ) {
|
||||
if (listAttachmentsId != null && (!listAttachmentsId.isEmpty())) {
|
||||
List<WorkspaceItem> attachments = new ArrayList<WorkspaceItem>();
|
||||
for (String itemId : listAttachmentsId) {
|
||||
attachments.add(workspace.getItem(itemId));
|
||||
try {
|
||||
attachments.add(workspace.getItem(itemId));
|
||||
} catch (Exception e) {
|
||||
_log.error(
|
||||
"getPublicLinksForAttachs FAILED while trying workspace.getItem id=" + itemId);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
if(attachments!=null && attachments.size() > 0){
|
||||
if (attachments != null && attachments.size() > 0) {
|
||||
builder.append("\n\n\nThe following ");
|
||||
String msg = attachments.size()>1?"files were attached to this message:":"file was attached to this message:";
|
||||
builder.append(msg+"\n");
|
||||
String msg = attachments.size() > 1 ? "files were attached to this message:"
|
||||
: "file was attached to this message:";
|
||||
builder.append(msg + "\n");
|
||||
for (WorkspaceItem workspaceItem : attachments) {
|
||||
|
||||
if(workspaceItem.getType().equals(WorkspaceItemType.FILE_ITEM)) {
|
||||
if (workspaceItem.getType().equals(WorkspaceItemType.FILE_ITEM)) {
|
||||
FolderItem folderItem = (FolderItem) workspaceItem;
|
||||
String publicLink = "";
|
||||
String itemName = "";
|
||||
try {
|
||||
itemName = workspaceItem.getName();
|
||||
publicLink = workspace.getPublicLinkForFile(folderItem.getId()).toString();
|
||||
}
|
||||
catch (Exception e) {
|
||||
_log.warn("An error occurred when creating public link for attachment, skipping file: " + itemName);
|
||||
} catch (Exception e) {
|
||||
_log.error("An error occurred when creating public link for attachment, skipping file: "
|
||||
+ itemName);
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
|
||||
}
|
||||
builder.append(itemName + " ("+publicLink+")");
|
||||
builder.append(itemName + " (" + publicLink + ")");
|
||||
builder.append("\n");
|
||||
}
|
||||
}
|
||||
_log.debug("returning public links: "+builder.toString());
|
||||
_log.debug("returning public links: " + builder.toString());
|
||||
return builder.toString();
|
||||
}
|
||||
else return "";
|
||||
}
|
||||
else return "";
|
||||
} else
|
||||
return "";
|
||||
} else
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue