Skip to content

Commit cd0f56b

Browse files
garyrussellartembilan
authored andcommitted
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
1 parent b1dfb7b commit cd0f56b

File tree

31 files changed

+1477
-15
lines changed

31 files changed

+1477
-15
lines changed

build.gradle

+5-4
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,9 @@ project('spring-integration-ftp') {
446446
compile project(":spring-integration-file")
447447
compile "commons-net:commons-net:$commonsNetVersion"
448448
compile "org.springframework:spring-context-support:$springVersion"
449-
compile("javax.activation:javax.activation-api:$javaxActivationVersion", optional)
449+
compile ("javax.activation:javax.activation-api:$javaxActivationVersion", optional)
450+
compile ("org.apache.ftpserver:ftpserver-core:$ftpServerVersion", optional)
450451

451-
testCompile "org.apache.ftpserver:ftpserver-core:$ftpServerVersion"
452452
testCompile project(":spring-integration-file").sourceSets.test.output
453453
}
454454
}
@@ -661,10 +661,11 @@ project('spring-integration-sftp') {
661661
compile project(":spring-integration-stream")
662662
compile "com.jcraft:jsch:$jschVersion"
663663
compile "org.springframework:spring-context-support:$springVersion"
664-
compile("javax.activation:javax.activation-api:$javaxActivationVersion", optional)
664+
compile ("javax.activation:javax.activation-api:$javaxActivationVersion", optional)
665+
compile ("org.apache.sshd:sshd-sftp:$apacheSshdVersion", optional)
665666

666667
testCompile "org.apache.sshd:sshd-core:$apacheSshdVersion"
667-
testCompile "org.apache.sshd:sshd-sftp:$apacheSshdVersion"
668+
testCompile project(":spring-integration-event")
668669
testCompile project(":spring-integration-file").sourceSets.test.output
669670
}
670671
}

spring-integration-core/src/main/java/org/springframework/integration/events/IntegrationEvent.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.events;
1818

1919
import org.springframework.context.ApplicationEvent;
20+
import org.springframework.lang.Nullable;
2021

2122

