Skip to content

Commit 5ab035a

Browse files
committed
o Minor code refactoring
o Fixed javadoc warnings o Renamed some variables for clarity o Slight code improvement o Added a toString method to the Default%essageResource class o Bumped up a couple of dependencies
1 parent 583db0b commit 5ab035a

File tree

13 files changed

+262
-173
lines changed

13 files changed

+262
-173
lines changed

core/src/main/java/org/apache/ftpserver/command/CommandFactoryFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
8282
*/
8383
public class CommandFactoryFactory {
84-
8584
private static final HashMap<String, Command> DEFAULT_COMMAND_MAP = new HashMap<>();
8685

8786
static {
@@ -116,6 +115,7 @@ public class CommandFactoryFactory {
116115
DEFAULT_COMMAND_MAP.put("MDTM", new MDTM()); // rfc3659, 3
117116
// "MFCT, draft-somers-ftp-mfxx, 4
118117
// "MFF, draft-somers-ftp-mfxx, 5
118+
// "MIC, rfc2228, 3?
119119
DEFAULT_COMMAND_MAP.put("MFMT", new MFMT()); // draft-somers-ftp-mfxx, 3
120120
DEFAULT_COMMAND_MAP.put("MKD", new MKD()); // rfc959, 4.1.3
121121
DEFAULT_COMMAND_MAP.put("MLSD", new MLSD()); // rfc3659, 7
@@ -166,6 +166,7 @@ public class CommandFactoryFactory {
166166

167167
/**
168168
* Create an {@link CommandFactory} based on the configuration on the factory.
169+
*
169170
* @return The {@link CommandFactory}
170171
*/
171172
public CommandFactory createCommandFactory() {
@@ -210,6 +211,7 @@ public Map<String, Command> getCommandMap() {
210211

211212
/**
212213
* Add or override a command.
214+
*
213215
* @param commandName The command name, e.g. STOR
214216
* @param command The command
215217
*/

core/src/main/java/org/apache/ftpserver/command/impl/PASS.java

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
5454
*/
5555
public class PASS extends AbstractCommand {
56-
5756
private final Logger LOG = LoggerFactory.getLogger(PASS.class);
5857

5958
/**
@@ -64,46 +63,41 @@ public class PASS extends AbstractCommand {
6463
public void execute(final FtpIoSession session,
6564
final FtpServerContext context, final FtpRequest request)
6665
throws IOException, FtpException {
67-
6866
boolean success = false;
67+
ServerFtpStatistics stat = (ServerFtpStatistics) context .getFtpStatistics();
6968

70-
ServerFtpStatistics stat = (ServerFtpStatistics) context
71-
.getFtpStatistics();
7269
try {
73-
7470
// reset state variables
7571
session.resetState();
7672

7773
// argument check
7874
String password = request.getArgument();
7975

80-
8176
// check user name
8277
String userName = session.getUserArgument();
8378

84-
if (userName == null && session.getUser() == null) {
79+
if ((userName == null) && (session.getUser() == null)) {
8580
session.write(LocalizedFtpReply.translate(session, request, context,
86-
FtpReply.REPLY_503_BAD_SEQUENCE_OF_COMMANDS, "PASS",
87-
null));
81+
FtpReply.REPLY_503_BAD_SEQUENCE_OF_COMMANDS, "PASS", null));
82+
8883
return;
8984
}
9085

9186
// already logged-in
9287
if (session.isLoggedIn()) {
9388
session.write(LocalizedFtpReply.translate(session, request, context,
94-
FtpReply.REPLY_202_COMMAND_NOT_IMPLEMENTED, "PASS",
95-
null));
89+
FtpReply.REPLY_202_COMMAND_NOT_IMPLEMENTED, "PASS", null));
90+
9691
return;
9792
}
9893

9994
// anonymous login limit check
95+
boolean anonymous = UserManager.ANONYMOUS.equals(userName);
10096

101-
boolean anonymous = userName != null
102-
&& userName.equals("anonymous");
10397
if (anonymous) {
10498
int currAnonLogin = stat.getCurrentAnonymousLoginNumber();
105-
int maxAnonLogin = context.getConnectionConfig()
106-
.getMaxAnonymousLogins();
99+
int maxAnonLogin = context.getConnectionConfig().getMaxAnonymousLogins();
100+
107101
if (maxAnonLogin == 0) {
108102
LOG.debug("Currently {} anonymous users logged in, unlimited allowed", currAnonLogin);
109103
} else {
@@ -112,58 +106,53 @@ public void execute(final FtpIoSession session,
112106

113107
if (currAnonLogin >= maxAnonLogin) {
114108
LOG.debug("Too many anonymous users logged in, user will be disconnected");
115-
session
116-
.write(LocalizedFtpReply
117-
.translate(
118-
session,
119-
request,
120-
context,
109+
session.write(LocalizedFtpReply.translate(session, request, context,
121110
FtpReply.REPLY_421_SERVICE_NOT_AVAILABLE_CLOSING_CONTROL_CONNECTION,
122111
"PASS.anonymous", null));
112+
123113
return;
124114
}
125115
}
126116

127117
// login limit check
128118
int currLogin = stat.getCurrentLoginNumber();
129119
int maxLogin = context.getConnectionConfig().getMaxLogins();
120+
130121
if (maxLogin == 0) {
131122
LOG.debug("Currently {} users logged in, unlimited allowed", currLogin);
132123
} else {
133124
LOG.debug("Currently {} out of {} users logged in", currLogin, maxLogin);
134125
}
126+
135127
if (maxLogin != 0 && currLogin >= maxLogin) {
136128
LOG.debug("Too many users logged in, user will be disconnected");
137-
session
138-
.write(LocalizedFtpReply
139-
.translate(
140-
session,
141-
request,
142-
context,
129+
session.write(LocalizedFtpReply.translate(
130+
session, request, context,
143131
FtpReply.REPLY_421_SERVICE_NOT_AVAILABLE_CLOSING_CONTROL_CONNECTION,
144132
"PASS.login", null));
133+
145134
return;
146135
}
147136

148137
// authenticate user
149138
UserManager userManager = context.getUserManager();
150139
User authenticatedUser = null;
140+
151141
try {
152142
UserMetadata userMetadata = new UserMetadata();
153143

154144
if (session.getRemoteAddress() instanceof InetSocketAddress) {
155-
userMetadata.setInetAddress(((InetSocketAddress) session
156-
.getRemoteAddress()).getAddress());
145+
userMetadata.setInetAddress(((InetSocketAddress) session.getRemoteAddress()).getAddress());
157146
}
158-
userMetadata.setCertificateChain(session
159-
.getClientCertificates());
147+
148+
userMetadata.setCertificateChain(session.getClientCertificates());
160149

161150
Authentication auth;
151+
162152
if (anonymous) {
163153
auth = new AnonymousAuthentication(userMetadata);
164154
} else {
165-
auth = new UsernamePasswordAuthentication(userName,
166-
password, userMetadata);
155+
auth = new UsernamePasswordAuthentication(userName, password, userMetadata);
167156
}
168157

169158
authenticatedUser = userManager.authenticate(auth);
@@ -182,14 +171,9 @@ public void execute(final FtpIoSession session,
182171

183172
if (authenticatedUser != null) {
184173
if (!authenticatedUser.getEnabled()) {
185-
session
186-
.write(LocalizedFtpReply
187-
.translate(
188-
session,
189-
request,
190-
context,
191-
FtpReply.REPLY_530_NOT_LOGGED_IN,
192-
"PASS", null));
174+
session.write(LocalizedFtpReply.translate(
175+
session, request, context, FtpReply.REPLY_530_NOT_LOGGED_IN, "PASS", null));
176+
193177
return;
194178
}
195179

@@ -207,8 +191,7 @@ public void execute(final FtpIoSession session,
207191
session.setUserArgument(oldUserArgument);
208192
session.setMaxIdleTime(oldMaxIdleTime);
209193

210-
delayAfterLoginFailure(context.getConnectionConfig()
211-
.getLoginFailureDelay());
194+
delayAfterLoginFailure(context.getConnectionConfig().getLoginFailureDelay());
212195

213196
LOG.warn("Login failure - " + userName);
214197
session.write(LocalizedFtpReply.translate(session, request, context,
@@ -218,10 +201,9 @@ public void execute(final FtpIoSession session,
218201
session.increaseFailedLogins();
219202

220203
// kick the user if the max number of failed logins is reached
221-
int maxAllowedLoginFailues = context.getConnectionConfig()
222-
.getMaxLoginFailures();
223-
if (maxAllowedLoginFailues != 0
224-
&& session.getFailedLogins() >= maxAllowedLoginFailues) {
204+
int maxAllowedLoginFailues = context.getConnectionConfig().getMaxLoginFailures();
205+
206+
if ((maxAllowedLoginFailues != 0) && (session.getFailedLogins() >= maxAllowedLoginFailues)) {
225207
LOG.warn("User exceeded the number of allowed failed logins, session will be closed");
226208

227209
session.close(false).awaitUninterruptibly(10000);
@@ -232,22 +214,20 @@ public void execute(final FtpIoSession session,
232214

233215
// update different objects
234216
FileSystemFactory fmanager = context.getFileSystemManager();
235-
FileSystemView fsview = fmanager
236-
.createFileSystemView(authenticatedUser);
217+
FileSystemView fsview = fmanager.createFileSystemView(authenticatedUser);
237218
session.setLogin(fsview);
238219
stat.setLogin(session);
239220

240221
// everything is fine - send login ok message
241222
session.write(LocalizedFtpReply.translate(session, request, context,
242-
FtpReply.REPLY_230_USER_LOGGED_IN, "PASS", userName));
223+
FtpReply.REPLY_230_USER_LOGGED_IN, "PASS", userName));
224+
243225
if (anonymous) {
244-
LOG.info("Anonymous login success - " + password);
226+
LOG.info("Anonymous login success");
245227
} else {
246-
LOG.info("Login success - " + userName);
228+
LOG.info("Login success - {}", userName);
247229
}
248-
249230
} finally {
250-
251231
// if login failed - reset user
252232
if (!success) {
253233
session.reinitialize();
@@ -256,7 +236,6 @@ public void execute(final FtpIoSession session,
256236
}
257237

258238
private void delayAfterLoginFailure(final int loginFailureDelay) {
259-
260239
if (loginFailureDelay > 0) {
261240
LOG.debug("Waiting for {} milliseconds due to login failure", loginFailureDelay);
262241

core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemView.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
4040
*/
4141
public class NativeFileSystemView implements FileSystemView {
42-
43-
private final Logger LOG = LoggerFactory
44-
.getLogger(NativeFileSystemView.class);
42+
private final Logger LOG = LoggerFactory.getLogger(NativeFileSystemView.class);
4543

4644

4745
// the root directory will always end with '/'.
@@ -69,15 +67,15 @@ protected NativeFileSystemView(User user) throws FtpException {
6967
*
7068
* @param user The current user
7169
* @param caseInsensitive If the OS FS is case sensitive or not
70+
* @throws FtpException Actually, never thrown... To be removed!
7271
*/
73-
public NativeFileSystemView(User user, boolean caseInsensitive)
74-
throws FtpException {
72+
public NativeFileSystemView(User user, boolean caseInsensitive) throws FtpException {
7573
if (user == null) {
7674
throw new IllegalArgumentException("user can not be null");
7775
}
76+
7877
if (user.getHomeDirectory() == null) {
79-
throw new IllegalArgumentException(
80-
"User home directory can not be null");
78+
throw new IllegalArgumentException("User home directory can not be null");
8179
}
8280

8381
this.caseInsensitive = caseInsensitive;
@@ -90,7 +88,6 @@ public NativeFileSystemView(User user, boolean caseInsensitive)
9088
LOG.debug("Native filesystem view created for user \"{}\" with root \"{}\"", user.getName(), rootDir);
9189

9290
this.rootDir = rootDir;
93-
9491
this.user = user;
9592

9693
currDir = "/";
@@ -111,28 +108,28 @@ public FtpFile getHomeDirectory() {
111108
*/
112109
public FtpFile getWorkingDirectory() {
113110
FtpFile fileObj = null;
111+
114112
if (currDir.equals("/")) {
115113
fileObj = new NativeFtpFile("/", new File(rootDir), user);
116114
} else {
117115
File file = new File(rootDir, currDir.substring(1));
118116
fileObj = new NativeFtpFile(currDir, file, user);
119-
120117
}
118+
121119
return fileObj;
122120
}
123121

124122
/**
125123
* {@inheritDoc}
126124
*/
127125
public FtpFile getFile(String file) {
128-
129126
// get actual file object
130-
String physicalName = getPhysicalName(rootDir,
131-
currDir, file, caseInsensitive);
127+
String physicalName = getPhysicalName(rootDir, currDir, file, caseInsensitive);
132128
File fileObj = new File(physicalName);
133129

134130
// strip the root directory and return
135131
String userFileName = physicalName.substring(rootDir.length() - 1);
132+
136133
return new NativeFtpFile(userFileName, fileObj, user);
137134
}
138135

@@ -304,6 +301,7 @@ private String trimTrailingSlash(String path) {
304301
private String normalizeSeparateChar(final String pathName) {
305302
String normalizedPathName = pathName.replace(File.separatorChar, '/');
306303
normalizedPathName = normalizedPathName.replace('\\', '/');
304+
307305
return normalizedPathName;
308306
}
309307

0 commit comments

Comments
 (0)