1 13 package info.magnolia.cms.core; 14 15 import info.magnolia.cms.core.search.QueryManager; 16 import info.magnolia.cms.security.AccessDeniedException; 17 import info.magnolia.cms.security.AccessManager; 18 import info.magnolia.cms.security.Authenticator; 19 import info.magnolia.cms.security.Permission; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 import javax.jcr.ItemNotFoundException; 25 import javax.jcr.Node; 26 import javax.jcr.PathNotFoundException; 27 import javax.jcr.RepositoryException; 28 import javax.jcr.Workspace; 29 import javax.servlet.http.HttpServletRequest ; 30 31 import org.apache.commons.lang.StringUtils; 32 import org.slf4j.Logger; 33 import org.slf4j.LoggerFactory; 34 35 36 40 public class HierarchyManager { 41 42 45 private static Logger log = LoggerFactory.getLogger(HierarchyManager.class); 46 47 50 private Node startPage; 51 52 55 private Workspace workSpace; 56 57 60 private String userID; 61 62 65 private AccessManager accessManager; 66 67 70 private QueryManager queryManager; 71 72 75 public HierarchyManager() { 76 this.userID = "anonymous"; } 78 79 public HierarchyManager(String userID) { 80 this.userID = userID; 81 } 82 83 86 public HierarchyManager(HttpServletRequest request) { 87 this.userID = Authenticator.getUserId(request); 88 } 89 90 97 public void setStartPage(Node rootNode) throws PathNotFoundException, RepositoryException { 98 this.startPage = rootNode; 99 this.workSpace = this.startPage.getSession().getWorkspace(); 100 } 101 102 107 public void init(Node rootNode) throws PathNotFoundException, RepositoryException { 108 this.startPage = rootNode; 109 this.workSpace = this.startPage.getSession().getWorkspace(); 110 } 111 112 117 public void init(Node rootNode, AccessManager manager) throws PathNotFoundException, RepositoryException { 118 this.startPage = rootNode; 119 this.workSpace = this.startPage.getSession().getWorkspace(); 120 this.accessManager = manager; 121 } 122 123 127 public void setAccessManager(AccessManager accessManager) { 128 this.accessManager = accessManager; 129 } 130 131 135 public AccessManager getAccessManager() { 136 return this.accessManager; 137 } 138 139 143 public void setQueryManager(QueryManager queryManager) { 144 this.queryManager = queryManager; 145 } 146 147 public QueryManager getQueryManager() { 148 return this.queryManager; 149 } 150 151 159 public Content createPage(String path, String label) throws PathNotFoundException, RepositoryException, 160 AccessDeniedException { 161 Content newPage = (new Content( 162 this.startPage, 163 this.getNodePath(path, label), 164 ItemType.CONTENT.getSystemName(), 165 this.accessManager)); 166 this.setMetaData(newPage.getMetaData()); 167 return newPage; 168 } 169 170 180 public Content createContent(String path, String label, String contentType) throws PathNotFoundException, 181 RepositoryException, AccessDeniedException { 182 Content newPage = new Content(this.startPage, this.getNodePath(path, label), contentType, this.accessManager); 183 setMetaData(newPage.getMetaData()); 184 return newPage; 185 } 186 187 private String getNodePath(String path, String label) { 188 if (StringUtils.isEmpty(path) || (path.equals("/"))) { return label; 190 } 191 return getNodePath(path + "/" + label); } 193 194 private String getNodePath(String path) { 195 if (path != null && path.startsWith("/")) { return path.replaceFirst("/", StringUtils.EMPTY); } 198 return path; 199 } 200 201 205 public void setMetaData(MetaData md, String template) throws RepositoryException, AccessDeniedException { 206 md.setTemplate(template); 207 setMetaData(md); 208 } 209 210 214 public void setMetaData(MetaData md) throws RepositoryException, AccessDeniedException { 215 md.setCreationDate(); 216 md.setModificationDate(); 217 md.setAuthorId(this.userID); 218 md.setTitle(StringUtils.EMPTY); 219 } 220 221 225 public void updateMetaData(MetaData md) throws RepositoryException, AccessDeniedException { 226 md.setModificationDate(); 227 md.setAuthorId(this.userID); 228 } 229 230 238 public Content getPage(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException { 239 return this.getContent(path); 240 } 241 242 249 public Content getContent(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException { 250 if (path.equals("/")) { return this.getRoot(); 252 } 253 return (new Content(this.startPage, getNodePath(path), this.accessManager)); 254 } 255 256 265 public Content getContent(String path, boolean create, ItemType type) throws AccessDeniedException, 266 RepositoryException { 267 Content node; 268 try { 269 node = getContent(path); 270 } 271 catch (PathNotFoundException e) { 272 if (create) { 273 node = this.createContent(StringUtils.substringBeforeLast(path, "/"), StringUtils.substringAfterLast( 274 path, 275 "/"), type.toString()); 276 } 277 else { 278 throw e; 279 } 280 } 281 return node; 282 } 283 284 292 public Content getContentNode(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException { 293 return new Content(this.startPage, getNodePath(path), this.accessManager); 294 } 295 296 303 public NodeData getNodeData(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException { 304 String nodePath = getNodePath(path); 305 if (StringUtils.isEmpty(nodePath)) { 306 return null; 307 } 308 309 return new NodeData(this.startPage, nodePath, this.accessManager); 310 } 311 312 321 public Content getPage(String path, String templateName) throws PathNotFoundException, RepositoryException, 322 AccessDeniedException { 323 Content page = getContent(path); 324 if (page.getTemplate().equals(templateName)) { 325 return page; 326 } 327 Content pageToBeFound = null; 328 try { 329 if (page.hasChildren()) { 330 Collection children = page.getChildren(ItemType.CONTENT.getSystemName()); 331 Iterator iterator = children.iterator(); 332 while (iterator.hasNext()) { 333 Content child = (Content) iterator.next(); 334 if (child.getTemplate().equals(templateName)) { 335 return child; 336 } 337 if (child.hasChildren()) { 338 pageToBeFound = getPage(child.getHandle(), templateName); 339 } 340 if (pageToBeFound != null) { 341 return pageToBeFound; 342 } 343 } 344 } 345 } 346 catch (Exception e) { 347 log.error("Failed to get - " + path); log.error(e.getMessage(), e); 349 } 350 return pageToBeFound; 351 } 352 353 360 public void delete(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException { 361 if (this.isNodeData(path)) { 362 this.getNodeData(makeRelative(path)).delete(); 363 } 364 else { 365 this.startPage.getNode(makeRelative(path)).remove(); 366 } 367 368 } 369 370 private String makeRelative(String path) { 371 return StringUtils.stripStart(path, "/"); } 373 374 377 public Content getRoot() throws RepositoryException, AccessDeniedException { 378 return (new Content(this.startPage, this.accessManager)); 379 } 380 381 387 public boolean isPage(String path) throws AccessDeniedException { 388 Access.isGranted(this.accessManager, path, Permission.READ); 389 390 String nodePath = getNodePath(path); 391 if (StringUtils.isEmpty(nodePath)) { 392 return false; 393 } 394 395 try { 396 Node n = this.startPage.getNode(nodePath); 397 return (n.isNodeType(ItemType.CONTENT.getSystemName())); 398 } 399 catch (RepositoryException re) { 400 } 402 return false; 403 } 404 405 409 public boolean isExist(String path) { 410 try { 411 Access.isGranted(this.accessManager, path, Permission.READ); 412 } 413 catch (AccessDeniedException e) { 414 log.error(e.getMessage()); 415 return false; 416 } 417 boolean isExist = false; 418 try { 419 isExist = this.workSpace.getSession().itemExists(path); 420 } 421 catch (RepositoryException re) { 422 log.error("Exception caught", re); 423 } 424 return isExist; 425 } 426 427 430 public boolean isNodeType(String path, String type) { 431 try { 432 Node n = this.startPage.getNode(getNodePath(path)); 433 return n.isNodeType(type); 434 } 435 catch (RepositoryException re) { 436 log.error(re.getMessage()); 437 log.debug(re.getMessage(), re); 438 } 439 return false; 440 } 441 442 445 public boolean isNodeType(String path, ItemType type) { 446 return isNodeType(path, type.getSystemName()); 447 } 448 449 454 public boolean isNodeData(String path) throws AccessDeniedException { 455 Access.isGranted(this.accessManager, path, Permission.READ); 456 boolean result = false; 457 String nodePath = getNodePath(path); 458 if (StringUtils.isEmpty(nodePath)) { 459 return false; 460 } 461 try { 462 result = this.startPage.hasProperty(nodePath); 463 if (!result) { 464 result = this.startPage.hasProperty(nodePath + "/" + ItemType.JCR_DATA); 466 } 467 } 468 catch (RepositoryException e) { 469 } 471 return result; 472 } 473 474 479 public Content getContentByUUID(String uuid) throws ItemNotFoundException, RepositoryException, 480 AccessDeniedException { 481 return new Content(this.startPage.getSession().getNodeByUUID(uuid), this.accessManager); 482 } 483 484 487 public Workspace getWorkspace() { 488 return this.workSpace; 489 } 490 491 498 public void moveTo(String source, String destination) throws PathNotFoundException, RepositoryException, 499 AccessDeniedException { 500 Access.isGranted(this.accessManager, source, Permission.REMOVE); 501 Access.isGranted(this.accessManager, destination, Permission.WRITE); 502 this.workSpace.move(source, destination); 503 } 504 505 512 public void copyTo(String source, String destination) throws PathNotFoundException, RepositoryException, 513 AccessDeniedException { 514 Access.isGranted(this.accessManager, source, Permission.READ); 515 Access.isGranted(this.accessManager, destination, Permission.WRITE); 516 this.workSpace.copy(source, destination); 517 } 518 519 523 public void save() throws RepositoryException { 524 try { 525 this.startPage.getSession().save(); 526 } 527 catch (RepositoryException re) { 528 log.error(re.getMessage(), re); 529 throw re; 530 } 531 } 532 533 536 public boolean hasPendingChanges() throws RepositoryException { 537 return this.startPage.getSession().hasPendingChanges(); 538 } 539 540 546 public void refresh(boolean keepChanges) throws RepositoryException { 547 this.workSpace.getSession().refresh(keepChanges); 548 } 549 550 } 551 | Popular Tags |