001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package net.sf.drftpd.master.command;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Hashtable;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.MissingResourceException;
027: import java.util.ResourceBundle;
028:
029: import net.sf.drftpd.ObjectNotFoundException;
030: import net.sf.drftpd.master.BaseFtpConnection;
031:
032: import org.drftpd.commands.CommandHandler;
033: import org.drftpd.commands.CommandHandlerFactory;
034: import org.drftpd.commands.ImproperUsageException;
035: import org.drftpd.commands.Reply;
036: import org.drftpd.commands.ReplyException;
037: import org.drftpd.commands.UnhandledCommandException;
038: import org.drftpd.usermanager.NoSuchUserException;
039:
040: /**
041: * @author mog
042: * @version $Id: CommandManager.java 1378 2005-12-23 19:12:24Z zubov $
043: */
044: public class CommandManager {
045: private CommandManagerFactory _factory;
046:
047: /**
048: * String => CommandHandler
049: * Mapping commands to commandhandlers.
050: */
051: private Map commands = new Hashtable();
052:
053: /**
054: * Class => CommandHandler
055: * Kept so that CommandHandlers can look up each other.
056: */
057: private Hashtable hnds = new Hashtable();
058:
059: public CommandManager(BaseFtpConnection conn,
060: CommandManagerFactory initializer) {
061: _factory = initializer;
062:
063: for (Iterator iter = _factory.getHandlersMap().entrySet()
064: .iterator(); iter.hasNext();) {
065: Map.Entry entry = (Map.Entry) iter.next();
066: hnds.put(entry.getKey(), ((CommandHandlerFactory) entry
067: .getValue()).initialize(conn, this ));
068: }
069:
070: for (Iterator iter = _factory.getCommandsMap().entrySet()
071: .iterator(); iter.hasNext();) {
072: Map.Entry entry = (Map.Entry) iter.next();
073: commands.put((String) entry.getKey(), (CommandHandler) hnds
074: .get((Class) entry.getValue()));
075: }
076: }
077:
078: public Reply execute(BaseFtpConnection conn) throws ReplyException {
079: String command = conn.getRequest().getCommand();
080: CommandHandler handler = (CommandHandler) commands.get(command);
081:
082: if (handler == null) {
083: throw new UnhandledCommandException(
084: "No command handler for " + command);
085: }
086:
087: if (!conn.getCurrentDirectory().isValid()) {
088: conn.setCurrentDirectory(conn.getCurrentDirectory()
089: .getRoot());
090: }
091:
092: try {
093: if (conn.getUser().isDeleted()) {
094: conn.stop("You are deleted");
095: return new Reply(500, "You are deleted");
096: }
097: } catch (NoSuchUserException e1) {
098: // user hasn't authenticated yet
099: }
100:
101: try {
102: command = command.substring("SITE ".length()).toLowerCase();
103:
104: if (!conn.getGlobalContext().getConfig()
105: .checkPathPermission(command, conn.getUserNull(),
106: conn.getCurrentDirectory(), true)) {
107: //logger.debug("Blocking access to execute : SITE "+command);
108: return Reply.RESPONSE_530_ACCESS_DENIED;
109: }
110: } catch (java.lang.StringIndexOutOfBoundsException e) {
111: }
112:
113: try {
114: return handler.execute(conn);
115: } catch (ImproperUsageException e2) {
116: Reply response = (Reply) Reply.RESPONSE_501_SYNTAX_ERROR
117: .clone();
118: try {
119: response.addComment(ResourceBundle.getBundle(
120: handler.getClass().getName()).getString(
121: "help." + command + ".specific"));
122: } catch (MissingResourceException e) {
123: response
124: .addComment("Bug your siteop to add help for the "
125: + "\"SITE "
126: + command.toUpperCase()
127: + "\" " + "command");
128: }
129: return (response);
130:
131: }
132: }
133:
134: public CommandHandler getCommandHandler(Class clazz)
135: throws ObjectNotFoundException {
136: CommandHandler ret = (CommandHandler) hnds.get(clazz);
137:
138: if (ret == null) {
139: throw new ObjectNotFoundException();
140: }
141: return ret;
142: }
143:
144: public List<String> getHandledCommands(Class class1) {
145: ArrayList<String> list = new ArrayList<String>();
146:
147: for (Iterator iter = commands.entrySet().iterator(); iter
148: .hasNext();) {
149: Map.Entry element = (Map.Entry) iter.next();
150:
151: if (element.getValue().getClass().equals(class1)) {
152: list.add((String) element.getKey());
153: }
154: }
155:
156: return list;
157: }
158:
159: /**
160: * Class => CommandHandler
161: */
162: public Map getCommandHandlersMap() {
163: return Collections.unmodifiableMap(hnds);
164: }
165: }
|