INT-3973: SFTP - Support chmod

JIRA: https://jira.spring.io/browse/INT-3973

Add `chmod` to outbound adapter and gateway (put methods).

Polishing - PR Comments

Fix Checkstyle vulnerabilities
This commit is contained in:
Gary Russell
2016-03-21 19:19:27 -04:00
committed by Artem Bilan
parent 2936e97ea3
commit ad0839da8b
22 changed files with 392 additions and 39 deletions

View File

@@ -81,9 +81,14 @@ public abstract class AbstractRemoteFileOutboundGatewayParser extends AbstractCo
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "local-filename-generator-expression",
"localFilenameGeneratorExpressionString");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "mode", "fileExistsMode");
postProcessBuilder(builder, element);
return builder;
}
protected void postProcessBuilder(BeanDefinitionBuilder builder, Element element) {
// no-op
}
protected void configureFilter(BeanDefinitionBuilder builder, Element element, ParserContext parserContext,
String filterAttribute, String patternPrefix, String propertyName) {
String filter = element.getAttribute(filterAttribute);

View File

@@ -38,7 +38,7 @@ public abstract class RemoteFileOutboundChannelAdapterParser extends AbstractOut
@Override
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
BeanDefinitionBuilder handlerBuilder = BeanDefinitionBuilder.genericBeanDefinition(FileTransferringMessageHandler.class);
BeanDefinitionBuilder handlerBuilder = BeanDefinitionBuilder.genericBeanDefinition(handlerClass());
BeanDefinition templateDefinition = FileParserUtils.parseRemoteFileTemplate(element, parserContext, true,
getTemplateClass());
@@ -48,9 +48,18 @@ public abstract class RemoteFileOutboundChannelAdapterParser extends AbstractOut
if (StringUtils.hasText(mode)) {
handlerBuilder.addConstructorArgValue(mode);
}
postProcessBuilder(handlerBuilder, element);
return handlerBuilder.getBeanDefinition();
}
protected Class<?> handlerClass() {
return FileTransferringMessageHandler.class;
}
protected void postProcessBuilder(BeanDefinitionBuilder builder, Element element) {
// no-op
}
protected abstract Class<? extends RemoteFileOperations<?>> getTemplateClass();
}

View File

@@ -230,6 +230,8 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
private volatile FileExistsMode fileExistsMode;
private volatile Integer chmod;
/**
* Construct an instance using the provided session factory and callback for
* performing operations on the session.
@@ -430,6 +432,32 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
}
}
/**
* String setter for Spring XML convenience.
* @param chmod permissions as an octal string e.g "600";
* @see #setChmod(int)
* @since 4.3
*/
public void setChmodOctal(String chmod) {
Assert.notNull(chmod, "'chmod' cannot be null");
setChmod(Integer.parseInt(chmod, 8));
}
/**
* Set the file permissions after uploading, e.g. 0600 for
* owner read/write.
* @param chmod the permissions.
* @since 4.3
*/
public void setChmod(int chmod) {
Assert.isTrue(isChmodCapable(), "chmod operations not supported");
this.chmod = chmod;
}
public boolean isChmodCapable() {
return false;
}
@Override
protected void doInit() {
Assert.state(this.command != null || this.messageSessionCallback != null,
@@ -616,9 +644,24 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
if (path == null) {
throw new MessagingException(requestMessage, "No local file found for " + requestMessage);
}
if (this.chmod != null && isChmodCapable()) {
doChmod(this.remoteFileTemplate, path, this.chmod);
}
return path;
}
/**
* Set the mode on the remote file after transfer; the default implementation does
* nothing.
* @param remoteFileTemplate the remote file template.
* @param path the path.
* @param chmod the chmod to set.
* @since 4.3
*/
protected void doChmod(RemoteFileTemplate<F> remoteFileTemplate, String path, int chmod) {
// no-op
}
private Object doMput(Message<?> requestMessage) {
File file = null;
if (requestMessage.getPayload() instanceof File) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -42,6 +42,8 @@ public class FileTransferringMessageHandler<F> extends AbstractMessageHandler {
private final FileExistsMode mode;
private Integer chmod;
public FileTransferringMessageHandler(SessionFactory<F> sessionFactory) {
Assert.notNull(sessionFactory, "sessionFactory must not be null");
this.remoteFileTemplate = new RemoteFileTemplate<F>(sessionFactory);
@@ -131,6 +133,32 @@ public class FileTransferringMessageHandler<F> extends AbstractMessageHandler {
this.remoteFileTemplate.setTemporaryFileSuffix(temporaryFileSuffix);
}
/**
* String setter for Spring XML convenience.
* @param chmod permissions as an octal string e.g "600";
* @see #setChmod(int)
* @since 4.3
*/
public void setChmodOctal(String chmod) {
Assert.notNull(chmod, "'chmod' cannot be null");
setChmod(Integer.parseInt(chmod, 8));
}
/**
* Set the file permissions after uploading, e.g. 0600 for
* owner read/write.
* @param chmod the permissions.
* @since 4.3
*/
public void setChmod(int chmod) {
Assert.isTrue(isChmodCapable(), "chmod operations not supported");
this.chmod = chmod;
}
public boolean isChmodCapable() {
return false;
}
@Override
protected void onInit() throws Exception {
this.remoteFileTemplate.setBeanFactory(this.getBeanFactory());
@@ -139,7 +167,22 @@ public class FileTransferringMessageHandler<F> extends AbstractMessageHandler {
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
this.remoteFileTemplate.send(message, this.mode);
String path = this.remoteFileTemplate.send(message, this.mode);
if (this.chmod != null && isChmodCapable()) {
doChmod(this.remoteFileTemplate, path, this.chmod);
}
}
/**
* Set the mode on the remote file after transfer; the default implementation does
* nothing.
* @param remoteFileTemplate the remote file template.
* @param path the path.
* @param chmod the chmod to set.
* @since 4.3
*/
protected void doChmod(RemoteFileTemplate<F> remoteFileTemplate, String path, int chmod) {
// no-op
}
}