Added uploadArchive

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/widgets/workspace-uploader@171508 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-09-18 15:01:31 +00:00
parent 2a64c70052
commit 19a6deac59
2 changed files with 157 additions and 107 deletions

View File

@ -32,6 +32,7 @@ import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.gcube.common.homelibrary.util.MimeTypeUtil;
import org.gcube.common.portal.PortalContext; import org.gcube.common.portal.PortalContext;
import org.gcube.common.storagehub.model.items.FolderItem; import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.common.storagehub.model.items.Item; import org.gcube.common.storagehub.model.items.Item;
@ -550,26 +551,26 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
}else {//IS ARCHIVE UPLOAD }else {//IS ARCHIVE UPLOAD
//TODO UPLOAD ARCHIVE //TODO UPLOAD ARCHIVE
// logger.debug("Archive content type: "+uploadItem.getContentType()); logger.debug("Archive content type: "+uploadItem.getContentType());
//
// if (MimeTypeUtil.isZipContentType(uploadItem.getContentType())){ //UNZIP?? if (MimeTypeUtil.isZipContentType(uploadItem.getContentType())){ //UNZIP??
// logger.debug("Unziping content"); logger.debug("Unziping content");
// //THE WORKSPACE HAS BEEN INSTANCIED ABOVE //THE WORKSPACE HAS BEEN INSTANCIED ABOVE
// workspaceUploader = WorkspaceUploaderMng.uploadArchive(user, scopeGroupId, workspaceUploader, request, itemName, inputStream, destinationFolder, size); workspaceUploader = WorkspaceUploaderMng.uploadArchive(storageHubWrapper, user, scopeGroupId, workspaceUploader, request, itemName, inputStream, destinationFolder, size);
//
// if(workspaceUploader==null) if(workspaceUploader==null)
// throw new Exception("Error when creating uploader, it is null!"); throw new Exception("Error when creating uploader, it is null!");
//
// sendMessage(response, workspaceUploader.getIdentifier()); sendMessage(response, workspaceUploader.getIdentifier());
// } else{ } else{
// //THE WORKSPACE HAS BEEN INSTANCIED ABOVE //THE WORKSPACE HAS BEEN INSTANCIED ABOVE. DEFAULT CASE IS UPLOADING A FILE
// workspaceUploader = WorkspaceUploaderMng.uploadFile(user, scopeGroupId, request, workspaceUploader, request.getSession(), wa, itemName, inputStream, destinationFolder, contentType, isOverwrite, size); workspaceUploader = WorkspaceUploaderMng.uploadFile(storageHubWrapper, user, scopeGroupId, request, workspaceUploader, request.getSession(), itemName, inputStream, destinationFolder, contentType, isOverwrite, size);
//
// if(workspaceUploader==null) if(workspaceUploader==null)
// throw new Exception("Error when creating uploader, it is null!"); throw new Exception("Error when creating uploader, it is null!");
//
// sendMessage(response, workspaceUploader.getIdentifier()); sendMessage(response, workspaceUploader.getIdentifier());
// } }
} }
//saveWorkspaceUploaderInSession(workspaceUploader, request.getSession()); //saveWorkspaceUploaderInSession(workspaceUploader, request.getSession());

View File

