use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class FileTransferReader method readTransfer.
/**
* Open file transfer socket
*
* @throws com.axway.ats.common.filesystem.FileSystemOperationException
*/
public static void readTransfer(String host, int port, final LocalFileSystemOperations.FileTransferStatus transferStatus) throws FileSystemOperationException {
try {
final Socket socket = new Socket(host, port);
if (log.isDebugEnabled()) {
log.debug("Starting file transfer reader socket to " + host + ":" + port);
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
FileOutputStream fos = null;
DataInputStream dis = null;
try {
// socket.setReuseAddress(true);
socket.setSoTimeout(LocalFileSystemOperations.FILE_TRANSFER_TIMEOUT);
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");
String fileName = readString(dis, fileNameLength);
fileName = IoUtils.normalizeFilePath(fileName, // switch file separators according to the current OS
OS_TYPE);
File file = new File(fileName);
if (fdType.equals(LocalFileSystemOperations.FILE_COPY_SOCKET_COMMAND)) {
long fileSize = dis.readLong();
if (log.isDebugEnabled()) {
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 + "'. File transfer is interrupted.");
}
}
try {
fos = new FileOutputStream(file, false);
} catch (IOException e) {
throw new IOException("Could not create destination file '" + fileName + "'", e);
}
try {
byte[] buff = new byte[LocalFileSystemOperations.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;
}
} finally {
IoUtils.closeStream(fos, "Error closing descriptor for file " + fileName);
}
} else if (fdType.equals(LocalFileSystemOperations.DIR_CREATE_SOCKET_COMMAND)) {
if (!file.exists()) {
log.debug("Creating directory: " + fileName);
if (!file.mkdirs()) {
throw new IOException("Could not create all directories for path '" + 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 " + (LocalFileSystemOperations.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 socket");
synchronized (transferStatus) {
transferStatus.finished = true;
transferStatus.notify();
}
}
}
private void checkParamLengthForSocketTransfer(int cmdParamLength, String commandType) throws IOException {
if (cmdParamLength > LocalFileSystemOperations.INTERNAL_SOCKET_PARAMETER_MAX_LENGTH) {
throw new IOException("Illegal length for command " + commandType + ": " + cmdParamLength + "(max allowed is " + LocalFileSystemOperations.INTERNAL_SOCKET_PARAMETER_MAX_LENGTH + "); Probably non ATS agent has connected. Closing communication");
}
}
/**
* Reads some bytes from stream and converts them to string
* @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, LocalFileSystemOperations.DEFAULT_CHARSET);
}
});
thread.setName("ATSFileTransferSocketReader-port" + port + "__" + thread.getName());
thread.start();
} catch (Exception e) {
throw new FileSystemOperationException("Unable to open file transfer socket to " + host + ":" + port, e);
}
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class ContentFileSnapshot method loadFileContent.
/**
* Load the file as a String
*
* @param agent agent file is located at
* @param filePath full file path
* @return the file content
*/
protected String loadFileContent(String agent, String filePath) {
String fileContent;
if (agent == null) {
// It is a local file
fileContent = new LocalFileSystemOperations().readFile(filePath, "UTF-8");
} else {
// java reflection, so do not need to introduce compile dependency
try {
Class<?> fileSystemOperationsClass = Class.forName("com.axway.ats.action.filesystem.RemoteFileSystemOperations");
Object fileSystemOperationsInstance = fileSystemOperationsClass.getConstructor(String.class).newInstance(agent);
// call the printIt method
Method readFileMethod = fileSystemOperationsClass.getDeclaredMethod("readFile", String.class, String.class);
fileContent = readFileMethod.invoke(fileSystemOperationsInstance, filePath, "UTF-8").toString();
} catch (Exception e) {
// the other option is to add a difference to the FileTrace object, instead of throwing an exception here
throw new FileSystemOperationException("Error loading '" + filePath + "' file from " + agent, e);
}
}
return fileContent;
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method sendDirectoryTo.
/**
* Send directory contents to another machine
*
* @param fromDirName the source directory name
* @param toDirName the destination directory name
* @param toHost the destination machine host address
* @param toPort the destination machine port
* @param isRecursive whether to send content recursively or not
* @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 sendDirectoryTo(String fromDirName, String toDirName, String toHost, int toPort, boolean isRecursive, boolean failOnError) {
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 && toDirName.startsWith(fromDirName)) {
throw new IllegalArgumentException("Could not copy directories. The target directory is subdirectory of the source one");
}
File fromDir = new File(fromDirName);
checkFileExistence(fromDir);
Socket socket = null;
OutputStream sos = null;
try {
socket = new Socket(toHost, toPort);
sos = socket.getOutputStream();
sendFileToSocketStream(fromDir, toDirName, sos, failOnError);
sendFilesToSocketStream(fromDir.listFiles(), fromDirName, toDirName, sos, isRecursive, failOnError);
} catch (IOException ioe) {
throw new FileSystemOperationException("Unable to send directory '" + fromDirName + "' to '" + toDirName + "' on " + toHost + ":" + toPort, ioe);
} finally {
IoUtils.closeStream(sos);
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
log.error("Could not close the socket", e);
}
}
}
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method unlockFile.
/**
* Unlock file already locked with {@link #lockFile(String) lockFile()} method
*
* @param fileName file name
*/
@Override
public void unlockFile(String fileName) {
synchronized (lockedFiles) {
FileLock fileLock = lockedFiles.get(fileName);
if (fileLock != null) {
try {
fileLock.release();
} catch (Exception e) {
throw new FileSystemOperationException("Could not unlock file '" + fileName + "'", e);
} finally {
IoUtils.closeStream(fileLock.channel());
lockedFiles.remove(fileName);
}
} else {
log.warn("File '" + fileName + "' is not locked, so we will not try to unlock it");
}
}
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method purgeContents.
/**
* Deletes any contents that the directory might have, both files and folders; the directory
* itself is not deleted
*
* @param directoryName the target directory name
* @throws FileSystemOperationException if the parameter is not a directory or does not exist
*/
private void purgeContents(String directoryName) {
File file = new File(directoryName);
boolean exists = checkFileExistence(file, false);
if (exists) {
if (file.isDirectory()) {
DirectoryStream<Path> dirStream = null;
try {
dirStream = Files.newDirectoryStream(file.toPath());
Iterator<Path> it = dirStream.iterator();
while (it.hasNext()) {
Path path = it.next();
deleteRecursively(path.toFile());
}
} catch (Exception e) {
throw new FileSystemOperationException("Could not purge contents of directory '" + directoryName + "'", e);
} finally {
IoUtils.closeStream(dirStream);
}
} else {
throw new FileSystemOperationException(directoryName + " is not a directory! ");
}
}
}
Aggregations