KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > command > plugins > Request


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.master.command.plugins;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import net.sf.drftpd.FileExistsException;
24 import net.sf.drftpd.event.DirectoryFtpEvent;
25 import net.sf.drftpd.master.BaseFtpConnection;
26 import net.sf.drftpd.master.FtpReply;
27 import net.sf.drftpd.master.command.CommandManager;
28 import net.sf.drftpd.master.command.CommandManagerFactory;
29 import net.sf.drftpd.master.usermanager.NoSuchUserException;
30 import net.sf.drftpd.remotefile.LinkedRemoteFile;
31 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
32
33 import org.apache.log4j.Logger;
34 import org.drftpd.commands.CommandHandler;
35 import org.drftpd.commands.CommandHandlerFactory;
36 import org.drftpd.commands.UnhandledCommandException;
37
38 /**
39  * @author mog
40  * @version $Id: Request.java,v 1.15.2.1 2004/06/19 23:37:26 mog Exp $
41  */

42 public class Request implements CommandHandlerFactory, CommandHandler {
43     private static final String JavaDoc FILLEDPREFIX = "FILLED-for.";
44
45     private static final Logger logger = Logger.getLogger(Request.class);
46
47     private static final String JavaDoc REQPREFIX = "REQUEST-by.";
48
49     private FtpReply doSITE_REQFILLED(BaseFtpConnection conn) {
50         if (!conn.getRequest().hasArgument())
51             return FtpReply.RESPONSE_501_SYNTAX_ERROR;
52
53         LinkedRemoteFileInterface currdir = conn.getCurrentDirectory();
54         String JavaDoc reqname = conn.getRequest().getArgument();
55
56         for (Iterator JavaDoc iter = currdir.getFiles().iterator(); iter.hasNext();) {
57             LinkedRemoteFile file = (LinkedRemoteFile) iter.next();
58
59             if (!file.getName().startsWith(REQPREFIX))
60                 continue;
61             String JavaDoc username = file.getName().substring(REQPREFIX.length());
62             String JavaDoc myreqname = username.substring(username.indexOf('-') + 1);
63             username = username.substring(0, username.indexOf('-'));
64             if (myreqname.equals(reqname)) {
65                 String JavaDoc filledname = FILLEDPREFIX + username + "-" + myreqname;
66                 LinkedRemoteFile filledfile;
67                 try {
68                     filledfile =
69                         file.renameTo(
70                             file.getParentFile().getPath(),
71                             filledname);
72                 } catch (IOException JavaDoc e) {
73                     logger.warn("", e);
74                     return new FtpReply(200, e.getMessage());
75                 }
76                 //if (conn.getConfig().checkDirLog(conn.getUserNull(), file)) {
77
conn.getConnectionManager().dispatchFtpEvent(
78                     new DirectoryFtpEvent(
79                         conn.getUserNull(),
80                         "REQFILLED",
81                         filledfile));
82                 //}
83
try {
84                     conn.getUser().addRequestsFilled();
85                 } catch (NoSuchUserException e) {
86                     e.printStackTrace();
87                 }
88                 return new FtpReply(
89                     200,
90                     "OK, renamed " + myreqname + " to " + filledname);
91             }
92         }
93         return new FtpReply(200, "Couldn't find a request named " + reqname);
94     }
95     private FtpReply doSITE_REQUEST(BaseFtpConnection conn) {
96
97         if (!conn
98             .getConfig()
99             .checkPathPermission(
100                 "request",
101                 conn.getUserNull(),
102                 conn.getCurrentDirectory())) {
103             return FtpReply.RESPONSE_530_ACCESS_DENIED;
104         }
105
106         if (!conn.getRequest().hasArgument()) {
107             return FtpReply.RESPONSE_501_SYNTAX_ERROR;
108         }
109
110         String JavaDoc createdDirName =
111             REQPREFIX
112                 + conn.getUserNull().getUsername()
113                 + "-"
114                 + conn.getRequest().getArgument();
115         try {
116             LinkedRemoteFile createdDir =
117                 conn.getCurrentDirectory().createDirectory(
118                     conn.getUserNull().getUsername(),
119                     conn.getUserNull().getGroupName(),
120                     createdDirName);
121
122             //if (conn.getConfig().checkDirLog(conn.getUserNull(), createdDir)) {
123
conn.getConnectionManager().dispatchFtpEvent(
124                 new DirectoryFtpEvent(
125                     conn.getUserNull(),
126                     "REQUEST",
127                     createdDir));
128             //}
129
try {
130                 conn.getUser().addRequests();
131             } catch (NoSuchUserException e) {
132                 e.printStackTrace();
133             }
134             return new FtpReply(
135                 257,
136                 "\"" + createdDir.getPath() + "\" created.");
137         } catch (FileExistsException ex) {
138             return new FtpReply(
139                 550,
140                 "directory " + createdDirName + " already exists");
141         }
142     }
143
144     public FtpReply execute(BaseFtpConnection conn)
145         throws UnhandledCommandException {
146         String JavaDoc cmd = conn.getRequest().getCommand();
147         if ("SITE REQUEST".equals(cmd))
148             return doSITE_REQUEST(conn);
149         if ("SITE REQFILLED".equals(cmd))
150             return doSITE_REQFILLED(conn);
151         throw UnhandledCommandException.create(
152             Request.class,
153             conn.getRequest());
154     }
155
156     public String JavaDoc[] getFeatReplies() {
157         return null;
158     }
159
160     public CommandHandler initialize(
161         BaseFtpConnection conn,
162         CommandManager initializer) {
163         return this;
164     }
165     public void load(CommandManagerFactory initializer) {
166     }
167     public void unload() {
168     }
169 }
170
Popular Tags