Search in sources :

Example 51 with FileSystemOperationException

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

the class LocalFileSystemOperations method copyDirectory.

@Override
public void copyDirectory(String fromDirName, String toDirName, boolean isRecursive, boolean failOnError) {
    if (log.isDebugEnabled()) {
        log.debug("Copy contents of directory '" + fromDirName + "' to '" + toDirName + "'");
    }
    if (fromDirName == null) {
        throw new IllegalArgumentException("Could not copy directories. The source directory name is null");
    }
    if (toDirName == null) {
        throw new IllegalArgumentException("Could not copy directories. The target directory name is null");
    }
    if (isRecursive && IoUtils.normalizeDirPath(toDirName).startsWith(IoUtils.normalizeDirPath(fromDirName))) {
        throw new IllegalArgumentException("Could not copy directories. The target directory is subdirectory of the source one");
    }
    File sourceDir = new File(fromDirName);
    if (!(sourceDir.exists() && sourceDir.isDirectory())) {
        throw new FileSystemOperationException("Could not read source directory. Directory named '" + fromDirName + "' does not exist.");
    }
    copyDirectoryInternal(sourceDir, new File(toDirName), null, isRecursive, failOnError);
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 52 with FileSystemOperationException

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

the class LocalFileSystemOperations method getLastLinesFromFile.

@Override
public String[] getLastLinesFromFile(String fileName, int numLinesToRead, Charset charset) {
    LinkedList<String> lastLinesList = new LinkedList<String>();
    ReversedLinesFileReader reversedReader = null;
    try {
        reversedReader = new ReversedLinesFileReader(new File(fileName), 4096, charset);
        while (lastLinesList.size() < numLinesToRead) {
            String line = reversedReader.readLine();
            // check if the file has less lines than the wanted
            if (line != null) {
                lastLinesList.addFirst(line);
            } else {
                break;
            }
        }
        return lastLinesList.toArray(new String[lastLinesList.size()]);
    } catch (IOException ioe) {
        throw new FileSystemOperationException("Error reading file '" + fileName + "'", ioe);
    } finally {
        if (reversedReader != null) {
            IoUtils.closeStream(reversedReader);
        }
    }
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) ReversedLinesFileReader(org.apache.commons.io.input.ReversedLinesFileReader) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) LinkedList(java.util.LinkedList)

Example 53 with FileSystemOperationException

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

the class LocalFileSystemOperations method readFile.

/**
 * Read file from specific position. Used for file tail.<br>
 *
 * <b>NOTE:</b> If the file is replaced with the same byte content, then no change is assumed and 'null' is returned
 *
 * @param fileName         file name
 * @param fromBytePosition byte offset. Example: for already read 100 bytes next method call is expected to have 100 as value for this parameter
 *                         return  {@link FileTailInfo} object
 */
public FileTailInfo readFile(String fileName, long fromBytePosition) {
    RandomAccessFile reader = null;
    try {
        long position = 0;
        boolean isFileRotated = false;
        // Open the file for read only
        File file = new File(fileName);
        try {
            reader = new RandomAccessFile(file, "r");
        } catch (FileNotFoundException fne) {
            throw new FileSystemOperationException("File '" + fileName + "' not found.", fne);
        }
        long length = file.length();
        if (length < fromBytePosition) {
            // File was rotated
            isFileRotated = true;
            position = 0;
        } else {
            position = fromBytePosition;
        }
        reader.seek(position);
        String line = IoUtils.readLineWithEOL(reader);
        if (line != null) {
            StringBuilder sb = new StringBuilder(line.length());
            while (line != null) {
                sb.append(line);
                // TODO: consider file encoding
                line = IoUtils.readLineWithEOL(reader);
            }
            position = reader.getFilePointer();
            return new FileTailInfo(position, isFileRotated, sb.toString());
        }
    } catch (Exception e) {
        throw new FileSystemOperationException("Could not read file '" + fileName + "' from byte position " + fromBytePosition, e);
    } finally {
        IoUtils.closeStream(reader);
    }
    return null;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) FileTailInfo(com.axway.ats.common.filesystem.FileTailInfo) FileNotFoundException(java.io.FileNotFoundException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) 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 54 with FileSystemOperationException

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

the class LocalFileSystemOperations method extractGZip.

private void extractGZip(String gzipFilePath, String outputDirPath) {
    String outputFileName = new File(gzipFilePath).getName();
    outputFileName = outputFileName.substring(0, outputFileName.lastIndexOf(".gz"));
    String outputFilePath = outputDirPath + File.separator + outputFileName;
    new File(outputDirPath).mkdirs();
    InputStream in = null;
    try {
        String filePermissions = getFilePermissions(gzipFilePath);
        in = new GZIPInputStream(new FileInputStream(gzipFilePath));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outputFilePath));
        IoUtils.copyStream(in, out);
        if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
            // check if the OS is UNIX
            // set file permissions, after it is created
            this.setFilePermissions(outputFilePath, filePermissions);
        }
    } catch (Exception e) {
        String errorMsg = "Unable to gunzip " + gzipFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
        throw new FileSystemOperationException(errorMsg, e);
    } finally {
        IoUtils.closeStream(in, "Could not close stream for file '" + gzipFilePath + "'");
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) 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 55 with FileSystemOperationException

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

