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);
}
}
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);
}
}
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);
}
}
}
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);
}
}
}
}
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;
}
Aggregations