Search in sources :

Example 51 with ServiceException

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;
}
Also used : ServiceException(gov.ca.cwds.rest.services.ServiceException) FileOutputStream(java.io.FileOutputStream) CmsDocumentBlobSegment(gov.ca.cwds.data.persistence.cms.CmsDocumentBlobSegment) File(java.io.File) ServiceException(gov.ca.cwds.rest.services.ServiceException) LZWEncoder(gov.ca.cwds.rest.util.jni.LZWEncoder)

Aggregations

ServiceException (gov.ca.cwds.rest.services.ServiceException)51 Test (org.junit.Test)20 EntityNotFoundException (javax.persistence.EntityNotFoundException)14 EntityExistsException (javax.persistence.EntityExistsException)13 StaffPerson (gov.ca.cwds.data.persistence.cms.StaffPerson)7 PostedReferral (gov.ca.cwds.rest.api.domain.cms.PostedReferral)5 PostedReporter (gov.ca.cwds.rest.api.domain.cms.PostedReporter)5 PostedStaffPerson (gov.ca.cwds.rest.api.domain.cms.PostedStaffPerson)5 StaffPerson (gov.ca.cwds.rest.api.domain.cms.StaffPerson)5 PostedAllegation (gov.ca.cwds.rest.api.domain.cms.PostedAllegation)4 PostedAllegationPerpetratorHistory (gov.ca.cwds.rest.api.domain.cms.PostedAllegationPerpetratorHistory)4 PostedClient (gov.ca.cwds.rest.api.domain.cms.PostedClient)4 PostedLongText (gov.ca.cwds.rest.api.domain.cms.PostedLongText)4 Referral (gov.ca.cwds.rest.api.domain.cms.Referral)3 Reporter (gov.ca.cwds.rest.api.domain.cms.Reporter)3 Address (gov.ca.cwds.data.persistence.cms.Address)2 Allegation (gov.ca.cwds.data.persistence.cms.Allegation)2 AllegationPerpetratorHistory (gov.ca.cwds.data.persistence.cms.AllegationPerpetratorHistory)2 ChildClient (gov.ca.cwds.data.persistence.cms.ChildClient)2 Client (gov.ca.cwds.data.persistence.cms.Client)2