use of com.helger.commons.io.file.FileIOError in project as2-lib by phax.
the class AbstractDirectoryPollingModule method processFile.
protected void processFile(@Nonnull final File aFile) throws AS2Exception {
LOGGER.info("processing " + aFile.getAbsolutePath());
final IMessage aMsg = createMessage();
aMsg.attrs().putIn(CFileAttribute.MA_FILEPATH, aFile.getAbsolutePath());
aMsg.attrs().putIn(CFileAttribute.MA_FILENAME, aFile.getName());
/*
* asynch mdn logic 2007-03-12 save the file name into message object, it
* will be stored into pending information file
*/
aMsg.attrs().putIn(CFileAttribute.MA_PENDING_FILENAME, aFile.getName());
if (LOGGER.isDebugEnabled())
LOGGER.debug("AS2Message was created");
final CompositeParameters aParams = new CompositeParameters(false).add("date", new DateParameters()).add("msg", new MessageParameters(aMsg));
try {
updateMessage(aMsg, aFile);
LOGGER.info("file assigned to message " + aFile.getAbsolutePath() + aMsg.getLoggingText());
if (aMsg.getData() == null)
throw new AS2InvalidMessageException("No Data");
// Transmit the message - requires a module installed that implements the
// "send" action (like com.helger.as2lib.processor.sender.AS2SenderModule)
getSession().getMessageProcessor().handle(IProcessorSenderModule.DO_SEND, aMsg, null);
if (LOGGER.isDebugEnabled())
LOGGER.debug("AS2Message was successfully handled my the MessageProcessor");
/*
* asynch mdn logic 2007-03-12 If the return status is pending in msg's
* attribute "status" then copy the transmitted file to pending folder and
* wait for the receiver to make another HTTP call to post AsyncMDN
*/
if (CFileAttribute.MA_STATUS_PENDING.equals(aMsg.attrs().getAsString(CFileAttribute.MA_STATUS))) {
// Copy the file to the pending folder
final String sFolderName = AS2IOHelper.getSafeFileAndFolderName(aMsg.partnership().getAttribute(CFileAttribute.MA_STATUS_PENDING));
final String sFilename = FilenameHelper.getAsSecureValidASCIIFilename(aMsg.attrs().getAsString(CFileAttribute.MA_PENDING_FILENAME));
final File aPendingFile = new File(sFolderName, sFilename);
final FileIOError aIOErr = AS2IOHelper.getFileOperationManager().copyFile(aFile, aPendingFile);
if (aIOErr.isFailure())
throw new AS2Exception("File was successfully sent but not copied to pending folder: " + aPendingFile + " - " + aIOErr.toString());
LOGGER.info("Copied '" + aFile.getAbsolutePath() + "' to pending folder '" + aPendingFile.getAbsolutePath() + "'" + aMsg.getLoggingText());
}
if (attrs().containsKey(ATTR_SENT_DIRECTORY)) {
// If the Sent Directory option is set, move the transmitted file to
// the sent directory
File aSentFile = null;
try {
final String sSentDirectory = AS2IOHelper.getSafeFileAndFolderName(aParams.format(attrs().getAsString(ATTR_SENT_DIRECTORY)));
// Default to the original filename
final String sSentFilename = StringHelper.getNotEmpty(FilenameHelper.getAsSecureValidASCIIFilename(aParams.format(attrs().getAsString(ATTR_STORED_SENT_FILENAME))), aFile.getName());
aSentFile = new File(AS2IOHelper.getDirectoryFile(sSentDirectory), sSentFilename);
aSentFile = AS2IOHelper.moveFile(aFile, aSentFile, false, true);
if (LOGGER.isInfoEnabled())
LOGGER.info("Moved '" + aFile.getAbsolutePath() + "' to '" + aSentFile.getAbsolutePath() + "'" + aMsg.getLoggingText());
} catch (final IOException ex) {
new AS2Exception("File was successfully sent but not moved to sent folder: '" + aSentFile.getAbsolutePath() + "'", ex).terminate();
}
} else {
// The "Sent Directory" option was not set - so delete the file
if (LOGGER.isDebugEnabled())
LOGGER.debug("Trying to delete file " + aFile.getAbsolutePath());
if (AS2IOHelper.getFileOperationManager().deleteFileIfExisting(aFile).isFailure()) {
// Delete the file if a sent directory isn't set
throw new AS2Exception("File was successfully sent but not deleted: '" + aFile.getAbsolutePath() + "'");
}
if (LOGGER.isInfoEnabled())
LOGGER.info("Deleted file '" + aFile.getAbsolutePath() + "'" + aMsg.getLoggingText());
}
} catch (final AS2Exception ex) {
ex.terminate(aFile, aMsg);
final String sErrorDirectory = AS2IOHelper.getSafeFileAndFolderName(aParams.format(getAttributeAsStringRequired(ATTR_ERROR_DIRECTORY)));
// Use the source name as the default
final String sErrorFilename = StringHelper.getNotEmpty(FilenameHelper.getAsSecureValidASCIIFilename(aParams.format(attrs().getAsString(ATTR_STORED_ERROR_FILENAME))), aFile.getName());
AS2IOHelper.handleError(aFile, sErrorDirectory, sErrorFilename);
}
}
use of com.helger.commons.io.file.FileIOError in project as2-lib by phax.
the class AS2IOHelper method moveFile.
@Nonnull
public static File moveFile(@Nonnull final File aSrc, @Nonnull final File aDestFile, final boolean bOverwrite, final boolean bRename) throws IOException {
File aRealDestFile = aDestFile;
if (!bOverwrite && aRealDestFile.exists()) {
if (!bRename)
throw new IOException("File already exists: " + aRealDestFile);
aRealDestFile = getUniqueFile(aRealDestFile.getAbsoluteFile().getParentFile(), aRealDestFile.getName());
}
// Copy
FileIOError aIOErr = FOM.copyFile(aSrc, aRealDestFile);
if (aIOErr.isFailure())
throw new IOException("Copy failed: " + aIOErr.toString());
// Delete old
aIOErr = FOM.deleteFile(aSrc);
if (aIOErr.isFailure()) {
FOM.deleteFile(aRealDestFile);
throw new IOException("Move failed, unable to delete " + aSrc + ": " + aIOErr.toString());
}
return aRealDestFile;
}
use of com.helger.commons.io.file.FileIOError in project as2-lib by phax.
the class AS2ResourceHelper method close.
public void close() {
// close only once
if (!m_aInClose.getAndSet(true)) {
// Close all closeables before deleting files, because the closables might
// be the files to be deleted :)
final ICommonsList<Closeable> aCloseables = m_aRWLock.writeLockedGet(() -> {
final ICommonsList<Closeable> ret = m_aCloseables.getClone();
m_aCloseables.clear();
return ret;
});
if (aCloseables.isNotEmpty()) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Closing " + aCloseables.size() + " " + CAS2Info.NAME_VERSION + " stream handles");
for (final Closeable aCloseable : aCloseables) StreamHelper.close(aCloseable);
}
// Get and delete all temp files
final ICommonsList<File> aFiles = m_aRWLock.writeLockedGet(() -> {
final ICommonsList<File> ret = m_aTempFiles.getClone();
m_aTempFiles.clear();
return ret;
});
if (aFiles.isNotEmpty()) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Deleting " + aFiles.size() + " temporary " + CAS2Info.NAME_VERSION + " files");
for (final File aFile : aFiles) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Deleting temporary file '" + aFile.getAbsolutePath() + "'");
final FileIOError aError = AS2IOHelper.getFileOperationManager().deleteFileIfExisting(aFile);
if (aError.isFailure())
LOGGER.warn(" Failed to delete temporary " + CAS2Info.NAME_VERSION + " file " + aFile.getAbsolutePath() + ": " + aError.toString());
}
}
}
}
use of com.helger.commons.io.file.FileIOError in project ph-web by phax.
the class DiskFileItem method delete.
/**
* Deletes the underlying storage for a file item, including deleting any
* associated temporary disk file. Although this storage will be deleted
* automatically when the <code>FileItem</code> instance is garbage collected,
* this method can be used to ensure that this is done at an earlier time,
* thus preserving system resources.
*/
public void delete() {
m_aCachedContent = null;
final File aTempFile = getStoreLocation();
if (aTempFile != null) {
final FileIOError aIOError = FileOperations.deleteFileIfExisting(aTempFile);
if (aIOError.isFailure())
if (LOGGER.isErrorEnabled())
LOGGER.error("Failed to delete temporary file " + aTempFile + " with error " + aIOError.toString());
}
}
use of com.helger.commons.io.file.FileIOError in project ph-web by phax.
the class DiskFileItemFactory method deleteAllTemporaryFiles.
public void deleteAllTemporaryFiles() {
final ICommonsList<File> aTempFiles = m_aRWLock.writeLockedGet(() -> {
final ICommonsList<File> ret = m_aTempFiles.getClone();
m_aTempFiles.clear();
return ret;
});
for (final File aTempFile : aTempFiles) {
final FileIOError aIOError = FileOperations.deleteFileIfExisting(aTempFile);
if (aIOError.isFailure()) {
if (LOGGER.isErrorEnabled())
LOGGER.error("Failed to delete temporary file " + aTempFile + " with error " + aIOError.toString());
_addTempFile(aTempFile);
}
}
}
Aggregations