Document.java
10.9 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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.api.SlingIOException
* org.apache.sling.api.adapter.Adaptable
* org.apache.sling.api.adapter.SlingAdaptable
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.wrappers.ValueMapDecorator
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemfd.docmanager;
import com.adobe.aemfd.docmanager.internal.passivation.ByteArrayPassivationHandler;
import com.adobe.aemfd.docmanager.internal.passivation.FilePassivationHandler;
import com.adobe.aemfd.docmanager.internal.passivation.InputStreamPassivationHandler;
import com.adobe.aemfd.docmanager.internal.passivation.jcr.JcrURLPassivationHandler;
import com.adobe.aemfd.docmanager.internal.passivation.jcr.ProvidedJcrURLPassivationHandler;
import com.adobe.aemfd.docmanager.internal.passivation.url.URLPassivationHandler;
import com.adobe.aemfd.docmanager.internal.source.ByteArraySourceHandler;
import com.adobe.aemfd.docmanager.io.IOUtils;
import com.adobe.aemfd.docmanager.passivation.DocumentPassivationHandler;
import com.adobe.aemfd.docmanager.passivation.PassivationConnection;
import com.adobe.aemfd.docmanager.passivation.PassivationType;
import com.adobe.aemfd.docmanager.source.DocumentSourceHandler;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.sling.api.SlingIOException;
import org.apache.sling.api.adapter.Adaptable;
import org.apache.sling.api.adapter.SlingAdaptable;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class Document
extends SlingAdaptable
implements Closeable,
Adaptable {
private static final Logger logger = LoggerFactory.getLogger(Document.class);
private int maxInlineSize = 65536;
private Map<String, Object> attributes = new HashMap<String, Object>();
private String contentType = null;
private DocumentPassivationHandler passivationHandler;
private boolean passivated = false;
private DocumentSourceHandler sourceHandler;
private boolean disposed = false;
private byte[] inlineData = null;
public static final String SOURCE_URL_ATTRIBUTE = "adobe.docmanager.source.url";
public static final String SOURCE_FILE_ATTRIBUTE = "adobe.docmanager.source.file";
public static final String SOURCE_JCR_PATH_ATTRIBUTE = "adobe.docmanager.source.jcr.path";
public Document(byte[] data) {
this.passivationHandler = new ByteArrayPassivationHandler(data);
}
public Document(URL url) {
this.passivationHandler = new URLPassivationHandler(url);
this.setAttribute("adobe.docmanager.source.url", url.toExternalForm());
}
public Document(File file) {
this(file, false);
}
public Document(File file, boolean ownFile) {
this.passivationHandler = new FilePassivationHandler(file, ownFile);
this.setAttribute("adobe.docmanager.source.file", file.getPath());
}
public Document(InputStream is) {
this.passivationHandler = new InputStreamPassivationHandler(is);
}
public Document(String jcrPath) {
this.passivationHandler = new JcrURLPassivationHandler(jcrPath);
this.setAttribute("adobe.docmanager.source.jcr.path", jcrPath);
}
public Document(String jcrPath, ResourceResolver resolver) {
this(jcrPath, resolver, false);
}
public Document(String jcrPath, ResourceResolver resolver, boolean manageResolver) {
this.passivationHandler = new ProvidedJcrURLPassivationHandler(jcrPath, resolver, manageResolver);
this.setAttribute("adobe.docmanager.source.jcr.path", jcrPath);
}
public Document(DocumentPassivationHandler passivationHandler) {
this.passivationHandler = passivationHandler;
}
protected void checkDisposed() {
if (this.disposed) {
throw new IllegalStateException("Document is in a disposed state!");
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static DocumentSourceHandler doPassivate(Document d, DocumentPassivationHandler ph) throws IOException {
PassivationConnection pc = ph.openConnectionForPassivation();
PassivationType pType = null;
try {
DocumentSourceHandler result;
long l = pc.getLength();
String ct = pc.getContentType();
int mil = d.getMaxInlineSize();
if (l != -1) {
if (l > (long)mil) {
result = ph.handlePassivation(null, pc);
pType = PassivationType.EXTERNALIZED;
} else {
byte[] data = IOUtils.getBytes(pc.getContentStream(), (int)l);
d.inlineData = data;
ph.inlined();
result = new ByteArraySourceHandler(data, ct);
pType = PassivationType.INLINED;
}
} else {
int maxToRead = mil + 1;
byte[] preBuf = IOUtils.getBytes(pc.getContentStream(), maxToRead);
if (preBuf.length == maxToRead) {
result = ph.handlePassivation(preBuf, pc);
pType = PassivationType.EXTERNALIZED;
} else {
d.inlineData = preBuf;
ph.inlined();
result = new ByteArraySourceHandler(preBuf, ct);
pType = PassivationType.INLINED;
}
}
DocumentSourceHandler maxToRead = result;
ph.dispose(pc, pType != null, pType);
return maxToRead;
}
catch (Throwable var11_11) {
ph.dispose(pc, pType != null, pType);
throw var11_11;
}
}
protected void prePassivate() throws IOException {
}
protected void postPassivate() throws IOException {
}
public void passivate() throws IOException {
this.checkDisposed();
if (!this.passivated) {
this.prePassivate();
this.sourceHandler = Document.doPassivate(this, this.passivationHandler);
this.passivationHandler.release(true, this.inlineData == null ? PassivationType.EXTERNALIZED : PassivationType.INLINED);
this.passivationHandler = null;
this.passivated = true;
this.postPassivate();
}
}
public DocumentSourceHandler getSourceHandler() {
this.checkDisposed();
return this.sourceHandler;
}
public byte[] getInlineData() throws IOException {
this.passivate();
return this.inlineData;
}
public InputStream getInputStream() throws IOException {
this.passivate();
return this.sourceHandler.getInputStream();
}
public long length() throws IOException {
this.passivate();
return this.sourceHandler.length();
}
public String getContentType() throws IOException {
this.checkDisposed();
if (this.contentType != null) {
return this.contentType;
}
this.passivate();
return this.sourceHandler.getContentType();
}
public void dispose() {
if (this.disposed) {
logger.warn("dispose() called on already disposed document, ignoring...");
return;
}
if (!this.passivated) {
this.passivationHandler.release(false, null);
this.passivationHandler = null;
} else {
this.sourceHandler.dispose();
this.sourceHandler = null;
this.inlineData = null;
}
this.attributes.clear();
this.contentType = null;
this.disposed = true;
}
public int getMaxInlineSize() {
this.checkDisposed();
return this.maxInlineSize;
}
public void setMaxInlineSize(int maxInlineSize) {
this.checkDisposed();
if (this.passivated) {
throw new IllegalStateException("Document has already been passivated!");
}
this.maxInlineSize = maxInlineSize;
}
public Object getAttribute(String name) {
this.checkDisposed();
return this.attributes.get(name);
}
public void setAttribute(String name, Object val) {
this.checkDisposed();
this.attributes.put(name, val);
}
public void removeAttribute(String name) {
this.checkDisposed();
this.attributes.remove(name);
}
public void setContentType(String contentType) {
this.checkDisposed();
this.contentType = contentType;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void copyToFile(File file) throws IOException {
InputStream is = this.getInputStream();
try {
IOUtils.copyToFile(is, file);
}
finally {
try {
is.close();
}
catch (IOException e) {
logger.warn("Failed to close input-stream of type " + is.getClass().getName() + " while cleaning up in copyToFile()", (Throwable)e);
}
}
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
this.checkDisposed();
if (type == InputStream.class) {
try {
return (AdapterType)this.getInputStream();
}
catch (IOException e) {
throw new SlingIOException(e);
}
}
if (type == Map.class) {
return (AdapterType)Collections.unmodifiableMap(this.attributes);
}
if (type == ValueMap.class) {
return (AdapterType)((Object)new ValueMapDecoratorExt(Collections.unmodifiableMap(this.attributes)));
}
return (AdapterType)super.adaptTo(type);
}
protected void finalize() throws Throwable {
if (!this.disposed) {
this.dispose();
}
Object.super.finalize();
}
@Override
public void close() throws IOException {
this.dispose();
}
private void writeObject(ObjectOutputStream stream) throws IOException {
throw new UnsupportedOperationException("Serialization unsupported!");
}
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
throw new UnsupportedOperationException("Serialization unsupported!");
}
private static class ValueMapDecoratorExt
extends ValueMapDecorator {
private ValueMapDecoratorExt(Map base) {
super(base);
}
}
}