|
|
|
@ -1,6 +1,9 @@
|
|
|
|
|
package org.gcube.portlets.user.sbdportlets;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -8,6 +11,8 @@ import java.util.List;
|
|
|
|
|
import javax.portlet.ActionRequest;
|
|
|
|
|
import javax.portlet.ActionResponse;
|
|
|
|
|
import javax.portlet.PortletException;
|
|
|
|
|
import javax.portlet.ResourceRequest;
|
|
|
|
|
import javax.portlet.ResourceResponse;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
|
|
|
|
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager;
|
|
|
|
@ -17,11 +22,15 @@ import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser;
|
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
|
import org.gcube.common.homelibary.model.items.type.WorkspaceItemType;
|
|
|
|
|
import org.gcube.common.homelibrary.home.HomeLibrary;
|
|
|
|
|
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
|
|
|
|
|
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.Workspace;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
|
|
|
|
|
import org.gcube.common.homelibrary.util.WorkspaceUtil;
|
|
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
|
import org.gcube.portal.notifications.bean.GenericItemBean;
|
|
|
|
@ -30,10 +39,15 @@ import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
|
|
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
|
|
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
|
|
|
|
|
|
|
|
|
import com.liferay.portal.kernel.json.JSONFactoryUtil;
|
|
|
|
|
import com.liferay.portal.kernel.json.JSONObject;
|
|
|
|
|
import com.liferay.portal.kernel.log.Log;
|
|
|
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
|
|
|
|
import com.liferay.portal.kernel.servlet.SessionErrors;
|
|
|
|
|
import com.liferay.portal.kernel.servlet.SessionMessages;
|
|
|
|
|
import com.liferay.portal.kernel.upload.FileItem;
|
|
|
|
|
import com.liferay.portal.kernel.upload.UploadPortletRequest;
|
|
|
|
|
import com.liferay.portal.kernel.util.FileUtil;
|
|
|
|
|
import com.liferay.portal.kernel.util.ParamUtil;
|
|
|
|
|
import com.liferay.portal.kernel.util.WebKeys;
|
|
|
|
|
import com.liferay.portal.model.User;
|
|
|
|
@ -49,7 +63,11 @@ import com.liferay.util.bridges.mvc.MVCPortlet;
|
|
|
|
|
public class UploadDataset extends MVCPortlet {
|
|
|
|
|
private static Log _log = LogFactoryUtil.getLog(UploadDataset.class);
|
|
|
|
|
private static String UPLOAD_DATASET_ROLE_MANAGER_NAME = "UserUpload-Admin";
|
|
|
|
|
|
|
|
|
|
private final static String UPLOADED_FILE_ATTR_NAME = "myfile";
|
|
|
|
|
//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";
|
|
|
|
|
|
|
|
|
|
private LiferayRoleManager roleManager = new LiferayRoleManager();
|
|
|
|
|
private LiferayUserManager userManager = new LiferayUserManager();
|
|
|
|
|
|
|
|
|
@ -85,6 +103,76 @@ public class UploadDataset extends MVCPortlet {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
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);
|
|
|
|
|
File multipartFile = uploadRequest.getFile(UPLOADED_FILE_ATTR_NAME);
|
|
|
|
|
|
|
|
|
|
FileItem[] items = uploadRequest.getMultipartParameterMap().get(UPLOADED_FILE_ATTR_NAME);
|
|
|
|
|
String fileName = "";
|
|
|
|
|
String contentType = "";
|
|
|
|
|
for (int i = 0; i < items.length; i++) {
|
|
|
|
|
fileName = items[i].getFileName();
|
|
|
|
|
contentType = items[i].getContentType();
|
|
|
|
|
System.out.println(fileName);
|
|
|
|
|
System.out.println(contentType);
|
|
|
|
|
}
|
|
|
|
|
// Path written = Files.write(Paths.get(System.getProperty("catalina.home")+"/temp/"+fileName), FileUtil.getBytes(multipartFile));
|
|
|
|
|
// System.out.println("Written File = " + written.toRealPath());
|
|
|
|
|
JSONObject fileObject = JSONFactoryUtil.createJSONObject();
|
|
|
|
|
try {
|
|
|
|
|
Workspace workspace = getWorkspace(uploadRequest);
|
|
|
|
|
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);
|
|
|
|
|
} else {
|
|
|
|
|
_log.debug("nothing to do");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param uploadRequest is the XHR http servlet request
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private Workspace getWorkspace(UploadPortletRequest uploadRequest ) throws Exception {
|
|
|
|
|
Workspace workspace;
|
|
|
|
|
try{
|
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
|
|
|
ScopeProvider.instance.set(pContext.getCurrentScope(uploadRequest));
|
|
|
|
|
workspace = HomeLibrary.getUserWorkspace(pContext.getCurrentUser(uploadRequest).getUsername());
|
|
|
|
|
}catch(InternalErrorException | HomeNotFoundException | WorkspaceFolderNotFoundException e){
|
|
|
|
|
String msg = "Sorry, an error occurred when retrieving workspace item, Refresh an try again";
|
|
|
|
|
_log.error("HL error: ",e);
|
|
|
|
|
throw new Exception(msg);
|
|
|
|
|
}
|
|
|
|
|
return workspace;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String uploadToUserWorkspace(Workspace ownerWS, String fileName, String mimeType,InputStream fileData) throws Exception {
|
|
|
|
|
WorkspaceFolder attachment = null;
|
|
|
|
|
try{
|
|
|
|
|
attachment = (WorkspaceFolder) ownerWS.getItemByPath(ownerWS.getRoot().getPath() + "/" + ATTACHMENT_FOLDER);
|
|
|
|
|
} catch (ItemNotFoundException e) {
|
|
|
|
|
_log.info(ATTACHMENT_FOLDER + " Workspace Folder does not exists, creating it ");
|
|
|
|
|
attachment = ownerWS.getRoot().createFolder(ATTACHMENT_FOLDER, "Folder created automatically by the System");
|
|
|
|
|
}
|
|
|
|
|
String itemName = WorkspaceUtil.getUniqueName(fileName, attachment);
|
|
|
|
|
FolderItem item = WorkspaceUtil.createExternalFile(attachment, itemName, "File created automatically by the System", mimeType, fileData);
|
|
|
|
|
String toReturn = item.getId();
|
|
|
|
|
_log.debug("Uploaded " + item.getName() + " - Returned Workspace id=" +toReturn);
|
|
|
|
|
return toReturn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean sendNotificationToUser(HttpServletRequest request, long groupId, User currentUser, List<GCubeUser> theManagers
|
|
|
|
|
, String method_name, String people_institutions, String demo_tutorial, String[] attachmentIds, String experiment_tags) throws Exception {
|
|
|
|
|
String scope = PortalContext.getConfiguration().getCurrentScope(""+groupId);
|
|
|
|
|