Skip to content

Commit

Permalink
Make file and FTP modules as auto-config
Browse files Browse the repository at this point in the history
* Fix all the Checkstyle violations for those modules
  • Loading branch information
artembilan committed Jan 5, 2024
1 parent 296d80f commit 3f349ca
Show file tree
Hide file tree
Showing 28 changed files with 171 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 the original author or authors.
* Copyright 2020-2024 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.
Expand All @@ -23,6 +23,8 @@
import org.springframework.validation.annotation.Validated;

/**
* The configuration properties for file consumer.
*
* @author David Turanski
* @author Artem Bilan
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 the original author or authors.
* Copyright 2020-2024 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.
Expand All @@ -16,7 +16,7 @@

package org.springframework.cloud.fn.common.file;

import java.util.Collections;
import java.util.Map;

import org.springframework.integration.dsl.IntegrationFlowBuilder;
import org.springframework.integration.file.splitter.FileSplitter;
Expand All @@ -26,6 +26,8 @@
import org.springframework.util.MimeTypeUtils;

/**
* The utilities for file-based functions.
*
* @author Gary Russell
* @author Artem Bilan
* @author Christian Tzolov
Expand All @@ -45,29 +47,23 @@ private FileUtils() {
*/
public static IntegrationFlowBuilder enhanceFlowForReadingMode(IntegrationFlowBuilder flowBuilder,
FileConsumerProperties fileConsumerProperties) {

switch (fileConsumerProperties.getMode()) {
case contents:
flowBuilder.enrichHeaders(Collections.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE,
MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE))
.transform(new FileToByteArrayTransformer());
break;
case lines:
case contents -> flowBuilder
.enrichHeaders(Map.of(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE))
.transform(new FileToByteArrayTransformer());
case lines -> {
Boolean withMarkers = fileConsumerProperties.getWithMarkers();
if (withMarkers == null) {
withMarkers = false;
}
flowBuilder
.enrichHeaders(Collections.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE,
MimeTypeUtils.TEXT_PLAIN_VALUE))
flowBuilder.enrichHeaders(Map.of(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN_VALUE))
.split(new FileSplitter(true, withMarkers, fileConsumerProperties.getMarkersJson()));
break;
case ref:
flowBuilder.enrichHeaders(Collections.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE,
MimeTypeUtils.APPLICATION_JSON_VALUE));
break;
default:
throw new IllegalArgumentException(
fileConsumerProperties.getMode().name() + " is not a supported file reading mode.");
}
case ref ->
flowBuilder.enrichHeaders(Map.of(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON_VALUE));
default -> throw new IllegalArgumentException(
fileConsumerProperties.getMode().name() + " is not a supported file reading mode.");
}
return flowBuilder;
}
Expand All @@ -81,26 +77,21 @@ public static IntegrationFlowBuilder enhanceFlowForReadingMode(IntegrationFlowBu
*/
public static IntegrationFlowBuilder enhanceStreamFlowForReadingMode(IntegrationFlowBuilder flowBuilder,
FileConsumerProperties fileConsumerProperties) {

switch (fileConsumerProperties.getMode()) {
case contents:
flowBuilder.enrichHeaders(Collections.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE,
MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE))
.transform(new StreamTransformer());
break;
case lines:
case contents -> flowBuilder
.enrichHeaders(Map.of(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE))
.transform(new StreamTransformer());
case lines -> {
Boolean withMarkers = fileConsumerProperties.getWithMarkers();
if (withMarkers == null) {
withMarkers = false;
}
flowBuilder
.enrichHeaders(Collections.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE,
MimeTypeUtils.TEXT_PLAIN_VALUE))
flowBuilder.enrichHeaders(Map.of(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN_VALUE))
.split(new FileSplitter(true, withMarkers, fileConsumerProperties.getMarkersJson()));
break;
case ref:
default:
throw new IllegalArgumentException(fileConsumerProperties.getMode().name()
+ " is not a supported file reading mode when streaming.");
}
default -> throw new IllegalArgumentException(
fileConsumerProperties.getMode().name() + " is not a supported file reading mode when streaming.");
}
return flowBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* The file-based functions supporting classes.
*/
package org.springframework.cloud.fn.common.file;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* The remote file protocols supporting classes.
*/
package org.springframework.cloud.fn.common.file.remote;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2020 the original author or authors.
* Copyright 2015-2024 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.
Expand All @@ -18,15 +18,15 @@

