Rev 1085 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
368 | jmachado | 1 | package pt.estgp.estgweb.web; |
2 | |||
1005 | jmachado | 3 | import jomm.web.ftp.FileType; |
4 | import jomm.web.ftp.IFile; |
||
5 | import jomm.web.ftp.impl.FtpFile; |
||
368 | jmachado | 6 | import org.apache.commons.httpclient.util.URIUtil; |
1005 | jmachado | 7 | import org.apache.commons.net.ftp.FTP; |
379 | jmachado | 8 | import org.apache.commons.net.ftp.FTPClient; |
9 | import org.apache.commons.net.ftp.FTPFile; |
||
1005 | jmachado | 10 | import org.apache.log4j.Logger; |
11 | import org.dom4j.Document; |
||
12 | import org.dom4j.DocumentException; |
||
13 | import org.dom4j.Element; |
||
14 | import pt.estgp.estgweb.domain.UserSession; |
||
15 | import pt.estgp.estgweb.utils.ConfigProperties; |
||
16 | import pt.estgp.estgweb.utils.Dom4jUtil; |
||
17 | import pt.estgp.estgweb.web.exceptions.NotAuthorizedException; |
||
368 | jmachado | 18 | |
1005 | jmachado | 19 | import javax.servlet.ServletException; |
368 | jmachado | 20 | import javax.servlet.http.HttpServlet; |
21 | import javax.servlet.http.HttpServletRequest; |
||
22 | import javax.servlet.http.HttpServletResponse; |
||
23 | import javax.servlet.jsp.JspWriter; |
||
24 | import java.io.*; |
||
25 | import java.net.*; |
||
1005 | jmachado | 26 | import java.util.Date; |
27 | import java.util.HashMap; |
||
28 | import java.util.List; |
||
29 | import java.util.Map; |
||
368 | jmachado | 30 | import java.util.zip.GZIPInputStream; |
31 | import java.util.zip.InflaterInputStream; |
||
1005 | jmachado | 32 | import java.util.zip.ZipInputStream; |
368 | jmachado | 33 | |
34 | /** |
||
35 | * @author Jorge Machado |
||
36 | * @date 26/Fev/2008 |
||
37 | * @time 12:46:16 |
||
38 | * @see pt.estgp.estgweb.web |
||
39 | */ |
||
40 | public class DirWebProxy extends HttpServlet |
||
41 | { |
||
42 | |||
406 | jmachado | 43 | public static final 1.5.0/docs/api/java/lang/String.html">String FTP_ACTUAL_PATH_KEY = "pt.estgp.estgweb.web.DirWebProxy.FTP_ACTUAL_PATH_KEY"; |
44 | |||
368 | jmachado | 45 | /** |
851 | jmachado | 46 | * sss requests for both HTTP <code>GET</code> and <code>POST</code> methods. |
368 | jmachado | 47 | * |
48 | * @param request servlet request |
||
49 | * @param response servlet response |
||
50 | */ |
||
51 | |||
52 | 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(DirWebProxy.class); |
||
53 | |||
54 | |||
55 | protected static void processRequest(HttpServletRequest request, HttpServletResponse response) |
||
56 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
57 | { |
||
58 | |||
59 | 1.5.0/docs/api/java/lang/String.html">String element = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("element"); |
||
379 | jmachado | 60 | if (element.equals("node")) |
368 | jmachado | 61 | { |
379 | jmachado | 62 | processNode(request, response); |
368 | jmachado | 63 | } |
64 | else |
||
65 | { |
||
379 | jmachado | 66 | processLeaf(request, response); |
368 | jmachado | 67 | } |
68 | } |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @param request |
||
73 | * @param response |
||
74 | * @throws ServletException |
||
75 | * @throws IOException |
||
76 | */ |
||
77 | |||
78 | protected static void processNode(HttpServletRequest request, HttpServletResponse response) |
||
79 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
80 | { |
||
379 | jmachado | 81 | styleDirectory(request, response); |
368 | jmachado | 82 | } |
83 | |||
84 | protected static void processLeaf(HttpServletRequest request, HttpServletResponse response) |
||
85 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
86 | { |
||
87 | 1.5.0/docs/api/java/lang/String.html">String path = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("path"); |
||
379 | jmachado | 88 | 1.5.0/docs/api/java/lang/String.html">String startPath = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("startPath"); |
89 | 1.5.0/docs/api/java/lang/String.html">String leafUrl = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("leaf.url"); |
||
368 | jmachado | 90 | 1.5.0/docs/api/java/lang/String.html">String server = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("server"); |
91 | 1.5.0/docs/api/java/lang/String.html">String useOutputStream = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("useOutputStream"); |
||
92 | 1.5.0/docs/api/java/lang/String.html">String encoding = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("encoding"); |
||
379 | jmachado | 93 | 1.5.0/docs/api/java/lang/String.html">String proxyKey = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("proxyKey"); |
368 | jmachado | 94 | 1.5.0/docs/api/java/lang/String.html">String proxyHost = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("proxy.host"); |
95 | 1.5.0/docs/api/java/lang/String.html">String proxyPort = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("proxy.port"); |
||
96 | boolean style = 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean((1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("style")); |
||
97 | 1.5.0/docs/api/java/lang/String.html">String xml = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("xml"); |
||
98 | 1.5.0/docs/api/java/lang/String.html">String label = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("label"); |
||
99 | |||
100 | logger.info("server:" + server); |
||
101 | logger.info("path:" + path); |
||
874 | jmachado | 102 | logger.info("startPath:" + startPath); |
379 | jmachado | 103 | logger.info("proxyKey:" + proxyKey); |
368 | jmachado | 104 | logger.info("proxyHost:" + proxyHost); |
105 | logger.info("proxyPort:" + proxyPort); |
||
106 | logger.info("xml:" + xml); |
||
107 | logger.info("style:" + style); |
||
108 | logger.info("label:" + label); |
||
874 | jmachado | 109 | logger.info("useOutputStream:" + useOutputStream); |
368 | jmachado | 110 | |
111 | if (path != null) |
||
112 | { |
||
113 | try |
||
114 | { |
||
115 | 5+0%2Fdocs%2Fapi+Proxy">Proxy proxy = null; |
||
116 | if (proxyHost != null && proxyHost.length() > 0) |
||
117 | { |
||
118 | proxy = new 5+0%2Fdocs%2Fapi+Proxy">Proxy(5+0%2Fdocs%2Fapi+Proxy">Proxy.1.5.0/docs/api/java/lang/reflect/Type.html">Type.HTTP, new 1.5.0/docs/api/java/net/InetSocketAddress.html">InetSocketAddress(proxyHost, 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(proxyPort))); |
||
119 | } |
||
120 | |||
121 | |||
122 | |||
385 | jmachado | 123 | |
124 | if (!server.startsWith("ftp://")) |
||
379 | jmachado | 125 | { |
619 | jmachado | 126 | if(path.equals(startPath) ) |
385 | jmachado | 127 | path += leafUrl; |
619 | jmachado | 128 | 1.5.0/docs/api/java/lang/String.html">String finalURL; |
129 | if(leafUrl.indexOf("?")>0) |
||
130 | finalURL = server + leafUrl; |
||
131 | else |
||
132 | finalURL = server + URIUtil.encodePath(path, "ISO-8859-1"); |
||
379 | jmachado | 133 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(finalURL); |
368 | jmachado | 134 | |
379 | jmachado | 135 | if (useOutputStream != null && useOutputStream.equals("true")) |
368 | jmachado | 136 | { |
379 | jmachado | 137 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream = openStream(finalURL, proxy); |
138 | 1.5.0/docs/api/java/net/URLConnection.html">URLConnection con = url.openConnection(); |
||
139 | response.setContentType(con.getContentType()); |
||
140 | if (xml == null || xml.equals("false")) |
||
141 | response.setCharacterEncoding(encoding); |
||
142 | response.setContentLength(con.getContentLength()); |
||
143 | writeDirectStream(response, stream); |
||
144 | } |
||
145 | else if (xml == null || xml.equals("false")) |
||
146 | { |
||
147 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream = openStream(finalURL, proxy); |
||
148 | 1.5.0/docs/api/java/lang/String.html">String content = readContent2String(stream, encoding); |
||
149 | generateOutputLeaf(response, request, content, style); |
||
150 | } |
||
151 | else |
||
152 | { |
||
153 | 1.5.0/docs/api/java/net/URLConnection.html">URLConnection con = new 1.5.0/docs/api/java/net/URL.html">URL(finalURL).openConnection(); |
||
154 | con.connect(); |
||
155 | if (con.getContentLength() > 0) |
||
156 | { |
||
368 | jmachado | 157 | // Document dom = Dom4jUtil.parse(stream); |
379 | jmachado | 158 | generateOutputLeafXml(response, request, finalURL, proxy, style); |
159 | } |
||
368 | jmachado | 160 | } |
161 | } |
||
379 | jmachado | 162 | else |
163 | { |
||
164 | 5+0%2Fdocs%2Fapi+Document">Document directoryDocument = (5+0%2Fdocs%2Fapi+Document">Document) request.getAttribute("directoryDocument"); |
||
380 | jmachado | 165 | if(leafUrl != null && leafUrl.length() > 0) |
166 | { |
||
167 | 1.5.0/docs/api/java/lang/String.html">String endPath = ""; |
||
168 | if(startPath != null) |
||
169 | { |
||
170 | endPath = path.substring(startPath.length()); |
||
171 | } |
||
1069 | jmachado | 172 | if(leafUrl.endsWith("/") && endPath.startsWith("/")) |
173 | { |
||
174 | path = startPath + leafUrl.substring(0,leafUrl.length()-1) + endPath; |
||
175 | } |
||
176 | else |
||
177 | path = startPath + leafUrl + endPath; |
||
380 | jmachado | 178 | } |
1005 | jmachado | 179 | if(!extendDiretoryDocument(request, response, path, proxyKey, server, startPath + leafUrl, directoryDocument)) |
180 | return; |
||
390 | jmachado | 181 | |
379 | jmachado | 182 | styleDirectory(request, response); |
183 | } |
||
368 | jmachado | 184 | } |
697 | jmachado | 185 | catch (NotAuthorizedException e) |
186 | { |
||
187 | UserSession userSession = UserSessionProxy.loadUserSessionFromRequest(request); |
||
188 | 1.5.0/docs/api/java/lang/String.html">String username = userSession != null ? userSession.getUsername() : "UNKNOWN"; |
||
874 | jmachado | 189 | logger.warn(username + ":" + request.getRemoteAddr() + " cant access this area server:" + server + " and path:" + path + " " + request.getRemoteUser() + " " + request.getRequestURL() + " spath " + request.getServletPath() + " queryString " + request.getQueryString() ,e); |
697 | jmachado | 190 | response.sendError(401); |
191 | } |
||
368 | jmachado | 192 | catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e) |
193 | { |
||
706 | jmachado | 194 | |
195 | logger.warn("url not found for server:" + server + " and path:" + path + " " + "REMOTE USER" + request.getRemoteUser() + " " + request.getRequestURL()); |
||
368 | jmachado | 196 | response.sendError(404); |
197 | } |
||
198 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) |
||
199 | { |
||
884 | jmachado | 200 | logger.error(e, e); |
368 | jmachado | 201 | throw new 1.5.0/docs/api/java/io/IOException.html">IOException(e.toString()); |
202 | } |
||
203 | } |
||
204 | else |
||
205 | { |
||
206 | logger.warn("url not found for server:" + server + " and path:" + path); |
||
207 | response.sendError(404); |
||
208 | } |
||
379 | jmachado | 209 | } |
368 | jmachado | 210 | |
379 | jmachado | 211 | |
212 | public static 1.5.0/docs/api/java/lang/String.html">String FTP_FILE_PREFIX = "FTP_"; |
||
213 | |||
214 | public static void addAttribute(5+0%2Fdocs%2Fapi+Element">Element element, 1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/String.html">String value) |
||
215 | { |
||
380 | jmachado | 216 | element.addAttribute(name, value); |
368 | jmachado | 217 | } |
218 | |||
219 | |||
1085 | jmachado | 220 | protected static boolean extendDiretoryDocument(HttpServletRequest request,HttpServletResponse response, 1.5.0/docs/api/java/lang/String.html">String path, 1.5.0/docs/api/java/lang/String.html">String server, 1.5.0/docs/api/java/lang/String.html">String serverUrl, 1.5.0/docs/api/java/lang/String.html">String startPath, 5+0%2Fdocs%2Fapi+Document">Document directoryDocument) |
379 | jmachado | 221 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
368 | jmachado | 222 | { |
406 | jmachado | 223 | |
379 | jmachado | 224 | try |
368 | jmachado | 225 | { |
1085 | jmachado | 226 | UserSession userSession = UserSessionProxy.loadUserSessionFromRequest(request); |
227 | FTPFileProxy.UserCredentialsForFtp u = FTPFileProxy.getUserCredentialsForFtp(server, userSession); |
||
228 | |||
229 | 1.5.0/docs/api/java/lang/String.html">String username = u.username; |
||
230 | 1.5.0/docs/api/java/lang/String.html">String password = u.password; |
||
231 | |||
232 | if(username == null || password == null) //NAO PODE LIGAR |
||
1069 | jmachado | 233 | { |
1085 | jmachado | 234 | response.sendError(401); |
235 | return false; |
||
1069 | jmachado | 236 | } |
1085 | jmachado | 237 | |
379 | jmachado | 238 | |
239 | if (path != null) |
||
240 | { |
||
1085 | jmachado | 241 | FtpServer ftpServer = FtpServer.getServer(serverUrl, username, password); |
242 | FTPClient client = FTPFileProxy.getFtpClient(ftpServer, userSession, server, serverUrl); |
||
1005 | jmachado | 243 | if(client == null) |
244 | { |
||
245 | return false; |
||
246 | } |
||
1085 | jmachado | 247 | |
406 | jmachado | 248 | |
379 | jmachado | 249 | 5+0%2Fdocs%2Fapi+Element">Element actualNode = (5+0%2Fdocs%2Fapi+Element">Element) request.getAttribute("actualNode"); |
250 | |||
251 | if(startPath.equals("/")) |
||
252 | startPath = ""; |
||
406 | jmachado | 253 | request.setAttribute(FTP_ACTUAL_PATH_KEY,startPath); |
379 | jmachado | 254 | if (path.equals(startPath)) |
255 | { |
||
697 | jmachado | 256 | if(!client.changeWorkingDirectory(path)) |
257 | { |
||
258 | throw new NotAuthorizedException(); |
||
259 | } |
||
379 | jmachado | 260 | FTPFile[] files = client.listFiles(); |
1085 | jmachado | 261 | List<IFile> iFiles = FtpFile.init(files, serverUrl, path); |
379 | jmachado | 262 | //root |
263 | for (IFile iFile : iFiles) |
||
264 | { |
||
380 | jmachado | 265 | 5+0%2Fdocs%2Fapi+Element">Element iFileElem = actualNode.addElement("leaf"); |
1067 | jmachado | 266 | addAttribute(iFileElem, "node", "false"); |
379 | jmachado | 267 | addAttribute(iFileElem, "id", FTP_FILE_PREFIX + "0_" + iFile.getName()); |
380 | jmachado | 268 | addAttribute(iFileElem, "idPrefixSize","" + (FTP_FILE_PREFIX + "0_").length()); |
269 | |||
379 | jmachado | 270 | addAttribute(iFileElem, "label", iFile.getName()); |
271 | addAttribute(iFileElem, "visualization", "filesystem"); |
||
272 | addAttribute(iFileElem, "size", "" + iFile.getSize()); |
||
273 | addAttribute(iFileElem, "lastModification", "" + iFile.getLastUpdate().toString()); |
||
274 | if (iFile.getType() == FileType.DIRECTORY) |
||
275 | { |
||
276 | addAttribute(iFileElem, "ftpDirectory", "true"); |
||
277 | } |
||
278 | else |
||
279 | { |
||
392 | jmachado | 280 | addAttribute(iFileElem, "extension", iFile.getExtension()); |
379 | jmachado | 281 | addAttribute(iFileElem, "ftpDirectory", "false"); |
282 | addAttribute(iFileElem, "absolute", "true"); |
||
283 | addAttribute(iFileElem, "url", iFile.getCompleteUrl()); |
||
284 | } |
||
285 | } |
||
286 | } |
||
287 | else |
||
288 | { |
||
289 | |||
290 | 1.5.0/docs/api/java/lang/String.html">String pathAux = path.substring(startPath.length()); |
||
380 | jmachado | 291 | 1.5.0/docs/api/java/lang/String.html">String[] pathFragments; |
1069 | jmachado | 292 | if(pathAux.startsWith("/")) |
380 | jmachado | 293 | pathFragments = pathAux.substring(1).split("/"); |
294 | else |
||
295 | pathFragments = pathAux.split("/"); |
||
379 | jmachado | 296 | |
380 | jmachado | 297 | |
700 | jmachado | 298 | if(!client.changeWorkingDirectory(startPath)) |
299 | { |
||
300 | throw new NotAuthorizedException(); |
||
301 | } |
||
379 | jmachado | 302 | FTPFile[] files = client.listFiles(); |
1085 | jmachado | 303 | List<IFile> iFiles = FtpFile.init(files, serverUrl, path); |
379 | jmachado | 304 | 5+0%2Fdocs%2Fapi+Element">Element goDeepElement = null; |
380 | jmachado | 305 | 1.5.0/docs/api/java/lang/String.html">String actualId = null; |
379 | jmachado | 306 | for (IFile iFile : iFiles) |
307 | { |
||
1067 | jmachado | 308 | 5+0%2Fdocs%2Fapi+Element">Element iFileElem = actualNode.addElement("leaf"); |
309 | addAttribute(iFileElem, "node", "true"); |
||
379 | jmachado | 310 | addAttribute(iFileElem, "id", FTP_FILE_PREFIX + "0_" + iFile.getName()); |
380 | jmachado | 311 | addAttribute(iFileElem, "idPrefixSize","" + (FTP_FILE_PREFIX + "0_").length()); |
312 | |||
379 | jmachado | 313 | addAttribute(iFileElem, "label", iFile.getName()); |
314 | addAttribute(iFileElem, "visualization", "filesystem"); |
||
315 | addAttribute(iFileElem, "size", "" + iFile.getSize()); |
||
316 | addAttribute(iFileElem, "lastModification", "" + iFile.getLastUpdate().toString()); |
||
317 | if (iFile.getType() == FileType.DIRECTORY) |
||
318 | { |
||
319 | addAttribute(iFileElem, "ftpDirectory", "true"); |
||
320 | } |
||
321 | else |
||
322 | { |
||
392 | jmachado | 323 | addAttribute(iFileElem, "extension", iFile.getExtension()); |
379 | jmachado | 324 | addAttribute(iFileElem, "ftpDirectory", "false"); |
325 | addAttribute(iFileElem, "absolute", "true"); |
||
326 | addAttribute(iFileElem, "url", iFile.getCompleteUrl()); |
||
327 | } |
||
328 | |||
329 | if (iFile.getName().equals(pathFragments[0])) |
||
380 | jmachado | 330 | { |
331 | actualId = FTP_FILE_PREFIX + "0_" + iFile.getName(); |
||
379 | jmachado | 332 | goDeepElement = iFileElem; |
380 | jmachado | 333 | } |
379 | jmachado | 334 | } |
335 | |||
336 | |||
337 | |||
380 | jmachado | 338 | 1.5.0/docs/api/java/lang/String.html">String startPathAux = startPath; |
339 | |||
379 | jmachado | 340 | for (int i = 0; i < pathFragments.length; i++) |
341 | { |
||
342 | 5+0%2Fdocs%2Fapi+Element">Element nextGoDeepElement = null; |
||
343 | 1.5.0/docs/api/java/lang/String.html">String d = pathFragments[i]; |
||
380 | jmachado | 344 | startPathAux = startPathAux + "/" + d; |
706 | jmachado | 345 | if(!client.changeWorkingDirectory(startPathAux)) |
700 | jmachado | 346 | { |
347 | throw new NotAuthorizedException(); |
||
348 | } |
||
349 | |||
379 | jmachado | 350 | files = client.listFiles(); |
1085 | jmachado | 351 | iFiles = FtpFile.init(files, serverUrl, path); |
380 | jmachado | 352 | |
379 | jmachado | 353 | for (IFile iFile : iFiles) |
354 | { |
||
355 | 5+0%2Fdocs%2Fapi+Element">Element iFileElem; |
||
356 | |||
357 | if(i == pathFragments.length - 1) |
||
1067 | jmachado | 358 | { |
380 | jmachado | 359 | iFileElem = goDeepElement.addElement("leaf"); |
1067 | jmachado | 360 | addAttribute(iFileElem, "node","false"); |
361 | } |
||
379 | jmachado | 362 | else |
1067 | jmachado | 363 | { |
364 | iFileElem = goDeepElement.addElement("leaf"); |
||
365 | addAttribute(iFileElem, "node","true"); |
||
366 | } |
||
379 | jmachado | 367 | |
368 | addAttribute(iFileElem, "id", FTP_FILE_PREFIX + (i+1) + "_" + iFile.getName()); |
||
380 | jmachado | 369 | addAttribute(iFileElem, "idPrefixSize","" + (FTP_FILE_PREFIX + (i+1) + "_").length()); |
379 | jmachado | 370 | addAttribute(iFileElem, "label", iFile.getName()); |
371 | addAttribute(iFileElem, "visualization", "filesystem"); |
||
372 | addAttribute(iFileElem, "size", "" + iFile.getSize()); |
||
373 | addAttribute(iFileElem, "lastModification", "" + iFile.getLastUpdate().toString()); |
||
374 | if (iFile.getType() == FileType.DIRECTORY) |
||
375 | { |
||
376 | addAttribute(iFileElem, "ftpDirectory", "true"); |
||
377 | } |
||
378 | else |
||
379 | { |
||
380 | jmachado | 380 | addAttribute(iFileElem, "extension", iFile.getExtension()); |
379 | jmachado | 381 | addAttribute(iFileElem, "ftpDirectory", "false"); |
382 | addAttribute(iFileElem, "absolute", "true"); |
||
383 | addAttribute(iFileElem, "url", iFile.getCompleteUrl()); |
||
384 | } |
||
385 | |||
386 | if (i < pathFragments.length - 1 && iFile.getName().equals(pathFragments[i + 1])) |
||
380 | jmachado | 387 | { |
379 | jmachado | 388 | nextGoDeepElement = iFileElem; |
380 | jmachado | 389 | actualId = FTP_FILE_PREFIX + (i+1) + "_" + iFile.getName(); |
390 | } |
||
379 | jmachado | 391 | } |
392 | goDeepElement = nextGoDeepElement; |
||
393 | } |
||
380 | jmachado | 394 | request.setAttribute("ACTUAL_REMOTE_CHILD",actualId); |
379 | jmachado | 395 | } |
396 | client.quit(); |
||
397 | client.disconnect(); |
||
398 | } |
||
399 | else |
||
400 | { |
||
1085 | jmachado | 401 | throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + serverUrl + " and path:" + path); |
379 | jmachado | 402 | } |
403 | } |
||
404 | catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
405 | { |
||
698 | jmachado | 406 | if(e instanceof NotAuthorizedException) |
407 | throw (NotAuthorizedException)e; |
||
379 | jmachado | 408 | logger.error(e, e); |
409 | } |
||
1005 | jmachado | 410 | return true; |
379 | jmachado | 411 | } |
412 | |||
413 | |||
414 | private static void generateOutputLeafXml(HttpServletResponse response, HttpServletRequest request, 1.5.0/docs/api/java/lang/String.html">String finalURL, 5+0%2Fdocs%2Fapi+Proxy">Proxy proxy, boolean style) throws 1.5.0/docs/api/java/io/IOException.html">IOException, DocumentException |
||
415 | { |
||
416 | if (style) |
||
417 | { |
||
418 | Map<String, Object> parameters = new HashMap<String, Object>(); |
||
368 | jmachado | 419 | // OutputFormat outputFormat = new OutputFormat(); |
420 | // outputFormat.setSuppressDeclaration(true); |
||
421 | // outputFormat.setEncoding("UTF-8"); |
||
422 | // StringWriter writer = new StringWriter(); |
||
423 | // Dom4jUtil.write(dom,writer, outputFormat); |
||
424 | // org.w3c.dom.Document d = new org.dom4j.io.DOMWriter().write(dom); |
||
425 | // parameters.put("body",dom.selectSingleNode("/")); |
||
379 | jmachado | 426 | parameters.put("includeXML", finalURL); |
427 | styleDirectory(request, response, parameters, null); |
||
368 | jmachado | 428 | } |
429 | else |
||
430 | { |
||
379 | jmachado | 431 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream = openStream(finalURL, proxy); |
368 | jmachado | 432 | 5+0%2Fdocs%2Fapi+Document">Document dom = Dom4jUtil.parse(stream); |
379 | jmachado | 433 | Dom4jUtil.write(dom, response.getWriter()); |
368 | jmachado | 434 | } |
435 | } |
||
436 | |||
379 | jmachado | 437 | private static void generateOutputLeaf(HttpServletResponse response, HttpServletRequest request, 1.5.0/docs/api/java/lang/String.html">String content, boolean style) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
368 | jmachado | 438 | { |
379 | jmachado | 439 | if (style) |
368 | jmachado | 440 | { |
441 | // Map<String,Object> parameters = new HashMap<String,Object>(); |
||
442 | // parameters.put("body",content); |
||
379 | jmachado | 443 | styleDirectory(request, response, new HashMap<String, Object>(), content); |
368 | jmachado | 444 | } |
445 | else |
||
446 | { |
||
447 | response.getWriter().write(content); |
||
448 | } |
||
449 | } |
||
450 | |||
451 | private static void styleDirectory(HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
452 | { |
||
379 | jmachado | 453 | styleDirectory(request, response, new HashMap<String, Object>(), null); |
368 | jmachado | 454 | } |
455 | |||
380 | jmachado | 456 | // private static String normalizeXmlId(String id) |
457 | // { |
||
458 | // StringBuilder strBuilder = new StringBuilder(); |
||
459 | // strBuilder.append('_'); |
||
460 | // for(int i = 0; i < id.length();i++) |
||
461 | // { |
||
462 | // char c = id.charAt(i); |
||
463 | // if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) |
||
464 | // { |
||
465 | // strBuilder.append(c); |
||
466 | // } |
||
467 | // else |
||
468 | // { |
||
469 | // strBuilder.append('.'); |
||
470 | // } |
||
471 | // } |
||
472 | // return strBuilder.toString(); |
||
473 | // } |
||
474 | |||
379 | jmachado | 475 | private static void styleDirectory(HttpServletRequest request, HttpServletResponse response, Map<String, Object> parameters, 1.5.0/docs/api/java/lang/String.html">String bodyContent) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
368 | jmachado | 476 | { |
477 | 1.5.0/docs/api/java/lang/String.html">String stylesheet = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("stylesheet"); |
||
478 | 5+0%2Fdocs%2Fapi+Document">Document directoryDocument = (5+0%2Fdocs%2Fapi+Document">Document) request.getAttribute("directoryDocument"); |
||
479 | 1.5.0/docs/api/java/lang/String.html">String actualNodeId = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("actualNodeId"); |
||
379 | jmachado | 480 | 1.5.0/docs/api/java/lang/String.html">String actualRemoteNodeId = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("ACTUAL_REMOTE_CHILD"); |
368 | jmachado | 481 | UserSession userSession = UserSessionProxy.loadUserSessionFromRequest(request); |
884 | jmachado | 482 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Using stylesheet = " + stylesheet); |
368 | jmachado | 483 | |
379 | jmachado | 484 | parameters.put("contextPath", request.getContextPath()); |
668 | jmachado | 485 | 1.5.0/docs/api/java/lang/String.html">String username = "descolnhecido"; |
486 | 1.5.0/docs/api/java/lang/String.html">String roles = ""; |
||
487 | 1.5.0/docs/api/java/lang/String.html">String name = username; |
||
488 | |||
489 | if (userSession != null && userSession.getUsername() != null) |
||
490 | username = userSession.getUsername(); |
||
491 | if (userSession != null && userSession.getName() != null) |
||
492 | name = userSession.getName(); |
||
493 | if (userSession != null && userSession.getUser() != null) |
||
494 | roles = userSession.getUser().getRoles(); |
||
495 | |||
496 | parameters.put("username", username); |
||
497 | parameters.put("name", name); |
||
498 | parameters.put("roles", roles); |
||
499 | |||
379 | jmachado | 500 | parameters.put("ACTUAL", actualNodeId); |
501 | parameters.put("ACTUAL_REMOTE_CHILD", actualRemoteNodeId); |
||
368 | jmachado | 502 | |
503 | try |
||
504 | { |
||
379 | jmachado | 505 | 1.5.0/docs/api/java/lang/String.html">String html = Dom4jUtil.styleDocument(directoryDocument, stylesheet, parameters); |
506 | if (bodyContent != null) |
||
507 | html = html.replace("@@BODY_CONTENT@@", bodyContent); |
||
368 | jmachado | 508 | response.getWriter().write(html); |
509 | } |
||
510 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) |
||
511 | { |
||
379 | jmachado | 512 | logger.error(e, e); |
368 | jmachado | 513 | response.sendError(500); |
514 | } |
||
515 | } |
||
516 | |||
517 | private static 1.5.0/docs/api/java/lang/String.html">String readContent2String(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 1.5.0/docs/api/java/lang/String.html">String encoding) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
518 | { |
||
519 | byte[] buf = new byte[1024]; |
||
520 | int readedBytes; |
||
521 | 1.5.0/docs/api/java/io/StringWriter.html">StringWriter writer = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter(); |
||
522 | while ((readedBytes = stream.read(buf)) > 0) |
||
523 | { |
||
524 | 1.5.0/docs/api/java/lang/String.html">String s = new 1.5.0/docs/api/java/lang/String.html">String(buf, 0, readedBytes, encoding); |
||
525 | writer.write(s); |
||
526 | } |
||
527 | return writer.toString(); |
||
528 | } |
||
529 | |||
530 | private static void writeDirectStream(HttpServletResponse response, 5+0%2Fdocs%2Fapi+InputStream">InputStream stream) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
531 | { |
||
532 | byte[] buf = new byte[1024]; |
||
533 | int readedBytes; |
||
534 | 5+0%2Fdocs%2Fapi+OutputStream">OutputStream os = response.getOutputStream(); |
||
535 | while ((readedBytes = stream.read(buf)) > 0) |
||
536 | { |
||
537 | os.write(buf, 0, readedBytes); |
||
538 | } |
||
539 | os.close(); |
||
540 | } |
||
541 | |||
542 | private static 5+0%2Fdocs%2Fapi+InputStream">InputStream openStream(1.5.0/docs/api/java/lang/String.html">String requestURL, 5+0%2Fdocs%2Fapi+Proxy">Proxy proxy) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
543 | { |
||
544 | 5+0%2Fdocs%2Fapi+InputStream">InputStream in = null; |
||
545 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(requestURL); |
||
546 | |||
547 | 1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection con = null; |
||
548 | int responseCode = 0; |
||
549 | do |
||
550 | { |
||
551 | if (proxy != null) |
||
552 | con = (1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection) url.openConnection(proxy); |
||
553 | else |
||
554 | con = (1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection) url.openConnection(); |
||
555 | con.setRequestProperty("User-Agent", "ESTG-Baco/1.0"); |
||
556 | con.setRequestProperty("Accept-Encoding", "compress, gzip, identify"); |
||
557 | try |
||
558 | { |
||
559 | responseCode = con.getResponseCode(); |
||
560 | logger.debug("responseCode=" + responseCode); |
||
561 | } |
||
562 | catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e) |
||
563 | { |
||
564 | // assume it's a 503 response |
||
565 | logger.info(requestURL, e); |
||
566 | responseCode = 1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection.HTTP_UNAVAILABLE; |
||
567 | } |
||
568 | |||
569 | if (responseCode == 1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection.HTTP_UNAVAILABLE) |
||
570 | { |
||
571 | long retrySeconds = con.getHeaderFieldInt("Retry-After", -1); |
||
572 | if (retrySeconds == -1) |
||
573 | { |
||
574 | long now = (new 5+0%2Fdocs%2Fapi+Date">Date()).getTime(); |
||
575 | long retryDate = con.getHeaderFieldDate("Retry-After", now); |
||
576 | retrySeconds = retryDate - now; |
||
577 | } |
||
578 | if (retrySeconds == 0) |
||
579 | { // Apparently, it's a bad URL |
||
580 | throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("Bad URL?"); |
||
581 | } |
||
582 | logger.error("Server response: Retry-After=" + retrySeconds); |
||
583 | if (retrySeconds > 0) |
||
584 | { |
||
585 | try |
||
586 | { |
||
587 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(retrySeconds * 1000); |
||
588 | } |
||
589 | catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException ex) |
||
590 | { |
||
591 | ex.printStackTrace(); |
||
592 | } |
||
593 | } |
||
594 | } |
||
595 | } |
||
596 | while (responseCode == 1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection.HTTP_UNAVAILABLE); |
||
597 | 1.5.0/docs/api/java/lang/String.html">String contentEncoding = con.getHeaderField("Content-Encoding"); |
||
598 | logger.debug("contentEncoding=" + contentEncoding); |
||
599 | if ("compress".equals(contentEncoding)) |
||
600 | { |
||
601 | 1.5.0/docs/api/java/util/zip/ZipInputStream.html">ZipInputStream zis = new 1.5.0/docs/api/java/util/zip/ZipInputStream.html">ZipInputStream(con.getInputStream()); |
||
602 | zis.getNextEntry(); |
||
603 | in = zis; |
||
604 | } |
||
605 | else if ("gzip".equals(contentEncoding)) |
||
606 | { |
||
607 | in = new 1.5.0/docs/api/java/util/zip/GZIPInputStream.html">GZIPInputStream(con.getInputStream()); |
||
608 | } |
||
609 | else if ("deflate".equals(contentEncoding)) |
||
610 | { |
||
611 | in = new 1.5.0/docs/api/java/util/zip/InflaterInputStream.html">InflaterInputStream(con.getInputStream()); |
||
612 | } |
||
613 | else |
||
614 | { |
||
615 | in = con.getInputStream(); |
||
616 | } |
||
617 | return in; |
||
618 | } |
||
619 | |||
620 | public static void processHttpRequestService(JspWriter out, 1.5.0/docs/api/java/lang/String.html">String server, 1.5.0/docs/api/java/lang/String.html">String path) throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException |
||
621 | { |
||
622 | 1.5.0/docs/api/java/lang/String.html">String serverUrl = ConfigProperties.getProperty("server." + server); |
||
623 | 1.5.0/docs/api/java/lang/String.html">String serverEncoding = ConfigProperties.getProperty(server + ".encoding"); |
||
624 | 1.5.0/docs/api/java/lang/String.html">String confStartPath = ConfigProperties.getProperty("server." + server + ".start.path"); |
||
625 | if (confStartPath != null) |
||
626 | path = confStartPath + path; |
||
627 | |||
628 | |||
629 | try |
||
630 | { |
||
631 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(serverUrl + URIUtil.encodePath(path, "ISO-8859-1")); |
||
632 | 1.5.0/docs/api/java/net/URLConnection.html">URLConnection con = url.openConnection(); |
||
633 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream = con.getInputStream(); |
||
634 | |||
635 | byte[] buf = new byte[1024]; |
||
636 | int readedBytes; |
||
637 | |||
638 | while ((readedBytes = stream.read(buf)) > 0) |
||
639 | { |
||
640 | 1.5.0/docs/api/java/lang/String.html">String str = new 1.5.0/docs/api/java/lang/String.html">String(buf, 0, readedBytes, serverEncoding); |
||
641 | out.print(str); |
||
642 | } |
||
643 | } |
||
644 | catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e) |
||
645 | { |
||
646 | throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path); |
||
647 | } |
||
648 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) |
||
649 | { |
||
650 | throw new 1.5.0/docs/api/java/io/IOException.html">IOException(e.toString()); |
||
651 | } |
||
652 | |||
653 | } |
||
654 | |||
655 | |||
656 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
||
657 | /** |
||
658 | * Handles the HTTP <code>GET</code> method. |
||
659 | * |
||
660 | * @param request servlet request |
||
661 | * @param response servlet response |
||
662 | */ |
||
663 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
||
664 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
665 | { |
||
666 | processRequest(request, response); |
||
667 | } |
||
668 | |||
669 | /** |
||
670 | * Handles the HTTP <code>POST</code> method. |
||
671 | * |
||
672 | * @param request servlet request |
||
673 | * @param response servlet response |
||
674 | */ |
||
675 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
||
676 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
677 | { |
||
678 | processRequest(request, response); |
||
679 | } |
||
680 | |||
681 | /** |
||
682 | * Returns a short description of the servlet. |
||
683 | */ |
||
684 | public 1.5.0/docs/api/java/lang/String.html">String getServletInfo() |
||
685 | { |
||
686 | return "Proxy Servlet to forward to HTTP or FTP Proxy"; |
||
687 | } |
||
406 | jmachado | 688 | |
689 | public static void main(1.5.0/docs/api/java/lang/String.html">String[]args) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
690 | { |
||
691 | FtpServer server = FtpServer.getServer("ftp://www.global.estgp.pt","jmachado","linux5631"); |
||
692 | FTPClient client = server.getClient(); |
||
1005 | jmachado | 693 | if(client == null) |
694 | { |
||
695 | logger.warn("###################"); |
||
696 | logger.warn("################### > CANT CONNECT FTP"); |
||
697 | |||
698 | } |
||
699 | |||
406 | jmachado | 700 | client.setFileType(FTP.BINARY_FILE_TYPE); |
701 | 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream teste = new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream("D:\\resources.properties"); |
||
702 | client.changeWorkingDirectory("/Engenharia.Informatica.9119/AnoLectivo.2008-2009/Semestre1/Engenharia.Do.Software"); |
||
703 | client.makeDirectory("ttt"); |
||
704 | 1.5.0/docs/api/java/lang/System.html">System.out.println(client.storeFile("resources.properties",teste)); |
||
705 | client.quit(); |
||
706 | client.disconnect(); |
||
707 | } |
||
368 | jmachado | 708 | // </editor-fold> |
709 | } |