Search in sources :

Example 11 with FileSystemOperationException

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

the class LocalFileSystemOperations method extractTarGZip.

private void extractTarGZip(String tarGzipFilePath, String outputDirPath) {
    TarArchiveEntry entry = null;
    try (TarArchiveInputStream tis = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarGzipFilePath)))) {
        while ((entry = (TarArchiveEntry) tis.getNextEntry()) != null) {
            if (log.isDebugEnabled()) {
                log.debug("Extracting " + entry.getName());
            }
            File entryDestination = new File(outputDirPath, entry.getName());
            if (entry.isDirectory()) {
                entryDestination.mkdirs();
            } else {
                entryDestination.getParentFile().mkdirs();
                OutputStream out = new BufferedOutputStream(new FileOutputStream(entryDestination));
                IoUtils.copyStream(tis, out, false, true);
            }
            if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
                // check if the OS is UNIX
                // set file/dir permissions, after it is created
                Files.setPosixFilePermissions(entryDestination.getCanonicalFile().toPath(), getPosixFilePermission(entry.getMode()));
            }
        }
    } catch (Exception e) {
        String errorMsg = null;
        if (entry != null) {
            errorMsg = "Unable to gunzip " + entry.getName() + " from " + tarGzipFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
        } else {
            errorMsg = "Could not read data from " + tarGzipFilePath;
        }
        throw new FileSystemOperationException(errorMsg, e);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) AttributeNotSupportedException(com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 12 with FileSystemOperationException

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

the class LocalFileSystemOperations method extractTar.

private void extractTar(String tarFilePath, String outputDirPath) {
    TarArchiveEntry entry = null;
    try (TarArchiveInputStream tis = new TarArchiveInputStream(new FileInputStream(tarFilePath))) {
        while ((entry = (TarArchiveEntry) tis.getNextEntry()) != null) {
            if (log.isDebugEnabled()) {
                log.debug("Extracting " + entry.getName());
            }
            File entryDestination = new File(outputDirPath, entry.getName());
            if (entry.isDirectory()) {
                entryDestination.mkdirs();
            } else {
                entryDestination.getParentFile().mkdirs();
                OutputStream out = new BufferedOutputStream(new FileOutputStream(entryDestination));
                IoUtils.copyStream(tis, out, false, true);
            }
            if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
                // check if the OS is UNIX
                // set file/dir permissions, after it is created
                Files.setPosixFilePermissions(entryDestination.getCanonicalFile().toPath(), getPosixFilePermission(entry.getMode()));
            }
        }
    } catch (Exception e) {
        String errorMsg = null;
        if (entry != null) {
            errorMsg = "Unable to untar " + StringEscapeUtils.escapeJava(entry.getName()) + " from " + tarFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
        } else {
            errorMsg = "Could not read data from " + tarFilePath;
        }
        throw new FileSystemOperationException(errorMsg, e);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) AttributeNotSupportedException(com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 13 with FileSystemOperationException

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

the class LocalFileSystemOperations method readFile.

@Override
public String readFile(String fileName, String fileEncoding) {
    File file = new File(fileName);
    if (!file.exists()) {
        throw new FileSystemOperationException("File '" + fileName + "' does not exist");
    }
    if (!file.isFile()) {
        throw new FileSystemOperationException("File '" + fileName + "' is not a regular file");
    }
    if (file.length() > FILE_SIZE_FOR_WARNING) {
        log.warn("Large file detected for reading contents! Name '" + fileName + "', " + file.length() + " bytes. Make sure you have " + "enough heap size specified to be able to read it.");
    }
    StringBuilder fileContents = new StringBuilder();
    BufferedReader fileReader = null;
    try {
        if (fileEncoding == null) {
            // use default system file encoding
            fileReader = new BufferedReader(new FileReader(file));
        } else {
            fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), fileEncoding));
        }
        char[] charBuffer = new char[READ_BUFFER_SIZE];
        int charsRead = -1;
        while ((charsRead = fileReader.read(charBuffer)) > -1) {
            fileContents.append(charBuffer, 0, charsRead);
        }
        return fileContents.toString();
    } catch (FileNotFoundException fnfe) {
        throw new FileSystemOperationException("File '" + fileName + "' does not exist");
    } catch (IOException ioe) {
        throw new FileSystemOperationException("Error reading file '" + fileName + "'", ioe);
    } finally {
        if (fileReader != null) {
            IoUtils.closeStream(fileReader);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) 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) FileInputStream(java.io.FileInputStream)

Example 14 with FileSystemOperationException

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

the class LocalFileSystemOperations method createFile.

