protected FtpMessage deleteFile ( DeleteCommand delete , TestContext context ) { String remoteFilePath = context . replaceDynamicContentInString ( delete . getTarget ( ) . getPath ( ) ) ; try { if ( ! StringUtils . hasText ( remoteFilePath ) ) { return null ; } boolean success = true ; if ( isDirectory ( remoteFilePath ) ) { if ( ! ftpClient . changeWorkingDirectory ( remoteFilePath ) ) { throw new CitrusRuntimeException ( "Failed to change working directory to " + remoteFilePath + ". FTP reply code: " + ftpClient . getReplyString ( ) ) ; } if ( delete . isRecursive ( ) ) { FTPFile [ ] ftpFiles = ftpClient . listFiles ( ) ; for ( FTPFile ftpFile : ftpFiles ) { DeleteCommand recursiveDelete = new DeleteCommand ( ) ; DeleteCommand . Target target = new DeleteCommand . Target ( ) ; target . setPath ( remoteFilePath + "/" + ftpFile . getName ( ) ) ; recursiveDelete . setTarget ( target ) ; recursiveDelete . setIncludeCurrent ( true ) ; deleteFile ( recursiveDelete , context ) ; } } if ( delete . isIncludeCurrent ( ) ) { // we cannot delete the current working directory, so go to root directory and delete from there ftpClient . changeWorkingDirectory ( "/" ) ; success = ftpClient . removeDirectory ( remoteFilePath ) ; } } else { success = ftpClient . deleteFile ( remoteFilePath ) ; } if ( ! success ) { throw new CitrusRuntimeException ( "Failed to delete path " + remoteFilePath + ". FTP reply code: " + ftpClient . getReplyString ( ) ) ; } } catch ( IOException e ) { throw new CitrusRuntimeException ( "Failed to delete file from FTP server" , e ) ; } // If there was no file to delete, the ftpClient has the reply code from the previously executed // operation. Since we want to have a deterministic behaviour, we need to set the reply code and // reply string on our own! if ( ftpClient . getReplyCode ( ) != FILE_ACTION_OK ) { return FtpMessage . deleteResult ( FILE_ACTION_OK , String . format ( "%s No files to delete." , FILE_ACTION_OK ) , true ) ; } return FtpMessage . deleteResult ( ftpClient . getReplyCode ( ) , ftpClient . getReplyString ( ) , isPositive ( ftpClient . getReplyCode ( ) ) ) ; }
Performs delete file operation .