diff --git a/pom.xml b/pom.xml index b2d0fba0..9960b04f 100644 --- a/pom.xml +++ b/pom.xml @@ -45,7 +45,7 @@ 0.1.0 3.14.0 2.1.1 - 2020.0.3 + 2020.0.4 1.0.0.M5 2.5.1 2.5.1 @@ -80,7 +80,7 @@ org.springframework.boot spring-boot-starter-parent - 2.4.6 + 2.5.5 diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java index 0d1977c3..9032a621 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java @@ -20,7 +20,6 @@ import com.jmsoftware.maf.springcloudstarter.redis.RedisConfiguration; import com.jmsoftware.maf.springcloudstarter.service.CommonService; import com.jmsoftware.maf.springcloudstarter.service.impl.CommonServiceImpl; -import com.jmsoftware.maf.springcloudstarter.sftp.SftpConfiguration; import com.jmsoftware.maf.springcloudstarter.websocket.WebSocketConfiguration; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.exceptions.PersistenceException; @@ -61,7 +60,6 @@ MyBatisPlusConfiguration.class, RedisConfiguration.class, Swagger2Configuration.class, - SftpConfiguration.class, WebSecurityConfiguration.class, RestTemplateConfiguration.class, AsyncConfiguration.class, diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpClientConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpClientConfiguration.java deleted file mode 100644 index 15150203..00000000 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpClientConfiguration.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.jmsoftware.maf.springcloudstarter.sftp; - -import lombok.Data; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.core.io.Resource; -import org.springframework.validation.annotation.Validated; - -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; - -/** - *

SftpClientConfiguration

- *

SFTP client configuration

- * - * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com - * @date 2019-07-04 18:18 - **/ -@Data -@Validated -@ConfigurationProperties(prefix = SftpClientConfiguration.PREFIX) -public class SftpClientConfiguration { - /** - * The constant PREFIX. - */ - public static final String PREFIX = "sftp"; - /** - * The Enabled. - */ - @NotNull - private Boolean enabled = Boolean.FALSE; - /** - * SFTP server IP - */ - @NotBlank - private String host; - /** - * SFTP server port - */ - @NotNull - private Integer port; - /** - * Login user - */ - @NotBlank - private String user; - /** - * Login password - */ - @NotBlank - private String password; - /** - * Remote directory - */ - @NotBlank - private String directory; - /** - * Private key - */ - private Resource privateKey; - /** - * Private key pass phrase - */ - private String privateKeyPassPhrase; - /** - * The maximum cache size of session. Default: 10 - */ - private Integer sessionCacheSize = 10; - /** - * The session wait timeout (time unit: MILLISECONDS). Default: 10 * 1000L (10 seconds) - */ - private Long sessionWaitTimeout = 10 * 1000L; -} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpConfiguration.java deleted file mode 100644 index cd283ecb..00000000 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpConfiguration.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.jmsoftware.maf.springcloudstarter.sftp; - -import com.jcraft.jsch.ChannelSftp; -import lombok.extern.slf4j.Slf4j; -import lombok.val; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Import; -import org.springframework.expression.common.LiteralExpression; -import org.springframework.integration.annotation.ServiceActivator; -import org.springframework.integration.file.remote.session.CachingSessionFactory; -import org.springframework.integration.file.remote.session.SessionFactory; -import org.springframework.integration.sftp.outbound.SftpMessageHandler; -import org.springframework.integration.sftp.session.DefaultSftpSessionFactory; -import org.springframework.integration.sftp.session.SftpRemoteFileTemplate; -import org.springframework.messaging.MessageHandler; - -import java.io.File; - -/** - * Description: SftpConfiguration, change description here. - * - * @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 1/29/2021 2:09 PM - **/ -@Slf4j -@Import({ - SftpClientConfiguration.class -}) -@ConditionalOnProperty({"sftp.enabled"}) -public class SftpConfiguration { - @Bean - public SessionFactory sftpSessionFactory(SftpClientConfiguration sftpClientConfiguration) { - val factory = new DefaultSftpSessionFactory(true); - factory.setHost(sftpClientConfiguration.getHost()); - factory.setPort(sftpClientConfiguration.getPort()); - factory.setUser(sftpClientConfiguration.getUser()); - if (sftpClientConfiguration.getPrivateKey() != null) { - factory.setPrivateKey(sftpClientConfiguration.getPrivateKey()); - factory.setPrivateKeyPassphrase(sftpClientConfiguration.getPrivateKeyPassPhrase()); - } else { - factory.setPassword(sftpClientConfiguration.getPassword()); - } - factory.setAllowUnknownKeys(true); - // We return a caching session factory, so that we don't have to reconnect to SFTP server for each time - val cachingSessionFactory = new CachingSessionFactory<>(factory, sftpClientConfiguration.getSessionCacheSize()); - cachingSessionFactory.setSessionWaitTimeout(sftpClientConfiguration.getSessionWaitTimeout()); - log.warn("Initial bean: '{}'", cachingSessionFactory.getClass().getSimpleName()); - return cachingSessionFactory; - } - - @Bean - @ServiceActivator(inputChannel = "toSftpChannel") - @SuppressWarnings("UnresolvedMessageChannel") - public MessageHandler messageHandler(SessionFactory sftpSessionFactory, - SftpClientConfiguration sftpClientConfiguration) { - val handler = new SftpMessageHandler(sftpSessionFactory); - handler.setRemoteDirectoryExpression(new LiteralExpression(sftpClientConfiguration.getDirectory())); - handler.setFileNameGenerator(message -> { - if (message.getPayload() instanceof File) { - return ((File) message.getPayload()).getName(); - } else { - throw new IllegalArgumentException("File expected as payload."); - } - }); - log.warn("Initial bean: '{}'", handler.getClass().getSimpleName()); - return handler; - } - - @Bean - public SftpRemoteFileTemplate sftpRemoteFileTemplate(SessionFactory sftpSessionFactory, - SftpClientConfiguration sftpClientConfiguration) { - val sftpRemoteFileTemplate = new SftpRemoteFileTemplate(sftpSessionFactory); - sftpRemoteFileTemplate.setRemoteDirectoryExpression( - new LiteralExpression(sftpClientConfiguration.getDirectory())); - sftpRemoteFileTemplate.setAutoCreateDirectory(true); - // sftpRemoteFileTemplate.setBeanFactory(beanFactory); - sftpRemoteFileTemplate.afterPropertiesSet(); - log.warn("Initial bean: '{}'", sftpRemoteFileTemplate.getClass().getSimpleName()); - return sftpRemoteFileTemplate; - } - - @Bean - public SftpSubDirectoryRunner sftpSubDirectoryRunner(SftpRemoteFileTemplate sftpRemoteFileTemplate, - SftpClientConfiguration sftpClientConfiguration) { - log.warn("Initial bean: '{}'", SftpSubDirectoryRunner.class.getSimpleName()); - return new SftpSubDirectoryRunner(sftpRemoteFileTemplate, sftpClientConfiguration); - } - - @Bean - public SftpHelper sftpHelper(SftpRemoteFileTemplate sftpRemoteFileTemplate) { - log.warn("Initial bean: '{}'", SftpHelper.class.getSimpleName()); - return new SftpHelper(sftpRemoteFileTemplate); - } -} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpHelper.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpHelper.java deleted file mode 100644 index ca52fb97..00000000 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpHelper.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.jmsoftware.maf.springcloudstarter.sftp; - -import com.jcraft.jsch.ChannelSftp; -import com.jmsoftware.maf.springcloudstarter.util.FileUtil; -import javassist.NotFoundException; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import lombok.val; -import org.springframework.integration.file.support.FileExistsMode; -import org.springframework.integration.sftp.session.SftpRemoteFileTemplate; -import org.springframework.integration.support.MessageBuilder; -import org.springframework.messaging.Message; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.multipart.MultipartFile; - -import javax.validation.Valid; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.atomic.AtomicReference; - -/** - * Description: Sftp Helper - * - * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 2/26/2021 3:47 PM - */ -@Slf4j -@Validated -@RequiredArgsConstructor -@SuppressWarnings("unused") -public class SftpHelper { - /** - * The Sftp remote file template. - */ - private final SftpRemoteFileTemplate sftpRemoteFileTemplate; - - /** - * List all files under the full path - * - * @param fullPath directory full path - * @return file names - */ - public List listFiles(@NotBlank String fullPath) { - log.info("Listing files, full path: {}", fullPath); - return this.sftpRemoteFileTemplate.execute(session -> { - var strings = new String[0]; - try { - strings = session.listNames(fullPath); - } catch (IOException e) { - log.error("Exception occurred when listing files. Exception message: {}", e.getMessage(), e); - } - return Arrays.asList(strings); - }); - } - - /** - * Check whether file exists according to file path - * - * @param fileFullPath file's full path - * @return true - file exists; false - file not exists - */ - public boolean exist(@NotBlank String fileFullPath) { - log.info("Checking whether file exists in SFTP server, file full path: {}", fileFullPath); - return this.sftpRemoteFileTemplate.execute(session -> session.exists(fileFullPath)); - } - - /** - * Get file size - * - * @param fileFullPath file's full path - * @return file size (size unit: byte). Null if the file does not exist or path refers to a directory - * @throws IllegalArgumentException when file does not exist - */ - public Long getFileSize(@NotBlank String fileFullPath) throws IllegalArgumentException { - if (!this.exist(fileFullPath)) { - throw new IllegalArgumentException( - "Cannot get file size from SFTP server. Caused by: file does not exist, full path: " + fileFullPath); - } - String[] splits = fileFullPath.split("/"); - String fileName = splits[splits.length - 1]; - String listPath = fileFullPath.substring(0, fileFullPath.lastIndexOf(fileName) - 1); - log.info("Retrieve file size from SFTP server, full path: {}", fileFullPath); - final Long[] fileSize = new Long[1]; - this.sftpRemoteFileTemplate.execute(session -> { - ChannelSftp.LsEntry[] lsEntries = session.list(listPath); - for (ChannelSftp.LsEntry lsEntry : lsEntries) { - if (lsEntry.getFilename().equals(fileName)) { - fileSize[0] = lsEntry.getAttrs().getSize(); - } - } - return null; - }); - return fileSize[0]; - } - - /** - * Upload single file - * - * @param sftpUploadFile encapsulated object - * @return file 's full path if successful, else null - */ - public String upload(@Valid SftpUploadFile sftpUploadFile) { - log.info("Uploading single file to SFTP server. SftpUploadFile: {}", sftpUploadFile); - Message message = MessageBuilder.withPayload(sftpUploadFile.getFileToBeUploaded()).build(); - return this.sftpRemoteFileTemplate.send(message, sftpUploadFile.getSubDirectory(), - sftpUploadFile.getFileExistsMode()); - } - - /** - * Upload file - * - * @param multipartFile multipart file - * @param subDirectory SFTP server's sub directory (if sub directory doesn't exist, will be auto created). Not - * empty and it looks like this: "/some/sub/directory/" - * @param fileExistsMode This enumeration indicates what action shall be taken in case the destination file - * already exists. In default, it should be set as: FileExistsMode.REPLACE - * @param deleteSource true - delete source file; false - not delete source file - * @return file full path if successful, else null - * @throws IOException IO exception - */ - public String upload(@NotNull MultipartFile multipartFile, @NotBlank String subDirectory, - @NotNull FileExistsMode fileExistsMode, boolean deleteSource) throws IOException { - log.info("Uploading single multipart file to SFTP server. File name: {}", multipartFile.getOriginalFilename()); - File file = FileUtil.convertFrom(multipartFile); - SftpUploadFile sftpUploadFile = SftpUploadFile.builder() - .fileToBeUploaded(file) - .subDirectory(subDirectory) - .fileExistsMode(fileExistsMode) - .build(); - String fileFullPath = this.upload(sftpUploadFile); - if (deleteSource) { - val deleted = file.delete(); - log.debug("File deleted: {}, {}", deleted, file); - } - return fileFullPath; - } - - /** - * Read file from SFTP server - * - * @param fileFullPath file's full path - * @return buffered file stream - * @throws NotFoundException when file does not exist - */ - public BufferedInputStream read(@NotBlank String fileFullPath) throws NotFoundException { - log.info("Read file from SFTP server, file full path: {}", fileFullPath); - val inputStream = new AtomicReference(); - val got = this.sftpRemoteFileTemplate.get(fileFullPath, inputStream::set); - if (!got) { - val errorMessage = String.format("Cannot find the file! fileFullPath: %s", fileFullPath); - log.error(errorMessage); - throw new NotFoundException(errorMessage); - } - return new BufferedInputStream(inputStream.get()); - } - - /** - * Delete file according to file path - * - * @param fileFullPath file's full path - * @return true - file deleted; false - file not deleted - */ - public boolean delete(@NotBlank String fileFullPath) { - log.warn("Deleting SFTP server's file by file full path: {}", fileFullPath); - return this.sftpRemoteFileTemplate.remove(fileFullPath); - } -} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpSubDirectory.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpSubDirectory.java deleted file mode 100644 index 86bc2ef0..00000000 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpSubDirectory.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.jmsoftware.maf.springcloudstarter.sftp; - -import lombok.Getter; - -/** - *

SftpSubDirectory

- *

Reminder: if you want to add more custom sub directories in the future, please add en enum item in this class

- * - * TODO: think of another better way to configure the directory - * - * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com - * @date 2019-07-04 23:10 - **/ -@Getter -public enum SftpSubDirectory { - /** - * Sub directory for video - */ - VIDEO("video", "/video/"), - /** - * Sub directory for avatar - */ - AVATAR("avatar", "/avatar/"); - - private final String directoryName; - private final String subDirectory; - - SftpSubDirectory(String directoryName, String subDirectory) { - this.directoryName = directoryName; - this.subDirectory = subDirectory; - } -} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpSubDirectoryRunner.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpSubDirectoryRunner.java deleted file mode 100644 index 0daa2820..00000000 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpSubDirectoryRunner.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.jmsoftware.maf.springcloudstarter.sftp; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import lombok.val; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.integration.sftp.session.SftpRemoteFileTemplate; - -/** - *

SftpSubDirectoryRunner

- *

After dependency injection finished, we must inti the SFTP server's sub-directory for out business. If you want - * to customize initialization configuration, config SftpSubDirectory.

- * - * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com - * @date 2019-07-05 08:51 - * @see SftpSubDirectory - **/ -@Slf4j -@RequiredArgsConstructor -public class SftpSubDirectoryRunner implements ApplicationRunner { - private final SftpRemoteFileTemplate sftpRemoteFileTemplate; - private final SftpClientConfiguration sftpClientConfiguration; - - @Override - public void run(ApplicationArguments args) { - this.sftpRemoteFileTemplate.setAutoCreateDirectory(true); - this.sftpRemoteFileTemplate.execute(session -> { - if (!session.exists(this.sftpClientConfiguration.getDirectory())) { - log.info("Make directories for SFTP server. Directory: {}", this.sftpClientConfiguration.getDirectory()); - session.mkdir(this.sftpClientConfiguration.getDirectory()); - } else { - log.info("SFTP server remote directory exists: {}", this.sftpClientConfiguration.getDirectory()); - } - return null; - }); - - log.info("Staring to initial SFTP server sub-directory."); - this.sftpRemoteFileTemplate.execute(session -> { - for (val sftpSubDirectory : SftpSubDirectory.values()) { - val fullPath = this.sftpClientConfiguration.getDirectory() + sftpSubDirectory.getSubDirectory(); - if (!session.exists(fullPath)) { - log.info("SFTP server sub-directory does not exist. Creating sub-directory: {}", fullPath); - session.mkdir(fullPath); - } else { - log.info("SFTP server sub-directory exists. Path: {}", fullPath); - } - } - return null; - }); - log.warn("Initialing SFTP server sub-directory is done."); - } -} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpUploadFile.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpUploadFile.java deleted file mode 100644 index 22e92125..00000000 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/sftp/SftpUploadFile.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.jmsoftware.maf.springcloudstarter.sftp; - -import lombok.Builder; -import lombok.Data; -import org.springframework.integration.file.support.FileExistsMode; - -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; -import java.io.File; - -/** - *

SftpUploadFile

- *

Change description here

- * - * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com - * @date 2019-07-06 11:22 - **/ -@Data -@Builder -public class SftpUploadFile { - /** - * File to be uploaded to SFTP server - */ - @NotNull - private File fileToBeUploaded; - /** - * SFTP server's sub directory (if sub directory does'nt exist, will be auto created). Not empty and it looks - * like this: "/some/sub/directory/" - */ - @NotBlank - private String subDirectory; - /** - * This enumeration indicates what action shall be taken in case the destination file already exists. In default, - * it should be set as: FileExistsMode.REPLACE - */ - @NotNull - private FileExistsMode fileExistsMode; -}