SharedPdfDocumentContext.java
8.53 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemfd.docmanager.Document
* com.adobe.aemfd.docmanager.io.IOUtils
* com.adobe.aemfd.docmanager.source.DocumentSourceHandler
* com.adobe.aemfd.docmanager.source.FileBasedDocumentSourceHandler
* com.adobe.internal.io.ByteArrayByteReader
* com.adobe.internal.io.ByteReader
* com.adobe.internal.io.RandomAccessFileByteReader
* com.adobe.internal.pdftoolkit.core.exceptions.PDFException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.permissionprovider.ObjectOperations
* com.adobe.internal.pdftoolkit.pdf.document.PDFDocument
* com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions
* com.adobe.internal.pdftoolkit.pdf.document.PDFSaveOptions
* com.adobe.internal.pdftoolkit.pdf.document.PDFVersion
* com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm
* com.adobe.internal.pdftoolkit.services.permissions.PermissionsManager
* com.adobe.internal.pdftoolkit.services.xmp.DocumentMetadata
* com.adobe.internal.pdftoolkit.services.xmp.MetadataOptions
* com.adobe.internal.pdftoolkit.services.xmp.MetadataOptions$MetadataOptionsBuilder
* com.adobe.internal.pdftoolkit.services.xmp.XMPService
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemfd.pdfdocmanager;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.aemfd.docmanager.io.IOUtils;
import com.adobe.aemfd.docmanager.source.DocumentSourceHandler;
import com.adobe.aemfd.docmanager.source.FileBasedDocumentSourceHandler;
import com.adobe.aemfd.pdfdocmanager.SharedPdfDocument;
import com.adobe.aemfd.pdfdocmanager.internal.PermissionsManagerNoXFAPermissionsPatch;
import com.adobe.aemfd.pdfdocmanager.internal.io.DocumentDisposingFilterByteReader;
import com.adobe.aemfd.pdfdocmanager.internal.io.ManagedRandomAccessFileByteReader;
import com.adobe.internal.io.ByteArrayByteReader;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.RandomAccessFileByteReader;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.permissionprovider.ObjectOperations;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFVersion;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm;
import com.adobe.internal.pdftoolkit.services.permissions.PermissionsManager;
import com.adobe.internal.pdftoolkit.services.xmp.DocumentMetadata;
import com.adobe.internal.pdftoolkit.services.xmp.MetadataOptions;
import com.adobe.internal.pdftoolkit.services.xmp.XMPService;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SharedPdfDocumentContext {
private static final Logger logger = LoggerFactory.getLogger(SharedPdfDocumentContext.class);
private static MetadataOptions xmpOptions = null;
private SharedPdfDocumentContext() {
}
private static ByteReader newByteReader(Document doc) throws PDFIOException {
try {
byte[] inline = doc.getInlineData();
if (inline != null) {
return new ByteArrayByteReader(inline);
}
DocumentSourceHandler dsh = doc.getSourceHandler();
if (dsh == null) {
throw new IllegalStateException("Source handler for the given document is null even after passivation!");
}
if (dsh instanceof FileBasedDocumentSourceHandler) {
FileBasedDocumentSourceHandler fdsh = (FileBasedDocumentSourceHandler)dsh;
RandomAccessFile raf = new RandomAccessFile(fdsh.getSourceFile(), "r");
return new RandomAccessFileByteReader(raf);
}
File docFile = IOUtils.createTempFile((String)".pdf");
doc.copyToFile(docFile);
RandomAccessFile raf = new RandomAccessFile(docFile, "r");
return new ManagedRandomAccessFileByteReader(raf, docFile);
}
catch (IOException e) {
throw new PDFIOException("Error creating ByteReader for given document", (Throwable)e);
}
}
public static PDFDocument toPDFDocument(Document doc, PDFOpenOptions options) throws PDFException {
return SharedPdfDocumentContext.toPDFDocument(doc, options, false);
}
public static PDFDocument toPDFDocument(Document doc, PDFOpenOptions options, boolean ownDoc) throws PDFException {
Object br = SharedPdfDocumentContext.newByteReader(doc);
if (ownDoc) {
br = new DocumentDisposingFilterByteReader((ByteReader)br, doc);
}
PDFOpenOptions opts = options != null ? options : PDFOpenOptions.newInstance();
return PDFDocument.newInstance((ByteReader)br, (PDFOpenOptions)opts);
}
public static SharedPdfDocument toDocument(PDFDocument pdfDoc, PDFSaveOptions options) {
return new SharedPdfDocument(pdfDoc, options);
}
public static boolean doesVersionChangeForceFullSave(PDFDocument pdfDoc) throws PDFException {
boolean result = false;
PDFVersion t = pdfDoc.getToSaveVersion();
String toSaveVersionString = null;
if (t != null) {
toSaveVersionString = t.getVersionValue();
}
t = pdfDoc.getOriginalVersion();
String originalVersionString = null;
originalVersionString = t == null ? PDFVersion.vLatest.getVersionValue() : t.getVersionValue();
if (toSaveVersionString == null) {
toSaveVersionString = originalVersionString;
}
if (!originalVersionString.equals(toSaveVersionString)) {
String[] originalVersionElements = originalVersionString.split("\\.");
int originalVersionMajorVal = Integer.parseInt(originalVersionElements[0]);
int originalVersionMinorVal = 0;
if (originalVersionElements.length > 1) {
originalVersionMinorVal = Integer.parseInt(originalVersionElements[1]);
}
String[] toSaveVersionElements = toSaveVersionString.split("\\.");
int toSaveVersionMajorVal = Integer.parseInt(toSaveVersionElements[0]);
int toSaveVersionMinorVal = 0;
if (toSaveVersionElements.length > 1) {
toSaveVersionMinorVal = Integer.parseInt(toSaveVersionElements[1]);
}
if (toSaveVersionMajorVal == 1 && toSaveVersionMinorVal < 4) {
result = true;
}
if (toSaveVersionMajorVal < originalVersionMajorVal) {
result = true;
}
if (toSaveVersionMajorVal == originalVersionMajorVal && toSaveVersionMinorVal < originalVersionMinorVal) {
result = true;
}
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static MetadataOptions getMetadataOptions() {
if (xmpOptions != null) {
return xmpOptions;
}
MetadataOptions.MetadataOptionsBuilder optionsBuilder = MetadataOptions.MetadataOptionsBuilder.newInstance();
optionsBuilder.setAutoUpdate(true);
optionsBuilder.setDocumentIdGenerator(new Random());
optionsBuilder.setUpdateOnlyIfDocumentChanged(false);
Class<SharedPdfDocumentContext> class_ = SharedPdfDocumentContext.class;
synchronized (SharedPdfDocumentContext.class) {
if (xmpOptions == null) {
xmpOptions = optionsBuilder.build();
}
// ** MonitorExit[var1_1] (shouldn't be in output)
return xmpOptions;
}
}
public static void updateMetadataTimestamps(PDFDocument doc) {
try {
PDFInteractiveForm iform = doc.getInteractiveForm();
if (iform != null && iform.hasXFA()) {
return;
}
PermissionsManager pm = PermissionsManagerNoXFAPermissionsPatch.newInstance(doc);
if (pm.isPermitted(ObjectOperations.DOC_MODIFY)) {
DocumentMetadata md = XMPService.getDocumentMetadata((PDFDocument)doc, (MetadataOptions)SharedPdfDocumentContext.getMetadataOptions());
}
}
catch (PDFException e) {
logger.warn("Error encountered while updating PDF metadata timestamps", (Throwable)e);
}
}
}