1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.core.version;
35
36 import info.magnolia.cms.core.AbstractContent;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.HierarchyManager;
39 import info.magnolia.cms.core.ItemType;
40 import info.magnolia.cms.core.NodeData;
41 import info.magnolia.cms.core.DefaultContent;
42 import info.magnolia.cms.security.AccessDeniedException;
43 import info.magnolia.cms.security.AccessManager;
44 import info.magnolia.cms.security.AccessManagerImpl;
45 import info.magnolia.cms.security.Permission;
46 import info.magnolia.cms.security.PermissionImpl;
47 import info.magnolia.cms.util.ContentWrapper;
48 import info.magnolia.cms.util.HierarchyManagerWrapper;
49 import info.magnolia.cms.util.Rule;
50 import info.magnolia.cms.util.SimpleUrlPattern;
51
52 import java.util.ArrayList;
53 import java.util.Calendar;
54 import java.util.Collection;
55 import java.util.List;
56
57 import javax.jcr.PathNotFoundException;
58 import javax.jcr.RepositoryException;
59 import javax.jcr.Value;
60 import javax.jcr.Workspace;
61 import javax.jcr.lock.Lock;
62 import javax.jcr.lock.LockException;
63 import javax.jcr.nodetype.NodeType;
64 import javax.jcr.version.Version;
65 import javax.jcr.version.VersionHistory;
66 import javax.jcr.version.VersionIterator;
67
68 import org.apache.commons.lang.StringUtils;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72
73
74
75
76
77 public class ContentVersion extends DefaultContent {
78
79 private final class FixParentContentWrapper extends ContentWrapper {
80 private final Content parent;
81
82 private FixParentContentWrapper(Content wrappedContent, Content parent) {
83 super(wrappedContent);
84 this.parent = parent;
85 }
86
87 @Override
88 public Content getParent() throws RepositoryException {
89 return parent;
90 }
91
92 @Override
93 protected Content wrap(Content node) {
94 return new FixParentContentWrapper(node, this);
95 }
96 }
97
98
99
100
101 private static Logger log = LoggerFactory.getLogger(ContentVersion.class);
102
103
104
105
106 public static final String VERSION_USER = "versionUser";
107
108
109
110
111 public static final String NAME = "name";
112
113
114
115
116 private Version state;
117
118
119
120
121 private AbstractContent base;
122
123
124
125
126 private Rule rule;
127
128
129
130
131
132
133
134 public ContentVersion(Version thisVersion, AbstractContent base) throws RepositoryException {
135 if (thisVersion == null) {
136 throw new RepositoryException("Failed to get ContentVersion, version does not exist");
137 }
138 this.state = thisVersion;
139 this.base = base;
140
141 this.hierarchyManager = new HierarchyManagerWrapper(base.getHierarchyManager()) {
142 private AccessManagerImpl accessManager;
143
144 {
145
146 List<Permission> permissions = new ArrayList<Permission>(getWrappedHierarchyManager().getAccessManager().getPermissionList());
147 PermissionImpl p = new PermissionImpl();
148 p.setPattern(new SimpleUrlPattern("/jcr:system/jcr:versionStorage/*"));
149
150 p.setPermissions(8);
151 permissions.add(p);
152
153 accessManager = new AccessManagerImpl();
154 accessManager.setPermissionList(permissions);
155 }
156
157 @Override
158 public AccessManager getAccessManager() {
159 return accessManager;
160 }
161 };
162 this.init();
163 }
164
165
166
167
168
169 private void init() throws RepositoryException {
170 this.setNode(this.state.getNode(ItemType.JCR_FROZENNODE));
171 try {
172 if (!StringUtils.equalsIgnoreCase(this.state.getName(), VersionManager.ROOT_VERSION)) {
173 this.rule = VersionManager.getInstance().getUsedFilter(this);
174 }
175 }
176 catch (Exception e) {
177 log.error(e.getMessage(), e);
178 }
179 if (this.rule == null) {
180 log.info("failed to get filter used for creating this version, use open filter");
181 this.rule = new Rule();
182 }
183 }
184
185
186
187
188
189
190 public Calendar getCreated() throws RepositoryException {
191 return this.state.getCreated();
192 }
193
194
195
196
197
198
199 public String getVersionLabel() throws RepositoryException {
200 return this.state.getName();
201 }
202
203
204
205
206
207
208 public VersionHistory getContainingHistory() throws RepositoryException {
209 return this.state.getContainingHistory();
210 }
211
212
213
214
215 public String getName() {
216 try {
217 return VersionManager.getInstance().getSystemNode(this).getNodeData(NAME).getString();
218 }
219 catch (RepositoryException re) {
220 log.error("Failed to retrieve name from version system node", re);
221 return "";
222 }
223 }
224
225
226
227
228 public String getUserName() {
229 try {
230 return VersionManager.getInstance().getSystemNode(this).getNodeData(VERSION_USER).getString();
231 }
232 catch (RepositoryException re) {
233 log.error("Failed to retrieve user from version system node", re);
234 return "";
235 }
236 }
237
238
239
240
241 public String getHandle() {
242 return this.base.getHandle();
243 }
244
245 public Content getContent(String name) throws PathNotFoundException, RepositoryException, AccessDeniedException {
246 return new FixParentContentWrapper(super.getContent(name), this);
247 }
248
249
250
251
252
253
254
255
256 public Content createContent(String name) throws AccessDeniedException {
257 throw new AccessDeniedException("Not allowed to write on version preview");
258 }
259
260
261
262
263
264
265
266
267
268 public Content createContent(String name, String contentType) throws AccessDeniedException {
269 throw new AccessDeniedException("Not allowed to write on version preview");
270 }
271
272
273
274
275
276
277
278
279
280 public Content createContent(String name, ItemType contentType) throws AccessDeniedException {
281 throw new AccessDeniedException("Not allowed to write on version preview");
282 }
283
284
285
286
287
288
289
290
291 public NodeData createNodeData(String name) throws AccessDeniedException {
292 throw new AccessDeniedException("Not allowed to write on version preview");
293 }
294
295
296
297
298
299
300
301
302
303
304 public NodeData createNodeData(String name, Value value, int type) throws AccessDeniedException {
305 throw new AccessDeniedException("Not allowed to write on version preview");
306 }
307
308
309
310
311
312
313
314
315
316 public NodeData createNodeData(String name, Value value) throws AccessDeniedException {
317 throw new AccessDeniedException("Not allowed to write on version preview");
318 }
319
320
321
322
323
324
325
326
327
328 public NodeData createNodeData(String name, int type) throws AccessDeniedException {
329 throw new AccessDeniedException("Not allowed to write on version preview");
330 }
331
332
333
334
335
336 public void deleteNodeData(String name) throws RepositoryException {
337 throw new AccessDeniedException("Not allowed to write on version preview");
338 }
339
340
341
342
343
344
345 public void updateMetaData() throws AccessDeniedException {
346 throw new AccessDeniedException("Not allowed to write on version preview");
347 }
348
349
350
351
352
353 public Collection<Content> getChildren() {
354 try {
355 if (this.rule.isAllowed(this.base.getNodeTypeName())) {
356 return wrap(super.getChildren());
357 }
358 }
359 catch (RepositoryException re) {
360 log.error(re.getMessage(), re);
361 }
362 return this.base.getChildren();
363 }
364
365
366
367
368
369
370 public Collection<Content> getChildren(String contentType) {
371 if (this.rule.isAllowed(contentType)) {
372 return wrap(super.getChildren(contentType));
373 }
374 return this.base.getChildren(contentType);
375 }
376
377
378
379
380
381
382 public Collection<Content> getChildren(ItemType contentType) {
383 return this.getChildren(contentType.getSystemName());
384 }
385
386
387
388
389
390
391
392 public Collection<Content> getChildren(String contentType, String namePattern) {
393 if (this.rule.isAllowed(contentType)) {
394 return wrap(super.getChildren(contentType, namePattern));
395 }
396 return this.base.getChildren(contentType, namePattern);
397 }
398
399 private Collection<Content> wrap(Collection<Content> children) {
400 List<Content> transformed = new ArrayList<Content>();
401 for (Content child : children) {
402 transformed.add(new FixParentContentWrapper(child, this));
403 }
404 return transformed;
405 }
406
407
408
409
410 public boolean hasChildren() {
411 return (this.getChildren().size() > 0);
412 }
413
414
415
416
417
418 public boolean hasChildren(String contentType) {
419 return (this.getChildren(contentType).size() > 0);
420 }
421
422
423
424
425
426
427
428
429
430 public Content getParent() throws PathNotFoundException, RepositoryException, AccessDeniedException {
431 return this.base.getParent();
432 }
433
434
435
436
437
438
439
440
441
442 public Content getAncestor(int level) throws PathNotFoundException, RepositoryException, AccessDeniedException {
443 return this.base.getAncestor(level);
444 }
445
446
447
448
449
450
451 public Collection<Content> getAncestors() throws PathNotFoundException, RepositoryException {
452 return this.base.getAncestors();
453 }
454
455
456
457
458
459
460
461 public int getLevel() throws PathNotFoundException, RepositoryException {
462 return this.base.getLevel();
463 }
464
465
466
467
468
469
470
471 public void orderBefore(String srcName, String beforeName) throws RepositoryException {
472 throw new AccessDeniedException("Not allowed to write on version preview");
473 }
474
475
476
477
478
479
480
481
482
483 public int getIndex() throws RepositoryException {
484 return this.base.getIndex();
485 }
486
487
488
489
490
491 public NodeType getNodeType() throws RepositoryException {
492 log.warn("This is a Version node, it will always return NT_FROZEN as node type.");
493 log.warn("Use getNodeTypeName to retrieve base node primary type");
494 return super.getNodeType();
495 }
496
497
498
499
500
501
502
503 public void restore(String versionName, boolean removeExisting) throws RepositoryException {
504 throw new AccessDeniedException("Not allowed to write on version preview");
505 }
506
507
508
509
510
511
512
513 public void restore(Version version, boolean removeExisting) throws RepositoryException {
514 throw new AccessDeniedException("Not allowed to write on version preview");
515 }
516
517
518
519
520
521
522
523
524 public void restore(Version version, String relPath, boolean removeExisting) throws RepositoryException {
525 throw new AccessDeniedException("Not allowed to write on version preview");
526 }
527
528
529
530
531
532
533
534 public void restoreByLabel(String versionLabel, boolean removeExisting) throws RepositoryException {
535 throw new AccessDeniedException("Not allowed to write on version preview");
536 }
537
538
539
540
541
542 public Version addVersion() throws RepositoryException {
543 throw new AccessDeniedException("Not allowed to add version on version preview");
544 }
545
546
547
548
549
550
551
552 public Version addVersion(Rule rule) throws RepositoryException {
553 throw new AccessDeniedException("Not allowed to add version on version preview");
554 }
555
556
557
558
559
560
561
562
563
564
565 public boolean isModified() {
566 log.error("Not valid for version");
567 return false;
568 }
569
570
571
572
573
574 public VersionHistory getVersionHistory() throws RepositoryException {
575 throw new AccessDeniedException("Not allowed to read VersionHistory of Version");
576 }
577
578
579
580
581
582 public VersionIterator getAllVersions() throws RepositoryException {
583 throw new AccessDeniedException("Not allowed to get VersionIterator of Version");
584 }
585
586
587
588
589
590
591
592 public ContentVersion getBaseVersion() throws RepositoryException {
593 throw new AccessDeniedException("Not allowed to get base version of Version");
594 }
595
596
597
598
599
600
601
602 public ContentVersion getVersionedContent(Version version) throws RepositoryException {
603 throw new AccessDeniedException("Not allowed to get preview of Version itself");
604 }
605
606
607
608
609
610
611
612 public ContentVersion getVersionedContent(String versionName) throws RepositoryException {
613 throw new AccessDeniedException("Not allowed to get preview of Version itself");
614 }
615
616
617
618
619
620 public void save() throws RepositoryException {
621 throw new AccessDeniedException("Not allowed to write on version preview");
622 }
623
624
625
626
627
628
629 public boolean isGranted(long permissions) {
630 return (permissions & Permission.READ) == permissions;
631 }
632
633
634
635
636
637 public void delete() throws RepositoryException {
638 throw new AccessDeniedException("Not allowed to write on version preview");
639 }
640
641
642
643
644
645 public void delete(String path) throws RepositoryException {
646 throw new AccessDeniedException("Not allowed to write on version preview");
647 }
648
649
650
651
652
653 public String getUUID() {
654 return this.base.getUUID();
655 }
656
657
658
659
660
661
662 public void addMixin(String type) throws RepositoryException {
663 throw new AccessDeniedException("Not allowed to write on version preview");
664 }
665
666
667
668
669
670
671
672 public void removeMixin(String type) throws RepositoryException {
673 throw new AccessDeniedException("Not allowed to write on version preview");
674 }
675
676
677
678
679
680
681
682
683
684
685
686
687
688 public Lock lock(boolean isDeep, boolean isSessionScoped) throws LockException, RepositoryException {
689 throw new AccessDeniedException("Lock not supported on version preview");
690 }
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705 public Lock lock(boolean isDeep, boolean isSessionScoped, long yieldFor) throws LockException, RepositoryException {
706 throw new AccessDeniedException("Lock not supported on version preview");
707 }
708
709
710
711
712
713
714
715 public Lock getLock() throws LockException, RepositoryException {
716 throw new AccessDeniedException("Lock not supported on version preview");
717 }
718
719
720
721
722
723
724
725
726 public void unlock() throws LockException, RepositoryException {
727 throw new AccessDeniedException("Lock not supported on version preview");
728 }
729
730
731
732
733
734
735
736
737 public boolean holdsLock() throws RepositoryException {
738 throw new AccessDeniedException("Lock not supported on version preview");
739 }
740
741
742
743
744
745
746
747 public boolean isLocked() throws RepositoryException {
748 throw new AccessDeniedException("Lock not supported on version preview");
749 }
750
751
752
753
754
755 public HierarchyManager getHierarchyManager() {
756 return this.base.getHierarchyManager();
757 }
758
759
760
761
762
763
764 public AccessManager getAccessManager() {
765 return this.base.getAccessManager();
766 }
767
768 @Override
769 public Workspace getWorkspace() throws RepositoryException {
770 return this.base.getWorkspace();
771 }
772
773 }