1 package de.jwi.jfm.servlets; 2 3 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.io.PrintWriter ; 31 import java.security.Principal ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Properties ; 35 import java.util.StringTokenizer ; 36 37 import javax.servlet.RequestDispatcher ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.http.HttpServlet ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 43 import org.apache.commons.fileupload.DiskFileUpload; 44 import org.apache.commons.fileupload.FileItem; 45 import org.apache.commons.fileupload.FileUpload; 46 47 import de.jwi.jfm.FileComparator; 48 import de.jwi.jfm.Folder; 49 import de.jwi.jfm.OutOfSyncException; 50 import de.jwi.servletutil.PathHelper; 51 import de.jwi.servletutil.RealPath; 52 53 58 public class Controller extends HttpServlet 59 { 60 61 private static final String PATH_URL_SERVLET = "/path"; 62 63 private static final String CTX_DOWNLOAD_SERVLET = "/dlx"; 64 65 private static final String FILE_DOWNLOAD_SERVLET = "/dlf"; 66 67 private static String version = "unknown"; 68 69 private static final String VERSIONCONFIGFILE = "version.properties"; 70 71 public static final int NOP_ACTION = 0; 72 73 public static final int RENAME_ACTION = 1; 74 75 public static final int COPY_ACTION = 2; 76 77 public static final int MOVE_ACTION = 3; 78 79 public static final int DELETE_ACTION = 4; 80 81 public static final int DELETE_RECURSIVE_ACTION = 5; 82 83 public static final int MKDIR_ACTION = 6; 84 85 public static final int UNZIP_ACTION = 7; 86 87 public static final int ZIP_ACTION = 8; 88 89 public static final int GETURL_ACTION = 9; 90 91 public static final int FTPUP_ACTION = 10; 92 93 public static final int JOIN_ACTION = 11; 94 95 private Properties dirmapping = null; 96 97 private File tempDir = null; 98 99 private String filebase = null; 100 101 public void init() throws ServletException 102 { 103 tempDir = (File ) getServletContext().getAttribute( 104 "javax.servlet.context.tempdir"); 105 106 filebase = getServletContext().getInitParameter("filebase"); 107 108 String s = getServletContext().getInitParameter("dirmappings"); 109 110 dirmapping = new Properties (); 111 112 if (null != s) 113 { 114 StringTokenizer st = new StringTokenizer (s, ","); 115 while (st.hasMoreTokens()) 116 { 117 String s1 = st.nextToken(); 118 int p = s1.indexOf('='); 119 if (p > -1) 120 { 121 String key = s1.substring(0, p); 122 String val = s1.substring(p + 1); 123 dirmapping.setProperty(key, val); 124 } 125 } 126 } 127 128 try 129 { 130 InputStream is = getServletContext().getResourceAsStream( 131 "/WEB-INF/" + VERSIONCONFIGFILE); 132 133 Properties versionProperties = new Properties (); 134 versionProperties.load(is); 135 136 s = versionProperties.getProperty("version"); 137 if (null != s) 138 { 139 version = s; 140 } 141 } 142 catch (Exception e) 143 { 144 e.printStackTrace(System.err); 145 } 146 147 } 148 149 public void doGet(HttpServletRequest request, HttpServletResponse response) 150 throws IOException , ServletException 151 { 152 doPost(request, response); 153 } 154 155 public void doPost(HttpServletRequest request, HttpServletResponse response) 156 throws IOException , ServletException 157 { 158 String self = null; 159 String contextPath = null; 160 String pathInfo = null; 161 Folder folder = null; 162 try 163 { 164 contextPath = request.getContextPath(); 165 String servletPath = request.getServletPath(); 166 167 pathInfo = request.getPathInfo(); 168 169 if (null == pathInfo) 170 { 171 172 PrintWriter writer = response.getWriter(); 173 writer.print(contextPath + servletPath + " is alive."); 174 175 return; 176 } 177 178 if (CTX_DOWNLOAD_SERVLET.equals(servletPath) 179 || FILE_DOWNLOAD_SERVLET.equals(servletPath)) 180 { 181 doDownload(request, response); 182 return; 183 } 184 185 boolean isFileSystemPath = PATH_URL_SERVLET.equals(servletPath); 186 187 if (!pathInfo.endsWith("/")) 188 { 189 response.sendRedirect(request.getRequestURL() + "/"); 190 return; 191 } 192 193 String queryString = request.getQueryString(); 194 195 String pathTranslated = request.getPathTranslated(); 196 String requestURI = request.getRequestURI(); 197 String requestURL = request.getRequestURL().toString(); 198 199 self = contextPath + servletPath; 200 201 RealPath theRealPath; 202 203 theRealPath = isFileSystemPath ? PathHelper.getFileRealPath( 204 filebase, pathInfo) : PathHelper.getHttpRealPath( 205 getServletContext(), pathInfo, dirmapping); 206 207 if (null == theRealPath) 208 { 209 response.sendError(HttpServletResponse.SC_NOT_FOUND, request 210 .getRequestURI()); 211 return; 212 } 213 214 String realPath = theRealPath.getRealPath(); 215 216 String fileURL = requestURI.replaceFirst(contextPath, ""); 217 fileURL = fileURL.replaceFirst(servletPath, ""); 218 219 String fileViewUrl; 220 221 if (theRealPath.isHttpPath()) 222 { 223 if (pathInfo.startsWith(theRealPath.getContext() + "/WEB-INF")) 224 { 225 fileViewUrl = request.getContextPath() 226 + CTX_DOWNLOAD_SERVLET + pathInfo; 227 } 228 else 229 { 230 fileViewUrl = fileURL; 231 } 232 } 233 else 234 { 235 fileViewUrl = request.getContextPath() + FILE_DOWNLOAD_SERVLET 236 + pathInfo; 237 } 238 239 folder = new Folder(theRealPath, pathInfo, fileURL, fileViewUrl); 240 241 folder.load(); 242 243 String actionresult = ""; 244 245 if (FileUpload.isMultipartContent(request)) 246 { 247 try 248 { 249 actionresult = handleUpload(request, folder); 250 folder.load(); 251 } 252 catch (Exception e) 253 { 254 throw new ServletException (e.getMessage(), e); 255 } 256 } 257 else if (null != queryString) 258 { 259 try 260 { 261 actionresult = handleQuery(request, response, folder); 262 } 263 catch (OutOfSyncException e) 264 { 265 actionresult = e.getMessage(); 266 } 267 if (null == actionresult) { return; } 268 } 269 270 request.setAttribute("actionresult", actionresult); 271 } 272 catch (SecurityException e) 273 { 274 request.setAttribute("actionresult", e.getClass().getName() + " " 275 + e.getMessage()); 276 request.setAttribute("fatalerror", new Boolean (true)); 277 278 } 279 280 String s = request.getRemoteUser(); 281 282 Principal principal = request.getUserPrincipal(); 283 284 if (principal != null) 285 { 286 request.setAttribute("principal", principal.getName()); 287 } 288 289 request.setAttribute("self", self); 290 291 request.setAttribute("version", version); 292 293 request.setAttribute("serverInfo", getServletContext().getServerInfo()); 294 295 request.setAttribute("url", contextPath); 296 297 request.setAttribute("path", pathInfo); 298 299 request.setAttribute("folder", folder); 300 301 String forward = "/WEB-INF/fm.jsp"; 302 303 RequestDispatcher requestDispatcher = getServletContext() 304 .getRequestDispatcher(forward); 305 306 requestDispatcher.forward(request, response); 307 } 308 309 private String handleQuery(HttpServletRequest request, 310 HttpServletResponse response, Folder folder) 311 throws OutOfSyncException, IOException 312 { 313 String rc = ""; 314 315 String target = null; 316 int action = NOP_ACTION; 317 String [] selectedfiles = request.getParameterValues("index"); 318 319 String logout = request.getParameter("logout"); 320 if ("t".equals(logout)) 321 { 322 request.getSession().invalidate(); 323 return ""; 324 } 325 326 String sum = request.getParameter("sum"); 327 if ("t".equals(sum)) 328 { 329 folder.sum(); 330 return ""; 331 } 332 String sort = request.getParameter("sort"); 333 if ("nd".equals(sort)) 334 { 335 folder.sort(FileComparator.SORT_NAME_DOWN); 336 return ""; 337 } 338 else if ("nu".equals(sort)) 339 { 340 folder.sort(FileComparator.SORT_NAME_UP); 341 return ""; 342 } 343 else if ("su".equals(sort)) 344 { 345 folder.sort(FileComparator.SORT_SIZE_UP); 346 return ""; 347 } 348 else if ("sd".equals(sort)) 349 { 350 folder.sort(FileComparator.SORT_SIZE_DOWN); 351 return ""; 352 } 353 354 else if ("du".equals(sort)) 355 { 356 folder.sort(FileComparator.SORT_DATE_UP); 357 return ""; 358 } 359 else if ("dd".equals(sort)) 360 { 361 folder.sort(FileComparator.SORT_DATE_DOWN); 362 return ""; 363 } 364 365 OutputStream out = null; 366 367 String command = request.getParameter("command"); 368 if ("Mkdir".equals(command)) 369 { 370 target = request.getParameter("newdir"); 371 action = MKDIR_ACTION; 372 } 373 if ("GetURL".equals(command)) 374 { 375 target = request.getParameter("url"); 376 action = GETURL_ACTION; 377 } 378 else if ("Delete".equals(command)) 379 { 380 action = DELETE_ACTION; 381 } 382 else if ("Rename".equals(command)) 383 { 384 target = request.getParameter("renameto"); 385 action = RENAME_ACTION; 386 } 387 else if ("Unzip".equals(command)) 388 { 389 action = UNZIP_ACTION; 390 } 391 else if ("ZipDownload".equals(command)) 392 { 393 action = ZIP_ACTION; 394 response.setContentType("application/zip"); 395 396 response.setHeader("Content-Disposition", 397 "inline; filename=\"jFMdownload.zip\""); 398 399 out = response.getOutputStream(); 400 } 401 else if ("Copy".equals(command)) 402 { 403 target = request.getParameter("copyto"); 404 action = COPY_ACTION; 405 } 406 else if ("Move".equals(command)) 407 { 408 target = request.getParameter("moveto"); 409 action = MOVE_ACTION; 410 } 411 else if ("DeleteRecursively".equals(command)) 412 { 413 target = request.getParameter("confirm"); 414 action = DELETE_RECURSIVE_ACTION; 415 } 416 else if ("FtpUpload".equals(command)) 417 { 418 target = request.getParameter("ftpto"); 419 action = FTPUP_ACTION; 420 } 421 else if ("Join".equals(command)) 422 { 423 action = JOIN_ACTION; 424 } 425 if (NOP_ACTION == action) { return ""; } 426 427 try 428 { 429 rc = folder.action(action, out, selectedfiles, target); 430 } 431 catch (SecurityException e) 432 { 433 rc = "SecurityException: " + e.getMessage(); 434 return rc; 435 } 436 437 folder.load(); 438 439 return rc; 440 } 441 442 private String handleUpload(HttpServletRequest request, Folder folder) 443 throws Exception 444 { 445 DiskFileUpload upload = new DiskFileUpload(); 446 upload.setRepositoryPath(tempDir.toString()); 447 System.out.println(upload.getSizeMax()); 448 449 List items = upload.parseRequest(request); 452 453 Iterator itr = items.iterator(); 454 455 boolean unzip = false; 456 457 while (itr.hasNext()) 458 { 459 FileItem item = (FileItem) itr.next(); 460 461 if (item.isFormField()) 463 { 464 String name = item.getFieldName(); 465 String value = item.getString(); 466 if ("command".equals(name) && "unzip".equals(value)) 467 { 468 unzip = true; 469 } 470 } 471 else 472 { 473 String name = item.getFieldName(); 474 unzip = "unzip".equals(name); 475 476 if (!"".equals(item.getName())) 477 { 478 folder.upload(item, unzip); 479 } 480 489 } 491 } 492 return ""; 493 } 494 495 public void doDownload(HttpServletRequest request, 496 HttpServletResponse response) throws IOException 497 { 498 String servletPath = request.getServletPath(); 499 500 String pathInfo = request.getPathInfo(); 501 502 RealPath realPath = null; 503 504 if (CTX_DOWNLOAD_SERVLET.equals(servletPath)) 505 { 506 realPath = PathHelper.getHttpRealPath(getServletContext(), 507 pathInfo, dirmapping); 508 } 509 else 510 { 511 realPath = PathHelper.getFileRealPath(filebase, pathInfo); 512 } 513 514 File f = new File (realPath.getRealPath()); 515 String name = f.getName(); 516 517 String mimeType = getServletContext().getMimeType(name); 518 519 response.setContentType(mimeType); 520 521 response.setHeader("Content-Disposition", "inline; filename=\"" + name 522 + "\""); 523 524 OutputStream out = response.getOutputStream(); 525 526 FileInputStream in = new FileInputStream (f); 527 528 byte[] buf = new byte[512]; 529 int l; 530 531 try 532 { 533 while ((l = in.read(buf)) > 0) 534 { 535 out.write(buf, 0, l); 536 } 537 } 538 catch (IOException e) 539 { 540 throw e; 541 } 542 finally 543 { 544 in.close(); 545 } 546 547 } 548 } | Popular Tags |