Rev 1310 | Rev 1782 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
406 | jmachado | 1 | package pt.estgp.estgweb.services.ftpservices; |
2 | |||
904 | jmachado | 3 | import jomm.utils.DesUtils; |
1350 | jmachado | 4 | import jomm.utils.DiacriticFilter; |
904 | jmachado | 5 | import org.apache.commons.fileupload.FileItem; |
6 | import org.apache.commons.net.ftp.FTP; |
||
7 | import org.apache.commons.net.ftp.FTPClient; |
||
8 | import org.apache.log4j.Logger; |
||
9 | import pt.estgp.estgweb.domain.CourseUnitImpl; |
||
10 | import pt.estgp.estgweb.domain.UserSession; |
||
11 | import pt.estgp.estgweb.domain.UserSessionImpl; |
||
489 | jmachado | 12 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
13 | import pt.estgp.estgweb.utils.ConfigProperties; |
||
406 | jmachado | 14 | import pt.estgp.estgweb.web.FtpServer; |
904 | jmachado | 15 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
406 | jmachado | 16 | |
904 | jmachado | 17 | import java.io.File; |
18 | import java.io.IOException; |
||
406 | jmachado | 19 | |
20 | /** |
||
21 | * @author Jorge Machado |
||
22 | * @date 20/Mar/2008 |
||
23 | * @time 18:31:00 |
||
24 | * @see pt.estgp.estgweb.services.email |
||
25 | */ |
||
26 | public class FtpService implements IService |
||
27 | { |
||
28 | |||
29 | 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(FtpService.class); |
||
30 | |||
919 | jmachado | 31 | public static final 1.5.0/docs/api/java/lang/String.html">String FTP_PASSWORD = "pt.estgp.estgweb.services.ftpservices.FtpService.FTP_PASSWORD"; |
406 | jmachado | 32 | |
489 | jmachado | 33 | |
406 | jmachado | 34 | public 1.5.0/docs/api/java/lang/String.html">String run(FtpRequestForm ftpRequestForm, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
35 | { |
||
904 | jmachado | 36 | //userSession.getUser().get |
489 | jmachado | 37 | boolean isStaticAccess = ConfigProperties.getBooleanProperty(ftpRequestForm.getServerConfName()+".use.default.credentials.to.updates.and.deletes"); |
566 | jmachado | 38 | 1.5.0/docs/api/java/lang/String.html">String password = ftpRequestForm.getPassword(); |
39 | 1.5.0/docs/api/java/lang/String.html">String username = ftpRequestForm.getUsername(); |
||
489 | jmachado | 40 | //This only works for Contents of CourseUnits, if user uses SavePassword than the password saved will be used |
41 | if(!ftpRequestForm.isSavePassword() && isStaticAccess) |
||
406 | jmachado | 42 | { |
489 | jmachado | 43 | if(ftpRequestForm.getCourseUnitView() != null) |
44 | { |
||
45 | CourseUnitImpl courseUnit = (CourseUnitImpl) DaoFactory.getCourseUnitDaoImpl().load(ftpRequestForm.getCourseUnitView().getId()); |
||
46 | if(!courseUnit.isOwnedBy(userSession.getUser(),true)) |
||
47 | { |
||
48 | logger.warn("Trying to admin FTP Folder, no permission for username:" + userSession.getUsername()); |
||
49 | return "errors.ftp.no.permission"; |
||
50 | } |
||
51 | username = ConfigProperties.getProperty(ftpRequestForm.getServerConfName() + ".user"); |
||
52 | password = ConfigProperties.getProperty(ftpRequestForm.getServerConfName() + ".pass"); |
||
53 | } |
||
54 | else |
||
55 | return "errors.ftp.no.permission"; |
||
406 | jmachado | 56 | } |
57 | else |
||
58 | { |
||
489 | jmachado | 59 | if(ftpRequestForm.isSavePassword() && ftpRequestForm.getPassword() != null && ftpRequestForm.getPassword().trim().length() > 0) |
60 | { |
||
61 | ((UserSessionImpl)userSession).put(FTP_PASSWORD,DesUtils.getInstance().encrypt(ftpRequestForm.getPassword())); |
||
62 | } |
||
921 | jmachado | 63 | //ESTE CAMPO É COLOCADO EM MEMORIA LOGO NA AUTENTICACAO POR ISSO A POP3 PASS NAO CHEGA A SER USADA |
64 | //E A POP3PASS DAVA PROBLEMAS SE O USER ALGUMA VEZ A TINHA INICIADO |
||
489 | jmachado | 65 | 1.5.0/docs/api/java/lang/String.html">String passwordCode = (1.5.0/docs/api/java/lang/String.html">String) ((UserSessionImpl)userSession).get(FTP_PASSWORD); |
66 | |||
923 | jmachado | 67 | //System.out.println("SAVEDCODE"+passwordCode); |
922 | jmachado | 68 | |
566 | jmachado | 69 | if(password != null && password.trim().length() > 0) |
489 | jmachado | 70 | { |
566 | jmachado | 71 | |
72 | } |
||
1070 | jmachado | 73 | else |
566 | jmachado | 74 | { |
489 | jmachado | 75 | logger.info("using saved password"); |
76 | password = DesUtils.getInstance().decrypt(passwordCode); |
||
923 | jmachado | 77 | //System.out.println("SAVEDPASS:" + password); |
489 | jmachado | 78 | } |
1070 | jmachado | 79 | /*else if(userSession.getUser().getPop3password() != null && userSession.getUser().getPop3password().length() > 0) |
489 | jmachado | 80 | { |
81 | logger.info("trying pop 3 password"); |
||
82 | password = DesUtils.getInstance().decrypt(userSession.getUser().getPop3password()); |
||
83 | } |
||
566 | jmachado | 84 | else |
85 | { |
||
86 | logger.info("trying last used login password"); |
||
87 | password = DesUtils.getInstance().decrypt(userSession.getUser().getPassword()); |
||
1070 | jmachado | 88 | }*/ |
489 | jmachado | 89 | username = ftpRequestForm.getUsername(); |
90 | if(username == null || username.trim().length() == 0) |
||
91 | username = userSession.getUsername(); |
||
406 | jmachado | 92 | } |
93 | FtpServer server = FtpServer.getNewServer(ftpRequestForm.getServerUrl(),username,password); |
||
94 | FTPClient client = server.getClient(); |
||
1005 | jmachado | 95 | if(client == null) |
96 | { |
||
97 | logger.warn("###################"); |
||
98 | logger.warn("################### > CANT CONNECT FTP"); |
||
1286 | jmachado | 99 | return server.getErrorMsg(); |
1005 | jmachado | 100 | } |
101 | |||
406 | jmachado | 102 | 1.5.0/docs/api/java/lang/String.html">String result = "ftp.operation.success"; |
103 | client.setFileType(FTP.BINARY_FILE_TYPE); |
||
1286 | jmachado | 104 | logger.info("changing working path to " + ftpRequestForm.getStartPath()); |
406 | jmachado | 105 | client.changeWorkingDirectory(ftpRequestForm.getStartPath()); |
106 | if(ftpRequestForm.getFilesToImport() != null) |
||
489 | jmachado | 107 | { |
406 | jmachado | 108 | for(FileItem item: ftpRequestForm.getFilesToImport()) |
109 | { |
||
424 | jmachado | 110 | |
421 | jmachado | 111 | 1.5.0/docs/api/java/lang/String.html">String name = item.getName(); |
426 | jmachado | 112 | |
421 | jmachado | 113 | if(name != null) |
114 | { |
||
426 | jmachado | 115 | |
423 | jmachado | 116 | if(name.length() > 2 && name.charAt(1)==':') |
117 | { |
||
426 | jmachado | 118 | |
425 | jmachado | 119 | 1.5.0/docs/api/java/lang/String.html">String[] names = name.split("\\\\"); |
426 | jmachado | 120 | |
423 | jmachado | 121 | name = names[names.length - 1]; |
122 | } |
||
123 | else |
||
124 | { |
||
125 | name = new 1.5.0/docs/api/java/io/File.html">File(item.getName()).getName(); |
||
126 | } |
||
421 | jmachado | 127 | } |
423 | jmachado | 128 | |
1286 | jmachado | 129 | logger.info("trying to import file: " + name); |
1350 | jmachado | 130 | //client.setControlEncoding("ISO-8859-1"); |
131 | if(!client.storeFile(DiacriticFilter.stripAccents(name),item.getInputStream())) |
||
406 | jmachado | 132 | result = "errors.ftp.no.permission"; |
133 | } |
||
134 | } |
||
135 | if(ftpRequestForm.getNewFolderName() != null && ftpRequestForm.getNewFolderName().trim().length() > 0) |
||
136 | { |
||
137 | if(!client.makeDirectory(ftpRequestForm.getNewFolderName())) |
||
138 | result = "errors.ftp.no.permission"; |
||
139 | } |
||
140 | if(ftpRequestForm.getFileToDelete() != null && ftpRequestForm.getFileToDelete().trim().length() > 0) |
||
141 | { |
||
142 | if(!client.deleteFile(ftpRequestForm.getFileToDelete())) |
||
143 | result = "errors.ftp.no.permission"; |
||
144 | } |
||
145 | if(ftpRequestForm.getFolderToDelete() != null && ftpRequestForm.getFolderToDelete().trim().length() > 0) |
||
146 | { |
||
147 | int code = client.rmd(ftpRequestForm.getFolderToDelete()); |
||
148 | if(code != 250) |
||
149 | { |
||
150 | if(code == 550) |
||
151 | return "error.ftp.dir.not.empty"; |
||
152 | else if(code == 530) |
||
153 | return "errors.ftp.no.permission"; |
||
154 | } |
||
155 | |||
156 | } |
||
157 | client.quit(); |
||
158 | client.disconnect(); |
||
159 | return result; |
||
160 | } |
||
161 | } |