import org.apache.commons.net.ftp.FTPFile;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.ftp.session.DefaultFtpSessionFactory;

@Configuration
@AutoConfiguration
@EnableConfigurationProperties(FtpSessionFactoryProperties.class)
public class FtpSessionFactoryConfiguration {

Expand All @@ -40,8 +40,7 @@ public SessionFactory<FTPFile> ftpSessionFactory(FtpSessionFactoryProperties pro
ftpSessionFactory.setPassword(properties.getPassword());
ftpSessionFactory.setClientMode(properties.getClientMode().getMode());
if (properties.getCacheSessions() != null) {
CachingSessionFactory<FTPFile> csf = new CachingSessionFactory<>(ftpSessionFactory);
return csf;
return new CachingSessionFactory<>(ftpSessionFactory);
}
else {
return ftpSessionFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2020 the original author or authors.
* Copyright 2015-2024 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.
Expand Down Expand Up @@ -95,7 +95,7 @@ public enum ClientMode {
}

public int getMode() {
return mode;
return this.mode;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* The FTP session factory auto-configuration.
*/
package org.springframework.cloud.fn.common.ftp;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.cloud.fn.common.ftp.FtpSessionFactoryConfiguration
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2024 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.
Expand All @@ -18,29 +18,28 @@

import java.util.function.Consumer;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.fn.common.config.ComponentCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.Expression;
import org.springframework.integration.file.DefaultFileNameGenerator;
import org.springframework.integration.file.FileNameGenerator;
import org.springframework.integration.file.FileWritingMessageHandler;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;

/**
* The auto-configuration for file consumer.
*
* @author Mark Fisher
* @author Artem Bilan
* @author Soby Chacko
*/
@Configuration(proxyBeanMethods = false)
@AutoConfiguration
@EnableConfigurationProperties(FileConsumerProperties.class)
public class FileConsumerConfiguration {

private static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser();

private final FileConsumerProperties properties;

public FileConsumerConfiguration(FileConsumerProperties properties) {
Expand All @@ -56,15 +55,15 @@ public Consumer<Message<?>> fileConsumer(FileWritingMessageHandler fileWritingMe
public FileWritingMessageHandler fileWritingMessageHandler(FileNameGenerator fileNameGenerator,
@Nullable ComponentCustomizer<FileWritingMessageHandler> fileWritingMessageHandlerCustomizer) {

FileWritingMessageHandler handler = this.properties.getDirectoryExpression() != null
? new FileWritingMessageHandler(
EXPRESSION_PARSER.parseExpression(this.properties.getDirectoryExpression()))
Expression directoryExpression = this.properties.getDirectoryExpression();
FileWritingMessageHandler handler = (directoryExpression != null)
? new FileWritingMessageHandler(directoryExpression)
: new FileWritingMessageHandler(this.properties.getDirectory());
handler.setAutoCreateDirectory(true);
handler.setAppendNewLine(!properties.isBinary());
handler.setCharset(properties.getCharset());
handler.setAppendNewLine(!this.properties.isBinary());
handler.setCharset(this.properties.getCharset());
handler.setExpectReply(false);
handler.setFileExistsMode(properties.getMode());
handler.setFileExistsMode(this.properties.getMode());
handler.setFileNameGenerator(fileNameGenerator);

if (fileWritingMessageHandlerCustomizer != null) {
Expand All @@ -76,7 +75,7 @@ public FileWritingMessageHandler fileWritingMessageHandler(FileNameGenerator fil
@Bean
public FileNameGenerator fileNameGenerator() {
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
fileNameGenerator.setExpression(properties.getNameExpression());
fileNameGenerator.setExpression(this.properties.getNameExpression());
return fileNameGenerator;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2020 the original author or authors.
* Copyright 2015-2024 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.
Expand All @@ -21,12 +21,13 @@
import jakarta.validation.constraints.AssertTrue;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.expression.Expression;
import org.springframework.integration.file.support.FileExistsMode;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

/**
* Properties for the file sink.
* Properties for the file consumer.
*
* @author Mark Fisher
* @author Gary Russell
Expand All @@ -40,7 +41,8 @@ public class FileConsumerProperties {
private static final String DEFAULT_NAME = "file-consumer";

/**
* A flag to indicate whether adding a newline after the write should be suppressed.
* A flag to indicate whether adding a newline after the write operation should be
* suppressed.
*/
private boolean binary = false;

Expand All @@ -57,7 +59,7 @@ public class FileConsumerProperties {
/**
* The expression to evaluate for the parent directory of the target file.
*/
private String directoryExpression;
private Expression directoryExpression;

/**
* The FileExistsMode to use if the target file already exists.
Expand All @@ -80,55 +82,56 @@ public class FileConsumerProperties {
private String suffix = "";

public boolean isBinary() {
return binary;
return this.binary;
}

public void setBinary(boolean binary) {
this.binary = binary;
}

public String getCharset() {
return charset;
return this.charset;
}

public void setCharset(String charset) {
this.charset = charset;
}

public File getDirectory() {
return directory;
return this.directory;
}

public void setDirectory(File directory) {
this.directory = directory;
}

public String getDirectoryExpression() {
return directoryExpression;
public Expression getDirectoryExpression() {
return this.directoryExpression;
}

public void setDirectoryExpression(String directoryExpression) {
public void setDirectoryExpression(Expression directoryExpression) {
this.directoryExpression = directoryExpression;
}

public FileExistsMode getMode() {
return mode;
return this.mode;
}

public void setMode(FileExistsMode mode) {
this.mode = mode;
}

public String getName() {
return name;
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getNameExpression() {
return (nameExpression != null) ? nameExpression + " + '" + getSuffix() + "'" : "'" + name + getSuffix() + "'";
return (this.nameExpression != null) ? this.nameExpression + " + '" + getSuffix() + "'"
: "'" + this.name + getSuffix() + "'";
}

public void setNameExpression(String nameExpression) {
Expand All @@ -137,8 +140,8 @@ public void setNameExpression(String nameExpression) {

public String getSuffix() {
String suffixWithDotIfNecessary = "";
if (StringUtils.hasText(suffix)) {
suffixWithDotIfNecessary = suffix.startsWith(".") ? suffix : "." + suffix;
if (StringUtils.hasText(this.suffix)) {
suffixWithDotIfNecessary = this.suffix.startsWith(".") ? this.suffix : "." + this.suffix;
}
return suffixWithDotIfNecessary;
}
Expand All @@ -149,12 +152,12 @@ public void setSuffix(String suffix) {

@AssertTrue(message = "Exactly one of 'name' or 'nameExpression' must be set")
public boolean isMutuallyExclusiveNameAndNameExpression() {
return DEFAULT_NAME.equals(name) || nameExpression == null;
return DEFAULT_NAME.equals(this.name) || this.nameExpression == null;
}

@AssertTrue(message = "Exactly one of 'directory' or 'directoryExpression' must be set")
public boolean isMutuallyExclusiveDirectoryAndDirectoryExpression() {
return new File(DEFAULT_DIR).equals(directory) || directoryExpression == null;
return new File(DEFAULT_DIR).equals(this.directory) || this.directoryExpression == null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* The file consumer auto-configuration.
*/
package org.springframework.cloud.fn.consumer.file;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.cloud.fn.consumer.file.FileConsumerConfiguration
Loading

0 comments on commit 3f349ca

Please sign in to comment.