use of gov.ca.cwds.rest.services.ServiceException in project API by ca-cwds.
the class CmsDocumentDao method decompressLZW.
/**
* Decompress (inflate) an LZW-compressed document by assembling blob segments and calling native
* library.
*
* @param doc LZW archive to decompress
* @return base64-encoded String of decompressed document
*/
protected String decompressLZW(gov.ca.cwds.data.persistence.cms.CmsDocument doc) {
String retval = "";
try {
File src = File.createTempFile("src", ".lzw");
src.deleteOnExit();
File tgt = File.createTempFile("tgt", ".doc");
tgt.deleteOnExit();
FileOutputStream fos = new FileOutputStream(src);
for (CmsDocumentBlobSegment seg : doc.getBlobSegments()) {
final byte[] bytes = DatatypeConverter.parseHexBinary(seg.getDocBlob().trim());
fos.write(bytes, 0, bytes.length);
}
fos.flush();
fos.close();
// DECOMPRESS!
// TODO: Trap std::exception in shared library and return error code.
// The LZW library currently returns a blank when decompression fails, for safety, since
// unhandled C++ exceptions kill the JVM.
LZWEncoder lzw = new LZWEncoder();
lzw.fileCopyUncompress(src.getAbsolutePath(), tgt.getAbsolutePath());
retval = DatatypeConverter.printBase64Binary(Files.readAllBytes(Paths.get(tgt.getAbsolutePath())));
// For security reasons, remove temporary documents immediately.
// TODO: pass bytes to C++ library instead of file names.
src.delete();
tgt.delete();
} catch (Exception e) {
LOGGER.error("ERROR DECOMPRESSING LZW! " + e.getMessage(), e);
throw new ServiceException("ERROR DECOMPRESSING LZW! " + e.getMessage(), e);
}
return retval;
}
Aggregations