the class LocalFileSystemOperations method openFileTransferSocket.

/**
 * Open file transfer socket
 *
 * @return the port where the socket is listening
 * @throws FileSystemOperationException
 */
public int openFileTransferSocket() {
    int freePort = -1;
    try {
        final ServerSocket server;
        if (copyFileStartPort == null && copyFileEndPort == null) {
            server = new ServerSocket(0);
        } else {
            server = getServerSocket();
        }
        freePort = server.getLocalPort();
        log.debug("Starting file 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;
                FileOutputStream fos = null;
                DataInputStream dis = null;
                // name of current file/dir for transfer
                String fileName = null;
                try {
                    server.setReuseAddress(true);
                    server.setSoTimeout(FILE_TRANSFER_TIMEOUT);
                    socket = server.accept();
                    dis = new DataInputStream(socket.getInputStream());
                    int fdTypeLength = dis.readInt();
                    for (; ; ) {
                        checkParamLengthForSocketTransfer(fdTypeLength, "file type length");
                        // directory or file
                        String fdType = readString(dis, fdTypeLength);
                        int fileNameLength = dis.readInt();
                        checkParamLengthForSocketTransfer(fileNameLength, "file name length");
                        fileName = readString(dis, fileNameLength);
                        fileName = IoUtils.normalizeFilePath(fileName, // switch file separators according to the current OS
                        osType);
                        File file = new File(fileName);
                        if (fdType.equals(FILE_COPY_SOCKET_COMMAND)) {
                            long fileSize = dis.readLong();
                            log.debug("Creating file: " + fileName + " with size: " + fileSize + " bytes");
                            // if not, try to create all missing parent directories
                            if (!file.getParentFile().exists()) {
                                // the file's parent directory does not exist
                                if (!file.getParentFile().mkdirs()) {
                                    throw new IOException("Could not create parent directories of file '" + file + "'");
                                }
                            }
                            try {
                                fos = new FileOutputStream(file, false);
                            } catch (IOException e) {
                                throw new IOException("Could not create destination file '" + file + "'", e);
                            }
                            byte[] buff = new byte[FILE_TRANSFER_BUFFER_SIZE];
                            int readBytes = -1;
                            while (fileSize > 0 && (readBytes = dis.read(buff, 0, (int) Math.min(buff.length, fileSize))) > -1) {
                                fos.write(buff, 0, readBytes);
                                fos.flush();
                                fileSize -= readBytes;
                            }
                            IoUtils.closeStream(fos);
                        } else if (fdType.equals(DIR_CREATE_SOCKET_COMMAND)) {
                            if (!file.exists()) {
                                log.debug("Creating directory: " + fileName);
                                if (!file.mkdirs()) {
                                    throw new RuntimeException("Could not create full path for " + fileName);
                                }
                            }
                        } else {
                            log.error("Unknown socket command (must be the file descriptor type): " + fdType);
                            return;
                        }
                        // check for more files/directories
                        try {
                            fdTypeLength = dis.readInt();
                        } catch (EOFException eofe) {
                            // this is the end of the input stream
                            break;
                        }
                    }
                } 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(fos);
                    IoUtils.closeStream(dis);
                    IoUtils.closeStream(socket, "Could not close the Socket while trying to transfer file " + fileName);
                    IoUtils.closeStream(server, "Could not close the ServerSocket while trying to transfer " + "file " + fileName);
                    synchronized (transferStatus) {
                        transferStatus.finished = true;
                        transferStatus.notify();
                    }
                }
            }

            private void checkParamLengthForSocketTransfer(int cmdParamLength, String commandType) throws IOException {
                if (cmdParamLength > INTERNAL_SOCKET_PARAMETER_MAX_LENGTH) {
                    throw new IOException("Illegal length for command " + commandType + ": " + cmdParamLength + "(max allowed is " + INTERNAL_SOCKET_PARAMETER_MAX_LENGTH + "); Probably non ATS agent has connected. Closing communication");
                }
            }

            /**
             * @param dis data input stream
             * @param length the length of bytes to be read
             * @return the String representation of the read bytes
             * @throws IOException
             */
            private String readString(DataInputStream dis, int length) throws IOException {
                byte[] buff = new byte[length];
                // this method blocks until the specified bytes are read from the stream
                dis.readFully(buff, 0, length);
                return new String(buff, DEFAULT_CHARSET);
            }
        });
        thread.setName("ATSFileTransferSocket-port" + freePort + "__" + thread.getName());
        thread.start();
    } catch (Exception e) {
        throw new FileSystemOperationException("Unable to open file transfer socket", e);
    }
    return freePort;
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) 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) FileOutputStream(java.io.FileOutputStream) EOFException(java.io.EOFException) 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