agave-files/files-api/src/main/java/org/iplantc/service/io/resources/FileManagementResource.java
/**
	 * Deletes a file or folder with provenance and alerts
	 *
	 */
Deletes a file or folder with provenance and alerts
Deletes a file or folder with provenance and alerts
@Delete
	@Override
	public Representation delete()
	{
		AgaveLogServiceClient.log(AgaveLogServiceClient.ServiceKeys.FILES02.name(),
				AgaveLogServiceClient.ActivityKeys.IODelete.name(),
				username, "", getRequest().getClientInfo().getUpstreamAddress());
		try {
			LogicalFile logicalFile = null;
            try {
            	logicalFile = LogicalFileDao.findBySystemAndPath(system, remoteDataClient.resolvePath(path));
            }
            catch(Exception e){
    			if (log.isDebugEnabled()) {
    				String msg = "LogicalFileDao.findBySystemAndPath() failed, deletion continuing for logical file " +
    		                 	 path + ".";
    				log.debug(msg, e);
    			}
            }
            PermissionManager pm = new PermissionManager(system, remoteDataClient, logicalFile, username);
            AgaveSuccessRepresentation representation = new AgaveSuccessRepresentation();
            if (logicalFile == null) {
                if (!pm.canWrite(remoteDataClient.resolvePath(path))) {
                	String msg = "User does not have permission to delete " + path;
                	log.error(msg);
                    throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, 
                    		                    msg, new PermissionException());
                } else {
                    try {
                        remoteDataClient.delete(path);
                        if (representation.isNaked()) {
                        	setStatus(Status.SUCCESS_NO_CONTENT);
                        } else {
                        	setStatus(Status.SUCCESS_OK);
                        }
                        return representation;
                    } 
                    catch (FileNotFoundException e) {
                    	String msg = "Remote path " + path + " does not exist.";
                    	log.error(msg);
                    	setStatus(Status.CLIENT_ERROR_NOT_FOUND, msg);
                    	return new AgaveErrorRepresentation(msg);
                    }
                    catch (Exception e) {
                    	log.error("Failed to delete file agave://" + systemId + "/" + path, e);
                    	throw new ResourceException(Status.SERVER_ERROR_INTERNAL, 
                        		"Failed to delete " + path + " on the remote system.");
                    }
                }
            } else {
                try {
                    try {
                        if (!pm.canWrite(remoteDataClient.resolvePath(path))) {
                        	String msg = "User does not have permission to delete " + path;
                        	log.error(msg);
                            throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN, 
                            		                    msg, new PermissionException());
                        }
                    } 
                    catch (PermissionException e) {
                    	String msg = "Failed to retrieve permissions for " + path;
                    	log.error(msg, e);
                        throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN, 
                        		                    msg + ", " + e.getMessage());
                    }
                    try {
                    	remoteDataClient.delete(path);
					} catch (FileNotFoundException e) {
					}
                    logicalFile.addContentEvent(new FileEvent(FileEventType.DELETED, "Deleted via API", 
							getAuthenticatedUsername()));
                    LogicalFileDao.removeSubtree(logicalFile);
                    if (representation.isNaked()) {
                    	setStatus(Status.SUCCESS_NO_CONTENT);
                    } else {
                    	setStatus(Status.SUCCESS_OK);
                    }
                    return representation;
                }
                catch (ResourceException e) {
                	log.error(e.getMessage(), e);
                	throw e;
                }
                catch (Throwable e) {
                	log.error("File deletion failed: " + e.getMessage(), e);
                	throw new ResourceException(Status.SERVER_ERROR_INTERNAL, 
                			"File deletion failed");
                }
            }
        } 
		catch(ResourceException e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e) {
			String msg = "Failed to retrieve information for " + path;
			log.error(msg, e);
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, msg, e);
        } finally {
        	try {remoteDataClient.disconnect();} catch (Exception ignored) {}
		}
	}
@ Delete @ Override public Representation delete ( ) { AgaveLogServiceClient . log ( AgaveLogServiceClient . ServiceKeys . FILES02 . name ( ) , AgaveLogServiceClient . ActivityKeys . IODelete . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; try { LogicalFile logicalFile = null ; try { logicalFile = LogicalFileDao . findBySystemAndPath ( system , remoteDataClient . resolvePath ( path ) ) ; } catch ( Exception e ) { if ( log . isDebugEnabled ( ) ) { String msg = "LogicalFileDao.findBySystemAndPath() failed, deletion continuing for logical file " + path + "." ; log . debug ( msg , e ) ; } } PermissionManager pm = new PermissionManager ( system , remoteDataClient , logicalFile , username ) ; AgaveSuccessRepresentation representation = new AgaveSuccessRepresentation ( ) ; if ( logicalFile == null ) { if ( ! pm . canWrite ( remoteDataClient . resolvePath ( path ) ) ) { String msg = "User does not have permission to delete " + path ; log . error ( msg ) ; throw new ResourceException ( Status . CLIENT_ERROR_UNAUTHORIZED , msg , new PermissionException ( ) ) ; } else { try { remoteDataClient . delete ( path ) ; if ( representation . isNaked ( ) ) { setStatus ( Status . SUCCESS_NO_CONTENT ) ; } else { setStatus ( Status . SUCCESS_OK ) ; } return representation ; } catch ( FileNotFoundException e ) { String msg = "Remote path " + path + " does not exist." ; log . error ( msg ) ; setStatus ( Status . CLIENT_ERROR_NOT_FOUND , msg ) ; return new AgaveErrorRepresentation ( msg ) ; } catch ( Exception e ) { log . error ( "Failed to delete file agave://" + systemId + "/" + path , e ) ; throw new ResourceException ( Status . SERVER_ERROR_INTERNAL , "Failed to delete " + path + " on the remote system." ) ; } } } else { try { try { if ( ! pm . canWrite ( remoteDataClient . resolvePath ( path ) ) ) { String msg = "User does not have permission to delete " + path ; log . error ( msg ) ; throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , msg , new PermissionException ( ) ) ; } } catch ( PermissionException e ) { String msg = "Failed to retrieve permissions for " + path ; log . error ( msg , e ) ; throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , msg + ", " + e . getMessage ( ) ) ; } try { remoteDataClient . delete ( path ) ; } catch ( FileNotFoundException e ) { } logicalFile . addContentEvent ( new FileEvent ( FileEventType . DELETED , "Deleted via API" , getAuthenticatedUsername ( ) ) ) ; LogicalFileDao . removeSubtree ( logicalFile ) ; if ( representation . isNaked ( ) ) { setStatus ( Status . SUCCESS_NO_CONTENT ) ; } else { setStatus ( Status . SUCCESS_OK ) ; } return representation ; } catch ( ResourceException e ) { log . error ( e . getMessage ( ) , e ) ; throw e ; } catch ( Throwable e ) { log . error ( "File deletion failed: " + e . getMessage ( ) , e ) ; throw new ResourceException ( Status . SERVER_ERROR_INTERNAL , "File deletion failed" ) ; } } } catch ( ResourceException e ) { log . error ( e . getMessage ( ) , e ) ; throw e ; } catch ( Exception e ) { String msg = "Failed to retrieve information for " + path ; log . error ( msg , e ) ; throw new ResourceException ( Status . SERVER_ERROR_INTERNAL , msg , e ) ; } finally { try { remoteDataClient . disconnect ( ) ; } catch ( Exception ignored ) { } } }
Deletes a file or folder with provenance and alerts
Deletes a file or folder with provenance and alerts
// ignore missing files/folders // this should cascade delete the permissions when the logical file deletes //            }