public void execute(WebAppController controller, HttpServletRequest req, HttpServletResponse res) throws Exception
{
res.setHeader("Cache-Control", "no-cache");
ExoContainer container = ExoContainerContext.getCurrentContainer();
UploadService service = (UploadService)container.getComponentInstanceOfType(UploadService.class);
if (action == null || action.length() < 1)
return;
UploadServiceAction uploadActionService = UploadServiceAction.valueOf(action.toUpperCase());
if (uploadActionService == UploadServiceAction.PROGRESS)
{
Writer writer = res.getWriter();
if (uploadId == null)
return;
StringBuilder value = new StringBuilder();
value.append("{\n upload : {");
for (int i = 0; i < uploadId.length; i++)
{
UploadResource upResource = service.getUploadResource(uploadId[i]);
if (upResource == null)
continue;
if (upResource.getStatus() == UploadResource.FAILED_STATUS)
{
int limitMB = service.getUploadLimitsMB().get(uploadId[i]).intValue();
value.append("\n \"").append(uploadId[i]).append("\": {");
value.append("\n \"status\":").append('\"').append("failed").append("\",");
value.append("\n \"size\":").append('\"').append(limitMB).append("\"");
value.append("\n }");
continue;
}
double percent = 100;
if (upResource.getStatus() == UploadResource.UPLOADING_STATUS)
{
percent = (upResource.getUploadedSize() * 100) / upResource.getEstimatedSize();
}
value.append("\n \"").append(uploadId[i]).append("\": {");
value.append("\n \"percent\":").append('\"').append((int)percent).append("\",");
value.append("\n \"fileName\":").append('\"').append(encodeName(upResource.getFileName()))
.append("\"");
value.append("\n }");
if (i < uploadId.length - 1)
value.append(',');
}
value.append("\n }\n}");
writer.append(value);
}
else if (uploadActionService == UploadServiceAction.UPLOAD)
{
service.createUploadResource(req);
}
else if (uploadActionService == UploadServiceAction.DELETE)
{
service.removeUpload(uploadId[0]);
}
else if (uploadActionService == UploadServiceAction.ABORT)
{
//TODO: dang.tung - we don't need 2 statements because it'll show error when we reload browser
//UploadResource upResource = service.getUploadResource(uploadId[0]);
//if(upResource != null) upResource.setStatus(UploadResource.UPLOADED_STATUS) ;
service.removeUpload(uploadId[0]);
}
}