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