Subversion Repositories bacoAlunos

Rev

Rev 1 | Rev 165 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 fvelez 1
package pt.estgp.estgweb.web;
2
 
3
import org.apache.log4j.Logger;
4
import org.apache.commons.httpclient.util.URIUtil;
5
import org.apache.commons.net.ftp.FTPClient;
6
import org.apache.commons.net.ftp.FTPFile;
7
 
8
import javax.servlet.http.HttpServlet;
9
import javax.servlet.http.HttpServletRequest;
10
import javax.servlet.http.HttpServletResponse;
11
import javax.servlet.ServletException;
12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.io.OutputStream;
15
import java.io.FileNotFoundException;
16
import java.net.URL;
17
import java.net.URLConnection;
18
import java.net.MalformedURLException;
19
import java.net.SocketException;
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.ArrayList;
23
 
24
import pt.estgp.estgweb.utils.ConfigProperties;
25
import jomm.web.ftp.IFile;
26
import jomm.web.ftp.impl.FtpFile;
27
 
28
/**
29
 * @author Jorge Machado
30
 * @date 26/Fev/2008
31
 * @time 12:46:16
32
 * @see pt.estgp.estgweb.web
33
 */
34
public class FTPProxy extends HttpServlet {
35
 
36
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
37
     * @param request servlet request
38
     * @param response servlet response
39
     */
40
    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(FTPProxy.class);
41
 
42
    private static HashMap<String,FtpServer> servers = new HashMap<String,FtpServer>();
43
 
44
    /**
45
     * Add a ftpServer to Cache
46
     * @param urlStr to parse
47
     * @param user username
48
     * @param pass password
49
     * @return ftpServer
50
     */
51
    private static synchronized FtpServer addServer(1.5.0/docs/api/java/lang/String.html">String urlStr, 1.5.0/docs/api/java/lang/String.html">String user, 1.5.0/docs/api/java/lang/String.html">String pass)
52
    {
53
        try
54
        {
55
            1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(urlStr);
56
            1.5.0/docs/api/java/lang/String.html">String host = url.getHost();
57
            int port = url.getPort();
58
            if(port <= 0)
59
                port = 21;
60
            logger.info("Connecting to " + host + "in port:" + port);
61
            FtpServer ftpServer = new FTPProxy.FtpServer(host,port,user,pass);
62
            servers.put(urlStr,ftpServer);
63
            return ftpServer;
64
        }
65
        catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e)
66
        {
67
            logger.error(e,e);
68
        }
69
        catch (1.5.0/docs/api/java/io/IOException.html">IOException e)
70
        {
71
            logger.error(e,e);
72
        }
73
        return null;
74
    }
75
 
76
    private static FtpServer getServer(1.5.0/docs/api/java/lang/String.html">String urlStr, 1.5.0/docs/api/java/lang/String.html">String user, 1.5.0/docs/api/java/lang/String.html">String pass)
77
    {
78
        FtpServer ftpServer = servers.get(urlStr);
79
        if(ftpServer == null)
80
            ftpServer = addServer(urlStr,user,pass);
81
        return ftpServer;
82
    }
83
 
84
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
85
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException
86
    {
87
        1.5.0/docs/api/java/lang/String.html">String path = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("path");
88
        1.5.0/docs/api/java/lang/String.html">String server = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("server");
89
        1.5.0/docs/api/java/lang/String.html">String user = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("user");
90
        1.5.0/docs/api/java/lang/String.html">String pass = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("pass");
91
        logger.info("server:" + server);
92
        logger.info("path:" + path);
93
        if(path != null)
94
        {
95
            FtpServer ftpServer = getServer(server,user,pass);
96
            FTPClient client = ftpServer.getClient();
97
            client.changeWorkingDirectory(URIUtil.encodePath(path,"ISO-8859-1"));
98
            FTPFile[] files =  client.listFiles();
99
            List<IFile> iFiles = new ArrayList<IFile>();
100
            for(FTPFile ftpFile : files)
101
            {
144 jmachado 102
                IFile iFile;
103
                if(path.endsWith("/"))
104
                    iFile = new FtpFile(ftpFile,server + path + ftpFile.getName(),path);
105
                else
106
                    iFile = new FtpFile(ftpFile,server + path + "/" + ftpFile.getName(),path);
107
 
1 fvelez 108
                iFiles.add(iFile);
109
            }
110
            request.setAttribute("files",iFiles);
111
            getServletContext().getRequestDispatcher("/user/ftpclient/ftpclient.jsp").forward(request,response);
112
            client.quit();
113
        }
114
        else
115
        {
116
            throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path);
117
        }
118
    }
119
 
120
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
121
    /** Handles the HTTP <code>GET</code> method.
122
     * @param request servlet request
123
     * @param response servlet response
124
     */
125
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
126
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException {
127
        processRequest(request, response);
128
    }
129
 
130
    /** Handles the HTTP <code>POST</code> method.
131
     * @param request servlet request
132
     * @param response servlet response
133
     */
134
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
135
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException {
136
        processRequest(request, response);
137
    }
138
 
139
    /** Returns a short description of the servlet.
140
     */
141
    public 1.5.0/docs/api/java/lang/String.html">String getServletInfo() {
142
        return "Proxy Servlet to get pages form static web page servers";
143
    }
144
    // </editor-fold>
145
 
146
    public static class FtpServer
147
    {
148
        1.5.0/docs/api/java/lang/String.html">String host = null;
149
        int port = 21;
150
        1.5.0/docs/api/java/lang/String.html">String username = null;
151
        1.5.0/docs/api/java/lang/String.html">String password = null;
152
 
153
 
154
        public FtpServer(1.5.0/docs/api/java/lang/String.html">String host, int port)
155
        {
156
            this.host = host;
157
            if(port > 0)
158
                this.port = port;
159
        }
160
 
161
        public FtpServer(1.5.0/docs/api/java/lang/String.html">String host, int port, 1.5.0/docs/api/java/lang/String.html">String username, 1.5.0/docs/api/java/lang/String.html">String password)
162
        {
163
            this.host = host;
164
            this.port = port;
165
            this.username = username;
166
            this.password = password;
167
        }
168
 
169
        public FTPClient getClient() throws 1.5.0/docs/api/java/io/IOException.html">IOException
170
        {
171
            FTPClient ftp = new FTPClient();
172
            ftp.connect(host,port);
173
            if(username != null)
174
                ftp.user(username);
175
            if(password != null)
176
                ftp.pass(password);
177
            return ftp;
178
        }
179
 
180
        public 1.5.0/docs/api/java/lang/String.html">String getHost() {
181
            return host;
182
        }
183
 
184
        public void setHost(1.5.0/docs/api/java/lang/String.html">String host) {
185
            this.host = host;
186
        }
187
 
188
        public int getPort() {
189
            return port;
190
        }
191
 
192
        public void setPort(int port) {
193
            this.port = port;
194
        }
195
 
196
        public 1.5.0/docs/api/java/lang/String.html">String getUsername() {
197
            return username;
198
        }
199
 
200
        public void setUsername(1.5.0/docs/api/java/lang/String.html">String username) {
201
            this.username = username;
202
        }
203
 
204
        public 1.5.0/docs/api/java/lang/String.html">String getPassword() {
205
            return password;
206
        }
207
 
208
        public void setPassword(1.5.0/docs/api/java/lang/String.html">String password) {
209
            this.password = password;
210
        }
211
    }
212
}