2223
/**
@@ -32,14 +33,13 @@
3233
@SuppressWarnings("serial")
3334
public abstract class IntegrationEvent extends ApplicationEvent {
3435

35-
private final Throwable cause;
36+
protected final Throwable cause; // NOSONAR protected final
3637

3738
public IntegrationEvent(Object source) {
38-
super(source);
39-
this.cause = null;
39+
this(source, null);
4040
}
4141

42-
public IntegrationEvent(Object source, Throwable cause) {
42+
public IntegrationEvent(Object source, @Nullable Throwable cause) {
4343
super(source);
4444
this.cause = cause;
4545
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.file.remote.server;
18+
19+
import org.springframework.integration.events.IntegrationEvent;
20+
21+
/**
22+
* Base class for file server events. Typically, the source for these events will be some
23+
* kind of client/server session object containing information such as the client's ip
24+
* address.
25+
*
26+
* @author Gary Russell
27+
* @since 5.2
28+
*
29+
*/
30+
@SuppressWarnings("serial")
31+
public abstract class FileServerEvent extends IntegrationEvent {
32+
33+
public FileServerEvent(Object source) {
34+
super(source);
35+
}
36+
37+
public FileServerEvent(Object source, Throwable cause) {
38+
super(source, cause);
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Provides classes related to file servers.
3+
*/
4+
package org.springframework.integration.file.remote.server;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.ftp.server;
18+
19+
import org.apache.ftpserver.ftplet.FtpSession;
20+
21+
import org.springframework.integration.file.remote.server.FileServerEvent;
22+
23+
/**
24+
* {@code ApplicationEvent} generated from Apache Mina ftp events.
25+
*
26+
* @author Gary Russell
27+
* @since 5.2
28+
*
29+
*/
30+
public abstract class ApacheMinaFtpEvent extends FileServerEvent {
31+
32+
private static final long serialVersionUID = 1L;
33+
34+
public ApacheMinaFtpEvent(Object source) {
35+
super(source);
36+
}
37+
38+
public ApacheMinaFtpEvent(Object source, Throwable cause) {
39+
super(source, cause);
40+
}
41+
42+
public FtpSession getSession() {
43+
return (FtpSession) source;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return getClass().getSimpleName() + " [clientAddress=" + getSession().getClientAddress() + "]";
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.ftp.server;
18+
19+
import java.io.IOException;
20+
21+
import org.apache.ftpserver.ftplet.DefaultFtplet;
22+
import org.apache.ftpserver.ftplet.FtpException;
23+
import org.apache.ftpserver.ftplet.FtpRequest;
24+
import org.apache.ftpserver.ftplet.FtpSession;
25+
import org.apache.ftpserver.ftplet.FtpletResult;
26+
27+
import org.springframework.beans.factory.BeanNameAware;
28+
import org.springframework.beans.factory.InitializingBean;
29+
import org.springframework.context.ApplicationEventPublisher;
30+
import org.springframework.context.ApplicationEventPublisherAware;
31+
import org.springframework.util.Assert;
32+
33+
/**
34+
* A listener for FTP events emitted by an Apache Mina ftp server.
35+
* It emits selected events as Spring Framework {@code ApplicationEvent}s
36+
* which are subclasses of {@link ApacheMinaFtpEvent}.
37+
*
38+
* @author Gary Russell
39+
* @since 5.2
40+
*
41+
*/
42+
public class ApacheMinaFtplet extends DefaultFtplet
43+
implements ApplicationEventPublisherAware, BeanNameAware, InitializingBean {
44+
45+
private ApplicationEventPublisher applicationEventPublisher;
46+
47+
private String beanName;
48+
49+
@Override
50+
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
51+
this.applicationEventPublisher = applicationEventPublisher;
52+
}
53+
54+
protected ApplicationEventPublisher getApplicationEventPublisher() {
55+
return this.applicationEventPublisher;
56+
}
57+
58+
@Override
59+
public void setBeanName(String name) {
60+
this.beanName = name;
61+
}
62+
63+
public String getBeanName() {
64+
return this.beanName;
65+
}
66+
67+
@Override
68+
public void afterPropertiesSet() {
69+
Assert.state(this.applicationEventPublisher != null, "An ApplicationEventPublisher is required");
70+
}
71+
72+
@Override
73+
public FtpletResult onConnect(FtpSession session) throws FtpException, IOException {
74+
this.applicationEventPublisher.publishEvent(new SessionOpenedEvent(session));
75+
return super.onConnect(session);
76+
}
77+
78+
@Override
79+
public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException {
80+
this.applicationEventPublisher.publishEvent(new SessionClosedEvent(session));
81+
return super.onDisconnect(session);
82+
}
83+
84+
@Override
85+
public FtpletResult onDeleteEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
86+
this.applicationEventPublisher.publishEvent(new PathRemovedEvent(session, request, false));
87+
return super.onDeleteEnd(session, request);
88+
}
89+
90+
@Override
91+
public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
92+
this.applicationEventPublisher.publishEvent(new FileWrittenEvent(session, request, false));
93+
return super.onUploadEnd(session, request);
94+
}
95+
96+
@Override
97+
public FtpletResult onRmdirEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
98+
this.applicationEventPublisher.publishEvent(new PathRemovedEvent(session, request, true));
99+
return super.onRmdirEnd(session, request);
100+
}
101+
102+
@Override
103+
public FtpletResult onMkdirEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
104+
this.applicationEventPublisher.publishEvent(new DirectoryCreatedEvent(session, request));
105+
return super.onMkdirEnd(session, request);
106+
}
107+
108+
@Override
109+
public FtpletResult onAppendEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
110+
this.applicationEventPublisher.publishEvent(new FileWrittenEvent(session, request, false));
111+
return super.onAppendEnd(session, request);
112+
}
113+
114+
@Override
115+
public FtpletResult onRenameEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
116+
this.applicationEventPublisher.publishEvent(new PathMovedEvent(session, request));
117+
return super.onRenameEnd(session, request);
118+
}
119+
120+
@Override
121+
public String toString() {
122+
return "ApacheMinaSftpEventListener [beanName=" + this.beanName + "]";
123+
}
124+
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.ftp.server;
18+
19+
import org.apache.ftpserver.ftplet.FtpRequest;
20+
import org.apache.ftpserver.ftplet.FtpSession;
21+
22+
/**
23+
* An event emitted when a directory is created.
24+
*
25+
* @author Gary Russell
26+
* @since 5.2
27+
*
28+
*/
29+
public class DirectoryCreatedEvent extends FtpRequestEvent {
30+
31+
private static final long serialVersionUID = 1L;
32+
33+
public DirectoryCreatedEvent(FtpSession source, FtpRequest request) {
34+
super(source, request);
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.ftp.server;
18+
19+
import org.apache.ftpserver.ftplet.FtpRequest;
20+
import org.apache.ftpserver.ftplet.FtpSession;
21+
22+
/**
23+
* An event that is emitted when a file is written.
24+
*
25+
* @author Gary Russell
26+
* @since 5.2
27+
*
28+
*/
29+
public class FileWrittenEvent extends FtpRequestEvent {
30+
31+
private static final long serialVersionUID = 1L;
32+
33+
private final boolean append;
34+
35+
public FileWrittenEvent(FtpSession source, FtpRequest request, boolean append) {
36+
super(source, request);
37+
this.append = append;
38+
}
39+
40+
public boolean isAppend() {
41+
return this.append;
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "FileWrittenEvent [append=" + this.append
47+
+ ", request=" + this.request
48+
+ ", clientAddress=" + getSession().getClientAddress() + "]";
49+
}
50+
51+
}

0 commit comments

Comments
 (0)