Search in sources :

Example 1 with PdiAssembler

use of com.opentext.ia.sdk.sip.PdiAssembler in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class FileArchiver method run.

private void run(String rootPath, String sip) throws IOException {
    File sipFile = new File(sip).getCanonicalFile();
    if (!sipFile.getParentFile().mkdirs() && !sipFile.getParentFile().isDirectory()) {
        throw new IllegalStateException("Could not create all required directories in path " + sipFile.getParentFile().getAbsolutePath());
    }
    System.out.printf("%nSample 2: Archiving files from %s into %s%n", rootPath, sipFile.getPath());
    // Tell InfoArchive where and how to archive the data
    URI entityUri = URI.create("urn:com.opentext.ia.sdk.sample.file:1.0");
    String entityName = "file";
    PackagingInformation prototype = PackagingInformation.builder().dss().application("fileApplication").holding("fileHolding").producer("SIP SDK").entity(entityName).schema(entityUri.toString()).end().build();
    // Define a mapping from our domain object to the PDI XML
    PdiAssembler<File> pdiAssembler = new XmlPdiAssembler<File>(entityUri, entityName) {

        @Override
        protected void doAdd(File file, Map<String, ContentInfo> contentInfo) {
            try {
                String path = relativePath(file, rootPath);
                getBuilder().element("path", path).element("size", Long.toString(file.length())).element("permissions", permissionsOf(file)).element("contentType", Files.probeContentType(file.toPath())).elements("hashes", "hash", contentInfo.get(path).getContentHashes(), (hash, builder) -> {
                    builder.attribute("algorithm", hash.getHashFunction()).attribute("encoding", hash.getEncoding()).attribute("value", hash.getValue());
                });
            } catch (IOException e) {
                throw new RuntimeIoException(e);
            }
        }
    };
    DigitalObjectsExtraction<File> contentAssembler = file -> Collections.singleton(DigitalObject.fromFile(relativePath(file, rootPath), file)).iterator();
    HashAssembler contentHashAssembler = new SingleHashAssembler(HashFunction.SHA256, Encoding.BASE64);
    // Assemble the SIP
    SipAssembler<File> assembler = SipAssembler.forPdiAndContentWithContentHashing(prototype, pdiAssembler, contentAssembler, contentHashAssembler);
    assembler.start(new FileBuffer(sipFile));
    try {
        addFilesIn(new File(rootPath), rootPath, relativePath(sipFile, rootPath), assembler);
    } finally {
        assembler.end();
    }
    // Show metrics about the assembly process
    SipMetrics metrics = assembler.getMetrics();
    System.out.printf("  Added %d files to SIP of %d bytes in %d ms%n", metrics.numDigitalObjects(), metrics.sipFileSize(), metrics.assemblyTime());
}
Also used : XmlPdiAssembler(com.opentext.ia.sdk.sip.XmlPdiAssembler) DigitalObject(com.opentext.ia.sdk.sip.DigitalObject) Files(java.nio.file.Files) PdiAssembler(com.opentext.ia.sdk.sip.PdiAssembler) IOException(java.io.IOException) PackagingInformation(com.opentext.ia.sdk.sip.PackagingInformation) SipMetrics(com.opentext.ia.sdk.sip.SipMetrics) File(java.io.File) ContentInfo(com.opentext.ia.sdk.sip.ContentInfo) HashFunction(com.opentext.ia.sdk.support.io.HashFunction) SipAssembler(com.opentext.ia.sdk.sip.SipAssembler) RuntimeIoException(com.opentext.ia.sdk.support.io.RuntimeIoException) Map(java.util.Map) SingleHashAssembler(com.opentext.ia.sdk.support.io.SingleHashAssembler) DigitalObjectsExtraction(com.opentext.ia.sdk.sip.DigitalObjectsExtraction) FileBuffer(com.opentext.ia.sdk.support.io.FileBuffer) Encoding(com.opentext.ia.sdk.support.io.Encoding) URI(java.net.URI) Collections(java.util.Collections) HashAssembler(com.opentext.ia.sdk.support.io.HashAssembler) SingleHashAssembler(com.opentext.ia.sdk.support.io.SingleHashAssembler) SingleHashAssembler(com.opentext.ia.sdk.support.io.SingleHashAssembler) HashAssembler(com.opentext.ia.sdk.support.io.HashAssembler) FileBuffer(com.opentext.ia.sdk.support.io.FileBuffer) IOException(java.io.IOException) URI(java.net.URI) RuntimeIoException(com.opentext.ia.sdk.support.io.RuntimeIoException) XmlPdiAssembler(com.opentext.ia.sdk.sip.XmlPdiAssembler) File(java.io.File) Map(java.util.Map) PackagingInformation(com.opentext.ia.sdk.sip.PackagingInformation) SipMetrics(com.opentext.ia.sdk.sip.SipMetrics)

Aggregations

ContentInfo (com.opentext.ia.sdk.sip.ContentInfo)1 DigitalObject (com.opentext.ia.sdk.sip.DigitalObject)1 DigitalObjectsExtraction (com.opentext.ia.sdk.sip.DigitalObjectsExtraction)1 PackagingInformation (com.opentext.ia.sdk.sip.PackagingInformation)1 PdiAssembler (com.opentext.ia.sdk.sip.PdiAssembler)1 SipAssembler (com.opentext.ia.sdk.sip.SipAssembler)1 SipMetrics (com.opentext.ia.sdk.sip.SipMetrics)1 XmlPdiAssembler (com.opentext.ia.sdk.sip.XmlPdiAssembler)1 Encoding (com.opentext.ia.sdk.support.io.Encoding)1 FileBuffer (com.opentext.ia.sdk.support.io.FileBuffer)1 HashAssembler (com.opentext.ia.sdk.support.io.HashAssembler)1 HashFunction (com.opentext.ia.sdk.support.io.HashFunction)1 RuntimeIoException (com.opentext.ia.sdk.support.io.RuntimeIoException)1 SingleHashAssembler (com.opentext.ia.sdk.support.io.SingleHashAssembler)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Files (java.nio.file.Files)1 Collections (java.util.Collections)1 Map (java.util.Map)1