From 5b68d9fb5d5794bb7a4f426b5d95a0040ea8b77d Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Fri, 8 Nov 2013 16:33:05 +0000 Subject: [PATCH] Fixed the problem of progress indicator on file upload progress bar git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-gwt-service@85240 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../gwtservice/server/LocalUploadServlet.java | 2 +- .../server/file/FileUploadListener.java | 54 ++++++++++++++++--- .../shared/file/FileUploadMonitor.java | 39 ++++++++------ 3 files changed, 72 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/LocalUploadServlet.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/LocalUploadServlet.java index 33635a6..dafa5c0 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/LocalUploadServlet.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/LocalUploadServlet.java @@ -76,7 +76,7 @@ public class LocalUploadServlet extends HttpServlet { ServletFileUpload upload = new ServletFileUpload(factory); - FileUploadListener uploadListener = new FileUploadListener(fileUploadSession.getFileUploadMonitor()); + FileUploadListener uploadListener = new FileUploadListener(fileUploadMonitor); upload.setProgressListener(uploadListener); FileItem uploadItem = null; diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/file/FileUploadListener.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/file/FileUploadListener.java index af144f8..b1db2bc 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/file/FileUploadListener.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/file/FileUploadListener.java @@ -3,8 +3,14 @@ */ package org.gcube.portlets.user.td.gwtservice.server.file; +import javax.servlet.http.HttpSession; + import org.apache.commons.fileupload.ProgressListener; +import org.gcube.portlets.user.td.gwtservice.server.SessionUtil; import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadMonitor; +import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadState; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -13,19 +19,55 @@ import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadMonitor; * */ public class FileUploadListener implements ProgressListener { - + + protected Logger logger=LoggerFactory.getLogger(FileUploadListener.class); + protected HttpSession session; protected FileUploadMonitor fileUploadMonitor; - + + private long num100Ks = 0; + private long theBytesRead = 0; + private long theContentLength = -1; + private int whichItem = 0; + private int percentDone = 0; + private boolean contentLengthKnown = false; + + + public FileUploadListener(FileUploadMonitor fileUploadMonitor) { - this.fileUploadMonitor = fileUploadMonitor; + this.fileUploadMonitor=fileUploadMonitor; + //this.session=session; + } /** * {@inheritDoc} */ - public void update(long pBytesRead, long pContentLength, int pItems) { - fileUploadMonitor.setTotalLenght(pContentLength); - fileUploadMonitor.setElaboratedLenght(pBytesRead); + public void update(long bytesRead, long contentLength, int items) { + if (contentLength > -1) { + contentLengthKnown = true; + } + theBytesRead = bytesRead; + theContentLength = contentLength; + whichItem = items; + + long nowNum100Ks = bytesRead / 100000; + // Only run this code once every 100K + if (nowNum100Ks > num100Ks) { + num100Ks = nowNum100Ks; + if (contentLengthKnown) { + percentDone = (int) Math.round(100.00 * bytesRead / contentLength); + } + + } + + //FileUploadSession fileUploadSession=SessionUtil.getFileUploadSession(session); + //FileUploadMonitor fileUploadMonitor=fileUploadSession.getFileUploadMonitor(); + fileUploadMonitor.setTotalLenght(contentLength); + fileUploadMonitor.setElaboratedLenght(bytesRead); + fileUploadMonitor.setPercentDone(Float.valueOf(percentDone)/100); + + fileUploadMonitor.setState(FileUploadState.INPROGRESS); + logger.debug("File Upload: "+fileUploadMonitor.toString()); } } diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/file/FileUploadMonitor.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/file/FileUploadMonitor.java index 68f3215..f7662fc 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/file/FileUploadMonitor.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/file/FileUploadMonitor.java @@ -20,6 +20,7 @@ public class FileUploadMonitor implements Serializable { protected FileUploadState state; protected String failureReason; protected String failureDetails; + protected float percentDone; public FileUploadMonitor(){ state = FileUploadState.INPROGRESS; @@ -30,6 +31,7 @@ public class FileUploadMonitor implements Serializable { this.elaboratedLenght = elaboratedLenght; this.state = state; this.failureReason = failureReason; + this.percentDone=0; } /** @@ -58,6 +60,22 @@ public class FileUploadMonitor implements Serializable { return failureDetails; } + public float getPercentDone() { + return percentDone; + } + + public void setPercentDone(float percentDone) { + this.percentDone = percentDone; + } + + public void setFailureReason(String failureReason) { + this.failureReason = failureReason; + } + + public void setFailureDetails(String failureDetails) { + this.failureDetails = failureDetails; + } + public void setState(FileUploadState state) { this.state = state; @@ -93,24 +111,13 @@ public class FileUploadMonitor implements Serializable { this.failureDetails = failureDetails; } - /** - * {@inheritDoc} - */ @Override public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("OperationProgress [totalLenght="); - builder.append(totalLenght); - builder.append(", elaboratedLenght="); - builder.append(elaboratedLenght); - builder.append(", state="); - builder.append(state); - builder.append(", failureReason="); - builder.append(failureReason); - builder.append(", failureDetails="); - builder.append(failureDetails); - builder.append("]"); - return builder.toString(); + return "FileUploadMonitor [totalLenght=" + totalLenght + + ", elaboratedLenght=" + elaboratedLenght + ", state=" + state + + ", failureReason=" + failureReason + ", failureDetails=" + + failureDetails + ", percentDone=" + percentDone + "]"; } + }