2015-10-02 11:46:32 +02:00
/ * *
2016-02-19 09:57:58 +01:00
*
2015-10-02 11:46:32 +02:00
* /
package org.gcube.portlets.widgets.workspaceuploader.server ;
2018-07-05 16:59:36 +02:00
import java.io.File ;
import java.io.FileOutputStream ;
2015-10-02 11:46:32 +02:00
import java.io.IOException ;
2018-07-05 16:59:36 +02:00
import java.io.InputStream ;
2015-10-02 11:46:32 +02:00
import java.util.ArrayList ;
2018-09-19 15:15:28 +02:00
import java.util.HashMap ;
2015-10-02 11:46:32 +02:00
import java.util.LinkedHashMap ;
import java.util.List ;
2018-09-19 15:15:28 +02:00
import java.util.Map ;
2015-10-02 11:46:32 +02:00
import java.util.Random ;
2018-07-05 16:59:36 +02:00
import java.util.UUID ;
2015-10-02 11:46:32 +02:00
import java.util.concurrent.TimeUnit ;
import javax.servlet.Servlet ;
import javax.servlet.ServletException ;
import javax.servlet.http.HttpServlet ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.HttpSession ;
import org.apache.commons.fileupload.FileItemFactory ;
import org.apache.commons.fileupload.FileItemIterator ;
import org.apache.commons.fileupload.FileItemStream ;
import org.apache.commons.fileupload.FileUploadBase ;
import org.apache.commons.fileupload.FileUploadException ;
import org.apache.commons.fileupload.disk.DiskFileItemFactory ;
import org.apache.commons.fileupload.servlet.ServletFileUpload ;
import org.apache.commons.fileupload.util.Streams ;
import org.apache.commons.io.FilenameUtils ;
2018-07-05 16:59:36 +02:00
import org.apache.commons.io.IOUtils ;
2018-07-02 12:09:28 +02:00
import org.apache.commons.lang.Validate ;
2016-11-24 12:28:48 +01:00
import org.gcube.common.portal.PortalContext ;
2018-07-02 12:09:28 +02:00
import org.gcube.common.storagehub.model.items.FolderItem ;
2018-06-25 12:42:41 +02:00
import org.gcube.common.storagehub.model.items.Item ;
2018-06-26 17:17:10 +02:00
import org.gcube.common.storagehubwrapper.server.StorageHubWrapper ;
2019-05-03 14:55:37 +02:00
import org.gcube.common.storagehubwrapper.server.tohl.Workspace ;
2018-06-26 17:17:10 +02:00
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceFolder ;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItem ;
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.InsufficientPrivilegesException ;
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.InternalErrorException ;
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.ItemAlreadyExistException ;
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.ItemNotFoundException ;
2018-07-05 16:59:36 +02:00
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.WrongItemTypeException ;
2019-05-03 14:55:37 +02:00
import org.gcube.common.storagehubwrapper.shared.tohl.impl.WorkspaceSharedFolder ;
2015-10-02 11:46:32 +02:00
import org.gcube.portlets.widgets.workspaceuploader.client.ConstantsWorkspaceUploader ;
2018-06-26 17:17:10 +02:00
import org.gcube.portlets.widgets.workspaceuploader.server.notification.NotificationsWorkspaceUploader ;
2016-07-14 14:30:46 +02:00
import org.gcube.portlets.widgets.workspaceuploader.server.notification.NotificationsWorkspaceUploaderProducer ;
2015-10-02 11:46:32 +02:00
import org.gcube.portlets.widgets.workspaceuploader.server.upload.AbstractUploadProgressListener ;
import org.gcube.portlets.widgets.workspaceuploader.server.upload.MemoryUploadListener ;
import org.gcube.portlets.widgets.workspaceuploader.server.upload.UploadCanceledException ;
import org.gcube.portlets.widgets.workspaceuploader.server.upload.UploadProgressInputStream ;
import org.gcube.portlets.widgets.workspaceuploader.server.upload.UploadProgressListener ;
import org.gcube.portlets.widgets.workspaceuploader.server.upload.WorkspaceUploaderMng ;
import org.gcube.portlets.widgets.workspaceuploader.server.util.WsUtil ;
import org.gcube.portlets.widgets.workspaceuploader.shared.HandlerResultMessage ;
import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploadFile ;
import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploaderItem ;
import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploaderItem.UPLOAD_STATUS ;
2017-01-20 14:26:38 +01:00
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2015-10-02 11:46:32 +02:00
import org.json.JSONArray ;
import org.json.JSONException ;
import org.json.JSONObject ;
import org.json.JSONTokener ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2016-05-11 10:25:22 +02:00
2015-10-02 11:46:32 +02:00
/ * *
* The Class WorkspaceUploadServletStream .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
2016-05-11 10:25:22 +02:00
* May 10 , 2016
2015-10-02 11:46:32 +02:00
* /
public class WorkspaceUploadServletStream extends HttpServlet implements Servlet {
public static final String UNKNOWN_UNKNOWN = " unknown/unknown " ;
private static final long serialVersionUID = 1778008252774571216L ;
protected static final String UTF_8 = " UTF-8 " ;
2016-11-24 12:28:48 +01:00
public static final String VRE_ID_ATTR_NAME = " gcube-vreid " ;
public static final String CURR_GROUP_ID = ConstantsWorkspaceUploader . CURR_GROUP_ID ;
2016-12-21 14:20:48 +01:00
2017-01-20 14:26:38 +01:00
//public static final String CURR_USER_ID = ConstantsWorkspaceUploader.CURR_USER_ID;
2016-12-21 14:20:48 +01:00
2015-10-02 11:46:32 +02:00
public static final String UPLOAD_TYPE = ConstantsWorkspaceUploader . UPLOAD_TYPE ;
public static final String ID_FOLDER = ConstantsWorkspaceUploader . ID_FOLDER ;
public static final String UPLOAD_FORM_ELEMENT = ConstantsWorkspaceUploader . UPLOAD_FORM_ELEMENT ;
public static final String CLIENT_UPLOAD_KEYS = ConstantsWorkspaceUploader . CLIENT_UPLOAD_KEYS ;
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
public static final String JSON_CLIENT_KEYS = ConstantsWorkspaceUploader . JSON_CLIENT_KEYS ;
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
public static final String IS_OVERWRITE = ConstantsWorkspaceUploader . IS_OVERWRITE ;
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
public static final String CANCEL_UPLOAD = ConstantsWorkspaceUploader . CANCEL_UPLOAD ;
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
public static final String FILE = " File " ;
2018-09-18 17:42:00 +02:00
public static final String ARCHIVE = " Archive " ;
2018-09-19 15:15:28 +02:00
public static final Map < String , String > SUPPORTED_UNPACKING_ARCHIVE = new HashMap < String , String > ( ) { {
put ( " zip " , " application/zip " ) ; //.zip
put ( " 7z " , " application/x-7z-compressed " ) ; //.7z
put ( " tar " , " application/x-tar " ) ; //tar
put ( " java archive " , " application/java-archive " ) ; //.jar
put ( " jar " , " application/x-java-archive " ) ;
put ( " gtar " , " application/x-gtar " ) ;
} } ;
2015-10-02 11:46:32 +02:00
public static Logger logger = LoggerFactory . getLogger ( WorkspaceUploadServletStream . class ) ;
private static boolean appEngine = false ;
/ *
* ( non - Javadoc )
2016-02-19 09:57:58 +01:00
*
2015-10-02 11:46:32 +02:00
* @see javax . servlet . GenericServlet # init ( )
* /
@Override
public void init ( ) throws ServletException {
super . init ( ) ;
String appe = getInitParameter ( " appEngine " ) ;
if ( appe ! = null ) {
appEngine = " true " . equalsIgnoreCase ( appe ) ;
} else {
appEngine = isAppEngine ( ) ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
logger . debug ( " init: appEngine is " + appEngine ) ;
}
/ * *
* { @inheritDoc }
* /
@Override
public void doGet ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
logger . debug ( " GET method in " + WorkspaceUploadServletStream . class . getName ( ) + " is running " ) ;
String clientUploadKey = request . getParameter ( CLIENT_UPLOAD_KEYS ) ;
if ( clientUploadKey = = null ) {
sendError ( response , " Internal error: UPLOAD KEY NOT FOUND " ) ;
return ;
}
logger . debug ( " GET method CLIENT_UPLOAD_KEY " + clientUploadKey ) ;
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
boolean cancelUpload = Boolean . parseBoolean ( request . getParameter ( CANCEL_UPLOAD ) ) ;
logger . debug ( " GET method CANCEL_UPLOAD " + cancelUpload ) ;
if ( cancelUpload ) {
2016-05-12 15:12:45 +02:00
boolean cancelled = cancelUpload ( request . getSession ( ) , clientUploadKey ) ;
if ( cancelled ) {
sendMessage ( response , " Upload aborted " + clientUploadKey ) ;
// try {
//// removeCurrentListener(request.getSession(), clientUploadKey);
// WsUtil.eraseWorkspaceUploaderInSession(request.getSession(), clientUploadKey);
// }catch (Exception e) {
// logger.warn("An error occurred during removing cancelled upload from session ");
// }
}
else
sendWarnMessage ( response , " Upload aborted for id: " + clientUploadKey + " has skipped, already aborted or completed? " ) ;
2015-10-02 11:46:32 +02:00
} else
logger . debug ( CANCEL_UPLOAD + " param not found " ) ;
return ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* { @inheritDoc }
* /
public void doPost ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
logger . info ( " POST on UploadServlet " ) ;
2016-07-14 14:30:46 +02:00
boolean requestIsNull = request = = null ;
logger . trace ( " [1] HttpServletRequest is: null? " + requestIsNull + " , URI: " + request . getRequestURI ( ) + " , ServerName: " + request . getServerName ( ) ) ;
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
if ( ! ServletFileUpload . isMultipartContent ( request ) ) {
logger . error ( " ERROR: multipart request not found " ) ;
sendError ( response , " ERROR: multipart request not found " ) ;
2016-08-30 17:11:06 +02:00
return ;
2015-10-02 11:46:32 +02:00
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
try {
2016-02-19 09:57:58 +01:00
2015-10-29 15:12:37 +01:00
logger . info ( " UPLOAD-SERVLET starting " ) ;
HttpSession session = request . getSession ( ) ;
logger . info ( " UPLOAD-SERVLET session: " + session ) ;
logger . debug ( " UPLOAD-SERVLET ( " + session . getId ( ) + " ) new upload request received. " ) ;
2016-08-30 17:11:06 +02:00
2017-01-20 14:26:38 +01:00
if ( WsUtil . isSessionExpired ( request ) ) {
logger . error ( " SESSION_EXPIRED: session is expired " ) ;
sendSessionExpired ( response , " SESSION_EXPIRED: session is expired " ) ;
return ;
}
2016-08-30 17:11:06 +02:00
2015-10-29 15:12:37 +01:00
String destinationId = null ;
String uploadType = null ;
2017-03-28 12:36:11 +02:00
boolean isOverwrite = true ; //CREATE A NEW VERSION OF FILE IS BEHAVIOUR BY DEFAULT
2015-10-29 15:12:37 +01:00
// String clientUploadKey = null;
FileItemStream uploadItem = null ;
ArrayList < String > listClientUploadKeys = null ;
2017-01-20 14:26:38 +01:00
GCubeUser user = PortalContext . getConfiguration ( ) . getCurrentUser ( request ) ;
2015-10-29 15:12:37 +01:00
FileItemFactory factory = new DiskFileItemFactory ( ) ;
ServletFileUpload servletFileUpload = new ServletFileUpload ( factory ) ;
2015-10-02 11:46:32 +02:00
/ * *
* An iterator to instances of < code > FileItemStream < / code >
* parsed from the request , in the order that they were
* transmitted .
* /
FileItemIterator fileItemIterator = servletFileUpload . getItemIterator ( request ) ;
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
int uploadItemsCnt = 0 ;
2016-11-24 12:28:48 +01:00
String scopeGroupId = " " ;
2015-10-02 11:46:32 +02:00
//GET FILE STREAM
while ( fileItemIterator . hasNext ( ) ) {
FileItemStream item = fileItemIterator . next ( ) ;
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
if ( item . isFormField ( ) & & ID_FOLDER . equals ( item . getFieldName ( ) ) ) {
destinationId = Streams . asString ( item . openStream ( ) ) ;
logger . debug ( " ID_FOLDER OK " + destinationId ) ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
if ( item . isFormField ( ) & & UPLOAD_TYPE . equals ( item . getFieldName ( ) ) ) {
uploadType = Streams . asString ( item . openStream ( ) ) ;
logger . debug ( " UPLOAD_TYPE OK " + uploadType ) ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
if ( item . isFormField ( ) & & IS_OVERWRITE . equals ( item . getFieldName ( ) ) ) {
2017-03-28 12:36:11 +02:00
try {
isOverwrite = Boolean . parseBoolean ( Streams . asString ( item . openStream ( ) ) ) ;
logger . debug ( " IS_OVERWRITE OK " + isOverwrite ) ;
} catch ( Exception e ) {
//Silent exception;
}
2015-10-02 11:46:32 +02:00
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
if ( item . isFormField ( ) & & CLIENT_UPLOAD_KEYS . equals ( item . getFieldName ( ) ) ) {
String jsonClientUploadKey = Streams . asString ( item . openStream ( ) ) ;
logger . debug ( " CLIENT_UPLOAD_KEY OK " + jsonClientUploadKey ) ;
LinkedHashMap < String , String > mapKeys = parseJSONClientUploadKeys ( jsonClientUploadKey ) ;
listClientUploadKeys = new ArrayList < String > ( mapKeys . keySet ( ) ) ;
removeListenersIfDone ( session , listClientUploadKeys ) ;
for ( String clientUploadKey : listClientUploadKeys ) {
String fileName = mapKeys . get ( clientUploadKey ) ;
2016-01-08 12:32:48 +01:00
WorkspaceUploaderItem workspaceUploader = createNewWorkspaceUploader ( clientUploadKey , destinationId , mapKeys . get ( clientUploadKey ) , isOverwrite ) ;
2015-10-02 11:46:32 +02:00
logger . debug ( " created " + workspaceUploader ) ;
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . WAIT , " Uploading " + fileName + " at 0% " , request . getSession ( ) ) ;
}
}
2016-12-21 14:20:48 +01:00
2016-11-24 12:28:48 +01:00
if ( item . isFormField ( ) & & CURR_GROUP_ID . equals ( item . getFieldName ( ) ) ) {
scopeGroupId = Streams . asString ( item . openStream ( ) ) ;
2016-12-21 14:20:48 +01:00
logger . debug ( " currentGroupId passed as parameter = " + scopeGroupId ) ;
logger . debug ( " currentGroupId into PortalContext scope= " + PortalContext . getConfiguration ( ) . getCurrentScope ( scopeGroupId ) ) ;
2016-11-24 12:28:48 +01:00
}
2016-12-21 14:20:48 +01:00
2017-01-20 14:26:38 +01:00
// if (item.isFormField() && CURR_USER_ID.equals(item.getFieldName())){
// currUserId = Streams.asString(item.openStream());
// logger.debug("currUserId passed as parameter = " + currUserId);
// logger.debug("currUserinto PortalContext = " + PortalContext.getConfiguration().getCurrentUser(request));
// }
2016-12-21 14:20:48 +01:00
2015-10-02 11:46:32 +02:00
//MUST BE THE LAST PARAMETER TRASMITTED
if ( UPLOAD_FORM_ELEMENT . equals ( item . getFieldName ( ) ) ) {
uploadItem = item ;
2016-11-24 12:28:48 +01:00
logger . debug ( " UPLOAD_FORM_ELEMENT OK " + uploadItem . getName ( ) + " scopeGroupId= " + scopeGroupId ) ;
2015-10-02 11:46:32 +02:00
// break;
2017-01-20 14:26:38 +01:00
uploadData ( user , scopeGroupId , request , response , uploadItem , destinationId , uploadType , listClientUploadKeys . get ( uploadItemsCnt ) , isOverwrite ) ;
2015-10-02 11:46:32 +02:00
uploadItemsCnt + + ;
}
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
} catch ( FileUploadException e ) {
logger . error ( " Error processing request in upload servlet " , e ) ;
sendError ( response , " Internal error: Error during request processing " ) ;
return ;
2015-10-29 15:12:37 +01:00
} catch ( Exception e ) {
logger . error ( " Error processing request in upload servlet " , e ) ;
sendError ( response , " Internal error: Error during request processing " ) ;
return ;
2015-10-02 11:46:32 +02:00
}
}
2016-02-19 09:57:58 +01:00
2018-07-05 16:59:36 +02:00
public File coyStreamToFile ( InputStream in , String fileExtension ) throws IOException {
File tempFile = File . createTempFile ( UUID . randomUUID ( ) . toString ( ) , fileExtension ) ;
tempFile . deleteOnExit ( ) ;
FileOutputStream out = new FileOutputStream ( tempFile ) ;
IOUtils . copy ( in , out ) ;
return tempFile ;
}
// public static InputStream clone(final InputStream inputStream) {
// try {
// inputStream.mark(0);
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// byte[] buffer = new byte[1024];
// int readLength = 0;
// while ((readLength = inputStream.read(buffer)) != -1) {
// outputStream.write(buffer, 0, readLength);
// }
// inputStream.reset();
// outputStream.flush();
// return new ByteArrayInputStream(outputStream.toByteArray());
// }
// catch (Exception ex) {
// ex.printStackTrace();
// }
// return null;
// }
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Removes the listener if done .
*
* @param session the session
* @param keys the keys
* /
private void removeListenersIfDone ( HttpSession session , List < String > keys ) {
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
for ( String key : keys ) {
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
AbstractUploadProgressListener listener = getCurrentListener ( session , key ) ;
if ( listener ! = null ) {
logger . debug ( " Listener found " ) ;
if ( listener . isCanceled ( ) | | listener . getPercentage ( ) > = 100 ) {
logger . debug ( " Listener isCanceled or 100%, removing " ) ;
removeCurrentListener ( session , key ) ;
}
} else
logger . debug ( " Session id: " + session . getId ( ) + " - " + key + " - Listener not found " ) ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Parses the json client upload keys .
*
* @param jsonClientUploadKeys the json client upload keys
* @return the linked hash map
* @throws FileUploadException the file upload exception
* /
@SuppressWarnings ( " rawtypes " )
private static LinkedHashMap < String , String > parseJSONClientUploadKeys ( final String jsonClientUploadKeys ) throws FileUploadException {
JSONTokener tokener = new JSONTokener ( jsonClientUploadKeys ) ;
JSONObject root ;
LinkedHashMap < String , String > keyFiles = null ;
try {
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
root = new JSONObject ( tokener ) ;
JSONArray jsonArray = root . getJSONArray ( JSON_CLIENT_KEYS ) ;
keyFiles = new LinkedHashMap < String , String > ( jsonArray . length ( ) ) ;
logger . debug ( " jsonArray : " + jsonArray . toString ( ) ) ;
for ( int i = 0 ; i < jsonArray . length ( ) ; i + + ) {
JSONObject object = jsonArray . getJSONObject ( i ) ;
logger . debug ( " object : " + object ) ;
String key = ( String ) object . keys ( ) . next ( ) ;
String value = object . getString ( key ) ;
logger . debug ( " key : " + key + " , value: " + value ) ;
keyFiles . put ( key , value ) ;
}
2018-10-03 16:45:19 +02:00
2015-10-02 11:46:32 +02:00
} catch ( JSONException e ) {
logger . error ( " An error occurred during parsing file names: " + keyFiles , e ) ;
throw new FileUploadException ( " An error occurred during parsing file names " ) ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
logger . debug ( " keyFiles: " + keyFiles ) ;
return keyFiles ;
}
2016-12-21 14:20:48 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Upload data .
*
2017-01-20 14:26:38 +01:00
* @param user the user
2016-12-21 14:20:48 +01:00
* @param scopeGroupId the scope group id
2015-10-02 11:46:32 +02:00
* @param request the request
* @param response the response
* @param uploadItem the upload item
* @param destinationId the destination id
* @param uploadType the upload type
* @param clientUploadKey the client upload key
* @param isOverwrite the is overwrite
* @throws ServletException the servlet exception
* @throws IOException Signals that an I / O exception has occurred .
* /
2017-01-20 14:26:38 +01:00
private void uploadData ( GCubeUser user , String scopeGroupId , HttpServletRequest request , final HttpServletResponse response , final FileItemStream uploadItem , String destinationId , String uploadType , String clientUploadKey , boolean isOverwrite ) throws ServletException , IOException {
2015-10-02 11:46:32 +02:00
String fileName = uploadItem . getName ( ) ;
logger . info ( " Upload servlet parameters: [fileName: " + fileName + " , destinationId: " + destinationId + " , uploadType: " + uploadType + " , isOverwrite: " + isOverwrite + " , clientUploadKey: " + clientUploadKey + " ] " ) ;
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
if ( uploadType = = null | | uploadType . isEmpty ( ) ) {
logger . error ( " Error processing request in upload servlet for: " + fileName + " . No upload type found " ) ;
sendError ( response , " Internal error: No upload type found " ) ;
return ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
if ( clientUploadKey = = null | | clientUploadKey . isEmpty ( ) ) {
logger . error ( " Error processing request in upload servlet for: " + fileName + " . No client upload key found " ) ;
sendError ( response , " Internal error: No client upload key found " ) ;
return ;
}
2019-05-03 14:55:37 +02:00
2015-10-02 11:46:32 +02:00
//CLIENT UPLOAD IS THE KEY
// WorkspaceUploaderItem workspaceUploader = createNewWorkspaceUploader(clientUploadKey,destinationId,fileName);
// saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.WAIT, "Uploading "+fileName+" at 0%", request.getSession());
//RETRIVE WORKSPACE UPLOADER FROM SESSION
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
WorkspaceUploaderItem workspaceUploader = null ;
try {
2016-11-24 12:28:48 +01:00
workspaceUploader = WsUtil . getWorkspaceUploaderInSession ( request , clientUploadKey ) ;
2015-10-16 11:00:36 +02:00
workspaceUploader . setIsOverwrite ( isOverwrite ) ; //SET IS OVERWRITE
2015-10-02 11:46:32 +02:00
} catch ( Exception e ) {
logger . error ( " Error during workspace uploader retrieving " , e ) ;
2015-10-14 15:32:40 +02:00
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " An error occurred during upload: " + fileName + " . Error processing request in upload servlet " , request . getSession ( ) ) ;
2015-10-02 11:46:32 +02:00
response . sendError ( HttpServletResponse . SC_INTERNAL_SERVER_ERROR , " Error processing request in upload servlet " ) ;
return ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
//TODO DEBUG REMOVE THIS
2015-10-14 15:32:40 +02:00
// saveWorkspaceUploaderStatus(workspaceUploader, UPLOAD_STATUS.FAILED, "An error occurred during upload: "+fileName+". Error processing request in upload servlet", request.getSession());
2015-10-02 11:46:32 +02:00
// response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error processing request in upload servlet");
// return;
2016-02-19 09:57:58 +01:00
2018-06-25 12:42:41 +02:00
StorageHubWrapper storageHubWrapper ;
2015-10-02 11:46:32 +02:00
try {
2018-06-25 12:42:41 +02:00
storageHubWrapper = WsUtil . getStorageHubWrapper ( request , scopeGroupId , user ) ;
2015-10-02 11:46:32 +02:00
} catch ( Exception e ) {
logger . error ( " Error during workspace retrieving " , e ) ;
2015-10-14 15:32:40 +02:00
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " An error occurred during upload: " + fileName + " . Error processing request in upload servlet " , request . getSession ( ) ) ;
2015-10-02 11:46:32 +02:00
response . sendError ( HttpServletResponse . SC_INTERNAL_SERVER_ERROR , " Error processing request in upload servlet " ) ;
return ;
}
2018-06-25 12:42:41 +02:00
if ( storageHubWrapper = = null ) {
2015-10-02 11:46:32 +02:00
logger . error ( " Now workspace found in session " ) ;
2015-10-14 15:32:40 +02:00
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " An error occurred during upload: " + fileName + " . No workspace in session " , request . getSession ( ) ) ;
2018-06-25 12:42:41 +02:00
sendError ( response , " Internal error: No workspace retrieved " ) ;
2015-10-02 11:46:32 +02:00
return ;
}
2019-01-29 10:45:36 +01:00
WorkspaceItem destinationItem = null ;
2019-05-03 14:55:37 +02:00
Workspace workspace = null ;
2015-10-02 11:46:32 +02:00
try {
2016-05-12 15:12:45 +02:00
logger . debug ( " getWorkspaceItem destination id: " + destinationId + " from HL " ) ;
2019-05-03 14:55:37 +02:00
workspace = storageHubWrapper . getWorkspace ( ) ;
destinationItem = workspace . getItem ( destinationId ) ;
2018-06-25 12:42:41 +02:00
} catch ( ItemNotFoundException | InternalErrorException e ) {
2015-10-02 11:46:32 +02:00
logger . error ( " Error, no destination folder found " , e ) ;
2015-10-14 15:32:40 +02:00
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " An error occurred during upload: " + fileName + " . No destination folder found " , request . getSession ( ) ) ;
2015-10-02 11:46:32 +02:00
sendError ( response , " Internal error: No destination folder found " ) ;
return ;
2018-10-17 12:50:41 +02:00
} catch ( Exception e ) {
logger . error ( " Error, no destination folder found " , e ) ;
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " An error occurred during upload: " + fileName , request . getSession ( ) ) ;
sendError ( response , " Internal error: No destination folder found " ) ;
return ;
2016-12-21 14:20:48 +01:00
}
2015-10-02 11:46:32 +02:00
2019-01-29 10:45:36 +01:00
if ( ! destinationItem . isFolder ( ) ) {
logger . error ( " Error processing request in upload servlet: The destination is not a folder " ) ;
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " An error occurred during upload: " + fileName + " . The destination is not a folder " , request . getSession ( ) ) ;
2019-09-16 12:19:31 +02:00
sendError ( response , " Internal error: Wrong destination item. The destination is not a folder " ) ;
2015-10-02 11:46:32 +02:00
return ;
}
2016-02-19 09:57:58 +01:00
2019-01-29 10:45:36 +01:00
final WorkspaceFolder destinationFolder = ( WorkspaceFolder ) destinationItem ;
2019-05-03 14:55:37 +02:00
2019-09-16 12:19:31 +02:00
try {
workspace . canUserWriteIntoFolder ( destinationFolder . getId ( ) ) ;
} catch ( Exception e1 ) {
logger . warn ( " The user cannot write in the destination folder with id: {} " , destinationFolder . getId ( ) ) ;
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " You are not authorized to write into folder: " + destinationFolder . getName ( ) , request . getSession ( ) ) ;
sendError ( response , " Internal error: You are not authorized to write into folder: " + destinationFolder . getName ( ) ) ;
return ;
2019-05-03 14:55:37 +02:00
}
2018-10-03 17:54:27 +02:00
2015-10-02 11:46:32 +02:00
try {
2018-10-03 16:28:52 +02:00
//Removing path from fileName
2018-06-26 17:17:10 +02:00
String itemName = fileName ;
2018-10-03 16:28:52 +02:00
//Getting extension
String extension = FilenameUtils . getExtension ( itemName ) ;
logger . debug ( " extension: " + extension ) ;
//Getting base name
String baseName = FilenameUtils . getBaseName ( itemName ) ; //Using base name in order to avoid Task #12470
2019-07-09 11:06:48 +02:00
//Task #17152
extension = extension . isEmpty ( ) ? " " : " . " + extension ;
itemName = baseName + extension ;
2018-10-03 16:28:52 +02:00
logger . debug ( " purged item name is: " + itemName ) ;
//SIZE
Long size = getContentLength ( request ) ;
logger . debug ( " size: " + size + " bytes " ) ;
2016-02-19 09:57:58 +01:00
//CONFIRM DESTINATION FOLDER
2015-10-02 11:46:32 +02:00
workspaceUploader . getFile ( ) . setParentId ( destinationFolder . getId ( ) ) ;
2016-02-19 09:57:58 +01:00
2018-10-03 16:45:19 +02:00
//Instancing the progress listener
2015-10-02 11:46:32 +02:00
final AbstractUploadProgressListener uploadProgressListener = createNewListener ( request . getSession ( ) , clientUploadKey ) ;
final UploadProgressInputStream inputStream = new UploadProgressInputStream ( uploadItem . openStream ( ) , size ) ;
inputStream . addListener ( uploadProgressListener ) ;
workspaceUploader . setUploadProgress ( uploadProgressListener . getUploadProgress ( ) ) ;
2016-02-19 09:57:58 +01:00
2018-07-06 11:47:26 +02:00
//USING isOverwrite to check if the file already exists
try {
isOverwrite = storageHubWrapper . getWorkspace ( ) . exists ( itemName , destinationFolder . getId ( ) ) ;
workspaceUploader . setIsOverwrite ( isOverwrite ) ;
logger . info ( " The file: " + itemName + " exists in the folder? " + isOverwrite ) ;
saveWorkspaceUploaderInSession ( workspaceUploader , request . getSession ( ) ) ;
} catch ( ItemNotFoundException | WrongItemTypeException e ) {
logger . warn ( " Error on checking if the file: " + itemName + " exists in the folder id: " + destinationFolder . getId ( ) ) ;
}
2015-10-02 11:46:32 +02:00
saveWorkspaceUploaderInSession ( workspaceUploader , request . getSession ( ) ) ;
2016-02-19 09:57:58 +01:00
2018-09-19 15:15:28 +02:00
String contentType = uploadItem . getContentType ( ) ;
logger . debug ( " Stream content type: " + contentType ) ;
2016-02-19 09:57:58 +01:00
2018-09-19 18:09:52 +02:00
if ( uploadType . compareTo ( ARCHIVE ) = = 0 ) {
//UPLOAD ARCHIVE
2018-09-18 17:42:00 +02:00
2018-09-19 15:15:28 +02:00
logger . debug ( " Uploding archive.... " ) ;
boolean isSupportedArchive = false ;
for ( String archive_extension : SUPPORTED_UNPACKING_ARCHIVE . keySet ( ) ) {
2016-02-19 09:57:58 +01:00
2018-09-19 15:15:28 +02:00
//is content-type supported?
if ( SUPPORTED_UNPACKING_ARCHIVE . get ( archive_extension ) . compareTo ( uploadItem . getContentType ( ) ) = = 0 ) {
isSupportedArchive = true ;
break ;
}
}
2016-02-19 09:57:58 +01:00
2018-09-19 15:15:28 +02:00
if ( isSupportedArchive ) { //Uploading Archive OK
logger . debug ( " Supported archive " + uploadItem . getContentType ( ) + " for unpacking " ) ;
2018-10-03 17:26:47 +02:00
//USING the baseName instead of itemName in oder to avoid adding extension as suffix
workspaceUploader = WorkspaceUploaderMng . uploadArchive ( storageHubWrapper , user , scopeGroupId , workspaceUploader , request , baseName , inputStream , destinationFolder , size ) ;
2018-09-19 15:15:28 +02:00
if ( workspaceUploader = = null )
throw new Exception ( " Error when creating uploader, it is null! " ) ;
2018-11-12 15:00:33 +01:00
/ *
* Incident # 10095 . Commented OK as response
* in order to avoid downloading of . dms file by Safari
* from MAC
* /
//sendMessage(response, workspaceUploader.getIdentifier());
2018-09-19 15:15:28 +02:00
} else {
logger . warn ( " Unsuppoterd archive for unpacking: " + uploadItem . getContentType ( ) ) ;
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . ABORTED , " Unpacking not supported for: " + uploadItem . getContentType ( ) + " Supported archive are: " + SUPPORTED_UNPACKING_ARCHIVE . keySet ( ) . toString ( ) , request . getSession ( ) ) ;
sendError ( response , " Internal error: Unpacking not supported for " + uploadItem . getContentType ( ) ) ;
return ;
}
2016-02-19 09:57:58 +01:00
2018-09-19 18:09:52 +02:00
} else {
//UPLOAD FILE. IT IS DEFAULT CASE
2018-09-18 17:01:31 +02:00
2018-09-19 18:09:52 +02:00
logger . debug ( " Uploding file.... " ) ;
2018-09-18 17:42:00 +02:00
workspaceUploader = WorkspaceUploaderMng . uploadFile ( storageHubWrapper , user , scopeGroupId , request , workspaceUploader , request . getSession ( ) , itemName , inputStream , destinationFolder , contentType , isOverwrite , size ) ;
2018-09-18 17:01:31 +02:00
2018-09-18 17:42:00 +02:00
if ( workspaceUploader = = null )
throw new Exception ( " Error when creating uploader, it is null! " ) ;
2018-09-18 17:01:31 +02:00
2018-11-08 12:44:38 +01:00
/ *
* Incident # 10095 . Commented OK as response
* in order to avoid downloading of . dms file by Safari
* from MAC
* /
//sendMessage(response, workspaceUploader.getIdentifier());
2015-10-02 11:46:32 +02:00
}
} catch ( InsufficientPrivilegesException e ) {
logger . error ( " Error creating elements " , e ) ;
2018-09-19 15:15:28 +02:00
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " Uploading error. Insufficient privileges " , request . getSession ( ) ) ;
2015-10-02 11:46:32 +02:00
sendError ( response , " Internal error: Insufficient privileges " ) ;
return ;
} catch ( InternalErrorException e ) {
logger . error ( " Error creating elements " , e ) ;
2018-09-19 15:15:28 +02:00
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " Uploading error: " + e . getMessage ( ) , request . getSession ( ) ) ;
2015-10-02 11:46:32 +02:00
sendError ( response , " Internal error: " + e . getMessage ( ) ) ;
return ;
} catch ( ItemAlreadyExistException e ) {
logger . error ( " Error creating elements " , e ) ;
2018-09-19 15:15:28 +02:00
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " Uploading error. An item with that name already exists " , request . getSession ( ) ) ;
2015-10-02 11:46:32 +02:00
sendError ( response , " Internal error: An item with that name already exists " ) ;
return ;
2016-05-20 17:06:10 +02:00
} catch ( IOException e ) {
logger . error ( " Error creating elements, is it cancel? " , e ) ;
2018-09-19 15:15:28 +02:00
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " Uploading error " , request . getSession ( ) ) ;
2016-05-20 17:06:10 +02:00
sendError ( response , " Internal error: An item with that name already exists " ) ;
return ;
// sendError(response, "Internal error: An item with that name already exists");
// return;
2015-10-02 11:46:32 +02:00
} catch ( Exception e ) {
logger . error ( " Error creating elements " , e ) ;
2018-09-19 15:15:28 +02:00
saveWorkspaceUploaderStatus ( workspaceUploader , UPLOAD_STATUS . FAILED , " Uploading error. " + e . getMessage ( ) , request . getSession ( ) ) ;
2015-10-02 11:46:32 +02:00
// sendError(response, "Internal error: An error occurred on uploading the file, try again later");
2016-05-20 17:06:10 +02:00
// return;
2015-10-02 11:46:32 +02:00
} finally {
removeCurrentListener ( request . getSession ( ) , clientUploadKey ) ;
}
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Save workspace uploader status .
*
* @param workspaceUploader the workspace uploader
* @param status the status
* @param description the description
* @param session the session
* @return the workspace uploader item
* /
private synchronized WorkspaceUploaderItem saveWorkspaceUploaderStatus ( WorkspaceUploaderItem workspaceUploader , UPLOAD_STATUS status , String description , HttpSession session ) {
workspaceUploader . setUploadStatus ( status ) ;
workspaceUploader . setStatusDescription ( description ) ;
try {
saveWorkspaceUploaderInSession ( workspaceUploader , session ) ;
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) ) ;
}
return workspaceUploader ;
}
/ * *
* Creates the new workspace uploader .
*
* @param clientUploadKey the client upload key
* @param folderParentId the folder parent id
* @param fileName the file name
2016-05-11 10:25:22 +02:00
* @param isOverwrite the is overwrite
2015-10-02 11:46:32 +02:00
* @return the workspace uploader item
* /
2016-01-08 12:32:48 +01:00
private WorkspaceUploaderItem createNewWorkspaceUploader ( String clientUploadKey , String folderParentId , String fileName , boolean isOverwrite ) {
2015-10-02 11:46:32 +02:00
//CLIENT UPLOAD IS THE KEY
WorkspaceUploaderItem workspaceUploader = new WorkspaceUploaderItem ( clientUploadKey ) ;
workspaceUploader . setClientUploadKey ( clientUploadKey ) ;
//Create File
2017-04-11 11:45:19 +02:00
WorkspaceUploadFile wsUploadFile = new WorkspaceUploadFile ( folderParentId , null , fileName , null ) ;
2015-10-02 11:46:32 +02:00
workspaceUploader . setFile ( wsUploadFile ) ;
2016-01-08 12:32:48 +01:00
workspaceUploader . setIsOverwrite ( isOverwrite ) ;
2015-10-02 11:46:32 +02:00
return workspaceUploader ;
}
/ * *
* Gets the random .
*
* @return the random
* /
private static int getRandom ( ) {
Random randomGenerator = new Random ( ) ;
return randomGenerator . nextInt ( Integer . MAX_VALUE ) ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Save workspace uploader in session .
*
* @param workspaceUploader the workspace uploader
* @param httpSession the http session
* @return the workspace uploader item
* @throws Exception the exception
* /
public static void saveWorkspaceUploaderInSession ( WorkspaceUploaderItem workspaceUploader , HttpSession httpSession ) throws Exception {
if ( workspaceUploader ! = null ) {
try {
WsUtil . putWorkspaceUploaderInSession ( httpSession , workspaceUploader ) ;
} catch ( Exception e ) {
logger . error ( " Error during WorkspaceUploaderItem save in session workspace uploader: " + workspaceUploader , e ) ;
2015-10-14 15:32:40 +02:00
throw new Exception ( " An error occurred in the upload. Try again " ) ;
2015-10-02 11:46:32 +02:00
}
} else
2015-10-14 15:32:40 +02:00
throw new Exception ( " An error occurred in the upload. Workspace Uploader not found. Abort and try again " ) ;
2015-10-02 11:46:32 +02:00
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
//TEST TIME
/ * *
* Prints the start time .
*
* @return the long
* /
public static Long printStartTime ( ) {
Long startTime = System . currentTimeMillis ( ) ;
logger . debug ( " Start time: " + startTime ) ;
return startTime ;
}
//TEST TIME
/ * *
* Prints the elapsed time .
*
* @param startTime the start time
* /
public static void printElapsedTime ( long startTime ) {
Long endTime = System . currentTimeMillis ( ) - startTime ;
String time = String . format ( " %d msc %d sec " , endTime , TimeUnit . MILLISECONDS . toSeconds ( endTime ) ) ;
logger . debug ( " Elapsed Time: " + time ) ;
}
2016-02-19 09:57:58 +01:00
2016-12-21 14:20:48 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Notify upload in shared folder .
*
2018-06-25 12:42:41 +02:00
* @param storageWrapper the storage wrapper
2017-01-20 14:26:38 +01:00
* @param currUser the curr user
2016-12-21 14:20:48 +01:00
* @param scopeGroupId the scope group id
2016-05-31 15:53:54 +02:00
* @param request the request
2015-10-02 11:46:32 +02:00
* @param httpSession the http session
* @param workspace the workspace
* @param itemId the item id
* @param destinationFolderId the destination folder id
* @param isOverwrite the is overwrite
* /
2018-06-25 12:42:41 +02:00
public static void notifyUploadInSharedFolder ( final StorageHubWrapper storageWrapper , final GCubeUser currUser , final String scopeGroupId , final HttpServletRequest request , final HttpSession httpSession , final String itemId , final String destinationFolderId , final boolean isOverwrite ) {
2016-07-14 14:30:46 +02:00
logger . trace ( " [2] HttpServletRequest is: URI: " + request . getRequestURI ( ) + " , ServerName: " + request . getServerName ( ) ) ;
2016-11-24 12:28:48 +01:00
final NotificationsWorkspaceUploaderProducer np = new NotificationsWorkspaceUploaderProducer ( scopeGroupId , httpSession , request ) ;
2015-10-02 11:46:32 +02:00
new Thread ( ) {
public void run ( ) {
2018-06-25 12:42:41 +02:00
//Item sourceItem;
2015-10-02 11:46:32 +02:00
try {
2018-06-25 12:42:41 +02:00
Item sourceItem = storageWrapper . getStorageHubClientService ( ) . getItem ( itemId ) ;
2018-06-26 17:17:10 +02:00
String sourceSharedId = null ;
2018-07-02 12:09:28 +02:00
try {
sourceSharedId = storageWrapper . getStorageHubClientService ( ) . getIdSharedFolder ( itemId ) ;
} catch ( Exception e ) {
//silent
}
2018-06-25 12:42:41 +02:00
Item folderDestinationItem = storageWrapper . getStorageHubClientService ( ) . getItem ( destinationFolderId ) ;
2018-07-02 12:09:28 +02:00
FolderItem folderDestination = null ;
if ( folderDestinationItem instanceof FolderItem ) {
folderDestination = ( FolderItem ) folderDestinationItem ;
}
Validate . notNull ( folderDestination , " The folder destionation is null " ) ;
2016-07-14 14:30:46 +02:00
logger . trace ( " [3] HttpServletRequest is: URI: " + request . getRequestURI ( ) + " , ServerName: " + request . getServerName ( ) ) ;
2018-07-02 12:09:28 +02:00
NotificationsWorkspaceUploader . checkSendNotifyChangedItemToShare ( storageWrapper , request , currUser , scopeGroupId , np , httpSession , sourceItem , sourceSharedId , folderDestination , isOverwrite ) ;
2018-06-25 12:42:41 +02:00
2015-10-02 11:46:32 +02:00
} catch ( Exception e ) {
logger . error ( " Error in notifyUploadInSharedFolder " , e ) ;
2016-02-19 09:57:58 +01:00
}
2015-10-02 11:46:32 +02:00
} ;
2016-02-19 09:57:58 +01:00
} . start ( ) ;
2015-10-02 11:46:32 +02:00
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Send error .
*
* @param response the response
* @param message the message
* @throws IOException Signals that an I / O exception has occurred .
* /
protected void sendError ( HttpServletResponse response , String message ) throws IOException {
try {
response . setStatus ( HttpServletResponse . SC_INTERNAL_SERVER_ERROR ) ;
HandlerResultMessage resultMessage = HandlerResultMessage . errorResult ( message ) ;
response . getWriter ( ) . write ( resultMessage . toString ( ) ) ;
//5.6 Closure of Response Object:
//When a response is closed, the container must immediately flush all remaining content in the response buffer to the client
2016-08-30 17:11:06 +02:00
// response.flushBuffer();
} catch ( IOException e ) {
logger . warn ( " IOException class name: " + e . getClass ( ) . getSimpleName ( ) ) ;
if ( e . getClass ( ) . getSimpleName ( ) . equals ( " ClientAbortException " ) )
logger . warn ( " Skipping ClientAbortException: " + e . getMessage ( ) ) ;
else
throw e ; //Sending Exceptions
}
}
/ * *
* Send session expired .
*
* @param response the response
* @param message the message
* @throws IOException Signals that an I / O exception has occurred .
* /
protected void sendSessionExpired ( HttpServletResponse response , String message ) throws IOException {
try {
response . setStatus ( HttpServletResponse . SC_UNAUTHORIZED ) ;
HandlerResultMessage resultMessage = HandlerResultMessage . sessionExpiredResult ( message ) ;
response . getWriter ( ) . write ( resultMessage . toString ( ) ) ;
//5.6 Closure of Response Object:
//When a response is closed, the container must immediately flush all remaining content in the response buffer to the client
2015-10-02 11:46:32 +02:00
// response.flushBuffer();
} catch ( IOException e ) {
logger . warn ( " IOException class name: " + e . getClass ( ) . getSimpleName ( ) ) ;
if ( e . getClass ( ) . getSimpleName ( ) . equals ( " ClientAbortException " ) )
logger . warn ( " Skipping ClientAbortException: " + e . getMessage ( ) ) ;
else
throw e ; //Sending Exceptions
}
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Send message .
*
* @param response the response
* @param message the message
* @throws IOException Signals that an I / O exception has occurred .
* /
protected void sendMessage ( HttpServletResponse response , String message ) throws IOException {
try {
response . setStatus ( HttpServletResponse . SC_ACCEPTED ) ;
2016-05-11 10:25:22 +02:00
message = message . replaceAll ( " : " , " " ) ;
2015-10-02 11:46:32 +02:00
HandlerResultMessage resultMessage = HandlerResultMessage . okResult ( message ) ;
response . getWriter ( ) . write ( resultMessage . toString ( ) ) ;
//5.6 Closure of Response Object:
//When a response is closed, the container must immediately flush all remaining content in the response buffer to the client
// response.flushBuffer();
} catch ( IOException e ) {
logger . warn ( " IOException class name: " + e . getClass ( ) . getSimpleName ( ) ) ;
if ( e . getClass ( ) . getSimpleName ( ) . equals ( " ClientAbortException " ) )
logger . warn ( " Skipping ClientAbortException: " + e . getMessage ( ) ) ;
else
throw e ; //Sending Exceptions
}
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Send warn message .
*
* @param response the response
* @param message the message
* @throws IOException Signals that an I / O exception has occurred .
* /
protected void sendWarnMessage ( HttpServletResponse response , String message ) throws IOException {
try {
response . setStatus ( HttpServletResponse . SC_ACCEPTED ) ;
HandlerResultMessage resultMessage = HandlerResultMessage . warnResult ( message ) ;
response . getWriter ( ) . write ( resultMessage . toString ( ) ) ;
//5.6 Closure of Response Object:
//When a response is closed, the container must immediately flush all remaining content in the response buffer to the client
// response.flushBuffer();
} catch ( IOException e ) {
logger . warn ( " IOException class name: " + e . getClass ( ) . getSimpleName ( ) ) ;
if ( e . getClass ( ) . getSimpleName ( ) . equals ( " ClientAbortException " ) )
logger . warn ( " Skipping ClientAbortException: " + e . getMessage ( ) ) ;
else
throw e ; //Sending Exceptions
}
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Mark the current upload process to be canceled .
*
* @param session the session
* @param clientUploadKey the client upload key
2016-05-12 15:12:45 +02:00
* @return true , if successful
2015-10-02 11:46:32 +02:00
* /
2016-05-12 15:12:45 +02:00
public boolean cancelUpload ( HttpSession session , String clientUploadKey ) {
2015-10-02 11:46:32 +02:00
logger . debug ( " UPLOAD-SERVLET ( " + session . getId ( ) + " ) cancelling Upload: " + clientUploadKey ) ;
AbstractUploadProgressListener listener = getCurrentListener ( session , clientUploadKey ) ;
if ( listener ! = null & & ! listener . isCanceled ( ) ) {
logger . info ( " CancelUpload listener is " + listener . toString ( ) ) ;
listener . setException ( new UploadCanceledException ( ) ) ;
2016-05-12 15:12:45 +02:00
return true ;
} else {
2015-10-02 11:46:32 +02:00
logger . info ( " Skipping cancel upload: listener is null or is cancel " ) ;
2016-05-12 15:12:45 +02:00
return false ;
}
2015-10-02 11:46:32 +02:00
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Get the listener active in this session .
*
* @param session the session
* @param clientUploadKey the client upload key
* @return the listener active
* /
protected AbstractUploadProgressListener getCurrentListener ( HttpSession session , String clientUploadKey ) {
if ( isAppEngine ( ) ) {
return MemoryUploadListener . current ( session . getId ( ) , clientUploadKey ) ;
} else {
return UploadProgressListener . current ( session , clientUploadKey ) ;
}
}
/ * *
* Just a method to detect whether the web container is running with
* appengine restrictions .
*
* @return true if the case of the application is running in appengine
* /
public boolean isAppEngine ( ) {
return appEngine ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Create a new listener for this session .
*
* @param session the session
* @param clientUploadKey the client upload key
* @return the appropriate listener
* /
protected AbstractUploadProgressListener createNewListener ( HttpSession session , String clientUploadKey ) {
if ( isAppEngine ( ) ) {
return new MemoryUploadListener ( session , clientUploadKey , 0 , 100 ) ;
} else {
return new UploadProgressListener ( session , clientUploadKey , 0 , 100 ) ;
}
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Gets the content length .
*
* @param request the request
* @return the content length
* /
private long getContentLength ( HttpServletRequest request ) {
long size = - 1 ;
try {
size = Long . parseLong ( request
. getHeader ( FileUploadBase . CONTENT_LENGTH ) ) ;
} catch ( NumberFormatException e ) {
}
return size ;
}
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
/ * *
* Remove the listener active in this session .
*
* @param session the session
* @param clientUploadKey the client upload key
* /
protected void removeCurrentListener ( HttpSession session , String clientUploadKey ) {
logger . debug ( " RemoveCurrentListener: " + clientUploadKey ) ;
AbstractUploadProgressListener listener = getCurrentListener ( session , clientUploadKey ) ;
2016-02-19 09:57:58 +01:00
2015-10-02 11:46:32 +02:00
if ( listener ! = null ) {
logger . debug ( " Removing listener: " + listener . getClientUploadKey ( ) ) ;
listener . remove ( ) ;
} else
logger . warn ( " Listener " + clientUploadKey + " is null " ) ;
}
}