-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathXdmPackage.java
More file actions
424 lines (334 loc) · 13.1 KB
/
Copy pathXdmPackage.java
File metadata and controls
424 lines (334 loc) · 13.1 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
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
package org.nhindirect.xd.common;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import javax.activation.DataHandler;
import org.apache.commons.lang3.StringUtils;
import org.nhindirect.xd.transform.util.type.MimeType;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class XdmPackage {
private String messageId;
private DirectDocuments documents;
@Deprecated
private static final String SUFFIX = ".xml";
private static final int BUFFER = 2048;
private static final String XDM_SUB_FOLDER = "IHE_XDM/SUBSET01/";
private static final String XDM_METADATA_FILE = "METADATA.xml";
public XdmPackage() {
this(UUID.randomUUID().toString());
}
public XdmPackage(String messageId) {
this.messageId = messageId;
this.documents = new DirectDocuments();
}
public void setDocuments(DirectDocuments documents) {
this.documents = documents;
}
public DirectDocuments getDocuments() {
return this.documents;
}
public File toFile() {
File xdmFile = null;
try {
xdmFile = new File(messageId + "-xdm.zip");
FileOutputStream dest = new FileOutputStream(xdmFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(dest));
zipOutputStream.setMethod(ZipOutputStream.DEFLATED);
for (DirectDocument2 document : documents.getDocuments()) {
if (document.getData() != null) {
String fileName = document.getMetadata().getId() ;
fileName = fileName.replace("urn:uuid:", "");
fileName = fileName + getSuffix(document.getMetadata().getMimeType());
document.getMetadata().setURI(fileName);
addEntry(zipOutputStream, document.getData(), XDM_SUB_FOLDER + fileName );
}
}
addEntry(zipOutputStream, documents.getSubmitObjectsRequestAsString().getBytes(), XDM_SUB_FOLDER + XDM_METADATA_FILE);
addEntry(zipOutputStream, getIndex().getBytes(), "INDEX.htm");
addEntry(zipOutputStream, getReadme().getBytes(), "README.txt");
if (SUFFIX.equals(".xml")) {
addEntry(zipOutputStream, getXsl().getBytes(), XDM_SUB_FOLDER + "CCD.xsl");
}
zipOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return xdmFile;
}
private void addEntry(ZipOutputStream zipOutputStream, byte[] data, String fileName) throws IOException {
InputStream inputStream = new ByteArrayInputStream(data);
BufferedInputStream outputStream = new BufferedInputStream(inputStream);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(zipEntry);
int count = 0;
byte dataOut[] = new byte[BUFFER];
while ((count = outputStream.read(dataOut, 0, BUFFER)) != -1) {
zipOutputStream.write(dataOut, 0, count);
}
}
/*
* Get the index file.
*/
public String getIndex() throws Exception {
String data;
byte[] bytes;
try {
bytes = readFile("INDEX_head.txt");
data = new String(bytes);
for (DirectDocument2 document : documents.getDocuments()) {
if (document.getData() != null) {
String file = XDM_SUB_FOLDER + document.getMetadata().getId() + getSuffix(document.getMetadata().getMimeType());
data += "<li><a href=\"" + file + "\">" + file + "</a> - " + document.getMetadata().getDescription() + "</li>";
}
}
bytes = readFile("INDEX_tail.txt");
data += new String(bytes);
} catch (Exception e) {
log.error("Unable to access index file.", e);
throw e;
}
return data;
}
/*
* Get the readme file.
*/
public String getReadme() throws Exception {
byte[] bytes;
try {
bytes = readFile("README.txt");
} catch (Exception e) {
log.error("Unable to access readme file.", e);
throw e;
}
return new String(bytes);
}
/*
* Get the xsl file.
*/
public String getXsl() throws Exception {
byte[] bytes;
try {
bytes = readFile("CCD.xsl");
} catch (Exception e) {
log.error("Unable to access xsl file.", e);
throw e;
}
return new String(bytes);
}
public static XdmPackage fromXdmZipDataHandler(DataHandler dataHandler) throws Exception {
File file = null;
try {
// Create a temporary work file
file = fileFromDataHandler(dataHandler);
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("Error creating temporary work file, unable to complete transformation.", e);
}
throw new Exception("Error creating temporary work file, unable to complete transformation.", e);
}
XdmPackage xdmPackage = fromXdmZipFile(file);
boolean delete = file.delete();
if (delete) {
if (log.isTraceEnabled()) {
log.trace("Deleted temporary work file " + file.getAbsolutePath());
}
} else {
if (log.isWarnEnabled()) {
log.warn("Unable to delete temporary work file " + file.getAbsolutePath());
}
}
return xdmPackage;
}
public static XdmPackage fromXdmZipFile(File file) throws Exception {
DirectDocuments documents = new DirectDocuments();
ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ);
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
ZipEntry zipEntry = null;
// Load metadata
while (zipEntries.hasMoreElements()) {
zipEntry = zipEntries.nextElement();
String zname = zipEntry.getName();
log.info("Processing a ZipEntry named " + zname);
if (!zipEntry.isDirectory()) {
String subsetDirspec = getSubmissionSetDirspec(zipEntry.getName());
// Read metadata
if (matchName(zname, subsetDirspec, XDM_METADATA_FILE)) {
ByteArrayOutputStream byteArrayOutputStream = readData(zipFile, zipEntry);
documents.setValues(byteArrayOutputStream.toString());
}
}
}
zipEntries = zipFile.entries();
// load data
while (zipEntries.hasMoreElements()) {
log.trace("Processing a ZipEntry");
zipEntry = zipEntries.nextElement();
String zname = zipEntry.getName();
if (!zipEntry.isDirectory()) {
String subsetDirspec = getSubmissionSetDirspec(zipEntry.getName());
// Read data
// if (StringUtils.contains(subsetDirspec, StringUtils.remove(XDM_SUB_FOLDER, "/"))
if (StringUtils.contains(subsetDirspec,XDM_SUB_FOLDER)
&& !StringUtils.contains(zname, ".xsl") && !StringUtils.contains(zname, XDM_METADATA_FILE)) {
ByteArrayOutputStream byteArrayOutputStream = readData(zipFile, zipEntry);
String digest = DirectDocument2.getSha1Hash(byteArrayOutputStream.toString());
System.out.println(digest);
DirectDocument2 document = documents.getDocumentByHash(digest);
if (document == null) {
log.warn("Unable to find metadata for document by hash. Creating document with no supporting metadata.");
document = new DirectDocument2();
documents.getDocuments().add(document);
}
document.setData(byteArrayOutputStream.toByteArray());
}
}
}
zipFile.close();
XdmPackage xdmPackage = new XdmPackage();
xdmPackage.setDocuments(documents);
return xdmPackage;
}
protected static String getSubmissionSetDirspec(String zipEntryName) {
if (zipEntryName == null) {
return null;
}
String ret = "";
zipEntryName = zipEntryName.replaceAll("\\\\", "/");
String[] components = zipEntryName.split("/");
for (int i = 0; i < components.length - 1; i++) {
ret += (components[i] + "/");
}
if (ret.length() == 0) {
return "";
}
ret = ret.substring(0, ret.length() - 1);
return ret;
}
/**
* Given a full ZipEntry filespec, extracts the name of the folder (if
* present) under the IHE_XDM root specified by IHE XDM.
*
* @param zipEntryName
* The ZIP entry name.
* @return the name of the folder.
*/
/* private static String getSubmissionSetDirspec(String zipEntryName) {
if (zipEntryName == null) {
return null;
}
String[] components = StringUtils.split(zipEntryName, "\\/");
return components[0];
}
*/
protected static boolean matchName(String zname, String subsetDirspec, String subsetFilespec) {
zname = zname.replaceAll("\\\\", "/");
String zipFilespec = subsetDirspec + "/" + subsetFilespec;
boolean ret = StringUtils.equals(zname, zipFilespec);
return ret;
}
/**
* Determine whether a filename matches the subset directory and file name.
*
* @param zname
* The name to compare.
* @param subsetDirspec
* The subset directory name.
* @param subsetFilespec
* The subset file name.
* @return true if the names match, false otherwise.
*/
/* private static boolean matchName(String zname, String subsetDirspec, String subsetFilespec) {
String zipFilespec = subsetDirspec + "\\" + subsetFilespec.replace('/', '\\');
boolean ret = StringUtils.equalsIgnoreCase(zname, zipFilespec);
if (!ret) {
zipFilespec = zipFilespec.replace('\\', '/');
ret = StringUtils.equalsIgnoreCase(zname, zipFilespec);
}
return ret;
}*/
/**
* Read data the data of a zipEntry within a zipFile.
*
* @param zipFile
* The ZipFile object.
* @param zipEntry
* The ZipEntry object.
* @return a ByteArrayOutputStream representing the data.
* @throws IOException
*/
private static ByteArrayOutputStream readData(ZipFile zipFile, ZipEntry zipEntry) throws IOException {
InputStream in = zipFile.getInputStream(zipEntry);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead = 0;
byte[] buffer = new byte[2048];
while ((bytesRead = in.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
in.close();
return baos;
}
/*
* Read a file and return the bytes.
*/
private byte[] readFile(String filename) throws IOException {
byte[] bytes;
InputStream is = this.getClass().getClassLoader().getResourceAsStream(filename);
bytes = new byte[is.available()];
is.read(bytes);
is.close();
return bytes;
}
private static File fileFromDataHandler(DataHandler dh) throws Exception {
File f = null;
OutputStream out = null;
InputStream inputStream = null;
final String fileName = UUID.randomUUID().toString() + ".zip";
try {
f = new File(fileName);
inputStream = dh.getInputStream();
out = new FileOutputStream(f);
byte buf[] = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
if (log.isErrorEnabled()) {
log.error("File not found - " + fileName, e);
}
throw e;
} catch (IOException e) {
if (log.isErrorEnabled()) {
log.error("Exception thrown while trying to read file from DataHandler object", e);
}
throw e;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (out != null) {
out.close();
}
}
if (log.isTraceEnabled()) {
log.trace("Created temporary work file " + f.getAbsolutePath());
}
return f;
}
private String getSuffix(String mimeType) {
return "." + MimeType.lookup(mimeType).getSuffix();
}
}