@ -56,7 +56,6 @@ public class WorkspaceUploaderMng {
* @param workspaceUploader the workspace uploader * @param workspaceUploader the workspace uploader
* @param httpSession the http session * @param httpSession the http session
* @param isOvewrite the is ovewrite * @param isOvewrite the is ovewrite
* @param wa the wa
* @param uploadFile the upload file * @param uploadFile the upload file
* @param itemName the item name * @param itemName the item name
* @param destinationFolder the destination folder * @param destinationFolder the destination folder
@ -87,9 +86,9 @@ public class WorkspaceUploaderMng {
if(createdItem!=null){ if(createdItem!=null){
WorkspaceUploadServletStream.printElapsedTime(startTime); WorkspaceUploadServletStream.printElapsedTime(startTime);
logger.debug("HL file: "+createdItem.getName() + " with id: "+createdItem.getId() + " uploaded correctly in "+destinationFolder.getPath()); logger.debug("StorageHub file: "+createdItem.getName() + " with id: "+createdItem.getId() + " uploaded correctly in "+destinationFolder.getPath());
workspaceUploader.getFile().setItemId(createdItem.getId()); //SET HL ID workspaceUploader.getFile().setItemId(createdItem.getId()); //SET STORAGEHUB ID
workspaceUploader.getFile().setParentId(createdItem.getParentId());//SET PARENT ID workspaceUploader.getFile().setParentId(createdItem.getParentId());//SET STORAGEHUB PARENT ID
//ADDING VERSION NAME //ADDING VERSION NAME
if(createdItem instanceof FileItem){ if(createdItem instanceof FileItem){
@ -106,7 +105,7 @@ public class WorkspaceUploaderMng {
} }
}else }else
logger.debug("HL file: "+createdItem.getName() + " has not version"); logger.debug("StorageHub file: "+createdItem.getName() + " has not version");
workspaceUploader.setStatusDescription("File \""+createdItem.getName()+"\" uploaded correctly in "+destinationFolder.getPath()); workspaceUploader.setStatusDescription("File \""+createdItem.getName()+"\" uploaded correctly in "+destinationFolder.getPath());
workspaceUploader.setUploadStatus(UPLOAD_STATUS.COMPLETED); workspaceUploader.setUploadStatus(UPLOAD_STATUS.COMPLETED);
@ -170,66 +169,115 @@ public class WorkspaceUploaderMng {
} }
/**
* Creates the workspace uploader archive.
*
* @param storageWrapper the storage wrapper
* @param currUser the curr user
* @param scopeGroupId the scope group id
* @param workspaceUploader the workspace uploader
* @param request the request
* @param uploadArchive the upload archive
* @param itemName the item name
* @param destinationFolder the destination folder
* @param totalBytes the total bytes
* @return the workspace uploader item
* @throws InternalErrorException the internal error exception
* @throws IOException Signals that an I/O exception has occurred.
*/
private static WorkspaceUploaderItem createWorkspaceUploaderArchive(StorageHubWrapper storageWrapper, final GCubeUser currUser, final String scopeGroupId, final WorkspaceUploaderItem workspaceUploader, final HttpServletRequest request, final InputStream uploadArchive, final String itemName, final WorkspaceFolder destinationFolder, final long totalBytes) throws InternalErrorException, IOException{
HttpSession httpSession = request.getSession();
logger.info("calling upload archive - [itemName: "+itemName+"]");
// /** WorkspaceItem createdItem = null;
// * Creates the workspace uploader archive. try {
// * workspaceUploader.setUploadStatus(UPLOAD_STATUS.IN_PROGRESS);
// * @param currUser the curr user workspaceUploader.setStatusDescription("Uploading "+itemName);
// * @param scopeGroupId the scope group id createdItem = storageWrapper.getWorkspace().uploadArchive(destinationFolder.getId(), uploadArchive, itemName);
// * @param workspaceUploader the workspace uploader
// * @param request the request if(createdItem!=null){
// * @param uploadFile the upload file logger.debug("StorageHub"+createdItem.getName() + " with id: "+createdItem.getId() + " uploaded correctly in "+destinationFolder.getPath());
// * @param itemName the item name workspaceUploader.getFile().setItemId(createdItem.getId()); //SET STORAGEHUB ID
// * @param destinationFolder the destination folder workspaceUploader.getFile().setParentId(createdItem.getParentId());//SET STORAGEHUB PARENT ID
// * @param totalBytes the total bytes
// * @return the workspace uploader item //ADDING VERSION NAME
// * @throws InternalErrorException the internal error exception if(createdItem instanceof FileItem){
// * @throws IOException Signals that an I/O exception has occurred. FileItem file = (FileItem) createdItem;
// */ WorkspaceVersion currVersion = file.getCurrentVersion();
// private static WorkspaceUploaderItem createWorkspaceUploaderArchive(final GCubeUser currUser, final String scopeGroupId, final WorkspaceUploaderItem workspaceUploader, final HttpServletRequest request, final InputStream uploadFile, final String itemName, final WorkspaceFolder destinationFolder, final long totalBytes) throws InternalErrorException, IOException{ if(currVersion!=null){
// HttpSession httpSession = request.getSession(); workspaceUploader.getFile().setVersionName(currVersion.getName());
// logger.info("calling upload archive - [itemName: "+itemName+"]"); logger.debug("StorageHub file: "+createdItem.getName() + " has version: "+currVersion.getName());
// try { }
// workspaceUploader.setUploadStatus(UPLOAD_STATUS.IN_PROGRESS); else{
// UnzipUtil.unzip(destinationFolder, uploadFile, itemName); //TODO NO VERSION
// //logger.debug("StorageHub file: "+createdItem.getName() + " has no current version");
// logger.info("Archive "+itemName+ " imported correctly in "+destinationFolder.getPath()); //workspaceUploader.getFile().setVersionName(file.getName());
// workspaceUploader.setStatusDescription("Archive "+itemName+ " imported correctly in "+destinationFolder.getPath()); }
// workspaceUploader.setUploadStatus(UPLOAD_STATUS.COMPLETED);
// }else
// try { logger.debug("StorageHub file: "+createdItem.getName() + " has not version");
workspaceUploader.setStatusDescription("Archive \""+createdItem.getName()+"\" uploaded correctly in "+destinationFolder.getPath());
workspaceUploader.setUploadStatus(UPLOAD_STATUS.COMPLETED);
final boolean isOvewrite = false;
WorkspaceUploadServletStream.notifyUploadInSharedFolder(storageWrapper, currUser, scopeGroupId, request, httpSession, createdItem.getId(), createdItem.getParentId(), isOvewrite);
}else{
workspaceUploader.setStatusDescription("An error occurred during upload: \""+itemName + "\". Try again");
workspaceUploader.setUploadStatus(UPLOAD_STATUS.FAILED);
}
try {
WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
} catch (Exception e1) {
logger.error("Error during WorkspaceUploaderItem session update: ",e1);
}
} catch (InternalErrorException e) {
logger.error("Error during upload: ",e);
workspaceUploader.setStatusDescription("An error occurred server-side during upload: "+itemName+". "+e.getMessage() +". Try again");
workspaceUploader.setUploadStatus(UPLOAD_STATUS.FAILED);
try {
WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
} catch (Exception e1) {
logger.error("Error during WorkspaceUploaderItem session update: ",e1);
}
} catch (InsufficientPrivilegesException | ItemAlreadyExistException | WorkspaceFolderNotFoundException | WrongDestinationException e) {
logger.error("Error during file uploading: ",e);
workspaceUploader.setUploadStatus(UPLOAD_STATUS.FAILED);
if (e instanceof InsufficientPrivilegesException){
String folderName = destinationFolder.getName();
workspaceUploader.setStatusDescription("You have not permission to upload in the folder: "+folderName);
}else{
workspaceUploader.setStatusDescription("An error occurred during upload: "+itemName+". "+e.getMessage());
}
try {
WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
} catch (Exception e1) {
logger.error("Error during WorkspaceUploaderItem session update: ",e1);
}
//IS unreachable
}catch(UploadCanceledException e){
logger.info("UploadCanceledException thrown by client..");
workspaceUploader.setStatusDescription("Aborted upload: "+itemName);
workspaceUploader.setUploadStatus(UPLOAD_STATUS.ABORTED);
try {
// WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader); // WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
// } catch (Exception e1) { // workspaceUploader.setErasable(true);
// logger.error("Error during WorkspaceUploaderItem session update: ",e1); WsUtil.forceEraseWorkspaceUploaderInSession(httpSession, workspaceUploader);
// } } catch (Exception e1) {
// } catch (InternalErrorException e) { logger.error("Error during WorkspaceUploaderItem session update: ",e1);
// logger.error("Error during uploading: ",e); }
// workspaceUploader.setStatusDescription("An error occurred during upload: "+itemName+". "+e.getMessage()); }finally{
// workspaceUploader.setUploadStatus(UPLOAD_STATUS.FAILED); try {
// try { // StreamUtils.deleteTempFile(uploadFile);
// WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader); WsUtil.setErasableWorkspaceUploaderInSession(request, workspaceUploader.getIdentifier());
// } catch (Exception e1) { } catch (Exception e2) {
// logger.error("Error during WorkspaceUploaderItem session update: ",e1); logger.error("Error during setErasableWorkspaceUploaderInSession session update: ",e2);
// } }
// }catch(UploadCanceledException e){ }
// logger.info("UploadCanceledException thrown by client.."); return workspaceUploader;
// workspaceUploader.setStatusDescription("Aborted upload: "+itemName); }
// workspaceUploader.setUploadStatus(UPLOAD_STATUS.ABORTED);
// try {
// WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
// } catch (Exception e1) {
// logger.error("Error during WorkspaceUploaderItem session update: ",e1);
// }
// }finally{
// try {
//// StreamUtils.deleteTempFile(uploadFile);
// WsUtil.setErasableWorkspaceUploaderInSession(request, workspaceUploader.getIdentifier());
// } catch (Exception e2) {
// // TODO: handle exception
// }
// }
// return workspaceUploader;
// }
/** /**
@ -261,30 +309,31 @@ public class WorkspaceUploaderMng {
} }
// /** /**
// * Upload archive. * Upload archive.
// * *
// * @param currUser the curr user * @param storageWrapper the storage wrapper
// * @param scopeGroupId the scope group id * @param currUser the curr user
// * @param workspaceUploader the workspace uploader * @param scopeGroupId the scope group id
// * @param request the request * @param workspaceUploader the workspace uploader
// * @param itemName the item name * @param request the request
// * @param file the file * @param itemName the item name
// * @param destinationFolder the destination folder * @param file the file
// * @param totalBytes the total bytes * @param destinationFolder the destination folder
// * @return the workspace uploader item * @param totalBytes the total bytes
// * @throws Exception the exception * @return the workspace uploader item
// */ * @throws Exception the exception
// public static WorkspaceUploaderItem uploadArchive(GCubeUser currUser, String scopeGroupId, WorkspaceUploaderItem workspaceUploader, HttpServletRequest request, String itemName, InputStream file, WorkspaceFolder destinationFolder, long totalBytes) throws Exception { */
// public static WorkspaceUploaderItem uploadArchive(StorageHubWrapper storageWrapper, GCubeUser currUser, String scopeGroupId, WorkspaceUploaderItem workspaceUploader, HttpServletRequest request, String itemName, InputStream file, WorkspaceFolder destinationFolder, long totalBytes) throws Exception {
// try {
// return createWorkspaceUploaderArchive(currUser, scopeGroupId, workspaceUploader, request, file, itemName, destinationFolder, totalBytes); try {
// return createWorkspaceUploaderArchive(storageWrapper, currUser, scopeGroupId, workspaceUploader, request, file, itemName, destinationFolder, totalBytes);
// } catch (Exception e) {
// logger.error("Error when uploading Archive to HL creation: ",e); } catch (Exception e) {
// throw new Exception("An error occurred during upload:: "+itemName+". Try again"); logger.error("Error when uploading Archive to HL creation: ",e);
// } throw new Exception("An error occurred during upload:: "+itemName+". Try again");
// } }
}
/** /**
* Upload file status. * Upload file status.