1 13 package info.magnolia.cms.gui.dialog; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.gui.control.Button; 17 import info.magnolia.cms.gui.control.Hidden; 18 import info.magnolia.cms.gui.misc.CssConstants; 19 import info.magnolia.cms.gui.misc.Sources; 20 import info.magnolia.cms.gui.misc.Spacer; 21 import info.magnolia.cms.i18n.MessagesManager; 22 23 import java.io.IOException ; 24 import java.io.UnsupportedEncodingException ; 25 import java.io.Writer ; 26 import java.net.URLDecoder ; 27 import java.text.DateFormat ; 28 import java.text.SimpleDateFormat ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.Date ; 32 import java.util.Enumeration ; 33 import java.util.Hashtable ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 38 import javax.jcr.PropertyType; 39 import javax.jcr.RepositoryException; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 43 import org.apache.commons.httpclient.HttpURL; 44 import org.apache.commons.lang.StringUtils; 45 import org.apache.log4j.Logger; 46 import org.apache.webdav.lib.Property; 47 import org.apache.webdav.lib.WebdavResource; 48 import org.apache.webdav.lib.methods.XMLResponseMethodBase; 49 50 51 55 public class DialogWebDAV extends DialogBox { 56 57 private static final int ICONS_HEIGHT = 16; 58 59 private static final int ICONS_WIDTH = 23; 60 61 public static final String NULLGIF = "/admindocroot/0.gif"; 63 66 private static Logger log = Logger.getLogger(DialogWebDAV.class); 67 68 private String host = StringUtils.EMPTY; 70 71 private int port; 72 73 private String directory = StringUtils.EMPTY; 74 75 private String subDirectory = StringUtils.EMPTY; 76 77 private String user = StringUtils.EMPTY; 78 79 private String password = StringUtils.EMPTY; 80 81 private String protocol = "http"; 83 private WebdavResource davConnection; 84 85 88 protected DialogWebDAV() { 89 } 90 91 94 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode) 95 throws RepositoryException { 96 super.init(request, response, websiteNode, configNode); 97 initIconExtensions(); 98 } 99 100 public void setHost(String s) { 101 this.host = s; 102 } 103 104 public String getHost() { 105 return this.host; 106 } 107 108 public void setPort(String s) { 109 try { 110 this.port = (new Integer (s)).intValue(); 111 } 112 catch (NumberFormatException nfe) { 113 this.port = 0; 114 } 115 } 116 117 public void setPort(int i) { 118 this.port = i; 119 } 120 121 public int getPort() { 122 if (this.port == 0) { 123 return 80; 124 } 125 126 return this.port; 127 } 128 129 public void setDirectory(String s) { 130 this.directory = s; 131 } 132 133 public String getDirectory() { 134 return this.directory; 135 } 136 137 public void setSubDirectory(String s) { 138 this.subDirectory = s; 139 } 140 141 public String getSubDirectory() { 142 return this.subDirectory; 143 } 144 145 public void setUser(String s) { 146 this.user = s; 147 } 148 149 public String getUser() { 150 return this.user; 151 } 152 153 public void setPassword(String s) { 154 this.password = s; 155 } 156 157 public String getPassword() { 158 return this.password; 159 } 160 161 public void setProtocol(String s) { 162 this.protocol = s; 163 } 164 165 public String getProtocol() { 166 return this.protocol; 167 } 168 169 public String getHtmlDecodeURI(String s) { 170 return "<script type=\"text/javascript\">document.write(decodeURI(\"" + s + "\"));</script>"; } 172 173 public void setDAVConnection(WebdavResource w) { 174 this.davConnection = w; 175 } 176 177 public WebdavResource getDAVConnection() { 178 return this.davConnection; 179 } 180 181 private String getSizeValue() { 182 if (this.getWebsiteNode() != null) { 183 return this.getWebsiteNode().getNodeData(this.getName() + "_size").getString(); } 185 186 return StringUtils.EMPTY; 187 } 188 189 private String getModDateValue() { 190 if (this.getWebsiteNode() != null) { 191 return this.getWebsiteNode().getNodeData(this.getName() + "_lastModified").getString(); } 193 194 return StringUtils.EMPTY; 195 } 196 197 public void setDAVConnection() { 198 WebdavResource wdr = null; 199 try { 200 201 wdr = new WebdavResource(new HttpURL( 202 this.getUser(), 203 this.getPassword(), 204 this.getHost(), 205 this.getPort(), 206 this.getDirectory())); 207 208 } 209 catch (Exception e) { 210 log.error(e.getMessage(), e); 211 } 212 this.setDAVConnection(wdr); 213 } 214 215 218 public void drawHtml(Writer out) throws IOException { 219 this.drawHtmlPre(out); 220 this.setDAVConnection(); 221 this.setSessionAttribute(); 222 String showName = " "; String showPath = StringUtils.EMPTY; 224 String showIcon = StringUtils.EMPTY; 225 if (StringUtils.isNotEmpty(this.getValue())) { 226 String valueTmp = StringUtils.EMPTY; 227 boolean isDirectory = false; 228 if (this.getValue().lastIndexOf("/") == this.getValue().length() - 1) { valueTmp = this.getValue().substring(0, this.getValue().length() - 1); 231 isDirectory = true; 232 this.setSubDirectory(this.getValue()); 233 showIcon = ICONS_PATH + ICONS_FOLDER; 234 } 235 else { 236 valueTmp = this.getValue(); 238 showIcon = this.getIconPath(this.getValue()); 239 } 240 if (valueTmp.indexOf("/") != -1) { showName = valueTmp.substring(valueTmp.lastIndexOf("/") + 1); if (!isDirectory) { 243 this.setSubDirectory(valueTmp.substring(0, valueTmp.lastIndexOf("/") + 1)); } 245 } 246 else { 247 showName = valueTmp; 248 if (!isDirectory) { 249 this.setSubDirectory(StringUtils.EMPTY); 250 } 251 } 252 showPath = "/" + this.getSubDirectory().substring(0, this.getSubDirectory().lastIndexOf("/") + 1); showPath = "<a HREF=\"javascript:mgnlDialogDAVBrowse('" + this.getName() + "_iFrame','selectedValue');\">" + this.getHtmlDecodeURI(showPath) + "</a>"; } 257 else { 258 showPath = "<i>" + MessagesManager.get(this.getRequest(), "dialog.webdav.noSelection") + "</i>"; showIcon = NULLGIF; 260 } 261 262 this.setDescription(MessagesManager.get(this.getRequest(), "dialog.webdav.connectedTo") + this.getProtocol() + "://" + this.getHost() + ":" + this.getPort() + this.getDirectory() + "<br />" + this.getDescription()); 267 out.write(Spacer.getHtml(2, 2)); 268 out.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"); out.write("<tr>"); out.write("<td><img id=\"" + this.getName() + "_showIcon\" SRC=\"" + this.getRequest().getContextPath() + showIcon + "\" width=\"" + ICONS_WIDTH + "\" height=\"" + ICONS_HEIGHT + "\"></td>"); out.write("<td id=\"" + this.getName() + "_showName\">" + this.getHtmlDecodeURI(showName) + "</td>"); out.write("</tr><tr height=\"4\"><td></td></tr><tr>"); out.write("<td><img SRC=\"" + this.getRequest().getContextPath() + ICONS_PATH + ICONS_FOLDER + "\"></td>"); out.write("<td id=\"" + this.getName() + "_showPath\">" + showPath + "</td>"); out.write("</tr></table>"); out.write(new Hidden(this.getName(), this.getValue()).getHtml()); 283 Hidden size = new Hidden(this.getName() + "_size", this.getSizeValue()); size.setType(PropertyType.TYPENAME_LONG); 285 out.write(size.getHtml()); 286 Hidden lastMod = new Hidden(this.getName() + "_lastModified", this.getModDateValue()); lastMod.setType(PropertyType.TYPENAME_DATE); 288 out.write(lastMod.getHtml()); 289 out.write(this.getHtmlSessionAttributeRemoveControl()); 290 out.write(Spacer.getHtml(12, 12)); 291 Button home = new Button(); 292 home.setSaveInfo(false); 293 home.setLabel(MessagesManager.get(this.getRequest(), "dialog.webdav.home")); home.setOnclick("mgnlDialogDAVBrowse('" + this.getName() + "_iFrame','homeDirectory')"); out.write(home.getHtml()); 297 Button refresh = new Button(); 298 refresh.setSaveInfo(false); 299 refresh.setLabel(MessagesManager.get(this.getRequest(), "dialog.webdav.refresh")); refresh.setOnclick("mgnlDialogDAVBrowse('" + this.getName() + "_iFrame','refreshDirectory')"); out.write(refresh.getHtml()); 303 Button up = new Button(); 304 up.setSaveInfo(false); 305 up.setId(this.getName() + "_upDiv"); up.setLabel(MessagesManager.get(this.getRequest(), "dialog.webdav.parentdirectory")); up.setOnclick("mgnlDialogDAVBrowse('" + this.getName() + "_iFrame','parentDirectory')"); out.write(up.getHtml()); 310 out.write(Spacer.getHtml(3, 3)); 311 out.write("<iframe"); out.write(" id=\"" + this.getName() + "_iFrame\""); out.write(" class=\"" + CssConstants.CSSCLASS_WEBDAVIFRAME + "\""); if (this.getConfigValue("height", null) != null) { out.write(" style=\"height:" + this.getConfigValue("height") + ";\")"); } 320 out.write(" frameborder=\"0\""); out.write(" SRC=\"/.magnolia/dialogpages/webDAVIFrame.html?" + SESSION_ATTRIBUTENAME_DIALOGOBJECT + "=" + this.getConfigValue(SESSION_ATTRIBUTENAME_DIALOGOBJECT) + "&mgnlCK=" + new Date ().getTime() + "\""); out.write(" reloadsrc=\"0\""); out.write(" usecss=\"1\""); out.write(" strict_output=\"1\""); out.write(" content_type=\"application/xhtml+xml\""); out.write(" scrolling=\"auto\""); out.write("></iframe>"); this.drawHtmlPost(out); 332 } 333 334 public void drawHtmlList(Writer out) { 335 Enumeration fileList = null; 336 String dir = this.getDirectory() + this.getSubDirectory(); 337 WebdavResource wdr = this.getDAVConnection(); 338 try { 339 dir = URLDecoder.decode(dir, "UTF-8"); } 341 catch (UnsupportedEncodingException uee) { 342 } 344 try { 345 if (StringUtils.isEmpty(dir)) { 346 fileList = wdr.propfindMethod(1); 347 } 348 else { 349 try { 350 fileList = wdr.propfindMethod(dir, 1); 351 } 352 catch (Exception e) { 353 dir = this.getDirectory(); 354 fileList = wdr.propfindMethod(dir, 1); 355 } 356 } 357 out.write("<html><head>"); out.write(new Sources(this.getRequest().getContextPath()).getHtmlCss()); 359 out.write(new Sources(this.getRequest().getContextPath()).getHtmlJs()); 360 String parentDirectory = StringUtils.EMPTY; 361 if (!this.getDirectory().equals(dir)) { 362 parentDirectory = this.getSubDirectory().substring(0, this.getSubDirectory().length() - 1); parentDirectory = parentDirectory.substring(0, parentDirectory.lastIndexOf("/") + 1); out.write("<script type=\"text/javascript\">mgnlDialogDAVShow('" + this.getName() + "_upDiv',true);</script>"); } 371 else { 372 out.write("<script type=\"text/javascript\">mgnlDialogDAVShow('" + this.getName() + "_upDiv',false);</script>"); } 376 out.write("</head>"); out.write("<body>"); out.write("<table cellpadding=\"3\" cellspacing=\"0\" border=\"0\" width=\"100%\">"); out.write("<form name=\"mgnlDialogDAVBrowseForm\" method=\"post\">"); out.write(new Hidden("subDirectory", StringUtils.EMPTY, false).getHtml()); out.write(new Hidden("parentDirectory", parentDirectory, false).getHtml()); out.write(new Hidden("homeDirectory", StringUtils.EMPTY, false).getHtml()); out.write(new Hidden("refreshDirectory", this.getSubDirectory(), false).getHtml()); out.write(new Hidden("selectedValue", this.getValue(), false).getHtml()); out.write(new Hidden(SESSION_ATTRIBUTENAME_DIALOGOBJECT, this 386 .getConfigValue(SESSION_ATTRIBUTENAME_DIALOGOBJECT), false).getHtml()); 387 out.write("</form>"); List fileListAS = new ArrayList (); 389 List dirListAS = new ArrayList (); 390 List selfAS = new ArrayList (); 391 while (fileList.hasMoreElements()) { 392 XMLResponseMethodBase.Response response = (XMLResponseMethodBase.Response) fileList.nextElement(); 393 Map properties = this.getDAVProperties(response); 394 if (StringUtils.isEmpty((String ) properties.get("name"))) { continue; 396 } 397 String name = (String ) properties.get("name"); if (this.getSubDirectory().equals(name) || this.getSubDirectory().equals(name + "/")) { properties.put("isSelf", "true"); } 402 if (name.indexOf("/") != -1) { name = name.substring(name.lastIndexOf("/") + 1); } 406 if (name.startsWith("._")) { continue; 408 } 409 if (name.startsWith(".") && !this.getConfigValue("showHiddenFiles", "false").equals("true")) { continue; 412 } 413 properties.put("name", name); String displayType = (String ) properties.get("displayType"); if (properties.get("isSelf") != null) { selfAS.add(properties); 417 } 418 else if (displayType.equals("folder")) { dirListAS.add(properties); 420 } 421 else { 422 fileListAS.add(properties); 423 } 424 } 425 int i = 0; 426 List parentAS = new ArrayList (); 427 if (!this.getDirectory().equals(dir)) { 428 Map parentProp = new Hashtable (); 429 String name = StringUtils.EMPTY; 430 if (StringUtils.isEmpty(parentDirectory)) { 431 name = "/"; } 433 else { 434 name = parentDirectory.substring(0, parentDirectory.length() - 1); 435 if (name.indexOf("/") != -1) { name = name.substring(0, name.lastIndexOf("/")); } 438 } 439 parentProp.put("name", name); parentProp.put("isParent", "true"); parentProp.put("href", parentDirectory); parentProp.put("displayType", "folder"); parentProp.put("sizeStringValue", StringUtils.EMPTY); parentProp.put("sizeStringUnit", StringUtils.EMPTY); parentProp.put("lastModifiedString", StringUtils.EMPTY); parentAS.add(parentProp); 447 i = drawHtmlList(out, parentAS, i); 448 } 449 i = drawHtmlList(out, selfAS, i); 450 i = drawHtmlList(out, dirListAS, i); 451 i = drawHtmlList(out, fileListAS, i); 452 if (i == 1) { 453 out.write("<tr><td colspan=\"3\"></td><td colspan=\"3\"><em>" + MessagesManager.get(this.getRequest(), "dialog.webdav.directoryIsEmpty") + "</em></td></tr>"); } 457 out.write("</table>"); out.write("</body></html>"); } 460 catch (Exception e) { 461 log.debug("Exception caught: " + e.getMessage(), e); } 463 } 464 465 public int drawHtmlList(Writer out, List as, int i) throws IOException { 466 467 boolean alt = (i % 2 == 0); 468 469 Collections.sort(as, new DialogWebDAVComparator()); 471 Iterator it = as.iterator(); 472 while (it.hasNext()) { 473 Map properties = (Hashtable ) it.next(); 474 String displayType = (String ) properties.get("displayType"); String name = (String ) properties.get("name"); if (!alt) { 477 out.write("<tr>"); } 479 else { 480 out.write("<tr class=" + CssConstants.CSSCLASS_BGALT + ">"); } 482 alt = !alt; 483 out.write("<td></td>"); out.write("<td>"); if (properties.get("isParent") == null && (displayType.indexOf("folder") == -1 || this .getConfigValue("allowDirectorySelection") .equals("true"))) { String lastModified = StringUtils.EMPTY; 490 if (properties.get("lastModified") != null) { lastModified = ((String ) properties.get("lastModified")) .replaceAll(" ", "%20"); } 494 out.write("<input type=\"radio\" name=\"" + this.getName() + "_radio\""); out.write(" onclick=mgnlDialogDAVSelect(\"" + this.getName() + "\",\"" + name + "\",\"" + i + "\",\"" + (String ) properties.get("size") + "\",\"" + lastModified + "\");"); boolean checked = false; 505 if (properties.get("isSelf") != null) { if (this.getValue().equals(this.getSubDirectory())) { 507 checked = true; 508 } 509 } 510 else { 511 if (this.getValue().equals(this.getSubDirectory() + name) 512 || this.getValue().equals(this.getSubDirectory() + name + "/")) { checked = true; 514 } 515 } 516 if (checked) { 517 out.write(MessagesManager.get(this.getRequest(), "dialog.webdav.checked")); } 519 out.write(" />"); } 521 out.write("</td>"); String idHidden = this.getName() + "_" + i + "_hidden"; String idIcon = this.getName() + "_" + i + "_icon"; i++; 526 String iconPath; 527 if (displayType.equals("folder")) { iconPath = ICONS_PATH + ICONS_FOLDER; 529 } 530 else { 531 iconPath = this.getIconPath(name); 532 } 533 out.write("<td>"); out.write("<img SRC=\"" + this.getRequest().getContextPath() + iconPath + "\" border=\"0\" id=\"" + idIcon + "\">"); out.write("</td>"); out.write("<td width=\"100%\">"); if (displayType.indexOf("folder") == 0) { if (properties.get("isSelf") != null) { out.write(this.getHtmlDecodeURI("<b><i>. " + name + "</i></b>")); } 543 else { 544 if (properties.get("isParent") != null) { name = "<b><i>.. " + name + "</i></b>"; } 547 out.write("<a HREF=\"javascript:mgnlDialogDAVBrowse('','" + idHidden + "');\">"); out.write(this.getHtmlDecodeURI(name)); 549 out.write("</a>"); } 551 } 552 else { 553 out.write(this.getHtmlDecodeURI(name)); 554 out.write("[<a HREF=\"" + this.getProtocol() + "://" + this.getHost() + ":" + this.getPort() + this.getDirectory() + (String ) properties.get("href") + "\" target=\"blank\">view</a>]"); } 560 out.write(new Hidden(idHidden, (String ) properties.get("href"), false).getHtml()); out.write("</td>"); out.write("<td style=\"text-align:right;\">" + (String ) properties.get("sizeStringValue") + "</td>"); out.write("<td>" + (String ) properties.get("sizeStringUnit") + "</td>"); out.write("<td></td>"); out.write("<td style='white-space:nowrap;'>" + (String ) properties.get("lastModifiedString") + "</td>"); out.write("<td></td>"); out.write("</tr>"); } 569 570 return i; 571 } 572 573 public Map getDAVProperties(XMLResponseMethodBase.Response response) { 574 Map properties = new Hashtable (); 575 576 Enumeration props = response.getProperties(); 577 String href = response.getHref(); 578 properties.put("href", href.replaceFirst(this.getDirectory(), StringUtils.EMPTY)); while (props.hasMoreElements()) { 580 Property property = (Property) props.nextElement(); 581 if (property.getLocalName().equalsIgnoreCase("getcontenttype")) { if (property.getPropertyAsString().equalsIgnoreCase("httpd/unix-directory")) { properties.put("displayType", "folder"); if (href.length() > this.getDirectory().length()) { 585 String name = href.replaceFirst(this.getDirectory(), StringUtils.EMPTY); 586 name = name.substring(0, name.length() - 1); 587 name = name.substring(0, name.length()); 588 properties.put("name", name); } 590 } 591 } 592 else if (property.getLocalName().equalsIgnoreCase("getcontentlength")) { properties.put("size", property.getPropertyAsString()); } 595 else if (property.getLocalName().equalsIgnoreCase("getlastmodified")) { properties.put("lastModifiedString", this.getFormattedDate(property.getPropertyAsString(), "MMM dd yyyy")); properties.put("lastModified", this.getFormattedDate(property.getPropertyAsString(), "yyyy-MM-dd, HH:mm:ss")); } 601 } 602 if (properties.get("name") == null) { if (href.length() > this.getDirectory().length()) { 604 String name = href.replaceFirst(this.getDirectory(), StringUtils.EMPTY); 605 properties.put("name", name); } 607 int index = href.lastIndexOf("."); if (index > -1) { 609 properties.put("displayType", (href.substring(index + 1)).toLowerCase()); } 611 else { 612 properties.put("displayType", "general"); } 614 } 615 if (properties.get("size") == null) { properties.put("size", StringUtils.EMPTY); properties.put("sizeStringValue", StringUtils.EMPTY); properties.put("sizeStringUnit", StringUtils.EMPTY); } 620 else { 621 String [] size = this.getFileSizeString((String ) properties.get("size")).split(" "); properties.put("sizeStringValue", size[0]); properties.put("sizeStringUnit", size[1]); } 625 return properties; 626 } 627 628 public String getFileSizeString(String fileSize) { 629 int bytes = (new Integer (fileSize)).intValue(); 630 int size = (bytes / 1024); 631 if (size == 0) { 632 return (bytes + " " + MessagesManager.get(this.getRequest(), "file.bytes")); } 634 else if (size >= 1024) { 635 return ((size / 1024) + " " + MessagesManager.get(this.getRequest(), "file.mb")); } 637 return (size + " " + MessagesManager.get(this.getRequest(), "file.kb")); } 639 640 public String getFormattedDate(String date, String format) { 641 SimpleDateFormat sdf = new SimpleDateFormat (format); 642 643 try { 644 Date x = DateFormat.getDateTimeInstance().parse(date); 645 return sdf.format(x); 646 } 647 catch (Exception e) { 648 log.error(e.getMessage(), e); 649 return date; 650 } 651 } 652 653 private String getHtmlSessionAttributeRemoveControl() { 654 return new Hidden(SESSION_ATTRIBUTENAME_DIALOGOBJECT_REMOVE, this 655 .getConfigValue(SESSION_ATTRIBUTENAME_DIALOGOBJECT), false).getHtml(); 656 } 657 } 658 | Popular Tags |