This commit is contained in:
Francesco Mangiacrapa 2013-03-12 15:10:01 +00:00
parent 1bd854d72a
commit 183ed76b2b
2 changed files with 97 additions and 10 deletions

View File

@ -121,7 +121,7 @@ public class DownloadServlet extends HttpServlet{
case REPORT_TEMPLATE:{ case REPORT_TEMPLATE:{
try{ try{
ReportTemplate reportTemplate = (ReportTemplate)folderItem; ReportTemplate reportTemplate = (ReportTemplate)folderItem;
String itemName = item.getName() + "." + Extensions.REPORT_TEMPLATE; String itemName = item.getName() + "." + Extensions.REPORT_TEMPLATE.getValue();
String contentDisposition = (viewContent)?"inline":"attachment"; String contentDisposition = (viewContent)?"inline":"attachment";
resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" ); resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" );
@ -143,7 +143,7 @@ public class DownloadServlet extends HttpServlet{
try{ try{
Report report = (Report)folderItem; Report report = (Report)folderItem;
String itemName = item.getName() + "." + Extensions.REPORT; String itemName = item.getName() + "." + Extensions.REPORT.getValue();
String contentDisposition = (viewContent)?"inline":"attachment"; String contentDisposition = (viewContent)?"inline":"attachment";
resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" ); resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" );

View File

@ -5,6 +5,8 @@ package org.gcube.portlets.user.workspace.server;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -18,6 +20,7 @@ import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.portlets.user.homelibrary.home.exceptions.InternalErrorException; import org.gcube.portlets.user.homelibrary.home.exceptions.InternalErrorException;
import org.gcube.portlets.user.homelibrary.home.workspace.Workspace; import org.gcube.portlets.user.homelibrary.home.workspace.Workspace;
@ -27,7 +30,12 @@ import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceItemType;
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.InsufficientPrivilegesException; import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.InsufficientPrivilegesException;
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.ItemAlreadyExistException; import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.ItemAlreadyExistException;
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.ItemNotFoundException; import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.ItemNotFoundException;
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.WrongDestinationException;
import org.gcube.portlets.user.homelibrary.home.workspace.folder.FolderItem; import org.gcube.portlets.user.homelibrary.home.workspace.folder.FolderItem;
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.Report;
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ReportTemplate;
import org.gcube.portlets.user.homelibrary.util.Extensions;
import org.gcube.portlets.user.homelibrary.util.MimeTypeUtil; import org.gcube.portlets.user.homelibrary.util.MimeTypeUtil;
import org.gcube.portlets.user.homelibrary.util.WorkspaceUtil; import org.gcube.portlets.user.homelibrary.util.WorkspaceUtil;
import org.gcube.portlets.user.homelibrary.util.zip.UnzipUtil; import org.gcube.portlets.user.homelibrary.util.zip.UnzipUtil;
@ -40,8 +48,18 @@ import org.gcube.portlets.user.workspace.server.util.Util;
*/ */
public class UploadServlet extends HttpServlet { public class UploadServlet extends HttpServlet {
public static final String UPLOAD_TYPE = "uploadType";
public static final String ID_FOLDER = "idFolder";
public static final String UPLOAD_FORM_ELEMENT = "uploadFormElement";
public static final String FILE = "File";
protected GCUBELog logger = new GCUBELog(UploadServlet.class); protected GCUBELog logger = new GCUBELog(UploadServlet.class);
public static final String D4ST = Extensions.REPORT_TEMPLATE.getValue(); //extension of record type
public static final String D4SR = Extensions.REPORT.getValue(); //extension of template type
/** /**
* *
*/ */
@ -91,15 +109,15 @@ public class UploadServlet extends HttpServlet {
while (it.hasNext()) { while (it.hasNext()) {
FileItem item = (FileItem) it.next(); FileItem item = (FileItem) it.next();
if (!item.isFormField() && "uploadFormElement".equals(item.getFieldName())) { if (!item.isFormField() && UPLOAD_FORM_ELEMENT.equals(item.getFieldName())) {
uploadItem = item; uploadItem = item;
} }
if (item.isFormField() && "idFolder".equals(item.getFieldName())){ if (item.isFormField() && ID_FOLDER.equals(item.getFieldName())){
destinationId = item.getString(); destinationId = item.getString();
} }
if (item.isFormField() && "uploadType".equals(item.getFieldName())){ if (item.isFormField() && UPLOAD_TYPE.equals(item.getFieldName())){
uploadType = item.getString(); uploadType = item.getString();
} }
@ -163,13 +181,46 @@ public class UploadServlet extends HttpServlet {
String itemName = WorkspaceUtil.getUniqueName(uploadItem.getName(), destinationFolder); String itemName = WorkspaceUtil.getUniqueName(uploadItem.getName(), destinationFolder);
String contentType = MimeTypeUtil.getMimeType(new BufferedInputStream(uploadItem.getInputStream())); String contentType = MimeTypeUtil.getMimeType(new BufferedInputStream(uploadItem.getInputStream()));
logger.trace("Content type: "+contentType); logger.trace("Content type: "+contentType + " unique name: "+itemName);
if(uploadType.compareTo("File")==0) //IS FILE UPLOAD // System.out.println("Content type: "+contentType + " unique name: "+itemName);
String extension = FilenameUtils.getExtension(itemName);
logger.trace("extension: "+extension);
// System.out.println("extension: "+extension);
if(uploadType.compareTo(FILE)==0) {//IS FILE UPLOAD
//DEBUG
// System.out.println("MimeTypeUtil.isZipContentType(contentType) "+MimeTypeUtil.isZipContentType(contentType));
// System.out.println("extension.compareToIgnoreCase(D4ST) "+extension.compareToIgnoreCase(D4ST));
// System.out.println("extension.compareToIgnoreCase(D4SR) "+extension.compareToIgnoreCase(D4SR));
if(MimeTypeUtil.isZipContentType(contentType) && (extension.compareToIgnoreCase(D4ST)==0)){ //Create REPORT TEMPLATE
String itemwithoutext = itemName.substring(0,itemName.lastIndexOf(D4ST)-1); //remove extension
// System.out.println("itemwithoutext " +itemwithoutext);
logger.trace("createTemplate: "+itemwithoutext);
createTemplate(wa, itemwithoutext, uploadItem.getInputStream(), destinationFolder, response);
}else if(MimeTypeUtil.isZipContentType(contentType) && (extension.compareToIgnoreCase(D4SR)==0)){ //Create REPORT
String itemwithoutext = itemName.substring(0,itemName.lastIndexOf(D4SR)-1); //remove extension
// System.out.println("itemwithoutext " +itemwithoutext);
logger.trace("createReport: "+itemwithoutext);
createReport(wa, itemwithoutext, uploadItem.getInputStream(), destinationFolder, response);
}else{ //CREATE AN EXTERNAL FILE
createExternalFile(itemName, uploadItem, destinationFolder, contentType, response); createExternalFile(itemName, uploadItem, destinationFolder, contentType, response);
}
else {//IS ARCHIVE UPLOAD }else {//IS ARCHIVE UPLOAD
if (MimeTypeUtil.isZipContentType(contentType)){ if (MimeTypeUtil.isZipContentType(contentType)){
logger.trace("Unziping content"); logger.trace("Unziping content");
@ -203,6 +254,42 @@ public class UploadServlet extends HttpServlet {
sendMessage(response, "File "+createdItem.getName()+" imported correctly in "+destinationFolder.getPath()); sendMessage(response, "File "+createdItem.getName()+" imported correctly in "+destinationFolder.getPath());
} }
private void createReport(Workspace wa, String itemName, InputStream stream, WorkspaceFolder destinationFolder, HttpServletResponse response) throws InsufficientPrivilegesException, ItemAlreadyExistException, InternalErrorException, IOException{
try {
itemName = WorkspaceUtil.getUniqueName(itemName, destinationFolder);
Report report = wa.createReport(itemName, "", Calendar.getInstance(), Calendar.getInstance(), "", "", "", 0, "", stream, destinationFolder.getId());
sendMessage(response, "File "+report.getName()+" imported correctly in "+destinationFolder.getPath());
} catch (WrongDestinationException e) {
logger.error("Error creating elements", e);
sendError(response, "Internal error: Wrong Destination");
} catch (WorkspaceFolderNotFoundException e) {
logger.error("Error creating elements", e);
sendError(response, "Internal error: Workspace Folder Not Found");
}
}
private void createTemplate(Workspace wa, String itemName, InputStream stream, WorkspaceFolder destinationFolder, HttpServletResponse response) throws InsufficientPrivilegesException, ItemAlreadyExistException, InternalErrorException, IOException{
try {
itemName = WorkspaceUtil.getUniqueName(itemName, destinationFolder);
ReportTemplate template = wa.createReportTemplate(itemName, "", Calendar.getInstance(), Calendar.getInstance(), "", "", 0, "", stream, destinationFolder.getId());
sendMessage(response, "File "+template.getName()+" imported correctly in "+destinationFolder.getPath());
} catch (WrongDestinationException e) {
logger.error("Error creating elements", e);
sendError(response, "Internal error: Wrong Destination");
} catch (WorkspaceFolderNotFoundException e) {
logger.error("Error creating elements", e);
sendError(response, "Internal error: Workspace Folder Not Found");
}
}
protected void sendError(HttpServletResponse response, String message) throws IOException protected void sendError(HttpServletResponse response, String message) throws IOException
{ {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);