/** * Verify that the current user has execute permission to the specified path * * @param session - the Session * @param path - the file system path * @throws FileSystemException - if the condition is not true */ protected void verifyExecutePermission(Session session, String path) { UserAccount userAccount = getUserAccount(session); FileSystemEntry entry = getFileSystem().getEntry(path); verifyFileSystemCondition(userAccount.canExecute(entry), path, "filesystem.cannotExecute"); }
private String getTextForKey(String key) { String msgKey = (key != null) ? key : INTERNAL_ERROR_KEY; try { return getReplyTextBundle().getString(msgKey); } catch (MissingResourceException e) { // No reply text is mapped for the specified key LOG.warn("No reply text defined for key [" + msgKey + "]"); return null; } }
/** * Return the string unless it is null or empty, in which case return the defaultString. * * @param string - the String to check; may be null * @param defaultString - the value to return if string is null or empty * @return string if not null and not empty; otherwise return defaultString */ protected String defaultIfNullOrEmpty(String string, String defaultString) { return (notNullOrEmpty(string) ? string : defaultString); }
/** * Use template method to centralize and ensure common validation */ public void handleCommand(Command command, Session session) { Assert.notNull(serverConfiguration, "serverConfiguration"); Assert.notNull(command, "command"); Assert.notNull(session, "session"); try { handle(command, session); } catch (CommandSyntaxException e) { handleException(command, session, e, ReplyCodes.COMMAND_SYNTAX_ERROR); } catch (IllegalStateException e) { handleException(command, session, e, ReplyCodes.ILLEGAL_STATE); } catch (NotLoggedInException e) { handleException(command, session, e, ReplyCodes.NOT_LOGGED_IN); } catch (InvalidFilenameException e) { handleFileSystemException(command, session, e, ReplyCodes.FILENAME_NOT_VALID, e.getPath()); } catch (FileSystemException e) { handleFileSystemException(command, session, e, replyCodeForFileSystemException, e.getPath()); } }
/** * Validate the UserAccount for the specified username. If valid, return true. If the UserAccount does * not exist or is invalid, log an error message, send back a reply code of 530 with an appropriate * error message, and return false. A UserAccount is considered invalid if the homeDirectory property * is not set or is set to a non-existent directory. * * @param username - the username * @param session - the session; used to send back an error reply if necessary * @return true only if the UserAccount for the named user is valid */ protected boolean validateUserAccount(String username, Session session) { UserAccount userAccount = serverConfiguration.getUserAccount(username); if (userAccount == null || !userAccount.isValid()) { LOG.error("UserAccount missing or not valid for username [" + username + "]: " + userAccount); sendReply(session, ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.userAccountNotValid", list(username)); return false; } String home = userAccount.getHomeDirectory(); if (!getFileSystem().isDirectory(home)) { LOG.error("Home directory configured for username [" + username + "] is not valid: " + home); sendReply(session, ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.homeDirectoryNotValid", list(username, home)); return false; } return true; }
/** * Send a reply for this command on the control connection. * * <p>The reply code is designated by the <code>replyCode</code> property, and the reply text * is retrieved from the <code>replyText</code> ResourceBundle, using the specified messageKey. * * @param session - the Session * @param replyCode - the reply code * @param messageKey - the resource bundle key for the reply text * @param args - the optional message arguments; defaults to [] * @throws AssertionError - if session is null * @see MessageFormat */ protected void sendReply(Session session, int replyCode, String messageKey, List args) { Assert.notNull(session, "session"); assertValidReplyCode(replyCode); String text = getTextForKey(messageKey); String replyText = (args != null && !args.isEmpty()) ? MessageFormat.format(text, args.toArray()) : text; String replyTextToLog = (replyText == null) ? "" : " " + replyText; String argsToLog = (args != null && !args.isEmpty()) ? (" args=" + args) : ""; LOG.info("Sending reply [" + replyCode + replyTextToLog + "]" + argsToLog); session.sendReply(replyCode, replyText); }
/** * Verify that the current user (if any) has already logged in successfully. * * @param session - the Session */ protected void verifyLoggedIn(Session session) { if (getUserAccount(session) == null) { throw new NotLoggedInException("User has not logged in"); } }
/** * Return the full, absolute path for the specified abstract pathname. * If path is null, return the current directory (stored in the session). If * path represents an absolute path, then return path as is. Otherwise, path * is relative, so assemble the full path from the current directory * and the specified relative path. * * @param session - the Session * @param path - the abstract pathname; may be null * @return the resulting full, absolute path */ protected String getRealPath(Session session, String path) { String currentDirectory = (String) session.getAttribute(SessionKeys.CURRENT_DIRECTORY); if (path == null) { return currentDirectory; } if (getFileSystem().isAbsolute(path)) { return path; } return getFileSystem().path(currentDirectory, path); }
/** * Verify that the current user has write permission to the specified path * * @param session - the Session * @param path - the file system path * @throws FileSystemException - if the condition is not true */ protected void verifyWritePermission(Session session, String path) { UserAccount userAccount = getUserAccount(session); FileSystemEntry entry = getFileSystem().getEntry(path); verifyFileSystemCondition(userAccount.canWrite(entry), path, "filesystem.cannotWrite"); }
/** * Verify that the current user has read permission to the specified path * * @param session - the Session * @param path - the file system path * @throws FileSystemException - if the condition is not true */ protected void verifyReadPermission(Session session, String path) { UserAccount userAccount = getUserAccount(session); FileSystemEntry entry = getFileSystem().getEntry(path); verifyFileSystemCondition(userAccount.canRead(entry), path, "filesystem.cannotRead"); }