INT-2390 backported INT-2350 and INT-2351 to 2.0.6

This commit is contained in:
Oleg Zhurakousky
2012-01-06 10:52:47 -05:00
parent 575a80f9c0
commit 9babf3d784
9 changed files with 130 additions and 57 deletions

View File

@@ -97,7 +97,7 @@ public class RemoteFileOutboundChannelAdapterParser extends AbstractOutboundChan
}
}
IntegrationNamespaceUtils.setValueIfAttributeDefined(handlerBuilder, element, "charset");
IntegrationNamespaceUtils.setValueIfAttributeDefined(handlerBuilder, element, "remote-file-separator");
handlerBuilder.addPropertyValue("remoteFileSeparator", element.getAttribute("remote-file-separator"));
return handlerBuilder.getBeanDefinition();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.springframework.expression.Expression;
import org.springframework.integration.Message;
@@ -74,7 +76,7 @@ public class FileTransferringMessageHandler extends AbstractMessageHandler {
}
public void setRemoteFileSeparator(String remoteFileSeparator) {
Assert.hasText(remoteFileSeparator, "'remoteFileSeparator' must not be empty");
Assert.notNull(remoteFileSeparator, "'remoteFileSeparator' must not be null");
this.remoteFileSeparator = remoteFileSeparator;
}
@@ -106,6 +108,9 @@ public class FileTransferringMessageHandler extends AbstractMessageHandler {
protected void onInit() throws Exception {
Assert.notNull(this.directoryExpressionProcessor, "remoteDirectoryExpression is required");
if (this.autoCreateDirectory){
Assert.hasText(this.remoteFileSeparator, "'remoteFileSeparator' must not be empty when 'autoCreateDirectory' is set to 'true'");
}
}
@@ -194,7 +199,13 @@ public class FileTransferringMessageHandler extends AbstractMessageHandler {
// write remote file first with .writing extension
String tempFilePath = remoteFilePath + this.temporaryFileSuffix;
if (this.autoCreateDirectory) {
session.mkdir(remoteDirectory);
try {
this.makeDirectories(remoteDirectory, session);
}
catch (IllegalStateException e) {
// Revert to old FTP behavior if recursive mkdir fails, for backwards compatibility
session.mkdir(remoteDirectory);
}
}
try {
session.write(fileInputStream, tempFilePath);
@@ -209,4 +220,35 @@ public class FileTransferringMessageHandler extends AbstractMessageHandler {
}
}
private void makeDirectories(String path, Session session) throws IOException {
if (!session.exists(path)){
int nextSeparatorIndex = path.lastIndexOf(remoteFileSeparator);
if (nextSeparatorIndex > -1){
List<String> pathsToCreate = new LinkedList<String>();
while (nextSeparatorIndex > -1){
String pathSegment = path.substring(0, nextSeparatorIndex);
if (session.exists(pathSegment)){
// no more paths to create
break;
}
else {
pathsToCreate.add(0, pathSegment);
nextSeparatorIndex = pathSegment.lastIndexOf(remoteFileSeparator);
}
}
for (String pathToCreate : pathsToCreate) {
if (logger.isDebugEnabled()){
logger.debug("Creating '" + pathToCreate + "'");
}
session.mkdir(pathToCreate);
}
}
else {
session.mkdir(path);
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -152,6 +152,10 @@ public class CachingSessionFactory implements SessionFactory, DisposableBean {
public void mkdir(String directory) throws IOException {
this.targetSession.mkdir(directory);
}
public boolean exists(String path) throws IOException {
return this.targetSession.exists(path);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,4 +46,6 @@ public interface Session {
void close();
boolean isOpen();
boolean exists(String path) throws IOException;
}