/
ConfluenceXMLPackage.java
2715 lines (2253 loc) · 91 KB
/
ConfluenceXMLPackage.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
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This 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 2.1 of
* the License, or (at your option) any later version.
*
* This software 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 this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.contrib.confluence.filter.input;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
import javax.inject.Inject;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.input.CloseShieldInputStream;
import org.apache.commons.io.input.CountingInputStream;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.InstantiationStrategy;
import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
import org.xwiki.contrib.confluence.filter.internal.WithoutControlCharactersReader;
import org.xwiki.contrib.confluence.filter.internal.input.ConfluenceCanceledException;
import org.xwiki.environment.Environment;
import org.xwiki.filter.FilterException;
import org.xwiki.filter.input.FileInputSource;
import org.xwiki.filter.input.InputSource;
import org.xwiki.filter.input.InputStreamInputSource;
import org.xwiki.filter.input.URLInputSource;
import org.xwiki.job.Job;
import org.xwiki.job.JobContext;
import org.xwiki.job.event.status.CancelableJobStatus;
import org.xwiki.job.event.status.JobProgressManager;
import org.xwiki.job.event.status.JobStatus;
import org.xwiki.xml.stax.StAXUtils;
import com.google.common.base.Strings;
/**
* Prepare a Confluence package to make it easier to import.
*
* @version $Id$
* @since 9.16
*/
@Component(roles = ConfluenceXMLPackage.class)
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
public class ConfluenceXMLPackage implements AutoCloseable
{
/**
* The name of the main package file.
*/
public static final String FILE_ENTITIES = "entities.xml";
/**
* The name of the file containing information about the instance.
*/
public static final String FILE_DESCRIPTOR = "exportDescriptor.properties";
/**
* The property key to access the space name.
*/
public static final String KEY_SPACE_NAME = "name";
/**
* The property key to access the space key.
*/
public static final String KEY_SPACE_KEY = "key";
/**
* The property key to access the space description.
*/
public static final String KEY_SPACE_DESCRIPTION = "description";
/**
* The property key to access the space home page.
*/
public static final String KEY_SPACE_HOMEPAGE = "homePage";
/**
* The property key to access the space permissions.
* @since 9.24.0
*/
public static final String KEY_SPACE_PERMISSIONS = "permissions";
/**
* The property key to access the space status.
* @since 9.31.0
*/
public static final String KEY_SPACE_STATUS = "spaceStatus";
/**
* The property key to access the space permission type.
* @since 9.24.0
*/
public static final String KEY_PERMISSION_TYPE = "type";
/**
* The property key to access the space permission group.
* @since 9.24.0
*/
public static final String KEY_SPACEPERMISSION_GROUP = "group";
/**
* The property key to access the space permission group.
* @since 9.24.0
*/
public static final String KEY_CONTENTPERMISSION_GROUP = "groupName";
/**
* The property key to access the space or content permission user subject.
* @since 9.24.0
*/
public static final String KEY_PERMISSION_ALLUSERSSUBJECT = "allUsersSubject";
/**
* The property key to access the space or content permission user subject.
* @since 9.24.0
*/
public static final String KEY_PERMISSION_USERSUBJECT = "userSubject";
/**
* The property key to access the space permission username.
* @since 9.24.0
*/
public static final String KEY_SPACEPERMISSION_USERNAME = "userName";
/**
* The property key to access the page home page.
*/
public static final String KEY_PAGE_HOMEPAGE = "homepage";
/**
* The property key to access the page parent.
*/
public static final String KEY_PAGE_PARENT = "parent";
/**
* The property key to access the page space.
*/
public static final String KEY_PAGE_SPACE = "space";
/**
* The property key to access the space permission's space.
* @since 9.47.0
*/
public static final String KEY_SPACE_PERMISSION_SPACE = "space";
/**
* The property key to access the page title.
*/
public static final String KEY_PAGE_TITLE = "title";
/**
* The property key to access the page contents.
*/
public static final String KEY_PAGE_CONTENTS = "bodyContents";
/**
* The property key to access the page creation author name.
*/
public static final String KEY_PAGE_CREATION_AUTHOR = "creatorName";
/**
* The property key to access the page creation author key.
*/
public static final String KEY_PAGE_CREATION_AUTHOR_KEY = "creator";
/**
* The property key to access the page creation date.
*/
public static final String KEY_PAGE_CREATION_DATE = "creationDate";
/**
* The property key to access the page revision.
*/
public static final String KEY_PAGE_REVISION = "version";
/**
* The property key to access the page revision author key.
*/
public static final String KEY_PAGE_REVISION_AUTHOR_KEY = "lastModifier";
/**
* The property key to access the page revision author name.
*/
public static final String KEY_PAGE_REVISION_AUTHOR = "lastModifierName";
/**
* The property key to access the page revision date.
*/
public static final String KEY_PAGE_REVISION_DATE = "lastModificationDate";
/**
* The property key to access the page revision comment.
*/
public static final String KEY_PAGE_REVISION_COMMENT = "versionComment";
/**
* The property key to access the page revisions.
*/
public static final String KEY_PAGE_REVISIONS = "historicalVersions";
/**
* The property key to access the page content status.
*/
public static final String KEY_PAGE_CONTENT_STATUS = "contentStatus";
/**
* The property key to access the page body.
*/
public static final String KEY_PAGE_BODY = "body";
/**
* The property key to access the page body type.
*/
public static final String KEY_PAGE_BODY_TYPE = "bodyType";
/**
* The property key to access the page lebellings.
*/
public static final String KEY_PAGE_LABELLINGS = "labellings";
/**
* Old property key to access the page comments.
*/
public static final String KEY_PAGE_COMMENTS = "comments";
/**
* The property key to access the original version.
*/
public static final String KEY_PAGE_ORIGINAL_VERSION = "originalVersion";
/**
* Old property to indicate attachment name.
*
* @see #KEY_ATTACHMENT_TITLE
*/
public static final String KEY_ATTACHMENT_NAME = "fileName";
/**
* The property key to access the attachment title.
*/
public static final String KEY_ATTACHMENT_TITLE = "title";
/**
* Old field containing attachment page id.
*
* @see #KEY_ATTACHMENT_CONTAINERCONTENT
*/
public static final String KEY_ATTACHMENT_CONTENT = "content";
/**
* Field containing content property page id or attachment id or comment id.
* @since 9.47.0
*/
public static final String KEY_CONTENT_PROPERTY_CONTENT = "content";
/**
* The property key to access the attachment content key.
*/
public static final String KEY_ATTACHMENT_CONTAINERCONTENT = "containerContent";
/**
* The property key to access the comment content key.
* @since 9.47.0
*/
public static final String KEY_COMMENT_CONTAINERCONTENT = "containerContent";
/**
* Old property to indicate attachment size.
*
* @see #KEY_ATTACHMENT_CONTENTPROPERTIES
* @see #KEY_ATTACHMENT_CONTENT_FILESIZE
*/
public static final String KEY_ATTACHMENT_CONTENT_SIZE = "fileSize";
/**
* Old property to indicate attachment media type.
*
* @see #KEY_ATTACHMENT_CONTENTPROPERTIES
* @see #KEY_ATTACHMENT_CONTENT_MEDIA_TYPE
*/
public static final String KEY_ATTACHMENT_CONTENTTYPE = "contentType";
/**
* The property key to access the attachment content properties.
*/
public static final String KEY_ATTACHMENT_CONTENTPROPERTIES = "contentProperties";
/**
* The property key to access the attachment content status.
*/
public static final String KEY_ATTACHMENT_CONTENTSTATUS = "contentStatus";
/**
* The property key to access the attachment minor edit status.
*/
public static final String KEY_ATTACHMENT_CONTENT_MINOR_EDIT = "MINOR_EDIT";
/**
* The property key to access the attachment content size.
*/
public static final String KEY_ATTACHMENT_CONTENT_FILESIZE = "FILESIZE";
/**
* The property key to access the attachment content media type.
*/
public static final String KEY_ATTACHMENT_CONTENT_MEDIA_TYPE = "MEDIA_TYPE";
/**
* The property key to access the attachment creation author.
*/
public static final String KEY_ATTACHMENT_CREATION_AUTHOR = "creatorName";
/**
* The property key to access the attachment creation date.
*/
public static final String KEY_ATTACHMENT_CREATION_DATE = "creationDate";
/**
* The property key to access the attachment revision author.
*/
public static final String KEY_ATTACHMENT_REVISION_AUTHOR = "lastModifierName";
/**
* The property key to access the attachment revision date.
*/
public static final String KEY_ATTACHMENT_REVISION_DATE = "lastModificationDate";
/**
* The property key to access the attachment revision comment.
*/
public static final String KEY_ATTACHMENT_REVISION_COMMENT = "comment";
/**
* Old property to indicate attachment revision.
*
* @see #KEY_ATTACHMENT_VERSION
*/
public static final String KEY_ATTACHMENT_ATTACHMENTVERSION = "attachmentVersion";
/**
* The property key to access the attachment version.
*/
public static final String KEY_ATTACHMENT_VERSION = "version";
/**
* Old property to indicate attachment original revision.
*
* @see #KEY_ATTACHMENT_ORIGINALVERSIONID
*/
public static final String KEY_ATTACHMENT_ORIGINALVERSION = "originalVersion";
/**
* The property key to access the attachment original version.
*/
public static final String KEY_ATTACHMENT_ORIGINALVERSIONID = "originalVersionId";
/**
* The property key to access the attachment DTO.
*/
public static final String KEY_ATTACHMENT_DTO = "imageDetailsDTO";
/**
* The property key to access the content id or the comment id which has this body content.
* Note that we don't keep it in our internal representation currently.
*/
public static final String KEY_BODY_CONTENT_CONTENT = "content";
/**
* The property key to access the label name.
*/
public static final String KEY_LABEL_NAME = "name";
/**
* The property key to access the label id.
*/
public static final String KEY_LABELLING_LABEL = "label";
/**
* The property key to access the page id to which the label is attached.
*/
public static final String KEY_LABELLING_CONTENT = "content";
/**
* The property key to access the group name.
*/
public static final String KEY_GROUP_NAME = "name";
/**
* The property key to access the group active status.
*/
public static final String KEY_GROUP_ACTIVE = "active";
/**
* The property key to access the group local status.
*/
public static final String KEY_GROUP_LOCAL = "local";
/**
* The property key to access the group creation date.
*/
public static final String KEY_GROUP_CREATION_DATE = "createdDate";
/**
* The property key to access the group revision date.
*/
public static final String KEY_GROUP_REVISION_DATE = "updatedDate";
/**
* The property key to access the group description.
*/
public static final String KEY_GROUP_DESCRIPTION = "description";
/**
* The property key to access the group members.
*/
public static final String KEY_GROUP_MEMBERUSERS = "memberusers";
/**
* The property key to access the group members.
*/
public static final String KEY_GROUP_MEMBERGROUPS = "membergroups";
/**
* The property key to access the username.
*/
public static final String KEY_USER_NAME = "name";
/**
* The property key to access the user active status.
*/
public static final String KEY_USER_ACTIVE = "active";
/**
* The property key to access the user creation date.
*/
public static final String KEY_USER_CREATION_DATE = "createdDate";
/**
* The property key to access the user revision date.
*/
public static final String KEY_USER_REVISION_DATE = "updatedDate";
/**
* The property key to access the user first name.
*/
public static final String KEY_USER_FIRSTNAME = "firstName";
/**
* The property key to access the user last name.
*/
public static final String KEY_USER_LASTNAME = "lastName";
/**
* The property key to access the user display name.
*/
public static final String KEY_USER_DISPLAYNAME = "displayName";
/**
* The property key to access the user email.
*/
public static final String KEY_USER_EMAIL = "emailAddress";
/**
* The property key to access the user password.
*/
public static final String KEY_USER_PASSWORD = "credential";
/**
* The property key that was formerly used to mark blog pages.
*
* @since 9.24.0
* @deprecated since 9.35.0
*/
@Deprecated(since = "9.35.0")
public static final String KEY_PAGE_BLOGPOST = "blogpost";
/**
* The old property key to access the content owning a comment.
*/
public static final String KEY_COMMENT_OWNER = "owner";
/**
* The property key to access the content owning a content permission set.
*/
public static final String KEY_CONTENT_PERMISSION_SET_OWNING_CONTENT = "owningContent";
/**
* The property key to access the content permission set owning a content permission.
*/
public static final String KEY_CONTENT_PERMISSION_OWNING_SET = "owningSet";
/**
* The date format in a Confluence package (2012-03-07 17:16:48.158).
*/
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
/**
* The id property key.
* @since 9.47.0
*/
public static final String KEY_ID = "id";
/**
* The old content permission sets key in pages.
* @since 9.47.0
*/
public static final String KEY_CONTENT_CONTENT_PERMISSION_SETS = "contentPermissionSets";
/**
* The old content permission key in content permission sets.
* @since 9.47.0
*/
public static final String KEY_CONTENT_PERMISSION_SET_CONTENT_PERMISSIONS = "contentPermissions";
/**
* Pattern to find the end of "intentionally damaged" CDATA end sections. Confluence does this to nest CDATA
* sections inside CDATA sections. Interestingly it does not care if there is a > after the ]].
*/
private static final Pattern FIND_BROKEN_CDATA_PATTERN = Pattern.compile("]] ");
/**
* Replacement to repair the CDATA.
*/
private static final String REPAIRED_CDATA_END = "]]";
private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory.newInstance();
private static final String FOLDER_INTERNALUSER = "internalusers";
private static final String FOLDER_USERIMPL = "userimpls";
private static final String FOLDER_GROUP = "groups";
private static final String FOLDER_OBJECTS = "objects";
private static final String FOLDER_SPACE_PERMISSIONS = KEY_SPACE_PERMISSIONS;
private static final String PROPERTIES_FILENAME = "properties.properties";
private static final String SPACE_STATUS_ARCHIVED = "ARCHIVED";
private static final String OBJECT_TYPE_SPACE = "Space";
private static final String OBJECT_TYPE_PAGE = "Page";
private static final String OBJECT_TYPE_BLOG_POST = "BlogPost";
private static final String OBJECT_TYPE_COMMENT = "Comment";
private static final String OBJECT_TYPE_CONTENT_PERMISSION_SET = "ContentPermissionSet";
private static final String OBJECT_TYPE_CONTENT_PERMISSION = "ContentPermission";
private static final String OBJECT_TYPE_SPACE_PERMISSION = "SpacePermission";
private static final String OBJECT_TYPE_ATTACHMENT = "Attachment";
private static final String OBJECT_TYPE_BODY_CONTENT = "BodyContent";
private static final String OBJECT_TYPE_CONTENT_PROPERTY = "ContentProperty";
private static final String OBJECT_TYPE_INTERNAL_GROUP = "InternalGroup";
private static final String OBJECT_TYPE_INTERNAL_USER = "InternalUser";
private static final String OBJECT_TYPE_LABEL = "Label";
private static final String OBJECT_TYPE_LABELLING = "Labelling";
private static final String OBJECT_TYPE_HIBERNATE_MEMBERSHIP = "HibernateMembership";
private static final String OBJECT_TYPE_CONFLUENCE_USER_IMPL = "ConfluenceUserImpl";
private static final Collection<String> SUPPORTED_OBJECTS = new HashSet<>(Arrays.asList(
OBJECT_TYPE_ATTACHMENT,
OBJECT_TYPE_BLOG_POST,
OBJECT_TYPE_BODY_CONTENT,
OBJECT_TYPE_COMMENT,
OBJECT_TYPE_CONTENT_PERMISSION,
OBJECT_TYPE_CONTENT_PERMISSION_SET,
OBJECT_TYPE_CONTENT_PROPERTY,
OBJECT_TYPE_INTERNAL_GROUP,
OBJECT_TYPE_INTERNAL_USER,
OBJECT_TYPE_LABEL,
OBJECT_TYPE_LABELLING,
OBJECT_TYPE_PAGE,
OBJECT_TYPE_SPACE,
OBJECT_TYPE_SPACE_PERMISSION
));
private static final String[] PARENT_PROPERTIES = new String[] {
KEY_CONTENT_PERMISSION_OWNING_SET,
KEY_CONTENT_PERMISSION_SET_OWNING_CONTENT,
KEY_COMMENT_OWNER,
KEY_ATTACHMENT_CONTAINERCONTENT,
KEY_ATTACHMENT_CONTENT,
KEY_COMMENT_CONTAINERCONTENT,
KEY_CONTENT_PROPERTY_CONTENT,
KEY_PAGE_SPACE
};
private static final String RESTORING_FROM_ANOTHER_VERSION_UNSUPPORTED_WARNING =
"Restoring from a different version is unsupported and may lead to unexpected results.";
private static final String PROPERTY_CLASS_SUFFIX = "--class";
private static final String ATTRIBUTE_CLASS = "class";
@Inject
private Environment environment;
@Inject
private JobProgressManager progress;
@Inject
private Logger logger;
@Inject
private JobContext jobContext;
private CancelableJobStatus jobStatus;
private File directory;
/**
* Indicate if {@link #directory} is temporary (extracted from a source package).
*/
private boolean temporaryDirectory;
private File entities;
private File descriptor;
private File tree;
// Maps a space id to all the pages in this space
private final Map<Long, List<Long>> pages = new LinkedHashMap<>();
// Maps a space id to all the blog pages in this space
private final Map<Long, List<Long>> blogPages = new LinkedHashMap<>();
// Maps a page id to its direct non-blog children
private final Map<Long, List<Long>> pageChildren = new LinkedHashMap<>();
// List of parent pages that have not been seen
private final Map<Long, Set<Long>> missingParents = new LinkedHashMap<>();
// maps a space id to its home page
private final Map<Long, Long> homePages = new LinkedHashMap<>();
private final Map<Long, List<Long>> orphans = new LinkedHashMap<>();
private final Map<String, Long> spacesByKey = new HashMap<>();
private final Map<Long, Map<String, Long>> pagesBySpaceAndTitle = new HashMap<>();
/**
* @return the content permission sets of the given page properties.
* @param pageProperties the page of which to get the permission sets.
* @since 9.47.0
*/
public static List<Object> getContentPermissionSets(ConfluenceProperties pageProperties)
{
return pageProperties.getList(KEY_CONTENT_CONTENT_PERMISSION_SETS);
}
/**
* @return the content permissions of the given permission set.
* @param permissionSetProperties the permission set of which to get the permissions.
* @since 9.47.0
*/
public static List<Object> getContentPermissions(ConfluenceProperties permissionSetProperties)
{
return permissionSetProperties.getList(KEY_CONTENT_PERMISSION_SET_CONTENT_PERMISSIONS);
}
/**
* @return the children of the given page.
* @param pageId the page of which to get the children
* @since 9.35.0
*/
public List<Long> getPageChildren(Long pageId)
{
return pageChildren.getOrDefault(pageId, Collections.emptyList());
}
/**
* @return the home page of the given space.
* @param spaceId the space of which to get the home page
* @since 9.35.0
*/
public Long getHomePage(Long spaceId)
{
return homePages.get(spaceId);
}
/**
* @return the orphans (pages which don't have a parent) of the given space
* @param spaceId the space of which to get the orphans
* @since 9.35.0
*/
public List<Long> getOrphans(Long spaceId)
{
List<Long> spaceOrphans = this.orphans.getOrDefault(spaceId, Collections.emptyList());
Collection<Long> spaceMissingParents = this.missingParents.get(spaceId);
if (spaceMissingParents == null || spaceMissingParents.isEmpty()) {
return spaceOrphans;
}
spaceOrphans = new ArrayList<>(spaceOrphans);
for (Long missingParent : spaceMissingParents) {
spaceOrphans.addAll(getPageChildren(missingParent));
}
return spaceOrphans;
}
/**
* @return a page id from a space key and its title
* @param spaceKey the space in which the page is supposed to be
* @param pageTitle the title of the page
* @since 9.35.0
*/
public Long getPageId(String spaceKey, String pageTitle)
{
Long spaceId = this.spacesByKey.get(spaceKey);
if (spaceId == null) {
return null;
}
Map<String, Long> pagesByTitle = this.pagesBySpaceAndTitle.get(spaceId);
if (pagesByTitle == null) {
return null;
}
return pagesByTitle.get(pageTitle);
}
/**
* @return the comments of the given page
* @param pageProperties the page of which to get the comments
* @since 9.47.0
*/
public List<Long> getPageComments(ConfluenceProperties pageProperties)
{
List<Long> l = getLongList(pageProperties, ConfluenceXMLPackage.KEY_PAGE_COMMENTS);
return l == null ? new ArrayList<>() : l;
}
/**
* @param source the source where to find the package to parse
* @param workingDirectory the directory to use to extract the conflence package for processing (can be null)
* @throws IOException when failing to access the package content
* @throws FilterException when any error happen during the reading of the package
* @since 9.37.0
*/
public void read(InputSource source, String workingDirectory) throws IOException, FilterException
{
if (source instanceof FileInputSource) {
fromFile(((FileInputSource) source).getFile());
} else if (source instanceof URLInputSource
&& ((URLInputSource) source).getURL().getProtocol().equals("file")) {
URI uri;
try {
uri = ((URLInputSource) source).getURL().toURI();
} catch (Exception e) {
throw new FilterException("The passed file URL is invalid", e);
}
fromFile(new File(uri));
} else {
try {
if (source instanceof InputStreamInputSource) {
fromStream((InputStreamInputSource) source);
} else {
throw new FilterException(
String.format("Unsupported input source of type [%s]", source.getClass().getName()));
}
} finally {
source.close();
}
}
this.entities = new File(this.directory, FILE_ENTITIES);
this.descriptor = new File(this.directory, FILE_DESCRIPTOR);
// Initialize
try {
createTree(workingDirectory);
} catch (Exception e) {
throw new FilterException("Failed to analyze the package index", e);
}
}
/**
* @param source the source where to find the package to parse
* @throws IOException when failing to access the package content
* @throws FilterException when any error happen during the reading of the package
*/
public void read(InputSource source) throws IOException, FilterException
{
read(source, null);
}
private void saveState() throws IllegalAccessException, IOException
{
File state = new File(this.tree, "state");
state.mkdir();
Files.write(getExtractedPackageVersionPath(state), getVersion().getBytes());
for (Field field : this.getClass().getDeclaredFields()) {
if (isStateField(field)) {
FileOutputStream fos = new FileOutputStream(new File(state, field.getName()));
ObjectOutputStream myObjectOutStream = new ObjectOutputStream(fos);
myObjectOutStream.writeObject(field.get(this));
fos.close();
}
}
}
private void clearState()
{
for (Field field : this.getClass().getDeclaredFields()) {
if (isStateField(field)) {
Object f;
try {
f = field.get(this);
} catch (IllegalAccessException e) {
logger.error("Unexpected error when clearing the state of field [{}]", field.getName(), e);
continue;
}
if (f instanceof Collection) {
((Collection) f).clear();
} else if (f instanceof Map) {
((Map) f).clear();
}
}
}
}
private static boolean isStateField(Field field)
{
Class<?> type = field.getType();
return !Modifier.isStatic(field.getModifiers())
&& (Map.class.isAssignableFrom(type) || Collection.class.isAssignableFrom(type));
}
private String getVersion()
{
String version = getClass().getPackage().getSpecificationVersion();
return version == null ? "" : version;
}
private boolean restoreState(File tree)
{
if (!tree.exists()) {
return false;
}
File state = new File(tree, "state");
if (!state.exists()) {
return false;
}
checkExtractedPackageVersion(tree, state);
for (Field field : this.getClass().getDeclaredFields()) {
if (isStateField(field) && !restoreStateField(field, state)) {
clearState();
return false;
}
}
this.tree = tree;
return true;
}
private boolean restoreStateField(Field field, File state)
{
String name = field.getName();
FileInputStream fis;
Object property;
try {
fis = new FileInputStream(new File(state, name));
ObjectInputStream objectInput = new ObjectInputStream(fis);
property = objectInput.readObject();
objectInput.close();
fis.close();
} catch (IOException | ClassNotFoundException e) {
logger.warn("Could not restore the package state: field [{}] is unreadable", name, e);
return false;
}
try {
Object f = field.get(this);
if (f instanceof Collection && property instanceof Collection) {
((Collection) f).addAll((Collection) property);
} else if (f instanceof Map && property instanceof Map) {
((Map) f).putAll((Map) property);
} else {
logger.warn("Could not restore the package state: wrong type for field [{}]", name);
return false;
}
} catch (IllegalAccessException e) {
logger.warn("Could not restore the Confluence package state", e);
return false;
}
return true;
}
private void checkExtractedPackageVersion(File tree, File state)
{
logger.info("Restoring from extracted Confluence package found at [{}]", tree.getPath());
try {
String version = Files.readString(getExtractedPackageVersionPath(state)).trim();
if (!getVersion().equals(version)) {
this.logger.warn(
"The package was extracted by version [{}]. Current version is [{}]. "
+ RESTORING_FROM_ANOTHER_VERSION_UNSUPPORTED_WARNING, version, getVersion());
}
} catch (IOException e) {
this.logger.warn(
"Could not determine the version of the extracted package. "
+ RESTORING_FROM_ANOTHER_VERSION_UNSUPPORTED_WARNING, e);
}
}
private static Path getExtractedPackageVersionPath(File state)
{
return Paths.get(state.getPath() + "/version.txt");
}
/**
* @param workingDirectoryPath the path to the working directory to restore the state from
* @return whether restoring succeeded
*/
public boolean restoreState(String workingDirectoryPath)
{
return restoreState(new File(workingDirectoryPath));
}
private void fromFile(File file) throws FilterException
{
if (file.isDirectory()) {
this.directory = file;