This repository was archived by the owner on Oct 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathNodesImpl.java
3578 lines (3109 loc) · 129 KB
/
NodesImpl.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2019 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.impl;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import org.alfresco.model.ApplicationModel;
import org.alfresco.model.ContentModel;
import org.alfresco.model.QuickShareModel;
import org.alfresco.query.PagingRequest;
import org.alfresco.query.PagingResults;
import org.alfresco.repo.action.executer.ContentMetadataExtracter;
import org.alfresco.repo.activities.ActivityType;
import org.alfresco.repo.content.ContentLimitViolationException;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.lock.mem.Lifetime;
import org.alfresco.repo.model.Repository;
import org.alfresco.repo.model.filefolder.FileFolderServiceImpl;
import org.alfresco.repo.node.getchildren.FilterProp;
import org.alfresco.repo.node.getchildren.FilterPropBoolean;
import org.alfresco.repo.node.getchildren.GetChildrenCannedQuery;
import org.alfresco.repo.node.integrity.IntegrityException;
import org.alfresco.repo.policy.BehaviourFilter;
import org.alfresco.repo.rendition2.RenditionDefinition2;
import org.alfresco.repo.rendition2.RenditionDefinitionRegistry2;
import org.alfresco.repo.rendition2.RenditionService2;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.repo.site.SiteModel;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.version.VersionModel;
import org.alfresco.repo.virtual.store.VirtualStore;
import org.alfresco.rest.antlr.WhereClauseParser;
import org.alfresco.rest.api.Activities;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.QuickShareLinks;
import org.alfresco.rest.api.model.AssocChild;
import org.alfresco.rest.api.model.AssocTarget;
import org.alfresco.rest.api.model.Document;
import org.alfresco.rest.api.model.Folder;
import org.alfresco.rest.api.model.LockInfo;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.NodePermissions;
import org.alfresco.rest.api.model.PathInfo;
import org.alfresco.rest.api.model.PathInfo.ElementInfo;
import org.alfresco.rest.api.model.QuickShareLink;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.api.nodes.NodeAssocService;
import org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException;
import org.alfresco.rest.framework.core.exceptions.DisabledServiceException;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.core.exceptions.InsufficientStorageException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.core.exceptions.NotFoundException;
import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException;
import org.alfresco.rest.framework.core.exceptions.RequestEntityTooLargeException;
import org.alfresco.rest.framework.core.exceptions.UnsupportedMediaTypeException;
import org.alfresco.rest.framework.resource.content.BasicContentInfo;
import org.alfresco.rest.framework.resource.content.BinaryResource;
import org.alfresco.rest.framework.resource.content.ContentInfoImpl;
import org.alfresco.rest.framework.resource.content.NodeBinaryResource;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.resource.parameters.SortColumn;
import org.alfresco.rest.framework.resource.parameters.where.Query;
import org.alfresco.rest.framework.resource.parameters.where.QueryHelper;
import org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionDefinition;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.activities.ActivitiesTransactionListener;
import org.alfresco.service.cmr.activities.ActivityInfo;
import org.alfresco.service.cmr.activities.ActivityPoster;
import org.alfresco.service.cmr.dictionary.AspectDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.lock.LockService;
import org.alfresco.service.cmr.lock.NodeLockedException;
import org.alfresco.service.cmr.model.FileExistsException;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.model.FileNotFoundException;
import org.alfresco.service.cmr.preference.PreferenceService;
import org.alfresco.service.cmr.repository.AssociationExistsException;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.DuplicateChildNodeNameException;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.MimetypeService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.Path;
import org.alfresco.service.cmr.repository.Path.Element;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.security.AccessPermission;
import org.alfresco.service.cmr.security.AccessStatus;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.OwnableService;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.cmr.thumbnail.ThumbnailService;
import org.alfresco.service.cmr.usage.ContentQuotaException;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.cmr.version.VersionType;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.alfresco.util.PropertyCheck;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.extensions.surf.util.Content;
import org.springframework.extensions.webscripts.servlet.FormData;
/**
* Centralises access to file/folder/node services and maps between representations.
*
* Note:
* This class was originally used for returning some basic node info when listing Favourites.
*
* It has now been re-purposed and extended to implement the new Nodes (RESTful) API for
* managing files & folders, as well as custom node types.
*
* @author steveglover
* @author janv
* @author Jamal Kaabi-Mofrad
*
* @since publicapi1.0
*/
public class NodesImpl implements Nodes
{
private static final Log logger = LogFactory.getLog(NodesImpl.class);
private enum Type
{
// Note: ordered
DOCUMENT, FOLDER
}
private NodeService nodeService;
private DictionaryService dictionaryService;
private FileFolderService fileFolderService;
private NamespaceService namespaceService;
private PermissionService permissionService;
private MimetypeService mimetypeService;
private ContentService contentService;
private ActionService actionService;
private VersionService versionService;
private PersonService personService;
private OwnableService ownableService;
private AuthorityService authorityService;
private ThumbnailService thumbnailService;
private RenditionService2 renditionService2;
private SiteService siteService;
private ActivityPoster poster;
private RetryingTransactionHelper retryingTransactionHelper;
private NodeAssocService nodeAssocService;
private LockService lockService;
private VirtualStore smartStore; // note: remove as part of REPO-1173
private enum Activity_Type
{
ADDED, UPDATED, DELETED, DOWNLOADED
}
private BehaviourFilter behaviourFilter;
// note: circular - Nodes/QuickShareLinks currently use each other (albeit for different methods)
private QuickShareLinks quickShareLinks;
private Repository repositoryHelper;
private ServiceRegistry sr;
private Set<String> defaultIgnoreTypesAndAspects;
// ignore types/aspects
private Set<QName> ignoreQNames;
private ConcurrentHashMap<String,NodeRef> ddCache = new ConcurrentHashMap<>();
private Set<String> nonAttachContentTypes = Collections.emptySet(); // pre-configured whitelist, eg. images & pdf
public void setNonAttachContentTypes(Set<String> nonAttachWhiteList)
{
this.nonAttachContentTypes = nonAttachWhiteList;
}
public void init()
{
PropertyCheck.mandatory(this, "serviceRegistry", sr);
PropertyCheck.mandatory(this, "behaviourFilter", behaviourFilter);
PropertyCheck.mandatory(this, "repositoryHelper", repositoryHelper);
PropertyCheck.mandatory(this, "quickShareLinks", quickShareLinks);
PropertyCheck.mandatory(this, "poster", poster);
this.namespaceService = sr.getNamespaceService();
this.fileFolderService = sr.getFileFolderService();
this.nodeService = sr.getNodeService();
this.permissionService = sr.getPermissionService();
this.dictionaryService = sr.getDictionaryService();
this.mimetypeService = sr.getMimetypeService();
this.contentService = sr.getContentService();
this.actionService = sr.getActionService();
this.versionService = sr.getVersionService();
this.personService = sr.getPersonService();
this.ownableService = sr.getOwnableService();
this.authorityService = sr.getAuthorityService();
this.thumbnailService = sr.getThumbnailService();
this.renditionService2 = sr.getRenditionService2();
this.siteService = sr.getSiteService();
this.retryingTransactionHelper = sr.getRetryingTransactionHelper();
this.lockService = sr.getLockService();
if (defaultIgnoreTypesAndAspects != null)
{
ignoreQNames = new HashSet<>(defaultIgnoreTypesAndAspects.size());
for (String type : defaultIgnoreTypesAndAspects)
{
ignoreQNames.add(createQName(type));
}
}
}
public void setServiceRegistry(ServiceRegistry sr)
{
this.sr = sr;
}
public void setBehaviourFilter(BehaviourFilter behaviourFilter)
{
this.behaviourFilter = behaviourFilter;
}
public void setRepositoryHelper(Repository repositoryHelper)
{
this.repositoryHelper = repositoryHelper;
}
public void setQuickShareLinks(QuickShareLinks quickShareLinks)
{
this.quickShareLinks = quickShareLinks;
}
public void setIgnoreTypes(Set<String> ignoreTypesAndAspects)
{
this.defaultIgnoreTypesAndAspects = ignoreTypesAndAspects;
}
public void setPoster(ActivityPoster poster)
{
this.poster = poster;
}
// Introduces permissions for Node Assoc (see public-rest-context.xml)
public void setNodeAssocService(NodeAssocService nodeAssocService)
{
this.nodeAssocService = nodeAssocService;
}
public void setSmartStore(VirtualStore smartStore)
{
this.smartStore = smartStore;
}
// excluded namespaces (aspects, properties, assoc types)
private static final List<String> EXCLUDED_NS = Arrays.asList(NamespaceService.SYSTEM_MODEL_1_0_URI);
// excluded aspects
private static final List<QName> EXCLUDED_ASPECTS = Arrays.asList();
// excluded properties
private static final List<QName> EXCLUDED_PROPS = Arrays.asList(
// top-level minimal info
ContentModel.PROP_NAME,
ContentModel.PROP_MODIFIER,
ContentModel.PROP_MODIFIED,
ContentModel.PROP_CREATOR,
ContentModel.PROP_CREATED,
ContentModel.PROP_CONTENT,
// other - TBC
ContentModel.PROP_INITIAL_VERSION,
ContentModel.PROP_AUTO_VERSION_PROPS,
ContentModel.PROP_AUTO_VERSION);
public static final List<QName> PROPS_USERLOOKUP = Arrays.asList(
ContentModel.PROP_CREATOR,
ContentModel.PROP_MODIFIER,
ContentModel.PROP_OWNER,
ContentModel.PROP_LOCK_OWNER,
ContentModel.PROP_WORKING_COPY_OWNER);
public final static Map<String,QName> PARAM_SYNONYMS_QNAME;
static
{
Map<String,QName> aMap = new HashMap<>(9);
aMap.put(PARAM_ISFOLDER, GetChildrenCannedQuery.SORT_QNAME_NODE_IS_FOLDER);
aMap.put(PARAM_NAME, ContentModel.PROP_NAME);
aMap.put(PARAM_CREATEDAT, ContentModel.PROP_CREATED);
aMap.put(PARAM_MODIFIEDAT, ContentModel.PROP_MODIFIED);
aMap.put(PARAM_CREATEBYUSER, ContentModel.PROP_CREATOR);
aMap.put(PARAM_MODIFIEDBYUSER, ContentModel.PROP_MODIFIER);
aMap.put(PARAM_MIMETYPE, GetChildrenCannedQuery.SORT_QNAME_CONTENT_MIMETYPE);
aMap.put(PARAM_SIZEINBYTES, GetChildrenCannedQuery.SORT_QNAME_CONTENT_SIZE);
aMap.put(PARAM_NODETYPE, GetChildrenCannedQuery.SORT_QNAME_NODE_TYPE);
PARAM_SYNONYMS_QNAME = Collections.unmodifiableMap(aMap);
}
// list children filtering (via where clause)
private final static Set<String> LIST_FOLDER_CHILDREN_EQUALS_QUERY_PROPERTIES =
new HashSet<>(Arrays.asList(new String[] {PARAM_ISFOLDER, PARAM_ISFILE, PARAM_NODETYPE, PARAM_ISPRIMARY, PARAM_ASSOC_TYPE}));
/*
* Validates that node exists.
*
* Note: assumes workspace://SpacesStore
*/
@Override
public NodeRef validateNode(String nodeId)
{
//belts-and-braces
if (nodeId == null)
{
throw new InvalidArgumentException("Missing nodeId");
}
return validateNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, nodeId);
}
@Override
public NodeRef validateNode(StoreRef storeRef, String nodeId)
{
String versionLabel = null;
int idx = nodeId.indexOf(";");
if (idx != -1)
{
versionLabel = nodeId.substring(idx + 1);
nodeId = nodeId.substring(0, idx);
if (versionLabel.equals("pwc"))
{
// TODO correct exception?
throw new EntityNotFoundException(nodeId);
}
}
NodeRef nodeRef = new NodeRef(storeRef, nodeId);
return validateNode(nodeRef);
}
@Override
public NodeRef validateNode(NodeRef nodeRef)
{
if (!nodeService.exists(nodeRef))
{
throw new EntityNotFoundException(nodeRef.getId());
}
return nodeRef;
}
/*
* Check that nodes exists and matches given expected/excluded type(s).
*/
@Override
public boolean nodeMatches(NodeRef nodeRef, Set<QName> expectedTypes, Set<QName> excludedTypes)
{
return nodeMatches(nodeRef, expectedTypes, excludedTypes, true);
}
@Override
public boolean isSubClass(NodeRef nodeRef, QName ofClassQName, boolean validateNodeRef)
{
if (validateNodeRef)
{
nodeRef = validateNode(nodeRef);
}
return isSubClass(getNodeType(nodeRef), ofClassQName);
}
private boolean nodeMatches(NodeRef nodeRef, Set<QName> expectedTypes, Set<QName> excludedTypes, boolean existsCheck)
{
if (existsCheck && (! nodeService.exists(nodeRef)))
{
throw new EntityNotFoundException(nodeRef.getId());
}
return typeMatches(getNodeType(nodeRef), expectedTypes, excludedTypes);
}
private QName getNodeType(NodeRef nodeRef)
{
return nodeService.getType(nodeRef);
}
private boolean isSubClass(QName className, QName ofClassQName)
{
return dictionaryService.isSubClass(className, ofClassQName);
}
protected boolean typeMatches(QName type, Set<QName> expectedTypes, Set<QName> excludedTypes)
{
if (((expectedTypes != null) && (expectedTypes.size() == 1)) &&
((excludedTypes == null) || (excludedTypes.size() == 0)))
{
// use isSubClass if checking against single expected type (and no excluded types)
return isSubClass(type, expectedTypes.iterator().next());
}
Set<QName> allExpectedTypes = new HashSet<>();
if (expectedTypes != null)
{
for (QName expectedType : expectedTypes)
{
allExpectedTypes.addAll(dictionaryService.getSubTypes(expectedType, true));
}
}
Set<QName> allExcludedTypes = new HashSet<>();
if (excludedTypes != null)
{
for (QName excludedType : excludedTypes)
{
allExcludedTypes.addAll(dictionaryService.getSubTypes(excludedType, true));
}
}
boolean inExpected = allExpectedTypes.contains(type);
boolean excluded = allExcludedTypes.contains(type);
return (inExpected && !excluded);
}
/**
* @deprecated review usage (backward compat')
*/
@Override
public Node getNode(String nodeId)
{
NodeRef nodeRef = validateNode(nodeId);
return new Node(nodeRef, null, nodeService.getProperties(nodeRef), null, sr);
}
/**
* @deprecated review usage (backward compat')
*/
public Node getNode(NodeRef nodeRef)
{
return new Node(nodeRef, null, nodeService.getProperties(nodeRef), null, sr);
}
private Type getType(NodeRef nodeRef)
{
return getType(getNodeType(nodeRef), nodeRef);
}
private Type getType(QName typeQName, NodeRef nodeRef)
{
// quick check for common types
if (typeQName.equals(ContentModel.TYPE_FOLDER) || typeQName.equals(ApplicationModel.TYPE_FOLDERLINK))
{
return Type.FOLDER;
}
else if (typeQName.equals(ContentModel.TYPE_CONTENT) || typeQName.equals(ApplicationModel.TYPE_FILELINK))
{
return Type.DOCUMENT;
}
// further checks
if (isSubClass(typeQName, ContentModel.TYPE_LINK))
{
if (isSubClass(typeQName, ApplicationModel.TYPE_FOLDERLINK))
{
return Type.FOLDER;
}
else if (isSubClass(typeQName, ApplicationModel.TYPE_FILELINK))
{
return Type.DOCUMENT;
}
NodeRef linkNodeRef = (NodeRef)nodeService.getProperty(nodeRef, ContentModel.PROP_LINK_DESTINATION);
if (linkNodeRef != null)
{
try
{
typeQName = getNodeType(linkNodeRef);
// drop-through to check type of destination
// note: edge-case - if link points to another link then we will return null
}
catch (InvalidNodeRefException inre)
{
// ignore
}
}
}
if (isSubClass(typeQName, ContentModel.TYPE_FOLDER))
{
if (! isSubClass(typeQName, ContentModel.TYPE_SYSTEM_FOLDER))
{
return Type.FOLDER;
}
return null; // unknown
}
else if (isSubClass(typeQName, ContentModel.TYPE_CONTENT))
{
return Type.DOCUMENT;
}
return null; // unknown
}
/**
* @deprecated note: currently required for backwards compat' (Favourites API)
*/
@Override
public Document getDocument(NodeRef nodeRef)
{
Type type = getType(nodeRef);
if ((type != null) && type.equals(Type.DOCUMENT))
{
Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
Document doc = new Document(nodeRef, getParentNodeRef(nodeRef), properties, null, sr);
doc.setVersionLabel((String) properties.get(ContentModel.PROP_VERSION_LABEL));
ContentData cd = (ContentData) properties.get(ContentModel.PROP_CONTENT);
if (cd != null)
{
doc.setSizeInBytes(BigInteger.valueOf(cd.getSize()));
doc.setMimeType((cd.getMimetype()));
}
setCommonProps(doc, nodeRef, properties);
return doc;
}
else
{
throw new InvalidArgumentException("Node is not a file: "+nodeRef.getId());
}
}
private void setCommonProps(Node node, NodeRef nodeRef, Map<QName,Serializable> properties)
{
node.setGuid(nodeRef);
node.setTitle((String)properties.get(ContentModel.PROP_TITLE));
node.setDescription((String)properties.get(ContentModel.PROP_DESCRIPTION));
node.setModifiedBy((String)properties.get(ContentModel.PROP_MODIFIER));
node.setCreatedBy((String)properties.get(ContentModel.PROP_CREATOR));
}
/**
* @deprecated note: currently required for backwards compat' (Favourites API)
*/
@Override
public Folder getFolder(NodeRef nodeRef)
{
Type type = getType(nodeRef);
if ((type != null) && type.equals(Type.FOLDER))
{
Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
Folder folder = new Folder(nodeRef, getParentNodeRef(nodeRef), properties, null, sr);
setCommonProps(folder, nodeRef, properties);
return folder;
}
else
{
throw new InvalidArgumentException("Node is not a folder: "+nodeRef.getId());
}
}
private NodeRef getParentNodeRef(NodeRef nodeRef)
{
if (repositoryHelper.getCompanyHome().equals(nodeRef))
{
return null; // note: does not make sense to return parent above C/H
}
return nodeService.getPrimaryParent(nodeRef).getParentRef();
}
public NodeRef validateOrLookupNode(String nodeId, String path)
{
NodeRef parentNodeRef;
if ((nodeId == null) || (nodeId.isEmpty()))
{
throw new InvalidArgumentException("Missing nodeId");
}
if (nodeId.equals(PATH_ROOT))
{
parentNodeRef = repositoryHelper.getCompanyHome();
}
else if (nodeId.equals(PATH_SHARED))
{
parentNodeRef = repositoryHelper.getSharedHome();
}
else if (nodeId.equals(PATH_MY))
{
NodeRef person = repositoryHelper.getPerson();
if (person == null)
{
throw new InvalidArgumentException("Unexpected - cannot use: " + PATH_MY);
}
parentNodeRef = repositoryHelper.getUserHome(person);
if (parentNodeRef == null)
{
throw new EntityNotFoundException(nodeId);
}
}
else
{
parentNodeRef = validateNode(nodeId);
}
if (path != null)
{
// check that parent is a folder before resolving relative path
if (! nodeMatches(parentNodeRef, Collections.singleton(ContentModel.TYPE_FOLDER), null, false))
{
throw new InvalidArgumentException("NodeId of folder is expected: "+parentNodeRef.getId());
}
// resolve path relative to current nodeId
parentNodeRef = resolveNodeByPath(parentNodeRef, path, true);
}
return parentNodeRef;
}
protected NodeRef resolveNodeByPath(final NodeRef parentNodeRef, String path, boolean checkForCompanyHome)
{
final List<String> pathElements = getPathElements(path);
if (!pathElements.isEmpty() && checkForCompanyHome)
{
/*
if (nodeService.getRootNode(parentNodeRef.getStoreRef()).equals(parentNodeRef))
{
// special case
NodeRef chNodeRef = repositoryHelper.getCompanyHome();
String chName = (String) nodeService.getProperty(chNodeRef, ContentModel.PROP_NAME);
if (chName.equals(pathElements.get(0)))
{
pathElements = pathElements.subList(1, pathElements.size());
parentNodeRef = chNodeRef;
}
}
*/
}
FileInfo fileInfo = null;
try
{
if (!pathElements.isEmpty())
{
fileInfo = fileFolderService.resolveNamePath(parentNodeRef, pathElements);
}
else
{
fileInfo = fileFolderService.getFileInfo(parentNodeRef);
if (fileInfo == null)
{
throw new EntityNotFoundException(parentNodeRef.getId());
}
}
}
catch (FileNotFoundException fnfe)
{
// convert checked exception
throw new NotFoundException("The entity with relativePath: " + path + " was not found.");
}
catch (AccessDeniedException ade)
{
// return 404 instead of 403 (as per security review - uuid vs path)
throw new NotFoundException("The entity with relativePath: " + path + " was not found.");
}
return fileInfo.getNodeRef();
}
private List<String> getPathElements(String path)
{
final List<String> pathElements = new ArrayList<>();
if (path != null && path.trim().length() > 0)
{
// There is no need to check for leading and trailing "/"
final StringTokenizer tokenizer = new StringTokenizer(path, "/");
while (tokenizer.hasMoreTokens())
{
pathElements.add(tokenizer.nextToken().trim());
}
}
return pathElements;
}
private NodeRef makeFolders(NodeRef parentNodeRef, List<String> pathElements)
{
NodeRef currentParentRef = parentNodeRef;
// just loop and create if necessary
for (final String element : pathElements)
{
final NodeRef contextNodeRef = currentParentRef;
// does it exist?
// Navigation should not check permissions
NodeRef nodeRef = AuthenticationUtil.runAs(new RunAsWork<NodeRef>()
{
@Override
public NodeRef doWork() throws Exception
{
return nodeService.getChildByName(contextNodeRef, ContentModel.ASSOC_CONTAINS, element);
}
}, AuthenticationUtil.getSystemUserName());
if (nodeRef == null)
{
try
{
// Checks for create permissions as the fileFolderService is a public service.
FileInfo createdFileInfo = fileFolderService.create(currentParentRef, element, ContentModel.TYPE_FOLDER);
currentParentRef = createdFileInfo.getNodeRef();
}
catch (AccessDeniedException ade)
{
throw new PermissionDeniedException(ade.getMessage());
}
catch (FileExistsException fex)
{
// Assume concurrency failure, so retry
throw new ConcurrencyFailureException(fex.getMessage());
}
}
else if (!isSubClass(nodeRef, ContentModel.TYPE_FOLDER, false))
{
String parentName = (String) nodeService.getProperty(contextNodeRef, ContentModel.PROP_NAME);
throw new ConstraintViolatedException("Name [" + element + "] already exists in the target parent: " + parentName);
}
else
{
// it exists
currentParentRef = nodeRef;
}
}
return currentParentRef;
}
@Override
public Node getFolderOrDocument(String nodeId, Parameters parameters)
{
String path = parameters.getParameter(PARAM_RELATIVE_PATH);
NodeRef nodeRef = validateOrLookupNode(nodeId, path);
Node node = getFolderOrDocumentFullInfo(nodeRef, null, null, parameters);
return node;
}
private Node getFolderOrDocumentFullInfo(NodeRef nodeRef, NodeRef parentNodeRef, QName nodeTypeQName, Parameters parameters)
{
return getFolderOrDocumentFullInfo(nodeRef, parentNodeRef, nodeTypeQName, parameters, null);
}
@Override
public Node getFolderOrDocumentFullInfo(NodeRef nodeRef, NodeRef parentNodeRef, QName nodeTypeQName, Parameters parameters, Map<String,UserInfo> mapUserInfo)
{
List<String> includeParam = new ArrayList<>();
if (parameters != null)
{
includeParam.addAll(parameters.getInclude());
}
// Add basic info for single get (above & beyond minimal that is used for listing collections)
includeParam.add(PARAM_INCLUDE_ASPECTNAMES);
includeParam.add(PARAM_INCLUDE_PROPERTIES);
return getFolderOrDocument(nodeRef, parentNodeRef, nodeTypeQName, includeParam, mapUserInfo);
}
@Override
public Node getFolderOrDocument(final NodeRef nodeRef, NodeRef parentNodeRef, QName nodeTypeQName, List<String> includeParam, Map<String, UserInfo> mapUserInfo)
{
if (mapUserInfo == null)
{
mapUserInfo = new HashMap<>(2);
}
if (includeParam == null)
{
includeParam = Collections.emptyList();
}
Node node;
Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
PathInfo pathInfo = null;
if (includeParam.contains(PARAM_INCLUDE_PATH))
{
ChildAssociationRef archivedParentAssoc = (ChildAssociationRef) properties.get(ContentModel.PROP_ARCHIVED_ORIGINAL_PARENT_ASSOC);
pathInfo = lookupPathInfo(nodeRef, archivedParentAssoc);
}
if (nodeTypeQName == null)
{
nodeTypeQName = getNodeType(nodeRef);
}
if (parentNodeRef == null)
{
parentNodeRef = getParentNodeRef(nodeRef);
}
Type type = getType(nodeTypeQName, nodeRef);
if (type == null)
{
// not direct folder (or file) ...
// might be sub-type of cm:cmobject (or a cm:link pointing to cm:cmobject or possibly even another cm:link)
node = new Node(nodeRef, parentNodeRef, properties, mapUserInfo, sr);
node.setIsFolder(false);
node.setIsFile(false);
}
else if (type.equals(Type.DOCUMENT))
{
node = new Document(nodeRef, parentNodeRef, properties, mapUserInfo, sr);
}
else if (type.equals(Type.FOLDER))
{
node = new Folder(nodeRef, parentNodeRef, properties, mapUserInfo, sr);
}
else
{
throw new RuntimeException("Unexpected - should not reach here: "+type);
}
if (includeParam.size() > 0)
{
node.setProperties(mapFromNodeProperties(properties, includeParam, mapUserInfo, EXCLUDED_NS, EXCLUDED_PROPS));
}
Set<QName> aspects = null;
if (includeParam.contains(PARAM_INCLUDE_ASPECTNAMES))
{
aspects = nodeService.getAspects(nodeRef);
node.setAspectNames(mapFromNodeAspects(aspects, EXCLUDED_NS, EXCLUDED_ASPECTS));
}
if (includeParam.contains(PARAM_INCLUDE_ISLINK))
{
boolean isLink = isSubClass(nodeTypeQName, ContentModel.TYPE_LINK);
node.setIsLink(isLink);
}
if (includeParam.contains(PARAM_INCLUDE_ISLOCKED))
{
boolean isLocked = isLocked(nodeRef, aspects);
node.setIsLocked(isLocked);
}
if (includeParam.contains(PARAM_INCLUDE_ISFAVORITE))
{
boolean isFavorite = isFavorite(nodeRef);
node.setIsFavorite(isFavorite);
}
if (includeParam.contains(PARAM_INCLUDE_ALLOWABLEOPERATIONS))
{
// note: refactor when requirements change
Map<String, String> mapPermsToOps = new HashMap<>(3);
mapPermsToOps.put(PermissionService.DELETE, OP_DELETE);
mapPermsToOps.put(PermissionService.ADD_CHILDREN, OP_CREATE);
mapPermsToOps.put(PermissionService.WRITE, OP_UPDATE);
mapPermsToOps.put(PermissionService.CHANGE_PERMISSIONS, OP_UPDATE_PERMISSIONS);
List<String> allowableOperations = new ArrayList<>(3);
for (Entry<String, String> kv : mapPermsToOps.entrySet())
{
String perm = kv.getKey();
String op = kv.getValue();
if (perm.equals(PermissionService.ADD_CHILDREN) && Type.DOCUMENT.equals(type))
{
// special case: do not return "create" (as an allowable op) for file/content types - note: 'type' can be null
continue;
}
else if (perm.equals(PermissionService.DELETE) && (isSpecialNode(nodeRef, nodeTypeQName)))
{
// special case: do not return "delete" (as an allowable op) for specific system nodes
continue;
}
else if (permissionService.hasPermission(nodeRef, perm) == AccessStatus.ALLOWED)
{
allowableOperations.add(op);
}
}
node.setAllowableOperations((allowableOperations.size() > 0 )? allowableOperations : null);
}
if (includeParam.contains(PARAM_INCLUDE_PERMISSIONS))
{
Boolean inherit = permissionService.getInheritParentPermissions(nodeRef);
List<NodePermissions.NodePermission> inheritedPerms = new ArrayList<>(5);
List<NodePermissions.NodePermission> setDirectlyPerms = new ArrayList<>(5);
Set<String> settablePerms = null;
boolean allowRetrievePermission = true;
try
{
for (AccessPermission accessPerm : permissionService.getAllSetPermissions(nodeRef))
{
NodePermissions.NodePermission nodePerm = new NodePermissions.NodePermission(accessPerm.getAuthority(), accessPerm.getPermission(), accessPerm.getAccessStatus().toString());
if (accessPerm.isSetDirectly())
{
setDirectlyPerms.add(nodePerm);
} else
{
inheritedPerms.add(nodePerm);
}
}
settablePerms = permissionService.getSettablePermissions(nodeRef);
}
catch (AccessDeniedException ade)
{
// ignore - ie. denied access to retrieve permissions, eg. non-admin on root (Company Home)
allowRetrievePermission = false;
}
// If the user does not have read permissions at
// least on a special node then do not include permissions and
// returned only node info that he's allowed to see
if (allowRetrievePermission)
{
NodePermissions nodePerms = new NodePermissions(inherit, inheritedPerms, setDirectlyPerms, settablePerms);
node.setPermissions(nodePerms);
}
}
if (includeParam.contains(PARAM_INCLUDE_ASSOCIATION))