Added UploadItemProperties class

This commit is contained in:
Francesco Mangiacrapa 2024-08-02 16:45:16 +02:00
parent 106dfc3a2b
commit 4a3f31a0d7
3 changed files with 196 additions and 107 deletions

View File

@ -34,6 +34,7 @@ import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.Validate;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.common.storagehub.model.items.Item;
@ -49,6 +50,7 @@ import org.gcube.portlets.widgets.workspaceuploader.client.ConstantsWorkspaceUpl
import org.gcube.portlets.widgets.workspaceuploader.server.notification.NotificationsWorkspaceUploader;
import org.gcube.portlets.widgets.workspaceuploader.server.notification.NotificationsWorkspaceUploaderProducer;
import org.gcube.portlets.widgets.workspaceuploader.server.upload.AbstractUploadProgressListener;
import org.gcube.portlets.widgets.workspaceuploader.server.upload.UploadItemProperties;
import org.gcube.portlets.widgets.workspaceuploader.server.upload.MemoryUploadListener;
import org.gcube.portlets.widgets.workspaceuploader.server.upload.UploadCanceledException;
import org.gcube.portlets.widgets.workspaceuploader.server.upload.UploadProgressInputStream;
@ -120,6 +122,11 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
private static boolean appEngine = false;
/**
* Inits the.
*
* @throws ServletException the servlet exception
*/
/*
* (non-Javadoc)
*
@ -244,16 +251,20 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
if (item.isFormField() && CLIENT_UPLOAD_KEYS.equals(item.getFieldName())) {
String jsonClientUploadKey = Streams.asString(item.openStream());
logger.debug("CLIENT_UPLOAD_KEY OK " + jsonClientUploadKey);
LinkedHashMap<String, String> mapKeys = parseJSONClientUploadKeys(jsonClientUploadKey);
listClientUploadKeys = new ArrayList<String>(mapKeys.keySet());
LinkedHashMap<String, UploadItemProperties> theMapOfUploadingFiles = parseJSONClientMapUploadPropertyFiles(
jsonClientUploadKey);
listClientUploadKeys = new ArrayList<String>(theMapOfUploadingFiles.keySet());
removeListenersIfDone(session, listClientUploadKeys);
for (String clientUploadKey : listClientUploadKeys) {
String fileName = mapKeys.get(clientUploadKey);
UploadItemProperties uip = theMapOfUploadingFiles.get(clientUploadKey);
String filename = uip.getFilename();
Long filesize = uip.getFilesize();
WorkspaceUploaderItem workspaceUploader = createNewWorkspaceUploader(clientUploadKey,
destinationId, mapKeys.get(clientUploadKey), isOverwrite);
destinationId, filename, filesize, isOverwrite);
logger.debug("created " + workspaceUploader);
saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.WAIT,
"Uploading " + fileName + " at 0%", request.getSession());
"Uploading " + filename + " at 0%", request.getSession());
}
}
@ -292,6 +303,14 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
}
}
/**
* Coy stream to file.
*
* @param in the in
* @param fileExtension the file extension
* @return the file
* @throws IOException Signals that an I/O exception has occurred.
*/
public File coyStreamToFile(InputStream in, String fileExtension) throws IOException {
File tempFile = File.createTempFile(UUID.randomUUID().toString(), fileExtension);
tempFile.deleteOnExit();
@ -300,25 +319,6 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
return tempFile;
}
// public static InputStream clone(final InputStream inputStream) {
// try {
// inputStream.mark(0);
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// byte[] buffer = new byte[1024];
// int readLength = 0;
// while ((readLength = inputStream.read(buffer)) != -1) {
// outputStream.write(buffer, 0, readLength);
// }
// inputStream.reset();
// outputStream.flush();
// return new ByteArrayInputStream(outputStream.toByteArray());
// }
// catch (Exception ex) {
// ex.printStackTrace();
// }
// return null;
// }
/**
* Removes the listener if done.
*
@ -379,6 +379,53 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
return keyFiles;
}
/**
* Parses the JSON client map upload property files.
*
* @param jsonClientUploadKeys the json client upload keys
* @return the linked hash map
* @throws FileUploadException the file upload exception
*/
@SuppressWarnings("rawtypes")
private static LinkedHashMap<String, UploadItemProperties> parseJSONClientMapUploadPropertyFiles(
final String jsonClientUploadKeys) throws FileUploadException {
JSONTokener tokener = new JSONTokener(jsonClientUploadKeys);
JSONObject root;
LinkedHashMap<String, UploadItemProperties> keyFiles = null;
ObjectMapper objectMapper = new ObjectMapper();
try {
root = new JSONObject(tokener);
JSONArray jsonArray = root.getJSONArray(JSON_CLIENT_KEYS);
keyFiles = new LinkedHashMap<String, UploadItemProperties>(jsonArray.length());
logger.debug("jsonArray :" + jsonArray.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
logger.debug("object :" + object);
String key = (String) object.keys().next();
String value = object.getString(key);
UploadItemProperties uploadRequest;
try {
uploadRequest = objectMapper.readValue(value, UploadItemProperties.class);
logger.debug("key :" + key + ", value: " + uploadRequest);
keyFiles.put(key, uploadRequest);
} catch (IOException e) {
String theError = "Serialization issue on: " + key;
logger.error(theError, e);
throw new JSONException(theError);
}
}
} catch (JSONException e) {
logger.error("An error occurred during parsing file names: " + keyFiles, e);
throw new FileUploadException("An error occurred during parsing file names");
}
logger.debug("keyFiles: " + keyFiles);
return keyFiles;
}
/**
* Upload data.
*
@ -534,31 +581,24 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
logger.debug("purged item name is: " + itemName);
// HttpServletRequest size
long requestContentLenght = getContentLength(request);
logger.info(
"HttpServletRequest " + FileUploadBase.CONTENT_LENGTH + "/size is: " + requestContentLenght);
logger.info("HttpServletRequest " + FileUploadBase.CONTENT_LENGTH + "/size is: " + requestContentLenght);
Long fileSize = null;
/*FileItemHeaders itemHeaders = uploadItem.getHeaders();
if (itemHeaders != null) {
Iterator<String> 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);
}
}
}*/
Long fileSize = workspaceUploader.getFile().getFileSize();
/*
* FileItemHeaders itemHeaders = uploadItem.getHeaders(); if (itemHeaders !=
* null) { Iterator<String> 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 size is: " + workspaceUploader.getFile().getFileSize());
if(fileSize==null)
logger.info("File size is: " + fileSize);
if (fileSize == null)
fileSize = requestContentLenght;
// CONFIRM DESTINATION FOLDER
@ -712,16 +752,19 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
* @param clientUploadKey the client upload key
* @param folderParentId the folder parent id
* @param fileName the file name
* @param filesize
* @param isOverwrite the is overwrite
* @return the workspace uploader item
*/
private WorkspaceUploaderItem createNewWorkspaceUploader(String clientUploadKey, String folderParentId,
String fileName, boolean isOverwrite) {
String fileName, Long filesize, boolean isOverwrite) {
// CLIENT UPLOAD IS THE KEY
WorkspaceUploaderItem workspaceUploader = new WorkspaceUploaderItem(clientUploadKey);
workspaceUploader.setClientUploadKey(clientUploadKey);
// Create File
WorkspaceUploadFile wsUploadFile = new WorkspaceUploadFile(folderParentId, null, fileName, null);
wsUploadFile.setFileSize(filesize);
workspaceUploader.setFile(wsUploadFile);
workspaceUploader.setIsOverwrite(isOverwrite);
return workspaceUploader;
@ -793,7 +836,6 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
* @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

View File

@ -0,0 +1,105 @@
package org.gcube.portlets.widgets.workspaceuploader.server.upload;
import java.io.Serializable;
import org.gcube.portlets.widgets.workspaceuploader.client.ConstantsWorkspaceUploader;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* The Class UploadItemProperties.
*
* contains properties sent from client for any file uploaded
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Aug 1, 2024
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class UploadItemProperties implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7226677146789586756L;
@JsonProperty(ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILENAME)
private String filename;
@JsonProperty(ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILESIZE)
private Long filesize;
/**
* Instantiates a new upload item properties.
*/
public UploadItemProperties() {
}
/**
* Instantiates a new upload item properties.
*
* @param filename the filename
* @param filesize the filesize
*/
public UploadItemProperties(String filename, Long filesize) {
super();
this.filename = filename;
this.filesize = filesize;
}
/**
* Gets the filename.
*
* @return the filename
*/
@JsonProperty(ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILENAME)
public String getFilename() {
return filename;
}
/**
* Gets the filesize.
*
* @return the filesize
*/
@JsonProperty(ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILESIZE)
public Long getFilesize() {
return filesize;
}
/**
* Sets the filename.
*
* @param filename the new filename
*/
public void setFilename(String filename) {
this.filename = filename;
}
/**
* Sets the filesize.
*
* @param filesize the new filesize
*/
public void setFilesize(Long filesize) {
this.filesize = filesize;
}
/**
* To string.
*
* @return the string
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("UploadItemProperties [filename=");
builder.append(filename);
builder.append(", filesize=");
builder.append(filesize);
builder.append("]");
return builder.toString();
}
}

View File

@ -1,58 +0,0 @@
package org.gcube.portlets.widgets.workspaceuploader.shared;
import java.io.Serializable;
import org.gcube.portlets.widgets.workspaceuploader.client.ConstantsWorkspaceUploader;
import com.fasterxml.jackson.annotation.JsonProperty;
public class UpladProperties implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7226677146789586756L;
@JsonProperty(value = ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILENAME)
String filename;
@JsonProperty(value = ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILESIZE)
Long filesize;
public UpladProperties() {
}
public UpladProperties(String filename, Long filesize) {
super();
this.filename = filename;
this.filesize = filesize;
}
public String getFilename() {
return filename;
}
public Long getFilesize() {
return filesize;
}
public void setFilename(String filename) {
this.filename = filename;
}
public void setFilesize(Long filesize) {
this.filesize = filesize;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("UpladProperties [filename=");
builder.append(filename);
builder.append(", filesize=");
builder.append(filesize);
builder.append("]");
return builder.toString();
}
}