1 23 24 package org.apache.slide.webdav.util; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.text.DateFormat ; 29 import java.text.SimpleDateFormat ; 30 import java.util.Date ; 31 import java.util.Enumeration ; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.apache.slide.common.NamespaceAccessToken; 37 import org.apache.slide.common.SlideException; 38 import org.apache.slide.common.SlideToken; 39 import org.apache.slide.content.Content; 40 import org.apache.slide.content.NodeRevisionDescriptor; 41 import org.apache.slide.content.NodeRevisionDescriptors; 42 import org.apache.slide.lock.Lock; 43 import org.apache.slide.lock.NodeLock; 44 import org.apache.slide.security.NodePermission; 45 import org.apache.slide.security.Security; 46 import org.apache.slide.structure.ObjectNode; 47 import org.apache.slide.structure.Structure; 48 import org.apache.slide.util.Messages; 49 import org.apache.slide.webdav.WebdavServletConfig; 50 51 57 public class DirectoryIndexGenerator { 58 59 60 62 63 66 public static final String DATE_FORMAT = 67 "EEE, d MMM yyyy kk:mm:ss z"; 68 69 70 73 private static final DateFormat formatter = 74 new SimpleDateFormat (DATE_FORMAT); 75 76 77 79 80 83 protected NamespaceAccessToken nat; 84 85 86 89 protected WebdavServletConfig config; 90 91 92 94 95 100 public DirectoryIndexGenerator(NamespaceAccessToken nat, 101 WebdavServletConfig config) { 102 103 if (nat == null) { 104 throw new IllegalArgumentException ( 105 "NamespaceAccessToken must not be null"); 106 } 107 this.nat = nat; 108 if (config == null) { 109 throw new IllegalArgumentException ( 110 "WebdavServletConfig must not be null"); 111 } 112 this.config = config; 113 } 114 private String stringToCharacterRef(String val) { 116 StringBuffer result = new StringBuffer (val.length() * 8); 117 for (int i = 0; i < val.length(); i++) { 118 result.append(charToCharacterRef(val.charAt(i))); 119 } 120 return result.toString(); 121 } 122 123 private String charToCharacterRef(char val) { 124 StringBuffer result = new StringBuffer (8); 125 result.append("&#x").append(Integer.toHexString((int) val).toUpperCase()).append(";"); 126 return result.toString(); 127 } 128 129 130 132 133 142 public void generate(HttpServletRequest req, HttpServletResponse res, SlideToken slideToken) 143 throws IOException , SlideException { 144 145 res.setContentType("text/html; charset=\"UTF-8\""); 146 147 Content content = nat.getContentHelper(); 149 Lock lock = nat.getLockHelper(); 150 Security security = nat.getSecurityHelper(); 151 Structure structure = nat.getStructureHelper(); 152 153 String resourcePath = WebdavUtils.getRelativePath(req, config); 155 ObjectNode object = structure.retrieve(slideToken, resourcePath); 156 String name = object.getUri(); 157 158 int trim = name.length(); 160 if (!name.endsWith("/")) 161 trim += 1; 162 if (name.equals("/")) 163 trim = 1; 164 165 PrintWriter writer = new PrintWriter (res.getWriter()); 166 167 writer.print("<html>\r\n"); 169 writer.print("<head>\r\n"); 170 writer.print("<meta http-equiv=\"Content-type\" content=\"text/html; charset=UTF-8\" >\r\n"); 171 writer.print("</meta>\r\n"); 172 writer.print("<title>"); 173 writer.print 174 (Messages.format 175 ("org.apache.slide.webdav.GetMethod.directorylistingfor", 176 name)); 177 writer.print("</title>\r\n</head>\r\n"); 178 writer.print("<body bgcolor=\"white\">\r\n"); 179 writer.print("<table width=\"90%\" cellspacing=\"0\"" + 180 " cellpadding=\"5\" align=\"center\">\r\n"); 181 182 writer.print("<tr><td colspan=\"3\"><font size=\"+2\">\r\n<strong>"); 184 writer.print 185 (Messages.format 186 ("org.apache.slide.webdav.GetMethod.directorylistingfor", 187 name)); 188 writer.print("</strong>\r\n</font></td></tr>\r\n"); 189 190 String parentDirectory = name; 192 if (parentDirectory.endsWith("/")) { 193 parentDirectory = 194 parentDirectory.substring(0, parentDirectory.length() - 1); 195 } 196 String scope = config.getScope(); 197 parentDirectory = parentDirectory.substring(scope.length()); 198 if (parentDirectory.lastIndexOf("/") >= 0) { 199 parentDirectory = parentDirectory.substring(0, parentDirectory.lastIndexOf("/")); 200 writer.print("<tr><td colspan=\"5\" bgcolor=\"#ffffff\">\r\n"); 201 writer.print("<a HREF=\""); 202 writer.print(WebdavUtils.getAbsolutePath(scope, req, config)); 203 if (parentDirectory.equals("")) 204 parentDirectory = "/"; 205 writer.print(parentDirectory); writer.print("\">"); 207 writer.print(Messages.format 208 ("org.apache.slide.webdav.GetMethod.parent", 209 parentDirectory)); 210 writer.print("</a>\r\n"); 211 writer.print("</td></tr>\r\n"); 212 } 213 214 Enumeration permissionsList = null; 215 Enumeration locksList = null; 216 try { 217 permissionsList = 218 security.enumeratePermissions(slideToken, object.getUri()); 219 locksList = lock.enumerateLocks(slideToken, object.getUri(), false); 220 } catch (SlideException e) { 221 } 224 225 if (org.apache.slide.util.Configuration.useIntegratedSecurity()) { 227 displayPermissions(permissionsList, writer, false); 228 } 229 230 displayLocks(locksList, writer, false); 232 233 writer.print("<tr><td colspan=\"5\" bgcolor=\"#ffffff\">"); 234 writer.print(" "); 235 writer.print("</td></tr>\r\n"); 236 237 writer.print("<tr bgcolor=\"#cccccc\">\r\n"); 239 writer.print("<td align=\"left\" colspan=\"3\">"); 240 writer.print("<font size=\"+1\"><strong>"); 241 writer.print(Messages.message 242 ("org.apache.slide.webdav.GetMethod.filename")); 243 writer.print("</strong></font></td>\r\n"); 244 writer.print("<td align=\"center\"><font size=\"+1\"><strong>"); 245 writer.print(Messages.message 246 ("org.apache.slide.webdav.GetMethod.size")); 247 writer.print("</strong></font></td>\r\n"); 248 writer.print("<td align=\"right\"><font size=\"+1\"><strong>"); 249 writer.print(Messages.message 250 ("org.apache.slide.webdav.GetMethod.lastModified")); 251 writer.print("</strong></font></td>\r\n"); 252 writer.print("</tr>\r\n"); 253 254 Enumeration resources = structure.getChildren(slideToken,object); 255 boolean shade = false; 256 257 while (resources.hasMoreElements()) { 258 String currentResource = ((ObjectNode)resources.nextElement()).getPath().toString(); 259 NodeRevisionDescriptor currentDescriptor = null; 260 permissionsList = null; 261 locksList = null; 262 try { 263 NodeRevisionDescriptors revisionDescriptors = 264 content.retrieve(slideToken, currentResource); 265 currentDescriptor = 267 content.retrieve(slideToken, revisionDescriptors); 268 } catch (SlideException e) { 269 } 274 275 try { 276 permissionsList = 277 security.enumeratePermissions(slideToken, currentResource); 278 locksList = lock.enumerateLocks(slideToken, currentResource, false); 279 } catch (SlideException e) { 280 } 283 284 String trimmed = currentResource.substring(trim); 285 if (trimmed.equalsIgnoreCase("WEB-INF") || 286 trimmed.equalsIgnoreCase("META-INF")) { 287 continue; 288 } 289 290 writer.print("<tr"); 291 if (shade) { 292 writer.print(" bgcolor=\"dddddd\""); 293 } else { 294 writer.print(" bgcolor=\"eeeeee\""); 295 } 296 writer.print(">\r\n"); 297 shade = !shade; 298 299 writer.print("<td align=\"left\" colspan=\"3\"> \r\n"); 300 writer.print("<a HREF=\""); 301 writer.print(WebdavUtils.getAbsolutePath(currentResource, req, config)); 302 writer.print("\"><tt>"); 303 writer.print(stringToCharacterRef(trimmed)); if (WebdavUtils.isCollection(currentDescriptor)) { 305 writer.print("/"); 306 } 307 writer.print("</tt></a></td>\r\n"); 308 309 writer.print("<td align=\"right\"><tt>"); 310 if (WebdavUtils.isCollection(currentDescriptor)) { 311 writer.print(" "); 312 } 313 else { 314 writer.print(renderSize(currentDescriptor.getContentLength())); 315 } 316 writer.print("</tt></td>\r\n"); 317 318 writer.print("<td align=\"right\"><tt>"); 319 if (currentDescriptor != null) { 320 writer.print(currentDescriptor.getLastModified()); 321 } else { 322 writer.print(" "); 323 } 324 writer.print("</tt></td>\r\n"); 325 326 writer.print("</tr>\r\n"); 327 328 if (org.apache.slide.util.Configuration.useIntegratedSecurity()) { 330 displayPermissions(permissionsList, writer, shade); 331 } 332 333 displayLocks(locksList, writer, shade); 335 } 336 337 writer.print("<tr><td colspan=\"5\"> </td></tr>\r\n"); 339 writer.print("<tr><td colspan=\"3\" bgcolor=\"#cccccc\">"); 340 writer.print("<font size=\"-1\">"); 341 writer.print(Messages.message 342 ("org.apache.slide.webdav.GetMethod.version")); 343 writer.print("</font></td>\r\n"); 344 writer.print("<td colspan=\"2\" align=\"right\" bgcolor=\"#cccccc\">"); 345 writer.print("<font size=\"-1\">"); 346 writer.print(formatter.format(new Date ())); 347 writer.print("</font></td></tr>\r\n"); 348 writer.print("</table>\r\n"); 349 writer.print("</body>\r\n"); 350 writer.print("</html>\r\n"); 351 352 writer.flush(); 354 } 355 356 357 359 360 367 protected void displayPermissions(Enumeration permissionsList, 368 PrintWriter writer, 369 boolean shade) 370 throws IOException { 371 372 boolean hideAcl = false; 373 String hideAclStr = config.getInitParameter( "directory-browsing-hide-acl" ); 374 if( "true".equalsIgnoreCase(hideAclStr) ) 375 hideAcl = true; 376 377 if( !hideAcl ) { 378 if ((permissionsList != null) && (permissionsList.hasMoreElements())) { 379 writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\"" 380 : " bgcolor=\"dddddd\"") + 381 ">\r\n"); 382 writer.print("<td align=\"left\" colspan=\"5\"><tt><b>"); 383 writer.print(Messages.message 384 ("org.apache.slide.webdav.GetMethod.aclinfo")); 385 writer.print("</b></tt></td>\r\n"); 386 writer.print("</tr>\r\n"); 387 writer.print("<tr"); 388 if (!shade) { 389 writer.print(" bgcolor=\"dddddd\""); 390 } else { 391 writer.print(" bgcolor=\"eeeeee\""); 392 } 393 writer.print(">\r\n"); 394 writer.print("<td align=\"left\" colspan=\"2\"><tt><b>"); 395 writer.print(Messages.message 396 ("org.apache.slide.webdav.GetMethod.subject")); 397 writer.print("</b></tt></td>\r\n"); 398 writer.print("<td align=\"left\"><tt><b>"); 399 writer.print(Messages.message 400 ("org.apache.slide.webdav.GetMethod.action")); 401 writer.print("</b></tt></td>\r\n"); 402 writer.print("<td align=\"right\"><tt><b>"); 403 writer.print(Messages.message 404 ("org.apache.slide.webdav.GetMethod.inheritable")); 405 writer.print("</b></tt></td>\r\n"); 406 writer.print("<td align=\"right\"><tt><b>"); 407 writer.print(Messages.message 408 ("org.apache.slide.webdav.GetMethod.deny")); 409 writer.print("</b></tt></td>\r\n"); 410 writer.print("</tr>\r\n"); 411 412 while (permissionsList.hasMoreElements()) { 413 writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\"" 414 : " bgcolor=\"dddddd\"") + 415 ">\r\n"); 416 NodePermission currentPermission = 417 (NodePermission) permissionsList.nextElement(); 418 writer.print("<td align=\"left\" colspan=\"2\"><tt>"); 419 writer.print(currentPermission.getSubjectUri()); 420 writer.print("</tt></td>\r\n"); 421 writer.print("<td align=\"left\"><tt>"); 422 writer.print(currentPermission.getActionUri()); 423 writer.print("</tt></td>\r\n"); 424 writer.print("<td align=\"right\"><tt>"); 425 writer.print(currentPermission.isInheritable()); 426 writer.print("</tt></td>\r\n"); 427 writer.print("<td align=\"right\"><tt>"); 428 writer.print(currentPermission.isNegative()); 429 writer.print("</tt></td>\r\n"); 430 writer.print("</tr>\r\n"); 431 } 432 } 433 } 434 } 435 436 437 444 protected void displayLocks(Enumeration locksList, PrintWriter writer, 445 boolean shade) 446 throws IOException { 447 448 boolean hideLocks = false; 449 String hideLocksStr = config.getInitParameter( "directory-browsing-hide-locks" ); 450 if( "true".equalsIgnoreCase(hideLocksStr) ) 451 hideLocks = true; 452 453 if( !hideLocks ) { 454 if ((locksList != null) && (locksList.hasMoreElements())) { 455 writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\"" 456 : " bgcolor=\"dddddd\"") + 457 ">\r\n"); 458 writer.print("<td align=\"left\" colspan=\"5\"><tt><b>"); 459 writer.print(Messages.message 460 ("org.apache.slide.webdav.GetMethod.locksinfo")); 461 writer.print("</b></tt></td>\r\n"); 462 writer.print("</tr>\r\n"); 463 writer.print("<tr"); 464 if (!shade) { 465 writer.print(" bgcolor=\"dddddd\""); 466 } else { 467 writer.print(" bgcolor=\"eeeeee\""); 468 } 469 writer.print(">\r\n"); 470 writer.print("<td align=\"left\"><tt><b>"); 471 writer.print(Messages.message 472 ("org.apache.slide.webdav.GetMethod.subject")); 473 writer.print("</b></tt></td>\r\n"); 474 writer.print("<td align=\"left\"><tt><b>"); 475 writer.print(Messages.message 476 ("org.apache.slide.webdav.GetMethod.type")); 477 writer.print("</b></tt></td>\r\n"); 478 writer.print("<td align=\"right\"><tt><b>"); 479 writer.print(Messages.message 480 ("org.apache.slide.webdav.GetMethod.expiration")); 481 writer.print("</b></tt></td>\r\n"); 482 writer.print("<td align=\"right\"><tt><b>"); 483 writer.print(Messages.message 484 ("org.apache.slide.webdav.GetMethod.inheritable")); 485 writer.print("</b></tt></td>\r\n"); 486 writer.print("<td align=\"right\"><tt><b>"); 487 writer.print(Messages.message 488 ("org.apache.slide.webdav.GetMethod.exclusive")); 489 writer.print("</b></tt></td>\r\n"); 490 writer.print("</tr>\r\n"); 491 492 while (locksList.hasMoreElements()) { 493 writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\"" 494 : " bgcolor=\"dddddd\"") + 495 ">\r\n"); 496 NodeLock currentLock = (NodeLock) locksList.nextElement(); 497 writer.print("<td align=\"left\"><tt>"); 498 writer.print(currentLock.getSubjectUri()); 499 writer.print("</tt></td>\r\n"); 500 writer.print("<td align=\"left\"><tt>"); 501 writer.print(currentLock.getTypeUri()); 502 writer.print("</tt></td>\r\n"); 503 writer.print("<td align=\"right\"><tt>"); 504 writer.print 505 (formatter.format(currentLock.getExpirationDate())); 506 writer.print("</tt></td>\r\n"); 507 writer.print("<td align=\"right\"><tt>"); 508 writer.print(currentLock.isInheritable()); 509 writer.print("</tt></td>\r\n"); 510 writer.print("<td align=\"right\"><tt>"); 511 writer.print(currentLock.isExclusive()); 512 writer.print("</tt></td>\r\n"); 513 } 514 } 515 } 516 } 517 518 519 524 protected String renderSize(long size) { 525 526 long leftSide = size / 1024; 527 long rightSide = (size % 1024) / 103; if ((leftSide == 0) && (rightSide == 0) && (size > 0)) 529 rightSide = 1; 530 531 return ("" + leftSide + "." + rightSide + " kb"); 532 } 533 534 535 } 536 537 | Popular Tags |