750 lines
29 KiB
Java
750 lines
29 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.workspace.server;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.io.StringReader;
|
|
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.http.HttpServlet;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.gcube.common.core.utils.logging.GCUBELog;
|
|
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;
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.ExternalResourceBrokenLinkException;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.ExternalResourcePluginNotFoundException;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.AquaMapsItem;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalFile;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalImage;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalResourceLink;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalUrl;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.Query;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.Report;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.ReportTemplate;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.gcube.Document;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.gcube.Metadata;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.ts.TimeSeries;
|
|
import org.gcube.common.homelibrary.util.Extensions;
|
|
import org.gcube.common.homelibrary.util.MimeTypeUtil;
|
|
import org.gcube.common.homelibrary.util.zip.ZipUtil;
|
|
import org.gcube.portlets.user.workspace.client.ConstantsExplorer;
|
|
import org.gcube.portlets.user.workspace.server.property.PortalUrlGroupGatewayProperty;
|
|
import org.gcube.portlets.user.workspace.server.util.WsUtil;
|
|
import org.gcube.portlets.user.workspace.shared.HandlerResultMessage;
|
|
|
|
/**
|
|
* @author Federico De Faveri defaveri@isti.cnr.it
|
|
*
|
|
*/
|
|
public class DownloadServlet extends HttpServlet{
|
|
|
|
private static final long serialVersionUID = -8423345575690165644L;
|
|
|
|
protected static GCUBELog logger = new GCUBELog(DownloadServlet.class);
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public void init() throws ServletException {
|
|
super.init();
|
|
logger.trace("Workspace DownloadServlet ready.");
|
|
}
|
|
|
|
|
|
|
|
|
|
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
String itemId = req.getParameter("id");
|
|
boolean viewContent = (req.getParameter("viewContent")==null)?false:req.getParameter("viewContent").equals("true");
|
|
boolean isValidItem = (req.getParameter(ConstantsExplorer.VALIDATEITEM)==null)?false:req.getParameter(ConstantsExplorer.VALIDATEITEM).equals("true");
|
|
boolean urlRedirectOnError = (req.getParameter(ConstantsExplorer.REDIRECTONERROR)==null)?false:req.getParameter(ConstantsExplorer.REDIRECTONERROR).equals("true");
|
|
|
|
|
|
logger.trace("Input Params [id: "+itemId + ", viewContent: "+viewContent+", "+ConstantsExplorer.VALIDATEITEM +": " +isValidItem+", urlRedirectOnError:" +urlRedirectOnError+"]");
|
|
|
|
if(itemId==null || itemId.isEmpty()){
|
|
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Item id is null");
|
|
return;
|
|
}
|
|
|
|
logger.trace("FILE DOWNLOAD REQUEST "+itemId);
|
|
|
|
Workspace wa = null;
|
|
try {
|
|
wa = WsUtil.getWorkspace(req.getSession());
|
|
} catch (Exception e) {
|
|
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during workspace retrieving");
|
|
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during workspace retrieving");
|
|
return;
|
|
}
|
|
|
|
if (wa == null) {
|
|
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error, no workspace in session");
|
|
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error, no workspace in session");
|
|
return;
|
|
}
|
|
|
|
WorkspaceItem item;
|
|
try {
|
|
|
|
item = wa.getItem(itemId);
|
|
|
|
if(isValidItem){ //ADDED 25/06/2013 - THIS CODE RETURN A SC_ACCEPT IS ITEM EXIST
|
|
String message = HttpServletResponse.SC_ACCEPTED+ ": The resource is available";
|
|
sendMessageResourceAvailable(resp, message);
|
|
logger.trace("response return: "+message);
|
|
return;
|
|
}
|
|
|
|
try{
|
|
//ACCOUNTING
|
|
item.markAsRead(true);
|
|
|
|
} catch (InternalErrorException e) {
|
|
logger.error("Requested item "+itemId+" has trow an internal error exception",e);
|
|
}
|
|
|
|
} catch (ItemNotFoundException e) {
|
|
logger.error("Requested item "+itemId+" not found",e);
|
|
// sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error, no items found");
|
|
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": The file has been deleted by another user.");
|
|
// sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": "+ConstantsExplorer.ERROR_ITEM_DOES_NOT_EXIST);
|
|
|
|
return;
|
|
}
|
|
|
|
switch (item.getType()) {
|
|
|
|
case SHARED_FOLDER:
|
|
case FOLDER:{
|
|
try {
|
|
File tmpZip = ZipUtil.zipFolder((WorkspaceFolder) item);
|
|
resp.setHeader( "Content-Disposition", "attachment; filename=\"" + item.getName() + ".zip\"" );
|
|
resp.setContentType("application/zip");
|
|
resp.setContentLength((int) 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();
|
|
|
|
out.close();
|
|
tmpZip.delete();
|
|
return;
|
|
|
|
} 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;
|
|
}
|
|
}
|
|
case FOLDER_ITEM:{
|
|
FolderItem folderItem = (FolderItem) item;
|
|
|
|
switch (folderItem.getFolderItemType()) {
|
|
|
|
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());
|
|
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();
|
|
|
|
out.close();
|
|
} 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;
|
|
|
|
}
|
|
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());
|
|
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();
|
|
|
|
out.close();
|
|
} 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;
|
|
|
|
}
|
|
case EXTERNAL_PDF_FILE:
|
|
case EXTERNAL_FILE:{
|
|
|
|
InputStream is = null;
|
|
OutputStream out = null;
|
|
try{
|
|
ExternalFile externalFile = (ExternalFile)folderItem;
|
|
|
|
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.setContentType(mimeType);
|
|
|
|
resp.setContentLength((int) externalFile.getLength());
|
|
|
|
|
|
//MODIFIED 22-05-2013 CLOSE STREAM
|
|
// IOUtils.copy(externalFile.getData(), resp.getOutputStream());
|
|
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);
|
|
|
|
is.close();
|
|
out.close();
|
|
|
|
} 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;
|
|
|
|
}
|
|
case EXTERNAL_IMAGE:{
|
|
|
|
try{
|
|
ExternalImage externalImage = (ExternalImage)folderItem;
|
|
|
|
String mimeType = externalImage.getMimeType();
|
|
String itemName = MimeTypeUtil.getNameWithExtension(item.getName(), mimeType);
|
|
|
|
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());
|
|
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();
|
|
|
|
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;
|
|
}
|
|
}
|
|
case EXTERNAL_URL:{
|
|
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());
|
|
|
|
//MODIFIED 22-05-2013 CLOSE STREAM
|
|
StringReader sr = new StringReader(externalUrl.getUrl());
|
|
OutputStream out = resp.getOutputStream();
|
|
IOUtils.copy(sr, out);
|
|
|
|
sr.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;
|
|
}
|
|
}
|
|
case QUERY:{
|
|
|
|
Query query = (Query)folderItem;
|
|
resp.setContentType("text/plain");
|
|
try {
|
|
resp.setContentLength((int) 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;
|
|
}
|
|
|
|
//MODIFIED 22-05-2013 CLOSE STREAM
|
|
OutputStream out = resp.getOutputStream();
|
|
StringReader sr = new StringReader(query.getQuery());
|
|
IOUtils.copy(sr, out);
|
|
sr.close();
|
|
|
|
out.close();
|
|
return;
|
|
|
|
}
|
|
case TIME_SERIES:{
|
|
try{
|
|
TimeSeries ts = (TimeSeries)folderItem;
|
|
File tmpZip = ZipUtil.zipTimeSeries(ts);
|
|
|
|
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 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:
|
|
case DOCUMENT:{
|
|
try{
|
|
Document document = (Document)item;
|
|
|
|
|
|
if (!viewContent){
|
|
File tmpZip = ZipUtil.zipDocument(document);
|
|
|
|
resp.setHeader( "Content-Disposition", "attachment; 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();
|
|
}
|
|
else{
|
|
String mimeType = document.getMimeType();
|
|
String itemName = MimeTypeUtil.getNameWithExtension(item.getName(), mimeType);
|
|
resp.setHeader( "Content-Disposition", "inline; filename=\"" + itemName + "\"" );
|
|
resp.setContentType(document.getMimeType());
|
|
resp.setContentLength((int) document.getLength());
|
|
|
|
//MODIFIED 22-05-2013 CLOSE STREAM
|
|
OutputStream out = resp.getOutputStream();
|
|
InputStream is = document.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;
|
|
}
|
|
}
|
|
|
|
case METADATA:{
|
|
try{
|
|
Metadata document = (Metadata)item;
|
|
|
|
resp.setContentType("text/html");
|
|
resp.setContentLength((int) document.getLength());
|
|
|
|
//MODIFIED 22-05-2013 CLOSE STREAM
|
|
OutputStream out = resp.getOutputStream();
|
|
StringReader sr = new StringReader(document.getData());
|
|
IOUtils.copy(sr, out);
|
|
sr.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;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
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(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;
|
|
|
|
}
|
|
|
|
protected void handleError(boolean urlRedirectOnError, HttpServletRequest req, HttpServletResponse resp, String itemId, String message) throws IOException{
|
|
|
|
logger.warn("Handle error occurred: "+message);
|
|
logger.trace("urlRedirectOnError is active: "+urlRedirectOnError);
|
|
if(urlRedirectOnError){
|
|
urlRedirect(req, resp, itemId);
|
|
}else
|
|
sendError(resp,message);
|
|
|
|
}
|
|
|
|
protected void sendError(HttpServletResponse response, String message) throws IOException
|
|
{
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
HandlerResultMessage resultMessage = HandlerResultMessage.errorResult(message);
|
|
logger.trace("error message: "+resultMessage);
|
|
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();
|
|
}
|
|
|
|
protected void sendMessage(HttpServletResponse response, String message) throws IOException
|
|
{
|
|
response.setStatus(HttpServletResponse.SC_ACCEPTED);
|
|
HandlerResultMessage resultMessage = HandlerResultMessage.okResult(message);
|
|
response.getWriter().write(resultMessage.toString());
|
|
response.flushBuffer();
|
|
}
|
|
|
|
protected void sendMessageResourceAvailable(HttpServletResponse response, String message) throws IOException
|
|
{
|
|
response.setStatus(HttpServletResponse.SC_ACCEPTED);
|
|
HandlerResultMessage resultMessage = HandlerResultMessage.okResult(message);
|
|
response.getWriter().write(resultMessage.toString());
|
|
response.flushBuffer();
|
|
}
|
|
|
|
protected void sendWarnMessage(HttpServletResponse response, String message) throws IOException
|
|
{
|
|
response.setStatus(HttpServletResponse.SC_ACCEPTED);
|
|
HandlerResultMessage resultMessage = HandlerResultMessage.warnResult(message);
|
|
response.getWriter().write(resultMessage.toString());
|
|
response.flushBuffer();
|
|
}
|
|
|
|
protected void urlRedirect(HttpServletRequest req, HttpServletResponse response, String fakePath) throws IOException {
|
|
|
|
String requestUrl = getRequestURL(req) +fakePath;
|
|
logger.trace("Url redirect on: "+requestUrl);
|
|
// System.out.println("Url redirect on: "+requestUrl);
|
|
response.sendRedirect(response.encodeRedirectURL(requestUrl));
|
|
return;
|
|
}
|
|
|
|
public static String getRequestURL(HttpServletRequest req) {
|
|
|
|
String scheme = req.getScheme(); // http
|
|
String serverName = req.getServerName(); // hostname.com
|
|
int serverPort = req.getServerPort(); // 80
|
|
String contextPath = req.getContextPath(); // /mywebapp
|
|
// String servletPath = req.getServletPath(); // /servlet/MyServlet
|
|
// String pathInfo = req.getPathInfo(); // /a/b;c=123
|
|
// String queryString = req.getQueryString(); // d=789
|
|
|
|
// Reconstruct original requesting URL
|
|
StringBuffer url = new StringBuffer();
|
|
url.append(scheme).append("://").append(serverName);
|
|
|
|
if ((serverPort != 80) && (serverPort != 443)) {
|
|
url.append(":").append(serverPort);
|
|
}
|
|
|
|
logger.trace("server: "+url);
|
|
logger.trace("omitted contextPath: "+contextPath);
|
|
// logger.trace("servletPath: "+servletPath);
|
|
// url.append(contextPath).append(servletPath);
|
|
|
|
// if (pathInfo != null) {
|
|
// url.append(pathInfo);
|
|
// }
|
|
// if (queryString != null) {
|
|
// url.append("?").append(queryString);
|
|
|
|
// }
|
|
|
|
PortalUrlGroupGatewayProperty p = new PortalUrlGroupGatewayProperty();
|
|
|
|
int lenght = p.getPath().length();
|
|
|
|
String groupgatewaypath = "/";
|
|
|
|
if(lenght>1){
|
|
|
|
String lastChar = p.getPath().substring(lenght-1, lenght-1);
|
|
|
|
groupgatewaypath+= lastChar.compareTo("/")!=0?p.getPath()+"/":p.getPath();
|
|
}
|
|
|
|
url.append(groupgatewaypath);
|
|
|
|
return url.toString();
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
InputStream is = null;
|
|
|
|
logger.trace("start");
|
|
|
|
// is = GCUBEStorage.getRemoteFile("/Home/test.user/Workspace3d660604-03ef-49eb-89c3-4c73f8a47914");
|
|
|
|
try{
|
|
|
|
Workspace ws = HomeLibrary.getHomeManagerFactory().getHomeManager().getHome("francesco.mangiacrapa").getWorkspace();
|
|
//
|
|
// ExternalFile f = (ExternalFile) ws.getItem("907ce8ef-5c0b-4601-83ac-215d1f432f6b");
|
|
|
|
WorkspaceItem wsItem = ws.getItem("907ce8ef-5c0b-4601-83ac-215d1f432f6b");
|
|
|
|
|
|
logger.trace("metadata info recovered from HL: [ID: "+wsItem.getId() +", name: "+wsItem.getName()+"]");
|
|
|
|
FileOutputStream out = new FileOutputStream(new File("/tmp/bla"));
|
|
// byte[] buffer = new byte[1024];
|
|
// int len;
|
|
// while ((len = is.read(buffer)) != -1) {
|
|
// out.write(buffer, 0, len);
|
|
// }
|
|
|
|
logger.trace("cast as external file");
|
|
ExternalFile f = (ExternalFile) wsItem;
|
|
|
|
is = f.getData();
|
|
|
|
IOUtils.copy(is, out);
|
|
is.close();
|
|
|
|
out.close();
|
|
|
|
// logger.trace("Sleeping");
|
|
// Thread.sleep(20000);
|
|
// logger.trace("Alive");
|
|
|
|
logger.trace("end");
|
|
}catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|