KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import net.sf.drftpd.master.BaseFtpConnection;
25 import net.sf.drftpd.master.FtpReply;
26 import net.sf.drftpd.master.FtpRequest;
27 import net.sf.drftpd.master.command.CommandManager;
28 import net.sf.drftpd.master.command.CommandManagerFactory;
29 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
30
31 import org.apache.log4j.Logger;
32 import org.drftpd.commands.CommandHandler;
33 import org.drftpd.commands.CommandHandlerFactory;
34
35 /**
36  * @author mog
37  * @version $Id: Search.java,v 1.11.2.1 2004/06/19 23:37:26 mog Exp $
38  */

39 public class Search implements CommandHandlerFactory, CommandHandler {
40     public void unload() {}
41     public void load(CommandManagerFactory initializer) {}
42
43     private static void findFile(
44         BaseFtpConnection conn,
45         FtpReply response,
46         LinkedRemoteFileInterface dir,
47         Collection JavaDoc searchstrings,
48         boolean files,
49         boolean dirs) {
50         //TODO optimize me, checking using regexp for all dirs is possibly slow
51
if (!conn.getConfig().checkPrivPath(conn.getUserNull(), dir)) {
52             Logger.getLogger(Search.class).debug("privpath: "+dir.getPath());
53             return;
54         }
55         for (Iterator JavaDoc iter = dir.getFiles().iterator(); iter.hasNext();) {
56             LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next();
57             if (file.isDirectory()) {
58                 findFile(conn, response, file, searchstrings, files, dirs);
59             }
60             if (dirs && file.isDirectory() || files && file.isFile()) {
61                 for (Iterator JavaDoc iterator = searchstrings.iterator();
62                     iterator.hasNext();
63                     ) {
64                     if(response.size() >= 100) return;
65                     String JavaDoc searchstring = (String JavaDoc) iterator.next();
66                     if (file.getName().toLowerCase().indexOf(searchstring)
67                         != -1) {
68                         response.addComment(file.getPath());
69                         if(response.size() >= 100) {
70                             response.addComment("<snip>");
71                             return;
72                         }
73                         break;
74                     }
75                 }
76             }
77         }
78     }
79
80     public FtpReply execute(BaseFtpConnection conn) {
81         FtpRequest request = conn.getRequest();
82         if (!request.hasArgument()) {
83             return FtpReply.RESPONSE_501_SYNTAX_ERROR;
84         }
85         String JavaDoc args[] = request.getArgument().toLowerCase().split(" ");
86         if (args.length == 0) {
87             return FtpReply.RESPONSE_501_SYNTAX_ERROR;
88         }
89         Collection JavaDoc searchstrings = Arrays.asList(args);
90         FtpReply response = (FtpReply) FtpReply.RESPONSE_200_COMMAND_OK.clone();
91         findFile(
92             conn,
93             response,
94             conn.getCurrentDirectory(),
95             searchstrings,
96             "SITE DUPE".equals(request.getCommand()),
97             "SITE SEARCH".equals(request.getCommand()));
98         return response;
99     }
100
101     public CommandHandler initialize(BaseFtpConnection conn, CommandManager initializer) {
102         return this;
103     }
104
105     public String JavaDoc[] getFeatReplies() {
106         return null;
107     }
108
109 }
110
Popular Tags