329: Workspace download servlet return wrong size if file is bigger than 2GB

Task-Url: https://support.d4science.org/issues/329

Added work around to manage big data (> 2GB)

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@115802 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2015-07-02 14:53:43 +00:00
parent 25b563d07d
commit f1ecb642b0
2 changed files with 37 additions and 187 deletions

View File

@ -5,7 +5,6 @@ package org.gcube.portlets.user.workspace.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -20,7 +19,6 @@ import javax.servlet.http.HttpSession;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
@ -146,12 +144,9 @@ public class DownloadServlet extends HttpServlet{
File tmpZip = ZipUtil.zipFolder((WorkspaceFolder) item);
resp.setHeader( "Content-Disposition", "attachment; filename=\"" + item.getName() + ".zip\"" );
resp.setContentType("application/zip");
resp.setContentLength((int) tmpZip.length());
resp = setContentLength(resp, tmpZip.length());
OutputStream out = resp.getOutputStream();
//MODIFIED 22-05-2013 CLOSE STREAM
// IOUtils.copy(new FileInputStream(tmpZip), resp.getOutputStream());
FileInputStream fileTmpZip = new FileInputStream(tmpZip);
IOUtils.copy(fileTmpZip, resp.getOutputStream());
fileTmpZip.close();
@ -163,7 +158,6 @@ public class DownloadServlet extends HttpServlet{
} catch (Exception e) {
logger.error("Error during folder compression "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during folder compression: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during folder compression: "+e.getMessage());
return;
}
}
@ -175,29 +169,22 @@ public class DownloadServlet extends HttpServlet{
case REPORT_TEMPLATE:{
try{
ReportTemplate reportTemplate = (ReportTemplate)folderItem;
String extension = FilenameUtils.getExtension(item.getName());
String itemName = item.getName();
logger.trace("case REPORT_TEMPLATE extension is" +extension);
if(extension.compareToIgnoreCase(Extensions.REPORT_TEMPLATE.getName())!=0) //ADD EXTENSION?
itemName = "." + Extensions.REPORT_TEMPLATE.getName();
logger.trace("case REPORT_TEMPLATE itemName is" +extension);
// String itemName = item.getName() + "." + Extensions.REPORT_TEMPLATE.getValue();
String contentDisposition = (viewContent)?"inline":"attachment";
resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" );
resp.setContentType("application/zip");
resp.setContentLength((int) reportTemplate.getLength());
resp = setContentLength(resp, reportTemplate.getLength());
OutputStream out = resp.getOutputStream();
//MODIFIED 22-05-2013 CLOSE STREAM
// IOUtils.copy(reportTemplate.getData(), resp.getOutputStream());
InputStream is = reportTemplate.getData();
IOUtils.copy(is, resp.getOutputStream());
is.close();
@ -206,7 +193,6 @@ public class DownloadServlet extends HttpServlet{
} catch (Exception e) {
logger.error("Error during external item sending "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
return;
@ -215,28 +201,22 @@ public class DownloadServlet extends HttpServlet{
case REPORT:{
try{
Report report = (Report)folderItem;
String extension = FilenameUtils.getExtension(item.getName());
String itemName = item.getName();
logger.trace("case REPORT extension is" +extension);
if(extension.compareToIgnoreCase(Extensions.REPORT.getName())!=0) //ADD EXTENSION?
itemName = "." + Extensions.REPORT.getName();
logger.trace("case REPORT itemName is" +extension);
// String itemName = item.getName() + "." + Extensions.REPORT.getValue();
String contentDisposition = (viewContent)?"inline":"attachment";
resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" );
resp.setContentType("application/zip");
resp.setContentLength((int) report.getLength());
resp = setContentLength(resp, report.getLength());
OutputStream out = resp.getOutputStream();
//MODIFIED 22-05-2013 CLOSE STREAM
// IOUtils.copy(report.getData(), resp.getOutputStream());
InputStream is = report.getData();
IOUtils.copy(is, resp.getOutputStream());
is.close();
@ -245,7 +225,6 @@ public class DownloadServlet extends HttpServlet{
} catch (Exception e) {
logger.error("Error during external item sending "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
return;
@ -262,28 +241,11 @@ public class DownloadServlet extends HttpServlet{
String mimeType = externalFile.getMimeType();
logger.trace("EXTERNAL_FILE DOWNLOAD FOR "+externalFile.getId());
//COMMENTED 26/03/2013
// String itemName = MimeTypeUtil.getNameWithExtension(item.getName(), mimeType);
String contentDisposition = (viewContent)?"inline":"attachment";
//COMMENTED 26/03/2013
// resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" );
resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + item.getName() + "\"" );
resp.setHeader("Content-Disposition", contentDisposition+"; filename=\"" + item.getName() + "\"" );
resp.setContentType(mimeType);
resp.setContentLength((int) externalFile.getLength());
//MODIFIED 22-05-2013 CLOSE STREAM
// IOUtils.copy(externalFile.getData(), resp.getOutputStream());
resp = setContentLength(resp, externalFile.getLength());
is = externalFile.getData();
// //REMOVE THIS -- TODO TEMPORARY SOLUTION
// if(is==null){
// logger.error("Error during get input stream: IS is null");
// throw new Exception("Item is not reachable");
// }
out = resp.getOutputStream();
IOUtils.copy(is, out);
@ -293,7 +255,6 @@ public class DownloadServlet extends HttpServlet{
} catch (Exception e) {
logger.error("Error during external item retrieving "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
return;
@ -310,21 +271,9 @@ public class DownloadServlet extends HttpServlet{
String contentDisposition = (viewContent)?"inline":"attachment";
resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" );
resp.setContentType(externalImage.getMimeType());
resp.setContentLength((int) externalImage.getLength());
//MODIFIED 22-05-2013 CLOSE STREAM
// IOUtils.copy(externalImage.getData(), resp.getOutputStream());
resp = setContentLength(resp, externalImage.getLength());
OutputStream out = resp.getOutputStream();
InputStream is = externalImage.getData();
// //REMOVE THIS -- TODO TEMPORARY SOLUTION
// if(is==null){
// logger.error("Error during get input stream: IS is null");
// throw new Exception("Item is not reachable");
// }
IOUtils.copy(is, out);
is.close();
@ -333,7 +282,6 @@ public class DownloadServlet extends HttpServlet{
} catch (Exception e) {
logger.error("Error during item retrieving "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}
@ -341,18 +289,11 @@ public class DownloadServlet extends HttpServlet{
try{
ExternalUrl externalUrl = (ExternalUrl)folderItem;
//ADDED 20/06/2013
String itemName = MimeTypeUtil.getNameWithExtension(externalUrl.getName(), "text/uri-list");
String contentDisposition = (viewContent)?"inline":"attachment";
resp.setHeader("Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" );
// //ADDED 20/06/2013
// String itemName = externalUrl.getName() + ".uri";
// String contentDisposition = (viewContent)?"inline":"attachment";
// resp.setHeader("Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" );
resp.setContentType("text/uri-list");
resp.setContentLength((int) externalUrl.getLength());
resp = setContentLength(resp, externalUrl.getLength());
//MODIFIED 22-05-2013 CLOSE STREAM
StringReader sr = new StringReader(externalUrl.getUrl());
@ -365,7 +306,6 @@ public class DownloadServlet extends HttpServlet{
} catch (Exception e) {
logger.error("Error during item retrieving "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}
@ -374,11 +314,10 @@ public class DownloadServlet extends HttpServlet{
Query query = (Query)folderItem;
resp.setContentType("text/plain");
try {
resp.setContentLength((int) query.getLength());
resp = setContentLength(resp, query.getLength());
} catch (Exception e) {
logger.error("Error getting item lenght "+query,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
@ -399,9 +338,8 @@ public class DownloadServlet extends HttpServlet{
String contentDisposition = (viewContent)?"inline":"attachment";
resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + item.getName() + ".zip\"" );
resp.setContentType("application/zip");
resp.setContentLength((int) tmpZip.length());
resp = setContentLength(resp, tmpZip.length());
//MODIFIED 22-05-2013 CLOSE STREAM
OutputStream out = resp.getOutputStream();
@ -415,37 +353,9 @@ public class DownloadServlet extends HttpServlet{
} catch (Exception e) {
logger.error("Error during item retrieving "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}
/*case AQUAMAPS_ITEM:{
try{
AquaMapsItem aquamaps = (AquaMapsItem)folderItem;
File tmpZip = ZipUtil.zipAquaMapsItem(aquamaps);
String contentDisposition = (viewContent)?"inline":"attachment";
resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + item.getName() + ".zip\"" );
resp.setContentType("application/zip");
resp.setContentLength((int) tmpZip.length());
//MODIFIED 22-05-2013 CLOSE STREAM
OutputStream out = resp.getOutputStream();
FileInputStream fileTmpZip = new FileInputStream(tmpZip);
IOUtils.copy(fileTmpZip, out);
fileTmpZip.close();
out.close();
tmpZip.delete();
return;
} catch (Exception e) {
logger.error("Error during item retrieving "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}*/
case IMAGE_DOCUMENT:
case PDF_DOCUMENT:
case URL_DOCUMENT:
@ -459,7 +369,7 @@ public class DownloadServlet extends HttpServlet{
resp.setHeader( "Content-Disposition", "attachment; filename=\"" + item.getName() + ".zip\"" );
resp.setContentType("application/zip");
resp.setContentLength((int) tmpZip.length());
resp = setContentLength(resp, tmpZip.length());
//MODIFIED 22-05-2013 CLOSE STREAM
OutputStream out = resp.getOutputStream();
@ -475,8 +385,7 @@ public class DownloadServlet extends HttpServlet{
String itemName = MimeTypeUtil.getNameWithExtension(item.getName(), mimeType);
resp.setHeader( "Content-Disposition", "inline; filename=\"" + itemName + "\"" );
resp.setContentType(document.getMimeType());
resp.setContentLength((int) document.getLength());
resp = setContentLength(resp, document.getLength());
//MODIFIED 22-05-2013 CLOSE STREAM
OutputStream out = resp.getOutputStream();
InputStream is = document.getData();
@ -489,7 +398,6 @@ public class DownloadServlet extends HttpServlet{
} catch (Exception e) {
logger.error("Error during item retrieving "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}
@ -500,104 +408,47 @@ public class DownloadServlet extends HttpServlet{
GCubeItem metadata = (GCubeItem) item; //Cast GCubeItem
resp.setContentType("text/html");
resp.setContentLength((int) metadata.getLength());
resp = setContentLength(resp, metadata.getLength());
//MODIFIED 22-05-2013 CLOSE STREAM
OutputStream out = resp.getOutputStream();
// StringReader sr = new StringReader(metadata.getData());
InputStream is = metadata.getData();
IOUtils.copy(is, out);
is.close();
out.close();
return;
} catch (Exception e) {
logger.error("Error during item retrieving "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}
/*COMMENT ACCORDING TO HL
case EXTERNAL_RESOURCE_LINK:{//IT'S SAME OF EXTERNAL FILE
try{
ExternalResourceLink externalResourceLink = (ExternalResourceLink) folderItem;
OutputStream out = resp.getOutputStream();
try {
String mimeType = externalResourceLink.getMimeType();
if(mimeType == null){
logger.trace("mimeType is null... recover from MimeTypeUtil by BufferedInputStream");
BufferedInputStream bufferedStream = new BufferedInputStream(externalResourceLink.getData(), (int) externalResourceLink.getLength());
mimeType = MimeTypeUtil.getMimeType(externalResourceLink.getName(), bufferedStream);
}
logger.trace("setContentType with mimeType " + mimeType);
String itemName = MimeTypeUtil.getNameWithExtension(item.getName(), mimeType);
String contentDisposition = (viewContent)?"inline":"attachment";
resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" );
resp.setContentType(mimeType);
logger.trace("resoure link lenght: " +externalResourceLink.getLength());
resp.setContentLength((int) externalResourceLink.getLength());
if(externalResourceLink.getData()!=null){
logger.trace("Input stream is not null");
//MODIFIED 22-05-2013 CLOSE STREAM
InputStream eris = externalResourceLink.getData();
IOUtils.copy(eris, resp.getOutputStream());
eris.close();
}
else{
logger.error("Input stream is null "+itemId);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during inpunt stream retrieving, it's null");
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during inpunt stream retrieving, it's null");
}
} catch (ExternalResourceBrokenLinkException e) {
logger.error("Error during link resource retrieving "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during link retrieving, link broken!: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during link retrieving, link broken!: "+e.getMessage());
} catch (ExternalResourcePluginNotFoundException e) {
logger.error("Error during link resource retrieving "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during link retrieving, plugin not found!: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during link retrieving, plugin not found!: "+e.getMessage());
}
out.close();
} catch (InternalErrorException e) {
logger.error("Error during external item sending "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
} catch (Exception e) {
logger.error("Error during resource retrieving "+itemId,e);
handleError(urlRedirectOnError, req, resp, itemId,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during resource retrieving! "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during resource retrieving, plugin not found!: "+e.getMessage());
e.printStackTrace();
}
return;
}*/
}
}
}
handleError(urlRedirectOnError, req, resp, itemId,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving");
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving");
return;
}
/**
* Method to manage HttpServletResponse content length also to big data
* @param resp
* @param length
* @return
*/
protected HttpServletResponse setContentLength(HttpServletResponse resp, long length){
try{
if (length <= Integer.MAX_VALUE)
resp.setContentLength((int)length);
else
resp.addHeader("Content-Length", Long.toString(length));
}catch(Exception e){
//silent
}
return resp;
}
protected void handleError(boolean urlRedirectOnError, HttpServletRequest req, HttpServletResponse resp, String itemId, String message) throws IOException{
@ -619,8 +470,7 @@ public class DownloadServlet extends HttpServlet{
logger.trace("writing response...");
StringReader sr = new StringReader(resultMessage.toString());
IOUtils.copy(sr, response.getOutputStream());
// response.getWriter().write(resultMessage.toString());
logger.trace("response writed");
response.flushBuffer();
}
@ -634,8 +484,7 @@ public class DownloadServlet extends HttpServlet{
logger.trace("writing response...");
StringReader sr = new StringReader(resultMessage.toString());
IOUtils.copy(sr, response.getOutputStream());
// response.getWriter().write(resultMessage.toString());
logger.trace("response writed");
response.flushBuffer();
}
@ -701,7 +550,6 @@ public class DownloadServlet extends HttpServlet{
// }
// if (queryString != null) {
// url.append("?").append(queryString);
// }
PortalUrlGroupGatewayProperty p = new PortalUrlGroupGatewayProperty();
@ -722,6 +570,7 @@ public class DownloadServlet extends HttpServlet{
return url.toString();
}
/*
public static void main(String[] args) {
InputStream is = null;
@ -766,5 +615,5 @@ public class DownloadServlet extends HttpServlet{
}catch (Exception e) {
e.printStackTrace();
}
}
}*/
}

View File

@ -23,8 +23,9 @@ import org.gcube.common.scope.impl.ScopeBean;
public class SizeRetrieving {
public static String DEFAULT_SCOPE = "/gcube/devsec"; //DEV
public static String TEST_USER = "andrea.manzi";
// public static String DEFAULT_SCOPE = "/gcube/devsec"; //DEV
public static String DEFAULT_SCOPE = "/d4science.research-infrastructures.eu/gCubeApps/DESCRAMBLE";
public static String TEST_USER = "massimiliano.assante";
public static void main(String[] args) {