Rev 1005 | Rev 1085 | 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 | |||
36 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
||
37 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
38 | { |
||
39 | 1.5.0/docs/api/java/lang/String.html">String objectRequired = request.getPathInfo(); |
||
40 | objectRequired = objectRequired.substring(1); |
||
41 | 1.5.0/docs/api/java/lang/String.html">String server = objectRequired.substring(0,objectRequired.indexOf("/")); |
||
42 | 1.5.0/docs/api/java/lang/String.html">String path = objectRequired.substring(objectRequired.indexOf("/")); |
||
43 | |||
1069 | jmachado | 44 | UserSession userSession = UserSessionProxy.loadUserSessionFromRequest(request); |
45 | 1.5.0/docs/api/java/lang/String.html">String username = ConfigProperties.getProperty(server + ".user"); |
||
46 | 1.5.0/docs/api/java/lang/String.html">String passwordUserInSession = ConfigProperties.getProperty(server + ".pass"); |
||
47 | boolean useDefaultCredentialsRead = ConfigProperties.getBooleanProperty(server + ".use.default.credentials.to.read"); |
||
48 | if(!useDefaultCredentialsRead) |
||
49 | { |
||
50 | 1.5.0/docs/api/java/lang/String.html">String passwordUserInSessionCript = (1.5.0/docs/api/java/lang/String.html">String) ((UserSessionImpl)userSession).get(FtpService.FTP_PASSWORD); |
||
51 | if(passwordUserInSession == null || userSession.getUser() == null) |
||
52 | { |
||
53 | logger.warn("There is no user authenticated, is configured to use not default credentials, but will use default password for server " + server); |
||
54 | } |
||
55 | else |
||
56 | { |
||
57 | username = userSession.getUsername(); |
||
58 | passwordUserInSession = DesUtils.getInstance().decrypt(passwordUserInSessionCript); |
||
59 | logger.warn("Will use User Credentials for server " + server + " and user " + username); |
||
60 | } |
||
61 | } |
||
995 | jmachado | 62 | |
63 | |||
64 | if(username == null || passwordUserInSession == null) |
||
65 | { |
||
66 | response.sendError(401); |
||
67 | return; |
||
68 | } |
||
1069 | jmachado | 69 | |
995 | jmachado | 70 | if(path != null) |
71 | { |
||
72 | server = pt.estgp.estgweb.utils.ConfigProperties.getProperty("server." + server); |
||
73 | 1.5.0/docs/api/java/lang/String.html">String remoteName = path.substring(path.lastIndexOf("/")+1); |
||
74 | 1.5.0/docs/api/java/lang/String.html">String remoteDirectory = path.substring(0,path.lastIndexOf("/")); |
||
75 | FtpServer ftpServer = FtpServer.getNewServer(server,username,passwordUserInSession); |
||
76 | FTPClient client = null; |
||
77 | try |
||
78 | { |
||
79 | client = ftpServer.getClient(); |
||
1005 | jmachado | 80 | if(client == null) |
81 | { |
||
82 | logger.warn("###################"); |
||
83 | logger.warn("################### > CANT CONNECT FTP"); |
||
84 | response.sendError(408); |
||
85 | return; |
||
86 | } |
||
995 | jmachado | 87 | |
1005 | jmachado | 88 | |
995 | jmachado | 89 | client.changeWorkingDirectory(remoteDirectory); |
90 | |||
91 | |||
92 | FTPFile[] files = client.listFiles(); |
||
93 | boolean found = false; |
||
94 | for (FTPFile file : files) { |
||
95 | if (!file.isFile() || !file.getName().equals(remoteName)) { |
||
96 | continue; |
||
97 | } |
||
98 | found = true; |
||
99 | client.setFileType(FTP.BINARY_FILE_TYPE); |
||
100 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream = client.retrieveFileStream(file.getName()); |
||
101 | response.setContentLength((int)file.getSize()); |
||
102 | response.setContentType(MimeTypeGuesser.getInstance().guessMimeType(file.getName())); |
||
103 | response.setHeader("Content-disposition","inline; filename=" + file.getName()); |
||
104 | StreamsUtils.inputStream2OutputStream(stream, response.getOutputStream()); |
||
105 | stream.close(); |
||
106 | |||
107 | } |
||
108 | logger.info("Quiting Proxy"); |
||
109 | client.quit(); |
||
110 | client.disconnect(); |
||
111 | logger.info("Finnish Proxy"); |
||
112 | if(!found) |
||
113 | { |
||
114 | response.sendError(404); |
||
115 | return; |
||
116 | } |
||
117 | |||
118 | } |
||
119 | catch(1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
120 | { |
||
121 | if(client != null && client.isConnected()) |
||
122 | { |
||
123 | client.quit(); |
||
124 | client.disconnect(); |
||
125 | } |
||
126 | response.sendError(404); |
||
127 | return; |
||
128 | } |
||
129 | finally { |
||
130 | if(client != null && client.isConnected()) |
||
131 | { |
||
132 | client.quit(); |
||
133 | client.disconnect(); |
||
134 | } |
||
135 | } |
||
136 | logger.info("Closing"); |
||
137 | } |
||
138 | |||
139 | } |
||
140 | |||
141 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
||
142 | /** Handles the HTTP <code>GET</code> method. |
||
143 | * @param request servlet request |
||
144 | * @param response servlet response |
||
145 | */ |
||
146 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
||
147 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
148 | processRequest(request, response); |
||
149 | } |
||
150 | |||
151 | /** Handles the HTTP <code>POST</code> method. |
||
152 | * @param request servlet request |
||
153 | * @param response servlet response |
||
154 | */ |
||
155 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
||
156 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
157 | processRequest(request, response); |
||
158 | } |
||
159 | |||
160 | /** Returns a short description of the servlet. |
||
161 | */ |
||
162 | public 1.5.0/docs/api/java/lang/String.html">String getServletInfo() { |
||
163 | return "Proxy Servlet to get pages form static web page servers"; |
||
164 | } |
||
165 | // </editor-fold> |
||
166 | |||
167 | |||
168 | } |