1 13 package info.magnolia.cms.core; 14 15 import info.magnolia.cms.security.AccessDeniedException; 16 import info.magnolia.cms.security.AccessManager; 17 import info.magnolia.cms.security.Permission; 18 19 import java.io.InputStream ; 20 import java.util.ArrayList ; 21 import java.util.Calendar ; 22 import java.util.Collection ; 23 24 import javax.jcr.Node; 25 import javax.jcr.PathNotFoundException; 26 import javax.jcr.Property; 27 import javax.jcr.PropertyIterator; 28 import javax.jcr.PropertyType; 29 import javax.jcr.RepositoryException; 30 import javax.jcr.Value; 31 32 import org.apache.commons.lang.StringUtils; 33 import org.slf4j.Logger; 34 import org.slf4j.LoggerFactory; 35 36 37 42 public class NodeData extends ContentHandler { 43 44 47 private static Logger log = LoggerFactory.getLogger(NodeData.class); 48 49 52 private Property property; 53 54 57 private Node node; 58 59 62 protected NodeData() { 63 } 65 66 73 protected NodeData(Node workingNode, String name, AccessManager manager) 74 throws PathNotFoundException, 75 RepositoryException, 76 AccessDeniedException { 77 Access.isGranted(manager, Path.getAbsolutePath(workingNode.getPath(), name), Permission.READ); 78 this.init(workingNode, name); 79 this.setAccessManager(manager); 80 } 81 82 92 protected NodeData(Node workingNode, String name, int type, boolean createNew, AccessManager manager) 93 throws PathNotFoundException, 94 RepositoryException, 95 AccessDeniedException { 96 if (createNew) { 97 Access.isGranted(manager, Path.getAbsolutePath(workingNode.getPath(), name), Permission.WRITE); 98 this.init(workingNode, name, type, null); 99 } 100 else { 101 Access.isGranted(manager, Path.getAbsolutePath(workingNode.getPath(), name), Permission.READ); 102 this.init(workingNode, name); 103 } 104 this.setAccessManager(manager); 105 } 106 107 115 protected NodeData(Node workingNode, String name, Value value, AccessManager manager) 116 throws PathNotFoundException, 117 RepositoryException, 118 AccessDeniedException { 119 Access.isGranted(manager, Path.getAbsolutePath(workingNode.getPath(), name), Permission.WRITE); 120 this.init(workingNode, name, value.getType(), value); 121 this.setAccessManager(manager); 122 } 123 124 128 public NodeData(Node node, AccessManager manager) 129 throws PathNotFoundException, 130 RepositoryException, 131 AccessDeniedException { 132 Access.isGranted(manager, Path.getAbsolutePath(node.getPath()), Permission.READ); 133 this.node = node; 134 this.property = this.node.getProperty(ItemType.JCR_DATA); 135 this.setAccessManager(manager); 136 } 137 138 142 public NodeData(Property property, AccessManager manager) 143 throws PathNotFoundException, 144 RepositoryException, 145 AccessDeniedException { 146 this.property = property; 147 Access.isGranted(manager, Path.getAbsolutePath(this.property.getPath()), Permission.READ); 148 this.setAccessManager(manager); 149 } 150 151 158 private void init(Node workingNode, String name, int type, Value value) throws PathNotFoundException, 159 RepositoryException, AccessDeniedException { 160 if (PropertyType.BINARY == type) { 161 this.node = workingNode.addNode(name, ItemType.NT_RESOURCE); 162 if (null != value) { 163 this.property = this.node.setProperty(ItemType.JCR_DATA, value, value.getType()); 164 } 165 } 166 else { 167 if (null == value) { 168 this.property = workingNode.setProperty(name, StringUtils.EMPTY); 169 } 170 else { 171 this.property = workingNode.setProperty(name, value, value.getType()); 172 } 173 } 174 } 175 176 182 private void init(Node workingNode, String name) throws PathNotFoundException, RepositoryException, 183 AccessDeniedException { 184 try { 185 this.property = workingNode.getProperty(name); 186 } 187 catch (PathNotFoundException e) { 188 if (workingNode.hasNode(name)) { 189 this.node = workingNode.getNode(name); 191 this.property = this.node.getProperty(ItemType.JCR_DATA); 192 } 193 else { 194 throw e; 195 } 196 } 197 } 198 199 212 public Value getValue() { 213 try { 214 return this.property.getValue(); 215 } 216 catch (Exception e) { 217 if (log.isDebugEnabled()) { 218 log.debug(e.getMessage(), e); 219 } 220 return null; 221 } 222 } 223 224 230 public String getString(String lineBreak) { 231 try { 232 return this.getString().replaceAll("\n", lineBreak); } 234 catch (Exception e) { 235 return StringUtils.EMPTY; 236 } 237 } 238 239 243 public String getString() { 244 try { 245 return this.property.getString(); 246 } 247 catch (Exception e) { 248 return StringUtils.EMPTY; 249 } 250 } 251 252 256 public long getLong() { 257 try { 258 return this.property.getLong(); 259 } 260 catch (Exception e) { 261 return 0; 262 } 263 } 264 265 269 public double getDouble() { 270 try { 271 return this.property.getDouble(); 272 } 273 catch (Exception e) { 274 return 0; 275 } 276 } 277 278 282 public Calendar getDate() { 283 try { 284 return this.property.getDate(); 285 } 286 catch (Exception e) { 287 return null; 288 } 289 } 290 291 295 public boolean getBoolean() { 296 try { 297 return this.property.getBoolean(); 298 } 299 catch (Exception e) { 300 return false; 301 } 302 } 303 304 308 public InputStream getStream() { 309 try { 310 return this.property.getStream(); 311 } 312 catch (Exception e) { 313 return null; 314 } 315 } 316 317 330 public int getType() { 331 if (this.property != null) { 332 try { 333 return this.property.getType(); 334 } 335 catch (Exception e) { 336 log.warn("Unable to read property type for " + this.property); } 338 } 339 return PropertyType.UNDEFINED; 340 } 341 342 345 public String getName() { 346 try { 347 if (null != this.node) { 349 return this.node.getName(); 350 } 351 return this.property.getName(); 352 } 353 catch (Exception e) { 354 log.warn("Unable to read property name for " + this.property); return StringUtils.EMPTY; 356 } 357 } 358 359 363 public long getContentLength() { 364 try { 365 return this.property.getLength(); 366 } 367 catch (RepositoryException re) { 368 log.warn("Unable to read content length for " + this.property); return 0; 370 } 371 } 372 373 377 public Property getJCRProperty() { 378 return this.property; 379 } 380 381 386 public void setValue(String value) throws RepositoryException, AccessDeniedException { 387 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET); 388 this.property.setValue(value); 389 } 390 391 396 public void setValue(int value) throws RepositoryException, AccessDeniedException { 397 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET); 398 this.property.setValue(value); 399 } 400 401 406 public void setValue(long value) throws RepositoryException, AccessDeniedException { 407 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET); 408 this.property.setValue(value); 409 } 410 411 416 public void setValue(InputStream value) throws RepositoryException, AccessDeniedException { 417 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET); 418 if (this.node != null) { 419 this.property = this.node.setProperty(ItemType.JCR_DATA, value); 420 } 421 else { 422 log.error("This is not a valid Binary type, Binary NodeData must be created with PropertyType.BINARY"); 423 } 424 } 425 426 431 public void setValue(double value) throws RepositoryException, AccessDeniedException { 432 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET); 433 this.property.setValue(value); 434 } 435 436 441 public void setValue(boolean value) throws RepositoryException, AccessDeniedException { 442 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET); 443 this.property.setValue(value); 444 } 445 446 451 public void setValue(Calendar value) throws RepositoryException, AccessDeniedException { 452 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET); 453 this.property.setValue(value); 454 } 455 456 461 public void setValue(Value value) throws RepositoryException, AccessDeniedException { 462 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET); 463 this.property.setValue(value); 464 } 465 466 474 public void setAttribute(String name, String value) throws RepositoryException, AccessDeniedException, 475 UnsupportedOperationException { 476 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET); 477 if (null == this.node) { 478 throw new UnsupportedOperationException ("Attributes are only supported for BINARY type"); 479 } 480 this.node.setProperty(name, value); 481 } 482 483 491 public void setAttribute(String name, Calendar value) throws RepositoryException, AccessDeniedException, 492 UnsupportedOperationException { 493 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET); 494 if (null == this.node) { 495 throw new UnsupportedOperationException ("Attributes are only supported for BINARY type"); 496 } 497 this.node.setProperty(name, value); 498 } 499 500 505 public String getAttribute(String name) { 506 if (null == this.node) { 507 return ""; 508 } 509 try { 510 return this.node.getProperty(name).getString(); 511 } 512 catch (RepositoryException re) { 513 if (log.isDebugEnabled()) { 514 log.debug("Attribute [ " + name + " ] not set"); 515 } 516 return ""; 517 } 518 } 519 520 525 public Collection getAttributeNames() throws RepositoryException { 526 Collection names = new ArrayList (); 527 if (this.node == null) { 528 if (log.isDebugEnabled()) { 529 log.debug("Attributes are only supported for BINARY type"); 530 } 531 return names; 532 } 533 PropertyIterator properties = this.node.getProperties(); 534 while (properties.hasNext()) { 535 String name = properties.nextProperty().getName(); 536 if (!name.equalsIgnoreCase(ItemType.JCR_DATA)) { 537 names.add(name); 538 } 539 } 540 return names; 541 } 542 543 547 public boolean isExist() { 548 return (this.property != null); 549 } 550 551 555 public String getHandle() { 556 try { 557 if (null != this.node) { 558 return this.node.getPath(); 559 } 560 return this.property.getPath(); 561 } 562 catch (RepositoryException e) { 563 log.error("Failed to get handle"); log.error(e.getMessage(), e); 565 return StringUtils.EMPTY; 566 } 567 } 568 569 573 public void save() throws RepositoryException { 574 this.property.getSession().save(); 575 } 576 577 582 public boolean isGranted(long permissions) { 583 try { 584 Access.isGranted(this.accessManager, Path.getAbsolutePath(property.getPath()), permissions); 585 return true; 586 } 587 catch (RepositoryException re) { 588 log.error(re.getMessage(), re); 589 } 590 return false; 591 } 592 593 597 public void delete() throws RepositoryException { 598 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.property.getPath()), Permission.REMOVE); 599 if (null != this.node) { 600 this.node.remove(); 601 } 602 else { 603 this.property.remove(); 604 } 605 } 606 607 612 public void refresh(boolean keepChanges) throws RepositoryException { 613 this.property.refresh(keepChanges); 614 } 615 616 } 617 | Popular Tags |