1 17 package org.alfresco.repo.webdav; 18 19 import java.io.UnsupportedEncodingException ; 20 import java.net.URLEncoder ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 25 26 import org.alfresco.model.ContentModel; 27 import org.alfresco.service.ServiceRegistry; 28 import org.alfresco.service.cmr.dictionary.DictionaryService; 29 import org.alfresco.service.cmr.lock.LockService; 30 import org.alfresco.service.cmr.model.FileFolderService; 31 import org.alfresco.service.cmr.model.FileInfo; 32 import org.alfresco.service.cmr.model.FileNotFoundException; 33 import org.alfresco.service.cmr.repository.CopyService; 34 import org.alfresco.service.cmr.repository.MimetypeService; 35 import org.alfresco.service.cmr.repository.NodeRef; 36 import org.alfresco.service.cmr.repository.NodeService; 37 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; 38 import org.alfresco.service.cmr.search.SearchService; 39 import org.alfresco.service.cmr.security.AuthenticationService; 40 import org.alfresco.service.namespace.NamespaceService; 41 import org.alfresco.util.EqualsHelper; 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 import org.xml.sax.helpers.AttributesImpl ; 45 46 53 public class WebDAVHelper 54 { 55 57 public static final String PathSeperator = "/"; 59 public static final char PathSeperatorChar = '/'; 60 61 private static Log logger = LogFactory.getLog("org.alfresco.protocol.webdav"); 63 64 private ServiceRegistry m_serviceRegistry; 66 67 private NodeService m_nodeService; 69 private FileFolderService m_fileFolderService; 70 private SearchService m_searchService; 71 private NamespaceService m_namespaceService; 72 private DictionaryService m_dictionaryService; 73 private MimetypeService m_mimetypeService; 74 private LockService m_lockService; 75 private AuthenticationService m_authService; 76 77 79 private AttributesImpl m_nullAttribs = new AttributesImpl (); 80 81 87 protected WebDAVHelper(ServiceRegistry serviceRegistry, AuthenticationService authService) 88 { 89 m_serviceRegistry = serviceRegistry; 90 91 m_nodeService = m_serviceRegistry.getNodeService(); 92 m_fileFolderService = m_serviceRegistry.getFileFolderService(); 93 m_searchService = m_serviceRegistry.getSearchService(); 94 m_namespaceService = m_serviceRegistry.getNamespaceService(); 95 m_dictionaryService = m_serviceRegistry.getDictionaryService(); 96 m_mimetypeService = m_serviceRegistry.getMimetypeService(); 97 m_lockService = m_serviceRegistry.getLockService(); 98 99 m_authService = authService; 100 } 101 102 107 public final AuthenticationService getAuthenticationService() 108 { 109 return m_authService; 110 } 111 112 117 public final ServiceRegistry getServiceRegistry() 118 { 119 return m_serviceRegistry; 120 } 121 122 127 public final NodeService getNodeService() 128 { 129 return m_nodeService; 130 } 131 132 public FileFolderService getFileFolderService() 133 { 134 return m_fileFolderService; 135 } 136 137 142 public final SearchService getSearchService() 143 { 144 return m_searchService; 145 } 146 147 152 public final NamespaceService getNamespaceService() 153 { 154 return m_namespaceService; 155 } 156 157 162 public final DictionaryService getDictionaryService() 163 { 164 return m_dictionaryService; 165 } 166 167 172 public final MimetypeService getMimetypeService() 173 { 174 return m_mimetypeService; 175 } 176 177 182 public final LockService getLockService() 183 { 184 return m_lockService; 185 } 186 187 192 public final CopyService getCopyService() 193 { 194 return getServiceRegistry().getCopyService(); 195 } 196 197 204 public final String [] splitPath(String path) 205 { 206 if (path == null) 207 throw new IllegalArgumentException ("path may not be null"); 208 209 String [] pathStr = new String [] {"", ""}; 211 212 214 int pos = path.lastIndexOf(PathSeperatorChar); 215 if (pos == -1 || pos == (path.length() - 1)) 216 { 217 pathStr[1] = path; 219 } 220 else 221 { 222 pathStr[0] = path.substring(0, pos); 223 pathStr[1] = path.substring(pos + 1); 224 } 225 return pathStr; 227 } 228 229 235 public final List <String > splitAllPaths(String path) 236 { 237 if (path == null || path.length() == 0) 238 { 239 return Collections.emptyList(); 240 } 241 242 StringTokenizer token = new StringTokenizer (path, PathSeperator); 244 List <String > results = new ArrayList <String >(10); 245 while (token.hasMoreTokens()) 246 { 247 results.add(token.nextToken()); 248 } 249 return results; 250 } 251 252 261 public final FileInfo getNodeForPath(NodeRef rootNodeRef, String path, String servletPath) throws FileNotFoundException 262 { 263 if (rootNodeRef == null) 264 { 265 throw new IllegalArgumentException ("Root node may not be null"); 266 } 267 else if (path == null) 268 { 269 throw new IllegalArgumentException ("Path may not be null"); 270 } 271 272 FileFolderService fileFolderService = getFileFolderService(); 273 if ( path.length() == 0 || path.equals(PathSeperator) || EqualsHelper.nullSafeEquals(path, servletPath)) 275 { 276 return fileFolderService.getFileInfo(rootNodeRef); 277 } 278 279 if (servletPath != null && servletPath.length() > 0 && path.startsWith(servletPath)) 281 { 282 path = path.substring(servletPath.length()); 284 } 285 286 List <String > splitPath = splitAllPaths(path); 288 289 FileInfo fileInfo = m_fileFolderService.resolveNamePath(rootNodeRef, splitPath); 291 292 if (logger.isDebugEnabled()) 294 { 295 logger.debug("Fetched node for path: \n" + 296 " root: " + rootNodeRef + "\n" + 297 " path: " + path + "\n" + 298 " servlet path: " + servletPath + "\n" + 299 " result: " + fileInfo); 300 } 301 return fileInfo; 302 } 303 304 public final FileInfo getParentNodeForPath(NodeRef rootNodeRef, String path, String servletPath) throws FileNotFoundException 305 { 306 if (rootNodeRef == null) 307 { 308 throw new IllegalArgumentException ("Root node may not be null"); 309 } 310 else if (path == null) 311 { 312 throw new IllegalArgumentException ("Path may not be null"); 313 } 314 String [] paths = splitPath(path); 316 return getNodeForPath(rootNodeRef, paths[0], servletPath); 317 } 318 319 326 public final String getPathFromNode(NodeRef rootNodeRef, NodeRef nodeRef) throws FileNotFoundException 327 { 328 if (rootNodeRef == null || nodeRef == null) 330 throw new IllegalArgumentException ("Invalid node(s) in getPathFromNode call"); 331 332 if (rootNodeRef.equals(nodeRef)) 334 return ""; 335 336 FileFolderService fileFolderService = getFileFolderService(); 337 338 List <FileInfo> pathInfos = fileFolderService.getNamePath(rootNodeRef, nodeRef); 340 341 StringBuilder sb = new StringBuilder (pathInfos.size() * 20); 343 for (FileInfo fileInfo : pathInfos) 344 { 345 sb.append(WebDAVHelper.PathSeperatorChar); 346 sb.append(fileInfo.getName()); 347 } 348 if (logger.isDebugEnabled()) 350 { 351 logger.debug("Build name path for node: \n" + 352 " root: " + rootNodeRef + "\n" + 353 " target: " + nodeRef + "\n" + 354 " path: " + sb); 355 } 356 return sb.toString(); 357 } 358 359 365 public final String makeETag(NodeRef node) 366 { 367 369 StringBuilder etag = new StringBuilder (); 370 makeETagString(node, etag); 371 return etag.toString(); 372 } 373 374 380 public final String makeQuotedETag(NodeRef node) 381 { 382 StringBuilder etag = new StringBuilder (); 383 384 etag.append("\""); 385 makeETagString(node, etag); 386 etag.append("\""); 387 return etag.toString(); 388 } 389 390 396 protected final void makeETagString(NodeRef node, StringBuilder etag) 397 { 398 400 Object modVal = getNodeService().getProperty(node, ContentModel.PROP_MODIFIED); 401 402 etag.append(node.getId()); 403 404 if ( modVal != null) 405 { 406 etag.append("_"); 407 etag.append(DefaultTypeConverter.INSTANCE.longValue(modVal)); 408 } 409 } 410 411 416 public final AttributesImpl getNullAttributes() 417 { 418 return m_nullAttribs; 419 } 420 421 426 public final static String encodeURL(String s) 427 { 428 try 429 { 430 return replace(URLEncoder.encode(s, "UTF-8"), "+", "%20"); 431 } 432 catch (UnsupportedEncodingException err) 433 { 434 throw new RuntimeException (err); 435 } 436 } 437 438 447 public static String replace(String str, String repl, String with) 448 { 449 int lastindex = 0; 450 int pos = str.indexOf(repl); 451 452 if (pos < 0) 455 { 456 return str; 457 } 458 459 int len = repl.length(); 460 int lendiff = with.length() - repl.length(); 461 StringBuilder out = new StringBuilder ((lendiff <= 0) ? str.length() : (str.length() + (lendiff << 3))); 462 for (; pos >= 0; pos = str.indexOf(repl, lastindex = pos + len)) 463 { 464 out.append(str.substring(lastindex, pos)).append(with); 465 } 466 467 return out.append(str.substring(lastindex, str.length())).toString(); 468 } 469 470 475 public final static String encodeHTML(String string) 476 { 477 if (string == null) 478 { 479 return ""; 480 } 481 482 StringBuilder sb = null; String enc; 484 char c; 485 for (int i = 0; i < string.length(); i++) 486 { 487 enc = null; 488 c = string.charAt(i); 489 switch (c) 490 { 491 case '"': enc = """; break; case '&': enc = "&"; break; case '<': enc = "<"; break; case '>': enc = ">"; break; 496 case '\u00E4' : enc = "ä"; break; 498 case '\u00C4' : enc = "Ä"; break; 499 case '\u00F6' : enc = "ö"; break; 500 case '\u00D6' : enc = "Ö"; break; 501 case '\u00FC' : enc = "ü"; break; 502 case '\u00DC' : enc = "Ü"; break; 503 case '\u00DF' : enc = "ß"; break; 504 505 case '\u20AC': enc = "€"; break; 508 case '\u00AB': enc = "«"; break; 509 case '\u00BB': enc = "»"; break; 510 case '\u00A0': enc = " "; break; 511 512 default: 513 if (((int)c) >= 0x80) 514 { 515 enc = "&#" + ((int)c) + ";"; 517 } 518 break; 519 } 520 521 if (enc != null) 522 { 523 if (sb == null) 524 { 525 String soFar = string.substring(0, i); 526 sb = new StringBuilder (i + 8); 527 sb.append(soFar); 528 } 529 sb.append(enc); 530 } 531 else 532 { 533 if (sb != null) 534 { 535 sb.append(c); 536 } 537 } 538 } 539 540 if (sb == null) 541 { 542 return string; 543 } 544 else 545 { 546 return sb.toString(); 547 } 548 } 549 } 550 | Popular Tags |