use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class FileOutboundChannelAdapterIntegrationTests method saveToSubWithFileExpressionNull.
@Test
public void saveToSubWithFileExpressionNull() throws Exception {
final File directory = null;
final Message<File> messageWithFileHeader = MessageBuilder.fromMessage(message).setHeader("subDirectory", directory).build();
try {
this.inputChannelSaveToSubDirWithFile.send(messageWithFileHeader);
} catch (MessageHandlingException e) {
Assert.assertEquals("The provided Destination Directory expression " + "(headers['subDirectory']) must not evaluate to null.", e.getCause().getMessage());
return;
}
Assert.fail("Was expecting a MessageHandlingException to be thrown");
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class AbstractRemoteFileOutboundGateway method get.
/**
* Copy a remote file to the configured local directory.
* @param message the message.
* @param session the session.
* @param remoteDir the remote directory.
* @param remoteFilePath the remote file path.
* @param remoteFilename the remote file name.
* @param fileInfoParam the remote file info; if null we will execute an 'ls' command
* first.
* @return The file.
* @throws IOException Any IOException.
*/
protected File get(Message<?> message, Session<F> session, String remoteDir, String remoteFilePath, String remoteFilename, F fileInfoParam) throws IOException {
F fileInfo = fileInfoParam;
if (fileInfo == null) {
F[] files = session.list(remoteFilePath);
if (files == null) {
throw new MessagingException("Session returned null when listing " + remoteFilePath);
}
if (files.length != 1 || files[0] == null || isDirectory(files[0]) || isLink(files[0])) {
throw new MessagingException(remoteFilePath + " is not a file");
}
fileInfo = files[0];
}
File localFile = new File(generateLocalDirectory(message, remoteDir), generateLocalFileName(message, remoteFilename));
FileExistsMode fileExistsMode = this.fileExistsMode;
boolean appending = FileExistsMode.APPEND.equals(fileExistsMode);
boolean exists = localFile.exists();
boolean replacing = FileExistsMode.REPLACE.equals(fileExistsMode) || (exists && FileExistsMode.REPLACE_IF_MODIFIED.equals(fileExistsMode) && localFile.lastModified() != getModified(fileInfo));
if (!exists || appending || replacing) {
OutputStream outputStream;
String tempFileName = localFile.getAbsolutePath() + this.remoteFileTemplate.getTemporaryFileSuffix();
File tempFile = new File(tempFileName);
if (appending) {
outputStream = new BufferedOutputStream(new FileOutputStream(localFile, true));
} else {
outputStream = new BufferedOutputStream(new FileOutputStream(tempFile));
}
if (replacing) {
localFile.delete();
}
try {
session.read(remoteFilePath, outputStream);
} catch (Exception e) {
/* Some operation systems acquire exclusive file-lock during file processing
and the file can't be deleted without closing streams before.
*/
outputStream.close();
tempFile.delete();
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new MessagingException("Failure occurred while copying from remote to local directory", e);
}
} finally {
try {
outputStream.close();
} catch (Exception ignored2) {
// Ignore it
}
}
if (!appending && !tempFile.renameTo(localFile)) {
throw new MessagingException("Failed to rename local file");
}
if (this.options.contains(Option.PRESERVE_TIMESTAMP) || FileExistsMode.REPLACE_IF_MODIFIED.equals(fileExistsMode)) {
localFile.setLastModified(getModified(fileInfo));
}
if (this.options.contains(Option.DELETE)) {
boolean result = session.remove(remoteFilePath);
if (!result) {
logger.error("Failed to delete: " + remoteFilePath);
} else if (logger.isDebugEnabled()) {
logger.debug(remoteFilePath + " deleted");
}
}
} else if (FileExistsMode.REPLACE_IF_MODIFIED.equals(fileExistsMode)) {
if (logger.isDebugEnabled()) {
logger.debug("Local file '" + localFile + "' has the same modified timestamp, ignored");
}
if (this.command.equals(Command.MGET)) {
localFile = null;
}
} else if (!FileExistsMode.IGNORE.equals(fileExistsMode)) {
throw new MessageHandlingException(message, "Local file " + localFile + " already exists");
} else {
if (logger.isDebugEnabled()) {
logger.debug("Existing file skipped: " + localFile);
}
if (this.command.equals(Command.MGET)) {
localFile = null;
}
}
return localFile;
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class AbstractRemoteFileOutboundGateway method doGet.
private Object doGet(final Message<?> requestMessage) {
final String remoteFilePath = this.fileNameProcessor.processMessage(requestMessage);
final String remoteFilename = getRemoteFilename(remoteFilePath);
final String remoteDir = getRemoteDirectory(remoteFilePath, remoteFilename);
Session<F> session = null;
Object payload;
if (this.options.contains(Option.STREAM)) {
session = this.remoteFileTemplate.getSessionFactory().getSession();
try {
payload = session.readRaw(remoteFilePath);
} catch (IOException e) {
throw new MessageHandlingException(requestMessage, "Failed to get the remote file [" + remoteFilePath + "] as a stream", e);
}
} else {
payload = this.remoteFileTemplate.execute(session1 -> get(requestMessage, session1, remoteDir, remoteFilePath, remoteFilename, null));
}
return getMessageBuilderFactory().withPayload(payload).setHeader(FileHeaders.REMOTE_DIRECTORY, remoteDir).setHeader(FileHeaders.REMOTE_FILE, remoteFilename).setHeader(IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE, session);
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class FileOutboundGatewayParserTests method gatewayWithFailModeLowercase.
/**
* Test is exactly the same as {@link #gatewayWithFailMode()}. However, the
* mode is provided in lower-case ensuring that the mode can be provided
* in an case-insensitive fashion.
*
* Instead a {@link MessageHandlingException} will be thrown.
*/
@Test
public void gatewayWithFailModeLowercase() throws Exception {
final MessagingTemplate messagingTemplate = new MessagingTemplate();
messagingTemplate.setDefaultDestination(this.gatewayWithFailModeLowercaseChannel);
String expectedFileContent = "Initial File Content:";
File testFile = new File("test/fileToAppend.txt");
if (testFile.exists()) {
testFile.delete();
}
messagingTemplate.sendAndReceive(new GenericMessage<String>("Initial File Content:"));
final String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile));
assertEquals(expectedFileContent, actualFileContent);
try {
messagingTemplate.sendAndReceive(new GenericMessage<String>("String content:"));
} catch (MessageHandlingException e) {
assertTrue(e.getMessage().startsWith("The destination file already exists at '"));
return;
}
fail("Was expecting a MessageHandlingException to be thrown.");
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class FileOutboundGatewayParserTests method gatewayWithFailMode.
/**
* Test uses the Fail mode of the File Outbound Gateway. When persisting
* a payload using the File Outbound Gateway and the mode is set to Fail,
* then the destination {@link File} will be created and written if it does
* not yet exist. BUT if the destination {@link File} already exists, a
* {@link MessageHandlingException} will be thrown.
*/
@Test
public void gatewayWithFailMode() throws Exception {
final MessagingTemplate messagingTemplate = new MessagingTemplate();
messagingTemplate.setDefaultDestination(this.gatewayWithFailModeChannel);
String expectedFileContent = "Initial File Content:";
File testFile = new File("test/fileToAppend.txt");
if (testFile.exists()) {
testFile.delete();
}
messagingTemplate.sendAndReceive(new GenericMessage<String>("Initial File Content:"));
final String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile));
assertEquals(expectedFileContent, actualFileContent);
try {
messagingTemplate.sendAndReceive(new GenericMessage<String>("String content:"));
} catch (MessageHandlingException e) {
assertTrue(e.getMessage().startsWith("The destination file already exists at '"));
return;
}
fail("Was expecting a MessageHandlingException to be thrown.");
}
Aggregations