private void createFile(String filename, String fileContent, long size, boolean randomContent, EndOfLineStyle eol) {
    String terminationString;
    if (eol == null) {
        terminationString = EndOfLineStyle.getCurrentOsStyle().getTerminationString();
    } else {
        terminationString = eol.getTerminationString();
    }
    BufferedWriter fileWriter = null;
    try {
        fileWriter = new BufferedWriter(new FileWriter(filename));
        // if the file size is bigger than 0, fill the file, otherwise create an empty file
        if (size > 0) {
            if (randomContent) {
                // init the random number generator class
                Random randomGen = new Random();
                int charAsciiCode = randomGen.nextInt(END_CHARACTER_CODE_DECIMAL - START_CHARACTER_CODE_DECIMAL) + START_CHARACTER_CODE_DECIMAL;
                // write a preceding random char before the line separator if possible,
                // then output the line-separator and fill the remainder with random data
                fileWriter.write((char) charAsciiCode);
                fileWriter.write(terminationString);
                long currentSize = terminationString.length() + 1;
                while (currentSize < size) {
                    // write a random character
                    charAsciiCode = randomGen.nextInt(END_CHARACTER_CODE_DECIMAL - START_CHARACTER_CODE_DECIMAL) + START_CHARACTER_CODE_DECIMAL;
                    fileWriter.write((char) charAsciiCode);
                    currentSize++;
                }
            } else {
                int charAsciiCode = START_CHARACTER_CODE_DECIMAL;
                // write a preceding random char before the line separator if possible,
                // then output the line-separator and fill the remainder with random data
                fileWriter.write((char) charAsciiCode);
                fileWriter.write(terminationString);
                long currentSize = terminationString.length() + 1;
                while (currentSize < size) {
                    // the last character
                    if (++charAsciiCode > END_CHARACTER_CODE_DECIMAL) {
                        charAsciiCode = START_CHARACTER_CODE_DECIMAL;
                    }
                    fileWriter.write((char) charAsciiCode);
                    currentSize++;
                }
            }
        } else if (fileContent != null) {
            fileWriter.write(fileContent);
        }
    } catch (IOException ioe) {
        throw new FileSystemOperationException("Could not generate file", ioe);
    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
                log.info("Successfully created file '" + filename + (fileContent != null ? "' with user defined content." : "' with size " + size));
            } catch (IOException ioe) {
                log.error("Could not close file writer", ioe);
            }
        }
    }
}
Also used : Random(java.util.Random) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 15 with FileSystemOperationException

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

the class LocalFileSystemOperations method openFileTransferSocketForSending.

// TODO: move file transfer operations into child or utility class
/**
 * Open file transfer socket for sending specific file
 *
 * @param nameOfFileToSend name of local (source) file name
 * @param targetFileName   destination file name
 * @param failOnError      whether we should fail if file is modified during reading and sending
 * @return the port where the socket is listening
 * @throws FileSystemOperationException
 */
public int openFileTransferSocketForSending(String nameOfFileToSend, String targetFileName, boolean failOnError) {
    int freePort = -1;
    try {
        final ServerSocket server;
        if (copyFileStartPort == null && copyFileEndPort == null) {
            server = new ServerSocket(0);
        } else {
            server = getServerSocket();
        }
        freePort = server.getLocalPort();
        if (log.isDebugEnabled()) {
            log.debug("Starting file sending transfer server on port: " + freePort);
        }
        final FileTransferStatus transferStatus = new FileTransferStatus();
        fileTransferStates.put(freePort, transferStatus);
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                Socket socket = null;
                DataOutputStream dos = null;
                try {
                    server.setReuseAddress(true);
                    server.setSoTimeout(FILE_TRANSFER_TIMEOUT);
                    socket = server.accept();
                    dos = new DataOutputStream(socket.getOutputStream());
                    sendFileToSocketStream(new File(nameOfFileToSend), targetFileName, dos, failOnError);
                } catch (SocketTimeoutException ste) {
                    // timeout usually will be when waiting for client connection but theoretically could be also
                    // in the middle of reading data
                    log.error("Reached timeout of " + (FILE_TRANSFER_TIMEOUT / 1000) + " seconds while waiting for file/directory copy operation.", ste);
                    transferStatus.transferException = ste;
                } catch (IOException e) {
                    log.error("An I/O error occurred", e);
                    transferStatus.transferException = e;
                } finally {
                    IoUtils.closeStream(dos);
                    if (socket != null) {
                        try {
                            socket.close();
                        } catch (IOException e) {
                            log.error("Could not close the Socket", e);
                        }
                    }
                    try {
                        server.close();
                    } catch (IOException e) {
                        log.error("Could not close the ServerSocket", e);
                    }
                    synchronized (transferStatus) {
                        transferStatus.finished = true;
                        transferStatus.notify();
                    }
                }
            }
        });
        thread.setName("ATSFileTransferSocket-port" + freePort + "__" + thread.getName());
        thread.start();
    } catch (Exception e) {
        throw new FileSystemOperationException("Unable to open file transfer socket for sending file '" + nameOfFileToSend + "'", e);
    }
    return freePort;
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) DataOutputStream(java.io.DataOutputStream) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) AttributeNotSupportedException(com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) 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