|
| 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 | +} |
0 commit comments