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,46 @@
|
||||
/*
|
||||
* 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.sftp.server;
|
||||
|
||||
import org.apache.sshd.server.session.ServerSession;
|
||||
|
||||
import org.springframework.integration.file.remote.server.FileServerEvent;
|
||||
|
||||
/**
|
||||
* {@code ApplicationEvent} generated from Apache Mina sftp events.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public abstract class ApacheMinaSftpEvent extends FileServerEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public ApacheMinaSftpEvent(Object source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
public ApacheMinaSftpEvent(Object source, Throwable cause) {
|
||||
super(source, cause);
|
||||
}
|
||||
|
||||
public ServerSession getSession() {
|
||||
return (ServerSession) source;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.sftp.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.CopyOption;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.sshd.server.session.ServerSession;
|
||||
import org.apache.sshd.server.subsystem.sftp.FileHandle;
|
||||
import org.apache.sshd.server.subsystem.sftp.SftpEventListener;
|
||||
|
||||
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 SFTP events emitted by an Apache Mina sshd/sftp server.
|
||||
* It emits selected events as Spring Framework {@code ApplicationEvent}s
|
||||
* which are subclasses of {@link ApacheMinaSftpEvent}.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class ApacheMinaSftpEventListener implements SftpEventListener, 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 void initialized(ServerSession session, int version) {
|
||||
this.applicationEventPublisher.publishEvent(new SessionOpenedEvent(session, version));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroying(ServerSession session) throws IOException {
|
||||
this.applicationEventPublisher.publishEvent(new SessionClosedEvent(session));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void created(ServerSession session, Path path, Map<String, ?> attrs, Throwable thrown) {
|
||||
this.applicationEventPublisher.publishEvent(new DirectoryCreatedEvent(session, path, attrs));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removed(ServerSession session, Path path, boolean isDirectory, Throwable thrown) {
|
||||
this.applicationEventPublisher.publishEvent(new PathRemovedEvent(session, path, isDirectory, thrown));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void written(ServerSession session, String remoteHandle, FileHandle localHandle, long offset, byte[] data,
|
||||
int dataOffset, int dataLen, Throwable thrown) {
|
||||
|
||||
this.applicationEventPublisher.publishEvent(new FileWrittenEvent(session, remoteHandle, localHandle.getFile(),
|
||||
dataLen, thrown));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moved(ServerSession session, Path srcPath, Path dstPath, Collection<CopyOption> opts,
|
||||
Throwable thrown) {
|
||||
|
||||
this.applicationEventPublisher.publishEvent(new PathMovedEvent(session, srcPath, dstPath, thrown));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ApacheMinaSftpEventListener [beanName=" + this.beanName + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.sftp.server;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* An event emitted when a directory is created.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class DirectoryCreatedEvent extends ApacheMinaSftpEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Path path;
|
||||
|
||||
private final Map<String, ?> attrs;
|
||||
|
||||
public DirectoryCreatedEvent(Object source, Path path, Map<String, ?> attrs) {
|
||||
super(source);
|
||||
this.path = path;
|
||||
this.attrs = attrs;
|
||||
}
|
||||
|
||||
public Path getPath() {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
public Map<String, ?> getAttrs() {
|
||||
return this.attrs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DirectoryCreatedEvent [path=" + this.path
|
||||
+ ", attrs=" + this.attrs
|
||||
+ ", clientAddress=" + getSession().getClientAddress() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.sftp.server;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* An event that is emitted when a file is written.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class FileWrittenEvent extends ApacheMinaSftpEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final String remoteHandle;
|
||||
|
||||
private final Path file;
|
||||
|
||||
private final int dataLen;
|
||||
|
||||
public FileWrittenEvent(Object source, String remoteHandle, Path file, int dataLen, @Nullable Throwable thrown) {
|
||||
super(source, thrown);
|
||||
this.remoteHandle = remoteHandle;
|
||||
this.file = file;
|
||||
this.dataLen = dataLen;
|
||||
}
|
||||
|
||||
public String getRemoteHandle() {
|
||||
return this.remoteHandle;
|
||||
}
|
||||
|
||||
public Path getFile() {
|
||||
return this.file;
|
||||
}
|
||||
|
||||
public int getDataLen() {
|
||||
return this.dataLen;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FileWrittenEvent [remoteHandle=" + this.remoteHandle
|
||||
+ ", file=" + this.file
|
||||
+ ", dataLen=" + this.dataLen
|
||||
+ (this.cause == null ? "" : ", cause=" + this.cause)
|
||||
+ ", clientAddress=" + getSession().getClientAddress() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.sftp.server;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* An event emitted when a path is moved.
|
||||
* @author Gary Russell
|
||||
*
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class PathMovedEvent extends ApacheMinaSftpEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Path srcPath;
|
||||
|
||||
private final Path dstPath;
|
||||
|
||||
public PathMovedEvent(Object source, Path srcPath, Path dstPath, @Nullable Throwable thrown) {
|
||||
super(source, thrown);
|
||||
this.srcPath = srcPath;
|
||||
this.dstPath = dstPath;
|
||||
}
|
||||
|
||||
public Path getSrcPath() {
|
||||
return this.srcPath;
|
||||
}
|
||||
|
||||
public Path getDstPath() {
|
||||
return this.dstPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PathMovedEvent [srcPath=" + this.srcPath
|
||||
+ ", dstPath=" + this.dstPath
|
||||
+ (this.cause == null ? "" : ", cause=" + this.cause)
|
||||
+ ", clientAddress=" + getSession().getClientAddress() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.sftp.server;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* An event emitted when a file or directory is removed.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class PathRemovedEvent extends ApacheMinaSftpEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Path path;
|
||||
|
||||
private final boolean isDirectory;
|
||||
|
||||
public PathRemovedEvent(Object source, Path path, boolean isDirectory, @Nullable Throwable thrown) {
|
||||
super(source, thrown);
|
||||
this.path = path;
|
||||
this.isDirectory = isDirectory;
|
||||
}
|
||||
|
||||
public Path getPath() {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
public boolean isDirectory() {
|
||||
return this.isDirectory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PathRemovedEvent [path=" + this.path
|
||||
+ ", isDirectory=" + this.isDirectory
|
||||
+ (this.cause == null ? "" : ", cause=" + this.cause)
|
||||
+ ", clientAddress=" + getSession().getClientAddress() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.sftp.server;
|
||||
|
||||
import org.apache.sshd.server.session.ServerSession;
|
||||
|
||||
/**
|
||||
* An event emitted when a session is closed.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class SessionClosedEvent extends ApacheMinaSftpEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public SessionClosedEvent(ServerSession session) {
|
||||
super(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SessionClosedEvent [clientAddress=" + getSession().getClientAddress() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.sftp.server;
|
||||
|
||||
import org.apache.sshd.server.session.ServerSession;
|
||||
|
||||
/**
|
||||
* An event emitted when a session is opened.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 5.2
|
||||
*
|
||||
*/
|
||||
public class SessionOpenedEvent extends ApacheMinaSftpEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final int clientVersion;
|
||||
|
||||
public SessionOpenedEvent(ServerSession session, int version) {
|
||||
super(session);
|
||||
this.clientVersion = version;
|
||||
}
|
||||
|
||||
public int getClientVersion() {
|
||||
return this.clientVersion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SessionOpenedEvent [clientVersion=" + this.clientVersion + ", clientAddress="
|
||||
+ getSession().getClientAddress() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Provides classes related to SFTP servers.
|
||||
*/
|
||||
package org.springframework.integration.sftp.server;
|
||||
Reference in New Issue
Block a user