Subversion Repositories bacoAlunos

Rev

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