Add Apache MINA SftpEventListener
- republish certain events as `ApplicationEvent`s. * * Add ApacheMinaFtplet to provide the same functionality with FTP * Fix typo * * Polishing javadocs and event toString() methods
This commit is contained in:
committed by
Artem Bilan
parent
b1dfb7bfa3
commit
cd0f56bc87
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ftp.server;
|
||||
|
||||
import org.apache.ftpserver.ftplet.FtpSession;
|
||||
|
||||
import org.springframework.integration.file.remote.server.FileServerEvent;
|
||||
|
||||
/**
|
||||
* {@code ApplicationEvent} generated from Apache Mina ftp events.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public abstract class ApacheMinaFtpEvent extends FileServerEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public ApacheMinaFtpEvent(Object source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
public ApacheMinaFtpEvent(Object source, Throwable cause) {
|
||||
super(source, cause);
|
||||
}
|
||||
|
||||
public FtpSession getSession() {
|
||||
return (FtpSession) source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + " [clientAddress=" + getSession().getClientAddress() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright 2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ftp.server;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.ftpserver.ftplet.DefaultFtplet;
|
||||
import org.apache.ftpserver.ftplet.FtpException;
|
||||
import org.apache.ftpserver.ftplet.FtpRequest;
|
||||
import org.apache.ftpserver.ftplet.FtpSession;
|
||||
import org.apache.ftpserver.ftplet.FtpletResult;
|
||||
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A listener for FTP events emitted by an Apache Mina ftp server.
|
||||
* It emits selected events as Spring Framework {@code ApplicationEvent}s
|
||||
* which are subclasses of {@link ApacheMinaFtpEvent}.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class ApacheMinaFtplet extends DefaultFtplet
|
||||
implements ApplicationEventPublisherAware, BeanNameAware, InitializingBean {
|
||||
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
private String beanName;
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
protected ApplicationEventPublisher getApplicationEventPublisher() {
|
||||
return this.applicationEventPublisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanName(String name) {
|
||||
this.beanName = name;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return this.beanName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
Assert.state(this.applicationEventPublisher != null, "An ApplicationEventPublisher is required");
|
||||
}
|
||||
|
||||
@Override
|
||||
public FtpletResult onConnect(FtpSession session) throws FtpException, IOException {
|
||||
this.applicationEventPublisher.publishEvent(new SessionOpenedEvent(session));
|
||||
return super.onConnect(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException {
|
||||
this.applicationEventPublisher.publishEvent(new SessionClosedEvent(session));
|
||||
return super.onDisconnect(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FtpletResult onDeleteEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||
this.applicationEventPublisher.publishEvent(new PathRemovedEvent(session, request, false));
|
||||
return super.onDeleteEnd(session, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||
this.applicationEventPublisher.publishEvent(new FileWrittenEvent(session, request, false));
|
||||
return super.onUploadEnd(session, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FtpletResult onRmdirEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||
this.applicationEventPublisher.publishEvent(new PathRemovedEvent(session, request, true));
|
||||
return super.onRmdirEnd(session, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FtpletResult onMkdirEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||
this.applicationEventPublisher.publishEvent(new DirectoryCreatedEvent(session, request));
|
||||
return super.onMkdirEnd(session, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FtpletResult onAppendEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||
this.applicationEventPublisher.publishEvent(new FileWrittenEvent(session, request, false));
|
||||
return super.onAppendEnd(session, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FtpletResult onRenameEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||
this.applicationEventPublisher.publishEvent(new PathMovedEvent(session, request));
|
||||
return super.onRenameEnd(session, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ApacheMinaSftpEventListener [beanName=" + this.beanName + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ftp.server;
|
||||
|
||||
import org.apache.ftpserver.ftplet.FtpRequest;
|
||||
import org.apache.ftpserver.ftplet.FtpSession;
|
||||
|
||||
/**
|
||||
* An event emitted when a directory is created.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class DirectoryCreatedEvent extends FtpRequestEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public DirectoryCreatedEvent(FtpSession source, FtpRequest request) {
|
||||
super(source, request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ftp.server;
|
||||
|
||||
import org.apache.ftpserver.ftplet.FtpRequest;
|
||||
import org.apache.ftpserver.ftplet.FtpSession;
|
||||
|
||||
/**
|
||||
* An event that is emitted when a file is written.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class FileWrittenEvent extends FtpRequestEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final boolean append;
|
||||
|
||||
public FileWrittenEvent(FtpSession source, FtpRequest request, boolean append) {
|
||||
super(source, request);
|
||||
this.append = append;
|
||||
}
|
||||
|
||||
public boolean isAppend() {
|
||||
return this.append;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FileWrittenEvent [append=" + this.append
|
||||
+ ", request=" + this.request
|
||||
+ ", clientAddress=" + getSession().getClientAddress() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ftp.server;
|
||||
|
||||
import org.apache.ftpserver.ftplet.FtpRequest;
|
||||
import org.apache.ftpserver.ftplet.FtpSession;
|
||||
|
||||
/**
|
||||
* Base class for all events having an {@link FtpRequest}.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public abstract class FtpRequestEvent extends ApacheMinaFtpEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected final FtpRequest request; //NOSONAR protected final
|
||||
|
||||
public FtpRequestEvent(FtpSession source, FtpRequest request) {
|
||||
super(source);
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public FtpRequest getRequest() {
|
||||
return this.request;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + " [request=" + this.request
|
||||
+ ", clientAddress=" + getSession().getClientAddress() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ftp.server;
|
||||
|
||||
import org.apache.ftpserver.ftplet.FtpRequest;
|
||||
import org.apache.ftpserver.ftplet.FtpSession;
|
||||
|
||||
/**
|
||||
* An event emitted when a path is moved.
|
||||
* @author Gary Russell
|
||||
*
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class PathMovedEvent extends FtpRequestEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public PathMovedEvent(FtpSession source, FtpRequest request) {
|
||||
super(source, request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ftp.server;
|
||||
|
||||
import org.apache.ftpserver.ftplet.FtpRequest;
|
||||
import org.apache.ftpserver.ftplet.FtpSession;
|
||||
|
||||
/**
|
||||
* An event emitted when a file or directory is removed.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class PathRemovedEvent extends FtpRequestEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final boolean isDirectory;
|
||||
|
||||
public PathRemovedEvent(FtpSession source, FtpRequest request, boolean isDirectory) {
|
||||
super(source, request);
|
||||
this.isDirectory = isDirectory;
|
||||
}
|
||||
|
||||
public boolean isDirectory() {
|
||||
return this.isDirectory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PathRemovedEvent [isDirectory=" + this.isDirectory
|
||||
+ ", request=" + this.request
|
||||
+ ", clientAddress=" + getSession().getClientAddress() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ftp.server;
|
||||
|
||||
import org.apache.ftpserver.ftplet.FtpSession;
|
||||
|
||||
/**
|
||||
* An event emitted when a session is closed.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class SessionClosedEvent extends ApacheMinaFtpEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public SessionClosedEvent(FtpSession session) {
|
||||
super(session);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ftp.server;
|
||||
|
||||
import org.apache.ftpserver.ftplet.FtpSession;
|
||||
|
||||
/**
|
||||
* An event emitted when a session is opened.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class SessionOpenedEvent extends ApacheMinaFtpEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public SessionOpenedEvent(FtpSession session) {
|
||||
super(session);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Provides classes related to FTP servers.
|
||||
*/
|
||||
package org.springframework.integration.ftp.server;
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.springframework.integration.ftp;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
@@ -39,6 +41,7 @@ import org.junit.BeforeClass;
|
||||
import org.springframework.integration.file.remote.RemoteFileTestSupport;
|
||||
import org.springframework.integration.file.remote.session.CachingSessionFactory;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
import org.springframework.integration.ftp.server.ApacheMinaFtplet;
|
||||
import org.springframework.integration.ftp.session.DefaultFtpSessionFactory;
|
||||
|
||||
/**
|
||||
@@ -51,6 +54,8 @@ import org.springframework.integration.ftp.session.DefaultFtpSessionFactory;
|
||||
*/
|
||||
public class FtpTestSupport extends RemoteFileTestSupport {
|
||||
|
||||
private static final ApacheMinaFtplet FTPLET = new ApacheMinaFtplet();
|
||||
|
||||
private static volatile FtpServer server;
|
||||
|
||||
@BeforeClass
|
||||
@@ -61,7 +66,10 @@ public class FtpTestSupport extends RemoteFileTestSupport {
|
||||
ListenerFactory factory = new ListenerFactory();
|
||||
factory.setPort(0);
|
||||
serverFactory.addListener("default", factory.createListener());
|
||||
|
||||
serverFactory.setFtplets(new HashMap<>(Collections.singletonMap("springFtplet", FTPLET)));
|
||||
FTPLET.setApplicationEventPublisher(ev -> {
|
||||
// no-op
|
||||
});
|
||||
server = serverFactory.createServer();
|
||||
server.start();
|
||||
|
||||
@@ -94,6 +102,10 @@ public class FtpTestSupport extends RemoteFileTestSupport {
|
||||
return sf;
|
||||
}
|
||||
|
||||
protected static ApacheMinaFtplet ftplet() {
|
||||
return FTPLET;
|
||||
}
|
||||
|
||||
private static class TestUserManager implements UserManager {
|
||||
|
||||
private final BaseUser testUser;
|
||||
|
||||
@@ -28,11 +28,14 @@ import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
@@ -47,7 +50,9 @@ import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.integration.IntegrationMessageHeaderAccessor;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
@@ -58,10 +63,18 @@ import org.springframework.integration.file.remote.InputStreamCallback;
|
||||
import org.springframework.integration.file.remote.MessageSessionCallback;
|
||||
import org.springframework.integration.file.remote.RemoteFileTemplate;
|
||||
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway.Option;
|
||||
import org.springframework.integration.file.remote.session.CachingSessionFactory;
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
import org.springframework.integration.ftp.FtpTestSupport;
|
||||
import org.springframework.integration.ftp.gateway.FtpOutboundGateway;
|
||||
import org.springframework.integration.ftp.server.ApacheMinaFtpEvent;
|
||||
import org.springframework.integration.ftp.server.DirectoryCreatedEvent;
|
||||
import org.springframework.integration.ftp.server.FileWrittenEvent;
|
||||
import org.springframework.integration.ftp.server.PathMovedEvent;
|
||||
import org.springframework.integration.ftp.server.PathRemovedEvent;
|
||||
import org.springframework.integration.ftp.server.SessionClosedEvent;
|
||||
import org.springframework.integration.ftp.server.SessionOpenedEvent;
|
||||
import org.springframework.integration.ftp.session.FtpRemoteFileTemplate;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.support.PartialSuccessException;
|
||||
@@ -70,6 +83,7 @@ import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -142,9 +156,15 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
@Autowired
|
||||
private DirectChannel inboundNlst;
|
||||
|
||||
@Autowired
|
||||
private SessionFactory<FTPFile> sessionFactory;
|
||||
|
||||
@Autowired
|
||||
private SourcePollingChannelAdapter ftpInbound;
|
||||
|
||||
@Autowired
|
||||
private FtpRemoteFileTemplate template;
|
||||
|
||||
@Autowired
|
||||
private Config config;
|
||||
|
||||
@@ -673,6 +693,65 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
this.ftpInbound.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allEvents() throws InterruptedException {
|
||||
resetSessionCache();
|
||||
this.config.events.clear();
|
||||
this.config.latch = new CountDownLatch(1);
|
||||
this.template.execute(session -> {
|
||||
assertThat(session.mkdir("/ftpTarget/allEventsDir")).isTrue();
|
||||
session.write(new ByteArrayInputStream("foo".getBytes()), "/ftpTarget/allEventsDir/file.txt");
|
||||
session.append(new ByteArrayInputStream("bar".getBytes()), "/ftpTarget/allEventsDir/file.txt");
|
||||
session.rename("/ftpTarget/allEventsDir/file.txt", "/ftpTarget/allEventsDir/file2.txt");
|
||||
assertThat(session.remove("/ftpTarget/allEventsDir/file2.txt")).isTrue();
|
||||
session.rename("/ftpTarget/allEventsDir", "/ftpTarget/allEventsDir2");
|
||||
session.rmdir("/ftpTarget/allEventsDir2");
|
||||
return null;
|
||||
});
|
||||
resetSessionCache();
|
||||
assertThat(this.config.latch.await(10, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(this.config.events).hasSize(11);
|
||||
assertThat(this.config.events.get(0)).isInstanceOf(SessionOpenedEvent.class);
|
||||
assertThat(this.config.events.get(1)).isInstanceOf(DirectoryCreatedEvent.class);
|
||||
DirectoryCreatedEvent dce = (DirectoryCreatedEvent) this.config.events.get(1);
|
||||
assertThat(dce.getRequest().getArgument()).isEqualTo("/ftpTarget/allEventsDir");
|
||||
assertThat(this.config.events.get(2)).isInstanceOf(FileWrittenEvent.class);
|
||||
FileWrittenEvent fwe = (FileWrittenEvent) this.config.events.get(2);
|
||||
assertThat(fwe.getRequest().getArgument()).isEqualTo("/ftpTarget/allEventsDir/file.txt");
|
||||
assertThat(this.config.events.get(3)).isInstanceOf(FileWrittenEvent.class);
|
||||
fwe = (FileWrittenEvent) this.config.events.get(3);
|
||||
assertThat(fwe.getRequest().getArgument()).isEqualTo("/ftpTarget/allEventsDir/file.txt");
|
||||
assertThat(this.config.events.get(4)).isInstanceOf(PathRemovedEvent.class);
|
||||
PathRemovedEvent pre = (PathRemovedEvent) this.config.events.get(4);
|
||||
assertThat(pre.getRequest().getArgument()).isEqualTo("/ftpTarget/allEventsDir/file2.txt");
|
||||
assertThat(pre.isDirectory()).isFalse(); // implicit DELE before RNTO
|
||||
assertThat(this.config.events.get(5)).isInstanceOf(PathMovedEvent.class);
|
||||
PathMovedEvent pme = (PathMovedEvent) this.config.events.get(5);
|
||||
assertThat(pme.getRequest().getArgument()).isEqualTo("/ftpTarget/allEventsDir/file2.txt");
|
||||
assertThat(this.config.events.get(6)).isInstanceOf(PathRemovedEvent.class);
|
||||
pre = (PathRemovedEvent) this.config.events.get(6);
|
||||
assertThat(pre.getRequest().getArgument()).isEqualTo("/ftpTarget/allEventsDir/file2.txt");
|
||||
assertThat(pre.isDirectory()).isFalse();
|
||||
assertThat(this.config.events.get(7)).isInstanceOf(PathRemovedEvent.class);
|
||||
pre = (PathRemovedEvent) this.config.events.get(7);
|
||||
assertThat(pre.getRequest().getArgument()).isEqualTo("/ftpTarget/allEventsDir2");
|
||||
assertThat(pre.isDirectory()).isFalse(); // implicit DELE before RNTO
|
||||
assertThat(this.config.events.get(8)).isInstanceOf(PathMovedEvent.class);
|
||||
pme = (PathMovedEvent) this.config.events.get(8);
|
||||
assertThat(pme.getRequest().getArgument()).isEqualTo("/ftpTarget/allEventsDir2");
|
||||
assertThat(this.config.events.get(9)).isInstanceOf(PathRemovedEvent.class);
|
||||
pre = (PathRemovedEvent) this.config.events.get(9);
|
||||
assertThat(pre.getRequest().getArgument()).isEqualTo("/ftpTarget/allEventsDir2");
|
||||
assertThat(pre.isDirectory()).isTrue();
|
||||
assertThat(this.config.events.get(10)).isInstanceOf(SessionClosedEvent.class);
|
||||
this.config.events.clear();
|
||||
this.config.latch = null;
|
||||
}
|
||||
|
||||
private void resetSessionCache() {
|
||||
((CachingSessionFactory<?>) this.sessionFactory).resetCache();
|
||||
}
|
||||
|
||||
public static class SortingFileListFilter implements FileListFilter<File> {
|
||||
|
||||
@Override
|
||||
@@ -705,12 +784,18 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
public static class Config {
|
||||
|
||||
final List<ApacheMinaFtpEvent> events = new ArrayList<>();
|
||||
|
||||
private volatile String targetLocalDirectoryName;
|
||||
|
||||
private volatile CountDownLatch latch;
|
||||
|
||||
@Bean
|
||||
public SessionFactory<FTPFile> ftpSessionFactory() {
|
||||
public SessionFactory<FTPFile> ftpSessionFactory(ApplicationContext context) {
|
||||
FtpServerOutboundTests.ftplet().setApplicationEventPublisher(context);
|
||||
return FtpServerOutboundTests.sessionFactory();
|
||||
}
|
||||
|
||||
@@ -718,6 +803,23 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
return this.targetLocalDirectoryName;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FtpRemoteFileTemplate template(SessionFactory<FTPFile> sf) {
|
||||
return new FtpRemoteFileTemplate(sf);
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void handleEvent(ApacheMinaFtpEvent event) {
|
||||
if (this.latch != null) {
|
||||
if (this.events.size() > 0 || event instanceof SessionOpenedEvent) {
|
||||
this.events.add(event);
|
||||
if (event instanceof SessionClosedEvent) {
|
||||
this.latch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user