-
Notifications
You must be signed in to change notification settings - Fork 14
/
DocumentListZipExporter.java
250 lines (218 loc) · 8.76 KB
/
DocumentListZipExporter.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
package org.nuxeo.ecm.webapp.clipboard;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipOutputStream;
import org.nuxeo.common.utils.StringUtils;
import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.LifeCycleConstants;
import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
import org.nuxeo.ecm.core.api.impl.blob.StringBlob;
import org.nuxeo.runtime.api.Framework;
public class DocumentListZipExporter {
public static final String ZIP_ENTRY_ENCODING_PROPERTY = "zip.entry.encoding";
public static enum ZIP_ENTRY_ENCODING_OPTIONS {
ascii
}
private static final int BUFFER = 2048;
private static final String SUMMARY_FILENAME = "INDEX.txt";
public File exportWorklistAsZip(List<DocumentModel> documents,
CoreSession documentManager, boolean exportAllBlobs)
throws ClientException, IOException {
StringBuilder blobList = new StringBuilder();
File tmpFile = File.createTempFile("NX-BigZipFile-", ".zip");
tmpFile.deleteOnExit(); // file is deleted after being downloaded in
// DownloadServlet
FileOutputStream fout = new FileOutputStream(tmpFile);
ZipOutputStream out = new ZipOutputStream(fout);
out.setMethod(ZipOutputStream.DEFLATED);
out.setLevel(9);
byte[] data = new byte[BUFFER];
for (DocumentModel doc : documents) {
// first check if DM is attached to the core
if (doc.getSessionId() == null) {
// refetch the doc from the core
doc = documentManager.getDocument(doc.getRef());
}
// NXP-2334 : skip deleted docs
if (LifeCycleConstants.DELETED_STATE.equals(doc.getCurrentLifeCycleState())) {
continue;
}
BlobHolder bh = doc.getAdapter(BlobHolder.class);
if (doc.isFolder() && !isEmptyFolder(doc, documentManager)) {
addFolderToZip("", out, doc, data, documentManager, blobList,
exportAllBlobs);
} else if (bh != null) {
addBlobHolderToZip("", out, doc, data, blobList, bh,
exportAllBlobs);
}
}
if (blobList.length() > 1) {
addSummaryToZip(out, data, blobList);
}
try {
out.close();
fout.close();
} catch (ZipException e) {
return null;
}
return tmpFile;
}
private void addFolderToZip(String path, ZipOutputStream out,
DocumentModel doc, byte[] data, CoreSession documentManager,
StringBuilder blobList, boolean exportAllBlobs)
throws ClientException, IOException {
String title = doc.getTitle();
List<DocumentModel> docList = documentManager.getChildren(doc.getRef());
for (DocumentModel docChild : docList) {
// NXP-2334 : skip deleted docs
if (LifeCycleConstants.DELETED_STATE.equals(docChild.getCurrentLifeCycleState())) {
continue;
}
BlobHolder bh = docChild.getAdapter(BlobHolder.class);
String newPath = null;
if (path.length() == 0) {
newPath = title;
} else {
newPath = path + "/" + title;
}
if (docChild.isFolder()
&& !isEmptyFolder(docChild, documentManager)) {
addFolderToZip(newPath, out, docChild, data, documentManager,
blobList, exportAllBlobs);
} else if (bh != null) {
addBlobHolderToZip(newPath, out, docChild, data, blobList, bh,
exportAllBlobs);
}
}
}
private boolean isEmptyFolder(DocumentModel doc, CoreSession documentManager)
throws ClientException {
List<DocumentModel> docList = documentManager.getChildren(doc.getRef());
for (DocumentModel docChild : docList) {
// If there is a blob or a folder, it is not empty.
if (docChild.getAdapter(BlobHolder.class) != null
|| docChild.isFolder()) {
return false;
}
}
return true;
}
/**
* Writes a summary file and puts it in the archive.
*/
private void addSummaryToZip(ZipOutputStream out, byte[] data,
StringBuilder sb) throws IOException {
Blob content = new StringBlob(sb.toString());
BufferedInputStream buffi = new BufferedInputStream(
content.getStream(), BUFFER);
ZipEntry entry = new ZipEntry(SUMMARY_FILENAME);
out.putNextEntry(entry);
int count = buffi.read(data, 0, BUFFER);
while (count != -1) {
out.write(data, 0, count);
count = buffi.read(data, 0, BUFFER);
}
out.closeEntry();
buffi.close();
}
private void addBlobHolderToZip(String path, ZipOutputStream out,
DocumentModel doc, byte[] data, StringBuilder blobList,
BlobHolder bh, boolean exportAllBlobs) throws IOException,
ClientException {
List<Blob> blobs = new ArrayList<Blob>();
if (exportAllBlobs) {
if (bh.getBlobs() != null) {
blobs = bh.getBlobs();
}
} else {
Blob mainBlob = bh.getBlob();
if (mainBlob != null) {
blobs.add(mainBlob);
}
}
if (blobs.size() > 0) { // add document info
SimpleDateFormat format = new SimpleDateFormat(
"dd-MM-yyyy HH:mm:ss");
if (path.length() > 0) {
blobList.append(path).append('/');
}
blobList.append(doc.getTitle()).append(" ");
blobList.append(doc.getType()).append(" ");
Calendar c = (Calendar) doc.getPropertyValue("dc:modified");
if (c != null) {
blobList.append(format.format(c.getTime()));
}
blobList.append("\n");
}
for (Blob content : blobs) {
String fileName = content.getFilename();
if (fileName == null) {
// use a default value
fileName = "file.bin";
}
BufferedInputStream buffi = new BufferedInputStream(
content.getStream(), BUFFER);
// Workaround to deal with duplicate file names.
int tryCount = 0;
String entryPath = null;
String entryName = null;
while (true) {
try {
ZipEntry entry = null;
if (tryCount == 0) {
entryName = fileName;
} else {
entryName = formatFileName(fileName, "(" + tryCount
+ ")");
}
if (path.length() == 0) {
entryPath = entryName;
} else {
entryPath = path + "/" + entryName;
}
entryPath = escapeEntryPath(entryPath);
entry = new ZipEntry(entryPath);
out.putNextEntry(entry);
break;
} catch (ZipException e) {
tryCount++;
}
}
blobList.append(" - ").append(entryName).append("\n");
int count = buffi.read(data, 0, BUFFER);
while (count != -1) {
out.write(data, 0, count);
count = buffi.read(data, 0, BUFFER);
}
out.closeEntry();
buffi.close();
}
}
private String formatFileName(String filename, String count) {
StringBuilder sb = new StringBuilder();
CharSequence name = filename.subSequence(0, filename.lastIndexOf("."));
CharSequence extension = filename.subSequence(
filename.lastIndexOf("."), filename.length());
sb.append(name).append(count).append(extension);
return sb.toString();
}
protected String escapeEntryPath(String path) {
String zipEntryEncoding = Framework.getProperty(ZIP_ENTRY_ENCODING_PROPERTY);
if (zipEntryEncoding != null
&& zipEntryEncoding.equals(ZIP_ENTRY_ENCODING_OPTIONS.ascii.toString())) {
return StringUtils.toAscii(path);
}
return path;
}
}