forked from apache/camel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FtpEmbeddedService.java
234 lines (193 loc) · 8.52 KB
/
FtpEmbeddedService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.apache.camel.test.infra.ftp.services.embedded;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import org.apache.camel.test.infra.common.services.AbstractTestService;
import org.apache.camel.test.infra.ftp.common.FtpProperties;
import org.apache.camel.test.infra.ftp.services.FtpService;
import org.apache.commons.io.FileUtils;
import org.apache.ftpserver.ConnectionConfigFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.impl.DefaultFtpServer;
import org.apache.ftpserver.impl.FtpIoSession;
import org.apache.ftpserver.listener.Listener;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FtpEmbeddedService extends AbstractTestService implements FtpService {
protected static final String DEFAULT_LISTENER = "default";
private static final Logger LOG = LoggerFactory.getLogger(FtpEmbeddedService.class);
protected FtpServer ftpServer;
protected int port;
protected Path rootDir;
private final EmbeddedConfigurationBuilder embeddedConfigurationTemplate;
private ExtensionContext context;
public FtpEmbeddedService() {
this(EmbeddedConfigurationBuilder.defaultConfigurationTemplate());
}
protected FtpEmbeddedService(EmbeddedConfigurationBuilder embeddedConfigurationTemplate) {
this.embeddedConfigurationTemplate = embeddedConfigurationTemplate;
}
public void setUp() throws Exception {
embeddedConfigurationTemplate.withTestDirectory(context.getDisplayName());
EmbeddedConfiguration embeddedConfiguration = embeddedConfigurationTemplate.build();
rootDir = testDirectory().resolve(embeddedConfiguration.getTestDirectory());
FileUtils.deleteDirectory(rootDir.toFile());
FtpServerFactory factory = createFtpServerFactory(embeddedConfiguration);
ftpServer = factory.createServer();
ftpServer.start();
port = ((DefaultFtpServer) ftpServer).getListeners().values().stream()
.map(Listener::getPort).findAny().get();
}
@Deprecated
private Path testDirectory() {
return Paths.get("target", "ftp", context.getRequiredTestClass().getSimpleName());
}
protected void createUser(UserManager userMgr, String name, String password, Path home, boolean writePermission) {
try {
BaseUser user = new BaseUser();
user.setName(name);
user.setPassword(password);
user.setHomeDirectory(home.toString());
if (writePermission) {
user.setAuthorities(Collections.singletonList(new WritePermission()));
}
userMgr.save(user);
} catch (FtpException e) {
throw new IllegalStateException("Unable to create FTP user", e);
}
}
protected FtpServerFactory createFtpServerFactory(EmbeddedConfiguration embeddedConfiguration) {
NativeFileSystemFactory fsf = new NativeFileSystemFactory();
fsf.setCreateHome(true);
PropertiesUserManagerFactory pumf = new PropertiesUserManagerFactory();
pumf.setAdminName(embeddedConfiguration.getAdmin().getUsername());
pumf.setPasswordEncryptor(new ClearTextPasswordEncryptor());
pumf.setFile(null);
UserManager userMgr = pumf.createUserManager();
for (EmbeddedConfiguration.User user : embeddedConfiguration.getUsers()) {
final EmbeddedConfiguration.User.UserInfo userInfo = user.getUserInfo();
final Path homeDir = userInfo.getHome() == null ? rootDir : Path.of(userInfo.getHome());
createUser(userMgr, user.getUsername(), user.getPassword(), homeDir, userInfo.isWritePermission());
}
FtpServerFactory serverFactory = new FtpServerFactory();
serverFactory.setUserManager(userMgr);
serverFactory.setFileSystem(fsf);
serverFactory.setConnectionConfig(new ConnectionConfigFactory().createConnectionConfig());
ListenerFactory factory = new ListenerFactory();
factory.setPort(port);
factory.setServerAddress(embeddedConfiguration.getServerAddress());
final Listener listener = factory.createListener();
serverFactory.addListener(DEFAULT_LISTENER, listener);
return serverFactory;
}
public void tearDown() {
try {
if (ftpServer != null) {
ftpServer.stop();
}
} catch (Exception e) {
// ignore while shutting down as we could be polling during
// shutdown
// and get errors when the ftp server is stopping. This is only
// an issue
// since we host the ftp server embedded in the same jvm for
// unit testing
LOG.trace("Exception while shutting down: {}", e.getMessage(), e);
} finally {
ftpServer = null;
}
}
public void disconnectAllSessions() {
// stop all listeners
Map<String, Listener> listeners = ((DefaultFtpServer) ftpServer).getListeners();
for (Listener listener : listeners.values()) {
Set<FtpIoSession> sessions = listener.getActiveSessions();
for (FtpIoSession session : sessions) {
session.closeNow();
}
}
}
@Override
protected void registerProperties(BiConsumer<String, String> store) {
final String host = ((DefaultFtpServer) ftpServer).getListeners().values().stream()
.map(Listener::getServerAddress).findAny().get();
store.accept(FtpProperties.SERVER_HOST, host);
store.accept(FtpProperties.SERVER_PORT, String.valueOf(getPort()));
store.accept(FtpProperties.ROOT_DIR, rootDir.toString());
}
@Override
public void registerProperties() {
ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.GLOBAL);
registerProperties(store::put);
}
public Path getFtpRootDir() {
return rootDir;
}
public void suspend() {
ftpServer.suspend();
}
public void resume() {
ftpServer.resume();
port = ((DefaultFtpServer) ftpServer).getListeners().values().stream()
.map(Listener::getPort).findAny().get();
}
public int getPort() {
return port;
}
public int countConnections() {
int count = 0;
// stop all listeners
Map<String, Listener> listeners = ((DefaultFtpServer) ftpServer).getListeners();
for (Listener listener : listeners.values()) {
count += listener.getActiveSessions().size();
}
return count;
}
@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
this.context = extensionContext;
}
@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
this.context = null;
}
@Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
shutdown();
this.context = null;
}
@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
this.context = extensionContext;
initialize();
}
}