From 284f2e5dcaf7fbb7cafa97244a6fa623851b0fcf Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Tue, 30 Jul 2024 15:41:03 +0200 Subject: [PATCH] added getHeaders iterator --- .../client/uploader/MultipleDilaogUpload.java | 116 +--- .../server/WorkspaceUploadServletStream.java | 643 ++++++++++-------- 2 files changed, 390 insertions(+), 369 deletions(-) diff --git a/src/main/java/org/gcube/portlets/widgets/workspaceuploader/client/uploader/MultipleDilaogUpload.java b/src/main/java/org/gcube/portlets/widgets/workspaceuploader/client/uploader/MultipleDilaogUpload.java index e4885ab..f9472c4 100644 --- a/src/main/java/org/gcube/portlets/widgets/workspaceuploader/client/uploader/MultipleDilaogUpload.java +++ b/src/main/java/org/gcube/portlets/widgets/workspaceuploader/client/uploader/MultipleDilaogUpload.java @@ -43,21 +43,17 @@ public class MultipleDilaogUpload extends DialogUpload { private String idFolder; private UPLOAD_TYPE type; - /** * Instantiates a new multiple dilaog upload stream. * - * @param headerTitle - * the header title - * @param parentId - * the parent id - * @param uploadType - * the upload type + * @param headerTitle the header title + * @param parentId the parent id + * @param uploadType the upload type */ public MultipleDilaogUpload(String headerTitle, String parentId, UPLOAD_TYPE uploadType) { super(headerTitle, parentId, uploadType); - this.type =uploadType; + this.type = uploadType; this.idFolder = parentId; fileUploadID = GenerateUUID.get(10, 16); // is tagID @@ -65,7 +61,9 @@ public class MultipleDilaogUpload extends DialogUpload { this.addHandlers(); } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see com.google.gwt.user.client.ui.DialogBox#show() */ @Override @@ -73,7 +71,9 @@ public class MultipleDilaogUpload extends DialogUpload { super.show(); } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see com.google.gwt.user.client.ui.PopupPanel#center() */ @Override @@ -84,13 +84,13 @@ public class MultipleDilaogUpload extends DialogUpload { /** * Generate new upload client keys. * - * @param files the files - * @param parentId the parent id + * @param files the files + * @param parentId the parent id * @return the list */ public void generateFakeUploaders(String filesSelected, String parentId) { - if(filesSelected==null || filesSelected.isEmpty()) + if (filesSelected == null || filesSelected.isEmpty()) return; String[] files = filesSelected.split(FILE_DELEMITER); @@ -99,11 +99,12 @@ public class MultipleDilaogUpload extends DialogUpload { for (int i = 0; i < files.length; i++) { String normalizedFileName = files[i]; if (normalizedFileName.contains("\\")) { - files[i] = normalizedFileName.substring(normalizedFileName.lastIndexOf("\\") + 1); // remove C:\fakepath if exists + files[i] = normalizedFileName.substring(normalizedFileName.lastIndexOf("\\") + 1); // remove C:\fakepath + // if exists } } - GWT.log("generating fake uploaders on: "+Arrays.asList(files.toString())); + GWT.log("generating fake uploaders on: " + Arrays.asList(files.toString())); fakeUploaders = new ArrayList(files.length); for (int i = 0; i < files.length; i++) { WorkspaceUploaderItem fakeItem = new WorkspaceUploaderItem(); @@ -116,7 +117,7 @@ public class MultipleDilaogUpload extends DialogUpload { fakeUploaders.add(fakeItem); } - GWT.log("fakeUploaders generated: "+fakeUploaders.toString()); + GWT.log("fakeUploaders generated: " + fakeUploaders.toString()); // return fakeUploaders; } @@ -141,13 +142,11 @@ public class MultipleDilaogUpload extends DialogUpload { if (result == null) { removeLoading(); - new DialogResult(null, "Error during upload", - "An error occurred during file upload.").center(); + new DialogResult(null, "Error during upload", "An error occurred during file upload.").center(); return; } String strippedResult = new HTML(result).getText(); - final HandlerResultMessage resultMessage = HandlerResultMessage - .parseResult(strippedResult); + final HandlerResultMessage resultMessage = HandlerResultMessage.parseResult(strippedResult); switch (resultMessage.getStatus()) { case ERROR: @@ -159,12 +158,12 @@ public class MultipleDilaogUpload extends DialogUpload { GWT.log("Error during upload " + resultMessage.getMessage()); break; case WARN: { - GWT.log("Upload completed with warnings "+ resultMessage.getMessage()); + GWT.log("Upload completed with warnings " + resultMessage.getMessage()); removeLoading(); break; } - case SESSION_EXPIRED:{ - GWT.log("Upload aborted due to session expired: "+ resultMessage.getMessage()); + case SESSION_EXPIRED: { + GWT.log("Upload aborted due to session expired: " + resultMessage.getMessage()); Window.alert("Session expired, please reload the page"); removeLoading(); break; @@ -193,20 +192,19 @@ public class MultipleDilaogUpload extends DialogUpload { @Override public void onChange(ChangeEvent event) { GWT.log("browse return..."); - if (fileUpload.getFilename() == null - || fileUpload.getFilename().isEmpty()) { + if (fileUpload.getFilename() == null || fileUpload.getFilename().isEmpty()) { GWT.log("No file specified "); MultipleDilaogUpload.this.hide(); return; } String[] files = null; - GWT.log("Current Uploader has id: "+fileUploadID); + GWT.log("Current Uploader has id: " + fileUploadID); String filesSelected = getFilesSelected(fileUploadID, FILE_DELEMITER); GWT.log("getFilesSelected: " + filesSelected); files = filesSelected.split(FILE_DELEMITER); - if(isLimitExceeded(files.length)) + if (isLimitExceeded(files.length)) return; // GENERATE NEW UPLOADERS @@ -223,67 +221,23 @@ public class MultipleDilaogUpload extends DialogUpload { // ADD TO FORM PANEL // initJsonClientKeys(); jsonClientKeys.setValue(jsonKeys); - -// // CASE 1 FILE -// if (files.length == 1) { -// -// // recall: Some browser would write in -// // fileUploadField.getValue() C:\fakepath\$fileName -// final String label = files[0]; -// WorkspaceUploaderServiceAsync.Util.getInstance() -// .itemExistsInWorkpaceFolder(parentIdentifier, -// label, new AsyncCallback() { -// -// @Override -// public void onSuccess( -// final String itemId) { -// GWT.log("itemExistsInWorkpaceFolder: " -// + itemId); -// if (itemId != null) { -// removeItemAndSubmitForm(itemId); -// updateItemSubmitForm(itemId); -//// if (Window.confirm(label+ " exists in folder. If you continue, a new version will be created")) { -//// removeItemAndSubmitForm(itemId); -//// updateItemSubmitForm(itemId); -//// } -// } else -// submitForm(); // ITEM does NOT -// // EXIST SO -// // SUBMIT FORM; -// } -// -// @Override -// public void onFailure(Throwable caught) { -// GWT.log("Sorry an error occurred on the server " -// + caught.getLocalizedMessage() -// + ". Please try again later"); -// Window.alert(caught.getMessage()); -// } -// -// }); -// } else - submitForm(); + submitForm(); } }); - } - - /** * Checks if is limit exceeded. * * @param numbOfFiles the numb of files * @return true, if is limit exceeded */ - public boolean isLimitExceeded(int numbOfFiles){ + public boolean isLimitExceeded(int numbOfFiles) { if (numbOfFiles > ConstantsWorkspaceUploader.LIMIT_UPLOADS) { - Window.alert("Multiple upload limit is " - + ConstantsWorkspaceUploader.LIMIT_UPLOADS - + " files"); + Window.alert("Multiple upload limit is " + ConstantsWorkspaceUploader.LIMIT_UPLOADS + " files"); MultipleDilaogUpload.this.hide(); return true; } @@ -294,11 +248,12 @@ public class MultipleDilaogUpload extends DialogUpload { /** * Adds the new submit to monitor. */ - public void addNewSubmitToMonitor(){ + public void addNewSubmitToMonitor() { GWT.log("addNewSubmitToMonitor..."); int queueIndex = UploaderMonitor.getInstance().newQueue(); for (final WorkspaceUploaderItem workspaceUploaderItem : fakeUploaders) { - UploaderMonitor.getInstance().addNewUploaderToMonitorPanel(workspaceUploaderItem, workspaceUploaderItem.getFile().getFileName()); + UploaderMonitor.getInstance().addNewUploaderToMonitorPanel(workspaceUploaderItem, + workspaceUploaderItem.getFile().getFileName()); setVisible(false); removeLoading(); UploaderMonitor.getInstance().addNewUploaderToQueue(queueIndex, workspaceUploaderItem); @@ -308,7 +263,6 @@ public class MultipleDilaogUpload extends DialogUpload { UploaderMonitor.getInstance().doStartPollingQueue(queueIndex); } - /** * Creates the json key for files. * @@ -330,7 +284,7 @@ public class MultipleDilaogUpload extends DialogUpload { } jsonKeys = productObj.toString(); - GWT.log("updated jsonKeys: "+jsonKeys); + GWT.log("updated jsonKeys: " + jsonKeys); } catch (Exception e) { GWT.log("error " + e.getMessage()); jsonKeys = null; @@ -394,10 +348,8 @@ public class MultipleDilaogUpload extends DialogUpload { /** * Gets the files selected. * - * @param tagId - * the tag id - * @param fileDelimiter - * the file delimiter + * @param tagId the tag id + * @param fileDelimiter the file delimiter * @return the files selected */ public static native String getFilesSelected(final String tagId, final String fileDelimiter) /*-{ diff --git a/src/main/java/org/gcube/portlets/widgets/workspaceuploader/server/WorkspaceUploadServletStream.java b/src/main/java/org/gcube/portlets/widgets/workspaceuploader/server/WorkspaceUploadServletStream.java index dd382e5..9620be9 100644 --- a/src/main/java/org/gcube/portlets/widgets/workspaceuploader/server/WorkspaceUploadServletStream.java +++ b/src/main/java/org/gcube/portlets/widgets/workspaceuploader/server/WorkspaceUploadServletStream.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -24,6 +25,7 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.fileupload.FileItemFactory; +import org.apache.commons.fileupload.FileItemHeaders; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.FileUploadBase; @@ -67,14 +69,12 @@ import org.json.JSONTokener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /** * The Class WorkspaceUploadServletStream. * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * May 10, 2016 + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it May 10, 2016 */ -public class WorkspaceUploadServletStream extends HttpServlet implements Servlet{ +public class WorkspaceUploadServletStream extends HttpServlet implements Servlet { public static final String UNKNOWN_UNKNOWN = "unknown/unknown"; @@ -85,7 +85,8 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet public static final String VRE_ID_ATTR_NAME = "gcube-vreid"; public static final String CURR_GROUP_ID = ConstantsWorkspaceUploader.CURR_GROUP_ID; - //public static final String CURR_USER_ID = ConstantsWorkspaceUploader.CURR_USER_ID; + // public static final String CURR_USER_ID = + // ConstantsWorkspaceUploader.CURR_USER_ID; public static final String UPLOAD_TYPE = ConstantsWorkspaceUploader.UPLOAD_TYPE; @@ -105,15 +106,17 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet public static final String ARCHIVE = "Archive"; - public static final Map SUPPORTED_UNPACKING_ARCHIVE = new HashMap() {{ - put("zip","application/zip"); //.zip - put("7z","application/x-7z-compressed"); //.7z - put("tar","application/x-tar"); //tar - put("java archive","application/java-archive");//.jar - put("jar","application/x-java-archive"); - put("gtar","application/x-gtar"); - put("x-zip","application/x-zip-compressed"); - }}; + public static final Map SUPPORTED_UNPACKING_ARCHIVE = new HashMap() { + { + put("zip", "application/zip"); // .zip + put("7z", "application/x-7z-compressed"); // .7z + put("tar", "application/x-tar"); // tar + put("java archive", "application/java-archive");// .jar + put("jar", "application/x-java-archive"); + put("gtar", "application/x-gtar"); + put("x-zip", "application/x-zip-compressed"); + } + }; public static Logger logger = LoggerFactory.getLogger(WorkspaceUploadServletStream.class); @@ -135,7 +138,7 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet appEngine = isAppEngine(); } - logger.debug("init: appEngine is "+appEngine); + logger.debug("init: appEngine is " + appEngine); } /** @@ -143,32 +146,32 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet */ @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - logger.debug("GET method in "+WorkspaceUploadServletStream.class.getName()+" is running"); + logger.debug("GET method in " + WorkspaceUploadServletStream.class.getName() + " is running"); String clientUploadKey = request.getParameter(CLIENT_UPLOAD_KEYS); - if (clientUploadKey == null){ - sendError(response, "Internal error: UPLOAD KEY NOT FOUND"); - return; - } - logger.debug("GET method CLIENT_UPLOAD_KEY "+clientUploadKey); + if (clientUploadKey == null) { + sendError(response, "Internal error: UPLOAD KEY NOT FOUND"); + return; + } + logger.debug("GET method CLIENT_UPLOAD_KEY " + clientUploadKey); - boolean cancelUpload = Boolean.parseBoolean(request.getParameter(CANCEL_UPLOAD)); - logger.debug("GET method CANCEL_UPLOAD "+cancelUpload); - if (cancelUpload) { - boolean cancelled = cancelUpload(request.getSession(), clientUploadKey); - if(cancelled){ - sendMessage(response, "Upload aborted "+clientUploadKey); + boolean cancelUpload = Boolean.parseBoolean(request.getParameter(CANCEL_UPLOAD)); + logger.debug("GET method CANCEL_UPLOAD " + cancelUpload); + if (cancelUpload) { + boolean cancelled = cancelUpload(request.getSession(), clientUploadKey); + if (cancelled) { + sendMessage(response, "Upload aborted " + clientUploadKey); // try { //// removeCurrentListener(request.getSession(), clientUploadKey); // WsUtil.eraseWorkspaceUploaderInSession(request.getSession(), clientUploadKey); // }catch (Exception e) { // logger.warn("An error occurred during removing cancelled upload from session "); // } - } - else - sendWarnMessage(response, "Upload aborted for id: "+clientUploadKey +" has skipped, already aborted or completed?"); - }else - logger.debug(CANCEL_UPLOAD + " param not found"); - return; + } else + sendWarnMessage(response, + "Upload aborted for id: " + clientUploadKey + " has skipped, already aborted or completed?"); + } else + logger.debug(CANCEL_UPLOAD + " param not found"); + return; } /** @@ -176,8 +179,9 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.info("POST on UploadServlet"); - boolean requestIsNull = request==null; - logger.trace("[1] HttpServletRequest is: null? "+requestIsNull+", URI: "+request.getRequestURI() +", ServerName: "+request.getServerName()); + boolean requestIsNull = request == null; + logger.trace("[1] HttpServletRequest is: null? " + requestIsNull + ", URI: " + request.getRequestURI() + + ", ServerName: " + request.getServerName()); if (!ServletFileUpload.isMultipartContent(request)) { logger.error("ERROR: multipart request not found"); @@ -188,21 +192,20 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet try { logger.info("UPLOAD-SERVLET starting"); - HttpSession session = request.getSession(); - logger.info("UPLOAD-SERVLET session: "+session); - logger.debug("UPLOAD-SERVLET (" + session.getId() + ") new upload request received."); + HttpSession session = request.getSession(); + logger.info("UPLOAD-SERVLET session: " + session); + logger.debug("UPLOAD-SERVLET (" + session.getId() + ") new upload request received."); - - if(WsUtil.isSessionExpired(request)){ - logger.error("SESSION_EXPIRED: session is expired"); - sendSessionExpired(response, "SESSION_EXPIRED: session is expired"); - return; - } + if (WsUtil.isSessionExpired(request)) { + logger.error("SESSION_EXPIRED: session is expired"); + sendSessionExpired(response, "SESSION_EXPIRED: session is expired"); + return; + } String destinationId = null; String uploadType = null; - boolean isOverwrite = true; //CREATE A NEW VERSION OF FILE IS BEHAVIOUR BY DEFAULT - // String clientUploadKey = null; + boolean isOverwrite = true; // CREATE A NEW VERSION OF FILE IS BEHAVIOUR BY DEFAULT + // String clientUploadKey = null; FileItemStream uploadItem = null; ArrayList listClientUploadKeys = null; GCubeUser user = PortalContext.getConfiguration().getCurrentUser(request); @@ -210,55 +213,57 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet ServletFileUpload servletFileUpload = new ServletFileUpload(factory); /** - * An iterator to instances of FileItemStream - * parsed from the request, in the order that they were - *transmitted. + * An iterator to instances of FileItemStream parsed from the + * request, in the order that they were transmitted. */ FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(request); int uploadItemsCnt = 0; String scopeGroupId = ""; - //GET FILE STREAM + // GET FILE STREAM while (fileItemIterator.hasNext()) { FileItemStream item = fileItemIterator.next(); - if (item.isFormField() && ID_FOLDER.equals(item.getFieldName())){ + if (item.isFormField() && ID_FOLDER.equals(item.getFieldName())) { destinationId = Streams.asString(item.openStream()); - logger.debug("ID_FOLDER OK "+destinationId); + logger.debug("ID_FOLDER OK " + destinationId); } - if (item.isFormField() && UPLOAD_TYPE.equals(item.getFieldName())){ + if (item.isFormField() && UPLOAD_TYPE.equals(item.getFieldName())) { uploadType = Streams.asString(item.openStream()); - logger.debug("UPLOAD_TYPE OK " +uploadType); + logger.debug("UPLOAD_TYPE OK " + uploadType); } - if (item.isFormField() && IS_OVERWRITE.equals(item.getFieldName())){ - try{ + if (item.isFormField() && IS_OVERWRITE.equals(item.getFieldName())) { + try { isOverwrite = Boolean.parseBoolean(Streams.asString(item.openStream())); - logger.debug("IS_OVERWRITE OK "+ isOverwrite); - }catch(Exception e){ - //Silent exception; + logger.debug("IS_OVERWRITE OK " + isOverwrite); + } catch (Exception e) { + // Silent exception; } } - if(item.isFormField() && CLIENT_UPLOAD_KEYS.equals(item.getFieldName())){ + if (item.isFormField() && CLIENT_UPLOAD_KEYS.equals(item.getFieldName())) { String jsonClientUploadKey = Streams.asString(item.openStream()); - logger.debug("CLIENT_UPLOAD_KEY OK "+jsonClientUploadKey); + logger.debug("CLIENT_UPLOAD_KEY OK " + jsonClientUploadKey); LinkedHashMap mapKeys = parseJSONClientUploadKeys(jsonClientUploadKey); listClientUploadKeys = new ArrayList(mapKeys.keySet()); removeListenersIfDone(session, listClientUploadKeys); for (String clientUploadKey : listClientUploadKeys) { String fileName = mapKeys.get(clientUploadKey); - WorkspaceUploaderItem workspaceUploader = createNewWorkspaceUploader(clientUploadKey,destinationId,mapKeys.get(clientUploadKey),isOverwrite); - logger.debug("created "+workspaceUploader); - saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.WAIT, "Uploading "+fileName+" at 0%", request.getSession()); + WorkspaceUploaderItem workspaceUploader = createNewWorkspaceUploader(clientUploadKey, + destinationId, mapKeys.get(clientUploadKey), isOverwrite); + logger.debug("created " + workspaceUploader); + saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.WAIT, + "Uploading " + fileName + " at 0%", request.getSession()); } } - if (item.isFormField() && CURR_GROUP_ID.equals(item.getFieldName())){ + if (item.isFormField() && CURR_GROUP_ID.equals(item.getFieldName())) { scopeGroupId = Streams.asString(item.openStream()); logger.debug("currentGroupId passed as parameter = " + scopeGroupId); - logger.debug("currentGroupId into PortalContext scope= " + PortalContext.getConfiguration().getCurrentScope(scopeGroupId)); + logger.debug("currentGroupId into PortalContext scope= " + + PortalContext.getConfiguration().getCurrentScope(scopeGroupId)); } // if (item.isFormField() && CURR_USER_ID.equals(item.getFieldName())){ @@ -267,12 +272,13 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet // logger.debug("currUserinto PortalContext = " + PortalContext.getConfiguration().getCurrentUser(request)); // } - //MUST BE THE LAST PARAMETER TRASMITTED - if (UPLOAD_FORM_ELEMENT.equals(item.getFieldName())){ + // MUST BE THE LAST PARAMETER TRASMITTED + if (UPLOAD_FORM_ELEMENT.equals(item.getFieldName())) { uploadItem = item; - logger.debug("UPLOAD_FORM_ELEMENT OK "+uploadItem.getName() + " scopeGroupId="+scopeGroupId); + logger.debug("UPLOAD_FORM_ELEMENT OK " + uploadItem.getName() + " scopeGroupId=" + scopeGroupId); // break; - uploadData(user, scopeGroupId, request, response, uploadItem, destinationId, uploadType, listClientUploadKeys.get(uploadItemsCnt), isOverwrite); + uploadData(user, scopeGroupId, request, response, uploadItem, destinationId, uploadType, + listClientUploadKeys.get(uploadItemsCnt), isOverwrite); uploadItemsCnt++; } } @@ -288,7 +294,7 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet } } - public File coyStreamToFile(InputStream in, String fileExtension) throws IOException{ + public File coyStreamToFile(InputStream in, String fileExtension) throws IOException { File tempFile = File.createTempFile(UUID.randomUUID().toString(), fileExtension); tempFile.deleteOnExit(); FileOutputStream out = new FileOutputStream(tempFile); @@ -315,27 +321,25 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet // return null; // } - - /** * Removes the listener if done. * * @param session the session - * @param keys the keys + * @param keys the keys */ - private void removeListenersIfDone(HttpSession session, List keys){ + private void removeListenersIfDone(HttpSession session, List keys) { for (String key : keys) { - AbstractUploadProgressListener listener = getCurrentListener(session, key); - if (listener != null) { - logger.debug("Listener found"); - if (listener.isCanceled() || listener.getPercentage() >= 100){ - logger.debug("Listener isCanceled or 100%, removing"); - removeCurrentListener(session, key); - } - }else - logger.debug("Session id: "+session.getId() +" - "+key+" - Listener not found"); + AbstractUploadProgressListener listener = getCurrentListener(session, key); + if (listener != null) { + logger.debug("Listener found"); + if (listener.isCanceled() || listener.getPercentage() >= 100) { + logger.debug("Listener isCanceled or 100%, removing"); + removeCurrentListener(session, key); + } + } else + logger.debug("Session id: " + session.getId() + " - " + key + " - Listener not found"); } } @@ -348,7 +352,8 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet * @throws FileUploadException the file upload exception */ @SuppressWarnings("rawtypes") - private static LinkedHashMap parseJSONClientUploadKeys(final String jsonClientUploadKeys) throws FileUploadException{ + private static LinkedHashMap parseJSONClientUploadKeys(final String jsonClientUploadKeys) + throws FileUploadException { JSONTokener tokener = new JSONTokener(jsonClientUploadKeys); JSONObject root; LinkedHashMap keyFiles = null; @@ -357,76 +362,83 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet root = new JSONObject(tokener); JSONArray jsonArray = root.getJSONArray(JSON_CLIENT_KEYS); keyFiles = new LinkedHashMap(jsonArray.length()); - logger.debug("jsonArray :"+jsonArray.toString()); - for (int i=0; i headerNames = uploadItem.getHeaders().getHeaderNames(); + while(headerNames.hasNext()) { + String headerName = headerNames.next(); + logger.info("headerName: "+headerName +" value: "+itemHeaders.getHeader(headerName)); + } + + String contentLength = itemHeaders.getHeader(FileUploadBase.CONTENT_LENGTH); + if (contentLength != null) { + try { + fileSize = Long.parseLong(contentLength); + } catch (Exception e) { + logger.warn("Error on getting fileSize", e); + } + } + } + + logger.info("File " + FileUploadBase.CONTENT_LENGTH + "/size is: " + fileSize); + + if(fileSize==null) + fileSize = requestContentLenght; + + // CONFIRM DESTINATION FOLDER workspaceUploader.getFile().setParentId(destinationFolder.getId()); - //Instancing the progress listener - final AbstractUploadProgressListener uploadProgressListener = createNewListener(request.getSession(), clientUploadKey); - final UploadProgressInputStream inputStream = new UploadProgressInputStream(uploadItem.openStream(), size); + // Instancing the progress listener + final AbstractUploadProgressListener uploadProgressListener = createNewListener(request.getSession(), + clientUploadKey); + final UploadProgressInputStream inputStream = new UploadProgressInputStream(uploadItem.openStream(), + requestContentLenght); inputStream.addListener(uploadProgressListener); workspaceUploader.setUploadProgress(uploadProgressListener.getUploadProgress()); - - //Commented this. FIXING ISSUE #19232 + // Commented this. FIXING ISSUE #19232 // try { // //USING isOverwrite to check if the file already exists // isOverwrite = storageHubWrapper.getWorkspace().exists(itemName, destinationFolder.getId()); @@ -540,106 +588,116 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet saveWorkspaceUploaderInSession(workspaceUploader, request.getSession()); String contentType = uploadItem.getContentType(); - logger.debug("Stream content type: "+contentType); + logger.debug("Stream content type: " + contentType); - - if(uploadType.compareTo(ARCHIVE)==0){ - //UPLOAD ARCHIVE + if (uploadType.compareTo(ARCHIVE) == 0) { + // UPLOAD ARCHIVE logger.debug("Uploding archive...."); boolean isSupportedArchive = false; for (String archive_extension : SUPPORTED_UNPACKING_ARCHIVE.keySet()) { - //is content-type supported? - if(SUPPORTED_UNPACKING_ARCHIVE.get(archive_extension).compareTo(uploadItem.getContentType())==0){ + // is content-type supported? + if (SUPPORTED_UNPACKING_ARCHIVE.get(archive_extension) + .compareTo(uploadItem.getContentType()) == 0) { isSupportedArchive = true; break; } } - if (isSupportedArchive){ //Uploading Archive OK + if (isSupportedArchive) { // Uploading Archive OK - logger.debug("Supported archive " +uploadItem.getContentType() +" for unpacking"); - //USING the baseName instead of itemName in oder to avoid adding extension as suffix - workspaceUploader = WorkspaceUploaderMng.uploadArchive(storageHubWrapper, user, scopeGroupId, workspaceUploader, request, baseName, inputStream, destinationFolder, size); + logger.debug("Supported archive " + uploadItem.getContentType() + " for unpacking"); + // USING the baseName instead of itemName in oder to avoid adding extension as + // suffix + workspaceUploader = WorkspaceUploaderMng.uploadArchive(storageHubWrapper, user, scopeGroupId, + workspaceUploader, request, baseName, inputStream, destinationFolder, fileSize); - if(workspaceUploader==null) + if (workspaceUploader == null) throw new Exception("Error when creating uploader, it is null!"); /* - * Incident #10095. Commented OK as response - * in order to avoid downloading of .dms file by Safari - * from MAC + * Incident #10095. Commented OK as response in order to avoid downloading of + * .dms file by Safari from MAC */ - //sendMessage(response, workspaceUploader.getIdentifier()); + // sendMessage(response, workspaceUploader.getIdentifier()); - }else{ - logger.warn("Unsuppoterd archive for unpacking: " +uploadItem.getContentType()); - saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.ABORTED, "Unpacking not supported for: "+uploadItem.getContentType()+" Supported archive are: "+SUPPORTED_UNPACKING_ARCHIVE.keySet().toString(), request.getSession()); - sendError(response, "Internal error: Unpacking not supported for "+uploadItem.getContentType()); + } else { + logger.warn("Unsuppoterd archive for unpacking: " + uploadItem.getContentType()); + saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.ABORTED, + "Unpacking not supported for: " + uploadItem.getContentType() + " Supported archive are: " + + SUPPORTED_UNPACKING_ARCHIVE.keySet().toString(), + request.getSession()); + sendError(response, "Internal error: Unpacking not supported for " + uploadItem.getContentType()); return; } - }else{ - //UPLOAD FILE. IT IS DEFAULT CASE + } else { + // UPLOAD FILE. IT IS DEFAULT CASE logger.debug("Uploding file...."); - workspaceUploader = WorkspaceUploaderMng.uploadFile(storageHubWrapper, user, scopeGroupId, request, workspaceUploader, request.getSession(), itemName, inputStream, destinationFolder, contentType, isOverwrite, size); + workspaceUploader = WorkspaceUploaderMng.uploadFile(storageHubWrapper, user, scopeGroupId, request, + workspaceUploader, request.getSession(), itemName, inputStream, destinationFolder, contentType, + isOverwrite, fileSize); - if(workspaceUploader==null) + if (workspaceUploader == null) throw new Exception("Error when creating uploader, it is null!"); /* - * Incident #10095. Commented OK as response - * in order to avoid downloading of .dms file by Safari - * from MAC + * Incident #10095. Commented OK as response in order to avoid downloading of + * .dms file by Safari from MAC */ - //sendMessage(response, workspaceUploader.getIdentifier()); + // sendMessage(response, workspaceUploader.getIdentifier()); } } catch (InsufficientPrivilegesException e) { logger.error("Error creating elements", e); - saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, "Uploading error. Insufficient privileges", request.getSession()); + saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, + "Uploading error. Insufficient privileges", request.getSession()); sendError(response, "Internal error: Insufficient privileges"); return; } catch (InternalErrorException e) { logger.error("Error creating elements", e); - saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, "Uploading error:"+e.getMessage(), request.getSession()); - sendError(response, "Internal error: "+e.getMessage()); + saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, "Uploading error:" + e.getMessage(), + request.getSession()); + sendError(response, "Internal error: " + e.getMessage()); return; } catch (ItemAlreadyExistException e) { logger.error("Error creating elements", e); - saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, "Uploading error. An item with that name already exists", request.getSession()); + saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, + "Uploading error. An item with that name already exists", request.getSession()); sendError(response, "Internal error: An item with that name already exists"); return; - } catch (IOException e){ + } catch (IOException e) { logger.error("Error creating elements, is it cancel?", e); - saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, "Uploading error", request.getSession()); + saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, "Uploading error", + request.getSession()); sendError(response, "Internal error: An item with that name already exists"); return; // sendError(response, "Internal error: An item with that name already exists"); // return; - }catch (Exception e) { + } catch (Exception e) { logger.error("Error creating elements", e); - saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, "Uploading error. "+e.getMessage(), request.getSession()); + saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, "Uploading error. " + e.getMessage(), + request.getSession()); // sendError(response, "Internal error: An error occurred on uploading the file, try again later"); // return; - }finally{ + } finally { removeCurrentListener(request.getSession(), clientUploadKey); } } - /** * Save workspace uploader status. * * @param workspaceUploader the workspace uploader - * @param status the status - * @param description the description - * @param session the session + * @param status the status + * @param description the description + * @param session the session * @return the workspace uploader item */ - private synchronized WorkspaceUploaderItem saveWorkspaceUploaderStatus(WorkspaceUploaderItem workspaceUploader, UPLOAD_STATUS status, String description, HttpSession session){ + private synchronized WorkspaceUploaderItem saveWorkspaceUploaderStatus(WorkspaceUploaderItem workspaceUploader, + UPLOAD_STATUS status, String description, HttpSession session) { workspaceUploader.setUploadStatus(status); workspaceUploader.setStatusDescription(description); try { @@ -650,119 +708,129 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet return workspaceUploader; } - /** * Creates the new workspace uploader. * * @param clientUploadKey the client upload key - * @param folderParentId the folder parent id - * @param fileName the file name - * @param isOverwrite the is overwrite + * @param folderParentId the folder parent id + * @param fileName the file name + * @param isOverwrite the is overwrite * @return the workspace uploader item */ - private WorkspaceUploaderItem createNewWorkspaceUploader(String clientUploadKey, String folderParentId, String fileName, boolean isOverwrite){ - //CLIENT UPLOAD IS THE KEY + private WorkspaceUploaderItem createNewWorkspaceUploader(String clientUploadKey, String folderParentId, + String fileName, boolean isOverwrite) { + // CLIENT UPLOAD IS THE KEY WorkspaceUploaderItem workspaceUploader = new WorkspaceUploaderItem(clientUploadKey); workspaceUploader.setClientUploadKey(clientUploadKey); - //Create File + // Create File WorkspaceUploadFile wsUploadFile = new WorkspaceUploadFile(folderParentId, null, fileName, null); workspaceUploader.setFile(wsUploadFile); workspaceUploader.setIsOverwrite(isOverwrite); return workspaceUploader; } - /** + /** * Gets the random. * * @return the random */ - private static int getRandom(){ - Random randomGenerator = new Random(); - return randomGenerator.nextInt(Integer.MAX_VALUE); - } + private static int getRandom() { + Random randomGenerator = new Random(); + return randomGenerator.nextInt(Integer.MAX_VALUE); + } /** * Save workspace uploader in session. * * @param workspaceUploader the workspace uploader - * @param httpSession the http session + * @param httpSession the http session * @return the workspace uploader item * @throws Exception the exception */ - public static void saveWorkspaceUploaderInSession(WorkspaceUploaderItem workspaceUploader, HttpSession httpSession) throws Exception { + public static void saveWorkspaceUploaderInSession(WorkspaceUploaderItem workspaceUploader, HttpSession httpSession) + throws Exception { - if(workspaceUploader!=null){ + if (workspaceUploader != null) { try { WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader); } catch (Exception e) { - logger.error("Error during WorkspaceUploaderItem save in session workspace uploader: "+workspaceUploader,e); + logger.error( + "Error during WorkspaceUploaderItem save in session workspace uploader: " + workspaceUploader, + e); throw new Exception("An error occurred in the upload. Try again"); } - }else + } else throw new Exception("An error occurred in the upload. Workspace Uploader not found. Abort and try again"); } - //TEST TIME + // TEST TIME /** * Prints the start time. * * @return the long */ - public static Long printStartTime(){ - Long startTime = System.currentTimeMillis(); - logger.debug("Start time: "+startTime); + public static Long printStartTime() { + Long startTime = System.currentTimeMillis(); + logger.debug("Start time: " + startTime); return startTime; } - //TEST TIME + + // TEST TIME /** * Prints the elapsed time. * * @param startTime the start time */ - public static void printElapsedTime(long startTime){ + public static void printElapsedTime(long startTime) { Long endTime = System.currentTimeMillis() - startTime; String time = String.format("%d msc %d sec", endTime, TimeUnit.MILLISECONDS.toSeconds(endTime)); - logger.debug("Elapsed Time: "+time); + logger.debug("Elapsed Time: " + time); } - /** * Notify upload in shared folder. * - * @param storageWrapper the storage wrapper - * @param currUser the curr user - * @param scopeGroupId the scope group id - * @param request the request - * @param httpSession the http session - * @param workspace the workspace - * @param itemId the item id + * @param storageWrapper the storage wrapper + * @param currUser the curr user + * @param scopeGroupId the scope group id + * @param request the request + * @param httpSession the http session + * @param workspace the workspace + * @param itemId the item id * @param destinationFolderId the destination folder id - * @param isOverwrite the is overwrite + * @param isOverwrite the is overwrite */ - public static void notifyUploadInSharedFolder(final StorageHubWrapper storageWrapper, final GCubeUser currUser, final String scopeGroupId, final HttpServletRequest request, final HttpSession httpSession, final String itemId, final String destinationFolderId, final boolean isOverwrite){ - logger.trace("[2] HttpServletRequest is: URI: "+request.getRequestURI() +", ServerName: "+request.getServerName()); - final NotificationsWorkspaceUploaderProducer np = new NotificationsWorkspaceUploaderProducer(scopeGroupId, httpSession,request); - new Thread(){ + public static void notifyUploadInSharedFolder(final StorageHubWrapper storageWrapper, final GCubeUser currUser, + final String scopeGroupId, final HttpServletRequest request, final HttpSession httpSession, + final String itemId, final String destinationFolderId, final boolean isOverwrite) { + logger.trace("[2] HttpServletRequest is: URI: " + request.getRequestURI() + ", ServerName: " + + request.getServerName()); + final NotificationsWorkspaceUploaderProducer np = new NotificationsWorkspaceUploaderProducer(scopeGroupId, + httpSession, request); + new Thread() { public void run() { - //Item sourceItem; + // Item sourceItem; try { Item sourceItem = storageWrapper.getStorageHubClientService().getItem(itemId); String sourceSharedId = null; - try{ + try { sourceSharedId = storageWrapper.getStorageHubClientService().getIdSharedFolder(itemId); - }catch(Exception e){ - //silent + } catch (Exception e) { + // silent } - Item folderDestinationItem = storageWrapper.getStorageHubClientService().getItem(destinationFolderId); + Item folderDestinationItem = storageWrapper.getStorageHubClientService() + .getItem(destinationFolderId); FolderItem folderDestination = null; - if(folderDestinationItem instanceof FolderItem){ + if (folderDestinationItem instanceof FolderItem) { folderDestination = (FolderItem) folderDestinationItem; } Validate.notNull(folderDestination, "The folder destionation is null"); - logger.trace("[3] HttpServletRequest is: URI: "+request.getRequestURI() +", ServerName: "+request.getServerName()); - NotificationsWorkspaceUploader.checkSendNotifyChangedItemToShare(storageWrapper, request, currUser, scopeGroupId, np, httpSession, sourceItem, sourceSharedId, folderDestination); + logger.trace("[3] HttpServletRequest is: URI: " + request.getRequestURI() + ", ServerName: " + + request.getServerName()); + NotificationsWorkspaceUploader.checkSendNotifyChangedItemToShare(storageWrapper, request, currUser, + scopeGroupId, np, httpSession, sourceItem, sourceSharedId, folderDestination); } catch (Exception e) { logger.error("Error in notifyUploadInSharedFolder", e); @@ -776,48 +844,49 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet * Send error. * * @param response the response - * @param message the message + * @param message the message * @throws IOException Signals that an I/O exception has occurred. */ - protected void sendError(HttpServletResponse response, String message) throws IOException{ + protected void sendError(HttpServletResponse response, String message) throws IOException { try { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); HandlerResultMessage resultMessage = HandlerResultMessage.errorResult(message); response.getWriter().write(resultMessage.toString()); - //5.6 Closure of Response Object: - //When a response is closed, the container must immediately flush all remaining content in the response buffer to the client + // 5.6 Closure of Response Object: + // When a response is closed, the container must immediately flush all remaining + // content in the response buffer to the client // response.flushBuffer(); - } catch (IOException e){ - logger.warn("IOException class name: "+e.getClass().getSimpleName()); + } catch (IOException e) { + logger.warn("IOException class name: " + e.getClass().getSimpleName()); if (e.getClass().getSimpleName().equals("ClientAbortException")) - logger.warn("Skipping ClientAbortException: "+e.getMessage()); + logger.warn("Skipping ClientAbortException: " + e.getMessage()); else - throw e; //Sending Exceptions + throw e; // Sending Exceptions } } - /** * Send session expired. * * @param response the response - * @param message the message + * @param message the message * @throws IOException Signals that an I/O exception has occurred. */ - protected void sendSessionExpired(HttpServletResponse response, String message) throws IOException{ + protected void sendSessionExpired(HttpServletResponse response, String message) throws IOException { try { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); HandlerResultMessage resultMessage = HandlerResultMessage.sessionExpiredResult(message); response.getWriter().write(resultMessage.toString()); - //5.6 Closure of Response Object: - //When a response is closed, the container must immediately flush all remaining content in the response buffer to the client + // 5.6 Closure of Response Object: + // When a response is closed, the container must immediately flush all remaining + // content in the response buffer to the client // response.flushBuffer(); - } catch (IOException e){ - logger.warn("IOException class name: "+e.getClass().getSimpleName()); + } catch (IOException e) { + logger.warn("IOException class name: " + e.getClass().getSimpleName()); if (e.getClass().getSimpleName().equals("ClientAbortException")) - logger.warn("Skipping ClientAbortException: "+e.getMessage()); + logger.warn("Skipping ClientAbortException: " + e.getMessage()); else - throw e; //Sending Exceptions + throw e; // Sending Exceptions } } @@ -825,24 +894,25 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet * Send message. * * @param response the response - * @param message the message + * @param message the message * @throws IOException Signals that an I/O exception has occurred. */ - protected void sendMessage(HttpServletResponse response, String message) throws IOException{ + protected void sendMessage(HttpServletResponse response, String message) throws IOException { try { response.setStatus(HttpServletResponse.SC_ACCEPTED); message = message.replaceAll(":", ""); HandlerResultMessage resultMessage = HandlerResultMessage.okResult(message); response.getWriter().write(resultMessage.toString()); - //5.6 Closure of Response Object: - //When a response is closed, the container must immediately flush all remaining content in the response buffer to the client + // 5.6 Closure of Response Object: + // When a response is closed, the container must immediately flush all remaining + // content in the response buffer to the client // response.flushBuffer(); - } catch (IOException e){ - logger.warn("IOException class name: "+e.getClass().getSimpleName()); + } catch (IOException e) { + logger.warn("IOException class name: " + e.getClass().getSimpleName()); if (e.getClass().getSimpleName().equals("ClientAbortException")) - logger.warn("Skipping ClientAbortException: "+e.getMessage()); + logger.warn("Skipping ClientAbortException: " + e.getMessage()); else - throw e; //Sending Exceptions + throw e; // Sending Exceptions } } @@ -850,41 +920,42 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet * Send warn message. * * @param response the response - * @param message the message + * @param message the message * @throws IOException Signals that an I/O exception has occurred. */ - protected void sendWarnMessage(HttpServletResponse response, String message) throws IOException{ + protected void sendWarnMessage(HttpServletResponse response, String message) throws IOException { try { response.setStatus(HttpServletResponse.SC_ACCEPTED); HandlerResultMessage resultMessage = HandlerResultMessage.warnResult(message); response.getWriter().write(resultMessage.toString()); - //5.6 Closure of Response Object: - //When a response is closed, the container must immediately flush all remaining content in the response buffer to the client + // 5.6 Closure of Response Object: + // When a response is closed, the container must immediately flush all remaining + // content in the response buffer to the client // response.flushBuffer(); - } catch (IOException e){ - logger.warn("IOException class name: "+e.getClass().getSimpleName()); + } catch (IOException e) { + logger.warn("IOException class name: " + e.getClass().getSimpleName()); if (e.getClass().getSimpleName().equals("ClientAbortException")) - logger.warn("Skipping ClientAbortException: "+e.getMessage()); + logger.warn("Skipping ClientAbortException: " + e.getMessage()); else - throw e; //Sending Exceptions + throw e; // Sending Exceptions } } /** * Mark the current upload process to be canceled. * - * @param session the session + * @param session the session * @param clientUploadKey the client upload key * @return true, if successful */ public boolean cancelUpload(HttpSession session, String clientUploadKey) { - logger.debug("UPLOAD-SERVLET (" + session.getId()+ ") cancelling Upload: "+clientUploadKey); + logger.debug("UPLOAD-SERVLET (" + session.getId() + ") cancelling Upload: " + clientUploadKey); AbstractUploadProgressListener listener = getCurrentListener(session, clientUploadKey); if (listener != null && !listener.isCanceled()) { - logger.info("CancelUpload listener is "+listener.toString()); + logger.info("CancelUpload listener is " + listener.toString()); listener.setException(new UploadCanceledException()); return true; - }else{ + } else { logger.info("Skipping cancel upload: listener is null or is cancel"); return false; } @@ -893,7 +964,7 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet /** * Get the listener active in this session. * - * @param session the session + * @param session the session * @param clientUploadKey the client upload key * @return the listener active */ @@ -906,8 +977,8 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet } /** - * Just a method to detect whether the web container is running with - * appengine restrictions. + * Just a method to detect whether the web container is running with appengine + * restrictions. * * @return true if the case of the application is running in appengine */ @@ -918,7 +989,7 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet /** * Create a new listener for this session. * - * @param session the session + * @param session the session * @param clientUploadKey the client upload key * @return the appropriate listener */ @@ -930,7 +1001,6 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet } } - /** * Gets the content length. * @@ -940,8 +1010,7 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet private long getContentLength(HttpServletRequest request) { long size = -1; try { - size = Long.parseLong(request - .getHeader(FileUploadBase.CONTENT_LENGTH)); + size = Long.parseLong(request.getHeader(FileUploadBase.CONTENT_LENGTH)); } catch (NumberFormatException e) { } return size; @@ -950,17 +1019,17 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet /** * Remove the listener active in this session. * - * @param session the session + * @param session the session * @param clientUploadKey the client upload key */ protected void removeCurrentListener(HttpSession session, String clientUploadKey) { - logger.debug("RemoveCurrentListener: "+clientUploadKey); + logger.debug("RemoveCurrentListener: " + clientUploadKey); AbstractUploadProgressListener listener = getCurrentListener(session, clientUploadKey); if (listener != null) { - logger.debug("Removing listener: "+listener.getClientUploadKey()); + logger.debug("Removing listener: " + listener.getClientUploadKey()); listener.remove(); - }else - logger.warn("Listener "+clientUploadKey+ "is null"); + } else + logger.warn("Listener " + clientUploadKey + "is null"); } }