Rev 1310 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
995 | jmachado | 1 | package pt.estgp.estgweb.web; |
2 | |||
3 | import jomm.utils.DesUtils; |
||
4 | import jomm.utils.MimeTypeGuesser; |
||
5 | import jomm.utils.StreamsUtils; |
||
6 | import org.apache.commons.net.ftp.FTP; |
||
7 | import org.apache.commons.net.ftp.FTPClient; |
||
8 | import org.apache.commons.net.ftp.FTPFile; |
||
9 | import org.apache.log4j.Logger; |
||
10 | import pt.estgp.estgweb.domain.UserSession; |
||
11 | import pt.estgp.estgweb.domain.UserSessionImpl; |
||
12 | import pt.estgp.estgweb.services.ftpservices.FtpService; |
||
1069 | jmachado | 13 | import pt.estgp.estgweb.utils.ConfigProperties; |
995 | jmachado | 14 | |
15 | import javax.servlet.ServletException; |
||
16 | import javax.servlet.http.HttpServlet; |
||
17 | import javax.servlet.http.HttpServletRequest; |
||
18 | import javax.servlet.http.HttpServletResponse; |
||
19 | import java.io.IOException; |
||
20 | import java.io.InputStream; |
||
21 | |||
22 | /** |
||
23 | * @author Jorge Machado |
||
24 | * @date 26/Fev/2008 |
||
25 | * @time 12:46:16 |
||
26 | * @see pt.estgp.estgweb.web |
||
27 | */ |
||
28 | public class FTPFileProxy extends HttpServlet { |
||
29 | |||
30 | /** sss requests for both HTTP <code>GET</code> and <code>POST</code> methods. |
||
31 | * @param request servlet request |
||
32 | * @param response servlet response |
||
33 | */ |
||
34 | 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(FTPFileProxy.class); |
||
35 | |||
1085 | jmachado | 36 | public static class UserCredentialsForFtp |
995 | jmachado | 37 | { |
1085 | jmachado | 38 | 1.5.0/docs/api/java/lang/String.html">String username; |
39 | 1.5.0/docs/api/java/lang/String.html">String password; |
||
40 | } |
||
995 | jmachado | 41 | |
1085 | jmachado | 42 | /** |
43 | * |
||
44 | * @param server |
||
45 | * @param userSession |
||
46 | * @return CONFERE SITUACAO DE AUTENTICACAO E SERVIDOR e devolve user e pass em uso |
||
47 | */ |
||
48 | |||
49 | public static UserCredentialsForFtp getUserCredentialsForFtp(1.5.0/docs/api/java/lang/String.html">String server, UserSession userSession) |
||
50 | { |
||
51 | UserCredentialsForFtp u = new UserCredentialsForFtp(); |
||
1069 | jmachado | 52 | boolean useDefaultCredentialsRead = ConfigProperties.getBooleanProperty(server + ".use.default.credentials.to.read"); |
1098 | jmachado | 53 | 1.5.0/docs/api/java/lang/String.html">String passwordUserInSessionCript = null; |
54 | if(userSession != null && ((UserSessionImpl)userSession).get(FtpService.FTP_PASSWORD) != null) |
||
55 | passwordUserInSessionCript = (1.5.0/docs/api/java/lang/String.html">String) ((UserSessionImpl)userSession).get(FtpService.FTP_PASSWORD); |
||
1085 | jmachado | 56 | |
57 | if(!useDefaultCredentialsRead && // DEVE USAR AS DO USER |
||
58 | passwordUserInSessionCript != null && userSession.getUser() != null) // E ESTA AUTENTICADO |
||
1069 | jmachado | 59 | { |
1085 | jmachado | 60 | //ESTA AINDA PODE FALHAR AGORA QUANDO TENTAR SE FOR UM USER SEM LIGACAO AO FTP |
61 | //AI TEM DE TENTAR AS DEFAULT SE ESTIVER AUTORIZADO |
||
62 | u.username = userSession.getUsername(); |
||
63 | u.password = DesUtils.getInstance().decrypt(passwordUserInSessionCript); |
||
64 | logger.warn("Will use User Credentials for server " + server + " and user " + u.username); |
||
65 | } |
||
66 | else if(!useDefaultCredentialsRead && // DEVE USAR AS DO USER |
||
67 | (passwordUserInSessionCript == null || userSession.getUser() == null) && //MAS NAO AUTENTICADO |
||
68 | ConfigProperties.getBooleanProperty(server + ".use.default.credentials.to.read.if.user.fail.when.not.authenticated")) // MAS PODE USAR DEFAULTS SEM ESTAR AUTENTICADO |
||
69 | { |
||
70 | logger.warn("There is no user authenticated, is configured to use not default credentials, but will use default password for server " + server); |
||
71 | u.username = ConfigProperties.getProperty(server + ".user"); |
||
72 | u.password = ConfigProperties.getProperty(server + ".pass"); |
||
73 | } |
||
74 | else if(!useDefaultCredentialsRead && // DEVE USAR AS DO USER |
||
75 | (passwordUserInSessionCript == null || userSession.getUser() == null) && //MAS NAO AUTENTICADO |
||
76 | !ConfigProperties.getBooleanProperty(server + ".use.default.credentials.to.read.if.user.fail.when.not.authenticated")) // NO ENTANTO NAO PODE USAR DEFAULTS SEM ESTAR AUTENTICADO |
||
77 | { |
||
78 | u.username = null; |
||
79 | u.password = null; |
||
80 | } |
||
81 | else if(useDefaultCredentialsRead) // DEVE USAR AS DEFAULT SEMPRE |
||
82 | { |
||
83 | u.username = ConfigProperties.getProperty(server + ".user"); |
||
84 | u.password = ConfigProperties.getProperty(server + ".pass"); |
||
85 | } |
||
86 | return u; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param ftpServer |
||
91 | * @param userSession |
||
92 | * @param server |
||
93 | * @param serverUrl |
||
94 | * @return return ftpClient if OK or try to inspect policy of connecting with defaults if authentication fails |
||
95 | * @throws IOException |
||
96 | */ |
||
97 | public static FTPClient getFtpClient(FtpServer ftpServer, UserSession userSession, 1.5.0/docs/api/java/lang/String.html">String server, 1.5.0/docs/api/java/lang/String.html">String serverUrl) |
||
98 | throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
99 | { |
||
100 | FTPClient client = ftpServer.getClient(); |
||
101 | if(client == null) |
||
102 | { |
||
103 | //FALHOU PODE A PASS DO USER NAO DAR POR NAO ESTAR NO FTP |
||
104 | //TEM DE VERIFICAR SE PODE USAR DEFAULTS QUANDO AUTENTICACAO FALHA |
||
1371 | jmachado | 105 | if(userSession != null && userSession.getUsername() != null && |
1085 | jmachado | 106 | ConfigProperties |
107 | .getBooleanProperty( |
||
108 | server + ".use.default.credentials.to.read.if.user.fail.when.authenticated")) |
||
1069 | jmachado | 109 | { |
1085 | jmachado | 110 | // ESTA AUTENTICADO E TENTOU USAR A DO USER FALHOU, OU JA ESTA NA DEFAULT E FALHOU A MESMA |
111 | //COMO PODE TENTAR USAR AS DEFAULTS NESTE CASO VAI TENTAR |
||
112 | logger.warn("###################"); |
||
113 | logger.warn("################### > CANT CONNECT FTP"); |
||
114 | //VELHO response.sendError(408); |
||
115 | //NOVO |
||
116 | logger.info("User cant connect to directory, probably user is not in ftp server, trying defaults"); |
||
117 | ftpServer = FtpServer.getNewServer( |
||
118 | serverUrl |
||
119 | ,ConfigProperties.getProperty(server + ".user") |
||
120 | ,ConfigProperties.getProperty(server + ".pass")); |
||
121 | |||
122 | return ftpServer.getClient(); |
||
1069 | jmachado | 123 | } |
1371 | jmachado | 124 | else if(userSession != null && userSession.getUsername() != null && |
1085 | jmachado | 125 | !ConfigProperties |
126 | .getBooleanProperty( |
||
127 | server + ".use.default.credentials.to.read.if.user.fail.when.authenticated")) |
||
128 | { |
||
129 | // USER AUTENTICADO FALHOU E SEM AUTORIZACAO PARA LIGAR COM AS DEFAULTS |
||
130 | logger.info("System is not authorized to use default password for " + server); |
||
131 | return null; |
||
132 | } |
||
1069 | jmachado | 133 | else |
134 | { |
||
1085 | jmachado | 135 | //NEM HA USER JA DEVIA TER FALHADO ANTES |
136 | logger.info("No user authenticated will return without response"); |
||
137 | return null; |
||
1069 | jmachado | 138 | } |
1085 | jmachado | 139 | |
140 | //velho return; |
||
1069 | jmachado | 141 | } |
1085 | jmachado | 142 | return client; |
143 | } |
||
995 | jmachado | 144 | |
1085 | jmachado | 145 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
146 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
147 | { |
||
148 | 1.5.0/docs/api/java/lang/String.html">String objectRequired = request.getPathInfo(); |
||
149 | objectRequired = objectRequired.substring(1); |
||
150 | 1.5.0/docs/api/java/lang/String.html">String server = objectRequired.substring(0,objectRequired.indexOf("/")); |
||
151 | 1.5.0/docs/api/java/lang/String.html">String path = objectRequired.substring(objectRequired.indexOf("/")); |
||
995 | jmachado | 152 | |
1085 | jmachado | 153 | UserSession userSession = UserSessionProxy.loadUserSessionFromRequest(request); |
154 | |||
155 | |||
156 | //LOGICA DE LIGACAO NESTE METODO |
||
157 | UserCredentialsForFtp u = getUserCredentialsForFtp(server,userSession); |
||
158 | |||
159 | 1.5.0/docs/api/java/lang/String.html">String username = u.username; |
||
160 | 1.5.0/docs/api/java/lang/String.html">String password = u.password; |
||
161 | |||
162 | |||
163 | if(username == null || password == null) //NAO PODE LIGAR |
||
995 | jmachado | 164 | { |
165 | response.sendError(401); |
||
166 | return; |
||
167 | } |
||
1069 | jmachado | 168 | |
995 | jmachado | 169 | if(path != null) |
170 | { |
||
1085 | jmachado | 171 | 1.5.0/docs/api/java/lang/String.html">String serverUrl = pt.estgp.estgweb.utils.ConfigProperties.getProperty("server." + server); |
1371 | jmachado | 172 | 1.5.0/docs/api/java/lang/String.html">String startPath = pt.estgp.estgweb.utils.ConfigProperties.getProperty("server." + server + ".start.path"); |
995 | jmachado | 173 | 1.5.0/docs/api/java/lang/String.html">String remoteName = path.substring(path.lastIndexOf("/")+1); |
174 | 1.5.0/docs/api/java/lang/String.html">String remoteDirectory = path.substring(0,path.lastIndexOf("/")); |
||
1085 | jmachado | 175 | FtpServer ftpServer = FtpServer.getNewServer(serverUrl,username,password); |
995 | jmachado | 176 | FTPClient client = null; |
177 | try |
||
178 | { |
||
1085 | jmachado | 179 | client = getFtpClient(ftpServer,userSession,server,serverUrl); |
1005 | jmachado | 180 | if(client == null) |
181 | { |
||
182 | response.sendError(408); |
||
183 | return; |
||
184 | } |
||
995 | jmachado | 185 | |
1371 | jmachado | 186 | if(startPath.length() > 0) |
187 | { |
||
188 | if(remoteDirectory.startsWith("/")) |
||
189 | remoteDirectory = startPath + remoteDirectory; |
||
190 | else |
||
191 | remoteDirectory = startPath + "/" + remoteDirectory; |
||
192 | } |
||
193 | |||
194 | |||
995 | jmachado | 195 | client.changeWorkingDirectory(remoteDirectory); |
196 | |||
197 | |||
1085 | jmachado | 198 | |
995 | jmachado | 199 | FTPFile[] files = client.listFiles(); |
200 | boolean found = false; |
||
201 | for (FTPFile file : files) { |
||
202 | if (!file.isFile() || !file.getName().equals(remoteName)) { |
||
203 | continue; |
||
204 | } |
||
205 | found = true; |
||
206 | client.setFileType(FTP.BINARY_FILE_TYPE); |
||
207 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream = client.retrieveFileStream(file.getName()); |
||
208 | response.setContentLength((int)file.getSize()); |
||
209 | response.setContentType(MimeTypeGuesser.getInstance().guessMimeType(file.getName())); |
||
1088 | jmachado | 210 | response.setHeader("Content-disposition","attachment; filename=" + file.getName()); |
995 | jmachado | 211 | StreamsUtils.inputStream2OutputStream(stream, response.getOutputStream()); |
212 | stream.close(); |
||
213 | |||
214 | } |
||
215 | logger.info("Quiting Proxy"); |
||
216 | client.quit(); |
||
217 | client.disconnect(); |
||
218 | logger.info("Finnish Proxy"); |
||
219 | if(!found) |
||
220 | { |
||
221 | response.sendError(404); |
||
222 | return; |
||
223 | } |
||
224 | |||
225 | } |
||
226 | catch(1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
227 | { |
||
228 | if(client != null && client.isConnected()) |
||
229 | { |
||
230 | client.quit(); |
||
231 | client.disconnect(); |
||
232 | } |
||
233 | response.sendError(404); |
||
1085 | jmachado | 234 | logger.error(e,e); |
995 | jmachado | 235 | return; |
236 | } |
||
237 | finally { |
||
238 | if(client != null && client.isConnected()) |
||
239 | { |
||
240 | client.quit(); |
||
241 | client.disconnect(); |
||
242 | } |
||
243 | } |
||
244 | logger.info("Closing"); |
||
245 | } |
||
246 | |||
247 | } |
||
248 | |||
249 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
||
250 | /** Handles the HTTP <code>GET</code> method. |
||
251 | * @param request servlet request |
||
252 | * @param response servlet response |
||
253 | */ |
||
254 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
||
255 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
256 | processRequest(request, response); |
||
257 | } |
||
258 | |||
259 | /** Handles the HTTP <code>POST</code> method. |
||
260 | * @param request servlet request |
||
261 | * @param response servlet response |
||
262 | */ |
||
263 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
||
264 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
265 | processRequest(request, response); |
||
266 | } |
||
267 | |||
268 | /** Returns a short description of the servlet. |
||
269 | */ |
||
270 | public 1.5.0/docs/api/java/lang/String.html">String getServletInfo() { |
||
271 | return "Proxy Servlet to get pages form static web page servers"; |
||
272 | } |
||
273 | // </editor-fold> |
||
274 | |||
275 | |||
276 | } |