Search in sources :

Example 56 with FileSystemOperationException

use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.

the class LocalFileSystemOperations method replaceTextInFile.

@Override
public void replaceTextInFile(String fileName, Map<String, String> searchTokens, boolean isRegex) {
    BufferedReader inFileReader = null;
    BufferedWriter outFileWriter = null;
    File inputFile = null;
    File outputFile = null;
    StringBuilder info = new StringBuilder();
    for (String token : searchTokens.keySet()) {
        info.append(token);
        info.append(",");
    }
    info = info.deleteCharAt(info.length() - 1);
    try {
        try {
            inputFile = new File(fileName);
            outputFile = new File(fileName + "_" + System.currentTimeMillis() + ".tmp");
            inFileReader = new BufferedReader(new FileReader(inputFile));
            outFileWriter = new BufferedWriter(new FileWriter(outputFile, false));
            String currentLine = inFileReader.readLine();
            while (currentLine != null) {
                for (Entry<String, String> tokens : searchTokens.entrySet()) {
                    if (isRegex) {
                        currentLine = currentLine.replaceAll(tokens.getKey(), tokens.getValue());
                    } else {
                        currentLine = currentLine.replace(tokens.getKey(), tokens.getValue());
                    }
                }
                outFileWriter.write(currentLine);
                outFileWriter.newLine();
                // read a new line
                currentLine = inFileReader.readLine();
            }
            log.info("Successfully replaced all" + (isRegex ? " regular expression" : "") + " instances of '" + info.toString() + "' in file '" + fileName + "'");
        } finally {
            IoUtils.closeStream(inFileReader);
            IoUtils.closeStream(outFileWriter);
        }
        // after we are finished, rename the temporary file to the original one
        if (OperatingSystemType.getCurrentOsType().isUnix()) {
            // getting original file permissions before overriding operation
            String permissions = getFilePermissions(inputFile.getCanonicalPath());
            renameFile(outputFile.getCanonicalPath(), fileName, true);
            // restoring file permissions
            setFilePermissions(fileName, permissions);
        } else {
            renameFile(outputFile.getCanonicalPath(), fileName, true);
        }
    } catch (IOException ioe) {
        throw new FileSystemOperationException("Unable to replace" + (isRegex ? " regular expression" : "") + " instances of '" + info.toString() + "' in file '" + fileName + "'", ioe);
    }
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) ReversedLinesFileReader(org.apache.commons.io.input.ReversedLinesFileReader) FileReader(java.io.FileReader) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 57 with FileSystemOperationException

use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.

the class LocalFileSystemOperations method appendToFile.

@Override
public void appendToFile(String filePath, String contentToAdd) {
    if (!new File(filePath).exists()) {
        throw new FileSystemOperationException("Unable to append content to file '" + filePath + "' as it does not exist");
    }
    if (new File(filePath).isDirectory()) {
        throw new FileSystemOperationException("Unable to append content to '" + filePath + "' as it is a directory, but file was expected");
    }
    BufferedWriter fileWriter = null;
    try {
        fileWriter = new BufferedWriter(new FileWriter(filePath, true));
        fileWriter.write(contentToAdd);
    } catch (IOException ioe) {
        throw new FileSystemOperationException("Unable to append content to file '" + filePath + "'", ioe);
    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
                log.info("Successfully appended " + contentToAdd.length() + " characters to file '" + filePath + "'");
            } catch (IOException ioe) {
                log.error("Could not close file writer", ioe);
            }
        }
    }
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 58 with FileSystemOperationException

use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.

the class LocalFileSystemOperations method sendFileTo.

/**
 * Send file contents to another machine
 *
 * @param fromFileName the source file name
 * @param toFileName   the destination file name (Not a directory)
 * @param toHost       the destination host address
 * @param toPort       the destination port
 * @param failOnError  set to true if you want to be thrown an exception,
 *                     if there is still a process writing in the file that is being copied
 * @throws FileSystemOperationException
 */
public void sendFileTo(String fromFileName, String toFileName, String toHost, int toPort, boolean failOnError) {
    File file = new File(fromFileName);
    checkFileExistence(file);
    Socket socket = null;
    OutputStream sos = null;
    try {
        socket = new Socket(toHost, toPort);
        sos = socket.getOutputStream();
        sendFileToSocketStream(file, toFileName, sos, failOnError);
    } catch (IOException ioe) {
        throw new FileSystemOperationException("Unable to send file '" + fromFileName + "' to '" + toFileName + "' on " + toHost + ":" + toPort, ioe);
    } finally {
        IoUtils.closeStream(sos);
        IoUtils.closeStream(socket, "Could not close the socket for sending file " + fromFileName);
    }
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Aggregations

FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)58 IOException (java.io.IOException)32 File (java.io.File)31 RandomAccessFile (java.io.RandomAccessFile)29 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)29 FileDoesNotExistException (com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException)16 FileNotFoundException (java.io.FileNotFoundException)16 SocketTimeoutException (java.net.SocketTimeoutException)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)13 EOFException (java.io.EOFException)13 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)13 FileOutputStream (java.io.FileOutputStream)10 FileInputStream (java.io.FileInputStream)9 BufferedOutputStream (java.io.BufferedOutputStream)8 DataOutputStream (java.io.DataOutputStream)8 BaseTest (com.axway.ats.action.BaseTest)7 LocalFileSystemOperations (com.axway.ats.core.filesystem.LocalFileSystemOperations)7 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7