Rev 1288 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
379 | jmachado | 1 | package pt.estgp.estgweb.web; |
2 | |||
3 | import org.apache.commons.net.ftp.FTPClient; |
||
4 | import org.apache.log4j.Logger; |
||
1148 | jmachado | 5 | import pt.estgp.estgweb.domain.UserSession; |
6 | import pt.estgp.estgweb.domain.UserSessionImpl; |
||
379 | jmachado | 7 | |
1148 | jmachado | 8 | import javax.servlet.http.HttpServletRequest; |
1005 | jmachado | 9 | import java.io.IOException; |
1008 | jmachado | 10 | import java.io.InputStream; |
1005 | jmachado | 11 | import java.net.MalformedURLException; |
12 | import java.net.URL; |
||
379 | jmachado | 13 | import java.util.HashMap; |
14 | |||
15 | /** |
||
16 | * @author Jorge |
||
17 | * @date 18/Jan/2009 |
||
18 | * @time 18:35:11 |
||
19 | */ |
||
20 | public class FtpServer |
||
21 | { |
||
22 | 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(FtpServer.class); |
||
23 | |||
24 | |||
25 | private static HashMap<String, FtpServer> servers = new HashMap<String, FtpServer>(); |
||
26 | |||
27 | |||
28 | 1.5.0/docs/api/java/lang/String.html">String host = null; |
||
29 | int port = 21; |
||
30 | 1.5.0/docs/api/java/lang/String.html">String username = null; |
||
31 | 1.5.0/docs/api/java/lang/String.html">String password = null; |
||
1286 | jmachado | 32 | 1.5.0/docs/api/java/lang/String.html">String errorMsg = ""; |
379 | jmachado | 33 | |
34 | |||
35 | public FtpServer(1.5.0/docs/api/java/lang/String.html">String host, int port) |
||
36 | { |
||
37 | this.host = host; |
||
38 | if (port > 0) |
||
39 | this.port = port; |
||
40 | } |
||
41 | |||
42 | 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) |
||
43 | { |
||
44 | this.host = host; |
||
45 | this.port = port; |
||
46 | this.username = username; |
||
47 | this.password = password; |
||
48 | } |
||
49 | |||
1148 | jmachado | 50 | public static FtpServer getFtpServerForCurrentUser(1.5.0/docs/api/java/lang/String.html">String urlStr, HttpServletRequest request) throws 1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException |
51 | { |
||
52 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(urlStr); |
||
53 | int port = url.getPort(); |
||
54 | if (port <= 0) |
||
55 | port = 21; |
||
56 | 1.5.0/docs/api/java/lang/String.html">String host = url.getHost(); |
||
57 | UserSession userSession = UserSessionProxy.loadUserSessionFromRequest(request); |
||
58 | 1.5.0/docs/api/java/lang/String.html">String username = userSession.getUsername(); |
||
59 | 1.5.0/docs/api/java/lang/String.html">String password = ((UserSessionImpl)userSession).getUserPasswordInSession(); |
||
60 | return new FtpServer(host,port,username,password); |
||
61 | } |
||
62 | |||
1005 | jmachado | 63 | private static final int FTP_TIMEOUT_SECONDS = pt.estgp.estgweb.Globals.FTP_TIMEOUT_SECONDS; |
379 | jmachado | 64 | public FTPClient getClient() throws 1.5.0/docs/api/java/io/IOException.html">IOException |
65 | { |
||
1286 | jmachado | 66 | errorMsg = null; |
1008 | jmachado | 67 | final FTPClient ftp = new MyFTPClient(); |
1005 | jmachado | 68 | logger.info("CONNECTING... waiting " + FTP_TIMEOUT_SECONDS + " seconds"); |
69 | 1.5.0/docs/api/java/lang/Thread.html">Thread t = new 1.5.0/docs/api/java/lang/Thread.html">Thread(new 1.5.0/docs/api/java/lang/Runnable.html">Runnable() { |
||
70 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
71 | public void run() { |
||
72 | try { |
||
73 | ftp.connect(host, port); |
||
74 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
75 | logger.error(e,e); |
||
76 | } |
||
77 | } |
||
78 | }); |
||
79 | t.start(); |
||
80 | try { |
||
81 | for(int i=0;i<FTP_TIMEOUT_SECONDS;i++) |
||
82 | { |
||
83 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(1000); |
||
84 | if(!t.isAlive()) |
||
85 | { |
||
86 | if(ftp.isConnected()) |
||
87 | break; |
||
88 | } |
||
89 | } |
||
90 | if(!ftp.isConnected()) |
||
91 | { |
||
1286 | jmachado | 92 | errorMsg = "errors.ftp.timeout"; |
1005 | jmachado | 93 | logger.info("Not Connected will interrupt"); |
94 | t.interrupt(); |
||
95 | return null; |
||
96 | } |
||
97 | } catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException e) { |
||
98 | logger.error(e,e); |
||
1286 | jmachado | 99 | errorMsg = "errors.ftp.interrupt"; |
1005 | jmachado | 100 | } |
915 | jmachado | 101 | |
1085 | jmachado | 102 | if (username != null && password != null) |
103 | { |
||
104 | if(ftp.login(username,password)) |
||
105 | { |
||
106 | return ftp; |
||
107 | } |
||
1286 | jmachado | 108 | else |
109 | { |
||
110 | errorMsg = "errors.ftp.no.permission"; |
||
111 | logger.warn("Cant login user: " + username); |
||
112 | } |
||
1085 | jmachado | 113 | } |
1288 | jmachado | 114 | if(errorMsg == null) |
115 | errorMsg = "errors.ftp.unknown.error"; |
||
1085 | jmachado | 116 | return null; |
117 | |||
118 | /* estava assim |
||
379 | jmachado | 119 | if (username != null) |
120 | ftp.user(username); |
||
121 | if (password != null) |
||
1085 | jmachado | 122 | ftp.pass(password);*/ |
123 | //return ftp; |
||
379 | jmachado | 124 | } |
125 | |||
1286 | jmachado | 126 | |
127 | public 1.5.0/docs/api/java/lang/String.html">String getErrorMsg() { |
||
128 | return errorMsg; |
||
129 | } |
||
130 | |||
379 | jmachado | 131 | public 1.5.0/docs/api/java/lang/String.html">String getHost() |
132 | { |
||
133 | return host; |
||
134 | } |
||
135 | |||
136 | public void setHost(1.5.0/docs/api/java/lang/String.html">String host) |
||
137 | { |
||
138 | this.host = host; |
||
139 | } |
||
140 | |||
141 | public int getPort() |
||
142 | { |
||
143 | return port; |
||
144 | } |
||
145 | |||
146 | public void setPort(int port) |
||
147 | { |
||
148 | this.port = port; |
||
149 | } |
||
150 | |||
151 | public 1.5.0/docs/api/java/lang/String.html">String getUsername() |
||
152 | { |
||
153 | return username; |
||
154 | } |
||
155 | |||
156 | public void setUsername(1.5.0/docs/api/java/lang/String.html">String username) |
||
157 | { |
||
158 | this.username = username; |
||
159 | } |
||
160 | |||
161 | public 1.5.0/docs/api/java/lang/String.html">String getPassword() |
||
162 | { |
||
163 | return password; |
||
164 | } |
||
165 | |||
166 | public void setPassword(1.5.0/docs/api/java/lang/String.html">String password) |
||
167 | { |
||
168 | this.password = password; |
||
169 | } |
||
170 | |||
171 | public 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) |
||
172 | { |
||
173 | FtpServer ftpServer = servers.get(urlStr); |
||
174 | if (ftpServer == null) |
||
175 | ftpServer = addServer(urlStr, user, pass); |
||
176 | return ftpServer; |
||
177 | } |
||
178 | |||
406 | jmachado | 179 | public static FtpServer getNewServer(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) |
180 | { |
||
181 | |||
182 | try |
||
183 | { |
||
184 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(urlStr); |
||
185 | 1.5.0/docs/api/java/lang/String.html">String host = url.getHost(); |
||
186 | int port = url.getPort(); |
||
187 | if (port <= 0) |
||
188 | port = 21; |
||
189 | logger.info("Connecting to " + host + "in port:" + port); |
||
190 | return new FtpServer(host, port, user, pass); |
||
191 | } |
||
192 | catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e) |
||
193 | { |
||
194 | logger.error(e, e); |
||
195 | } |
||
196 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
197 | { |
||
198 | logger.error(e, e); |
||
199 | } |
||
200 | return null; |
||
201 | |||
202 | } |
||
203 | |||
204 | /** |
||
379 | jmachado | 205 | * Add a ftpServer to Cache |
206 | * |
||
207 | * @param urlStr to parse |
||
208 | * @param user username |
||
209 | * @param pass password |
||
210 | * @return ftpServer |
||
211 | */ |
||
212 | 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) |
||
213 | { |
||
214 | try |
||
215 | { |
||
216 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(urlStr); |
||
217 | 1.5.0/docs/api/java/lang/String.html">String host = url.getHost(); |
||
218 | int port = url.getPort(); |
||
219 | if (port <= 0) |
||
220 | port = 21; |
||
489 | jmachado | 221 | logger.info("Connecting to " + host + " in port:" + port); |
379 | jmachado | 222 | FtpServer ftpServer = new FtpServer(host, port, user, pass); |
223 | servers.put(urlStr, ftpServer); |
||
224 | return ftpServer; |
||
225 | } |
||
226 | catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e) |
||
227 | { |
||
228 | logger.error(e, e); |
||
229 | } |
||
230 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
231 | { |
||
232 | logger.error(e, e); |
||
233 | } |
||
234 | return null; |
||
235 | } |
||
406 | jmachado | 236 | |
1008 | jmachado | 237 | public static final int FTP_DATA_STORE_TIMEOUT_SECONDS = pt.estgp.estgweb.Globals.FTP_DATA_STORE_TIMEOUT_SECONDS; |
238 | public class MyFTPClient extends FTPClient implements 1.5.0/docs/api/java/lang/Runnable.html">Runnable |
||
239 | { |
||
240 | 1.5.0/docs/api/java/lang/String.html">String remote = ""; |
||
241 | 5+0%2Fdocs%2Fapi+InputStream">InputStream local = null; |
||
242 | boolean storeResult = false; |
||
406 | jmachado | 243 | |
1008 | jmachado | 244 | public void run() { |
245 | try { |
||
246 | storeResult = super.storeFile(remote, local); |
||
247 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
248 | logger.error(e,e); |
||
249 | } |
||
250 | } |
||
251 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
252 | public boolean storeFile(1.5.0/docs/api/java/lang/String.html">String remote, 5+0%2Fdocs%2Fapi+InputStream">InputStream local) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
253 | { |
||
1012 | jmachado | 254 | logger.info("FTP store Timeout protection of " + FTP_DATA_STORE_TIMEOUT_SECONDS + " seconds"); |
1008 | jmachado | 255 | this.remote = remote; |
256 | this.local = local; |
||
257 | 1.5.0/docs/api/java/lang/Thread.html">Thread t = new 1.5.0/docs/api/java/lang/Thread.html">Thread(this); |
||
258 | t.start(); |
||
259 | try { |
||
260 | for(int i=0;i<FTP_DATA_STORE_TIMEOUT_SECONDS;i++) |
||
261 | { |
||
262 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(1000); |
||
263 | if(!t.isAlive()) |
||
264 | { |
||
1011 | jmachado | 265 | break; |
1008 | jmachado | 266 | } |
267 | } |
||
1011 | jmachado | 268 | if(t.isAlive()) |
1008 | jmachado | 269 | { |
1011 | jmachado | 270 | logger.info("Timeout expired will interrupt"); |
1008 | jmachado | 271 | t.interrupt(); |
272 | return false; |
||
273 | } |
||
274 | } catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException e) { |
||
275 | logger.error(e,e); |
||
276 | } |
||
277 | return storeResult; |
||
278 | } |
||
279 | } |
||
280 | |||
281 | |||
282 | |||
379 | jmachado | 283 | } |