Search in sources :

Example 1 with XmlPdiAssembler

use of com.opentext.ia.sdk.sip.XmlPdiAssembler 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)

Example 2 with XmlPdiAssembler

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

the class FlexbileDomainObject method run.

private void run() throws IOException {
    System.out.println("\nSample 6: Assembling multiple domain object types into a single PDI");
    // Assembler for PDI, where the domain object is flexible
    PdiAssembler<Map<String, String>> pdiAssembler = new XmlPdiAssembler<Map<String, String>>(NAMESPACE, "aiu") {

        @Override
        protected void doAdd(Map<String, String> aiu, Map<String, ContentInfo> ignored) {
            aiu.forEach((key, value) -> getBuilder().element(key, value));
        }
    };
    // Collect the PDI XML in an in-memory buffer. For large PDIs, use a FileBuffer instead.
    DataBuffer dataBuffer = new MemoryBuffer();
    pdiAssembler.start(dataBuffer);
    try {
        // Assemble the PDI by adding domain objects to it.
        // In this sample, the domain objects are flexible objects
        pdiAssembler.add(wrapDomainObject("foo", "bar"));
        pdiAssembler.add(wrapDomainObject("gnu", "gnat"));
    } finally {
        pdiAssembler.end();
    }
    try (InputStream pdi = dataBuffer.openForReading()) {
        System.out.println(IOUtils.toString(pdi, StandardCharsets.UTF_8));
    }
}
Also used : MemoryBuffer(com.opentext.ia.sdk.support.io.MemoryBuffer) XmlPdiAssembler(com.opentext.ia.sdk.sip.XmlPdiAssembler) InputStream(java.io.InputStream) Map(java.util.Map) DataBuffer(com.opentext.ia.sdk.support.io.DataBuffer)

Example 3 with XmlPdiAssembler

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

the class MultipleDomainObjectsSingleSerialization method run.

private void run() throws IOException {
    System.out.println("\nSample 6: Assembling multiple domain object types into a single PDI");
    // Assembler for PDI, where the domain object is flexible
    PdiAssembler<Object> pdiAssembler = new XmlPdiAssembler<Object>(NAMESPACE, "aiu") {

        private final ObjectMapper mapper = new XmlMapper();

        @Override
        protected void doAdd(Object domainObject, Map<String, ContentInfo> ignored) {
            try {
                getBuilder().xml(mapper.writeValueAsString(domainObject));
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
    };
    // Collect the PDI XML in an in-memory buffer. For large PDIs, use a FileBuffer instead.
    DataBuffer dataBuffer = new MemoryBuffer();
    pdiAssembler.start(dataBuffer);
    try {
        // Assemble the PDI by adding domain objects to it.
        // In this sample, the domain objects are flexible objects
        pdiAssembler.add(wrapDomainObject(new Animal("Gnu")));
        pdiAssembler.add(wrapDomainObject(new Person("Remon", "Sinnema")));
    } finally {
        pdiAssembler.end();
    }
    try (InputStream pdi = dataBuffer.openForReading()) {
        System.out.println(IOUtils.toString(pdi, StandardCharsets.UTF_8));
    }
}
Also used : InputStream(java.io.InputStream) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) MemoryBuffer(com.opentext.ia.sdk.support.io.MemoryBuffer) XmlPdiAssembler(com.opentext.ia.sdk.sip.XmlPdiAssembler) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataBuffer(com.opentext.ia.sdk.support.io.DataBuffer)

Example 4 with XmlPdiAssembler

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

the class YamlSipIngester method run.

private void run(String rootPath) throws IOException {
    System.out.printf("%nSample 3: Assemble SIP from %s and ingest into InfoArchive%n", rootPath);
    // Load the configuration
    YamlConfiguration configuration = new YamlConfiguration(new File(rootPath, "configuration.yml"));
    // Tell InfoArchive where and how to archive the data
    URI entityUri = URI.create(configuration.getPdiSchemaName());
    String entityName = "animal";
    PackagingInformation prototype = PackagingInformation.builder().dss().application(configuration.getApplicationName()).holding(configuration.getHoldingName()).producer("SIP-SDK").entity(entityName).schema(entityUri.toString()).end().build();
    // Define a mapping from our domain object to the PDI XML
    XmlPdiAssembler<File> pdiAssembler;
    try (InputStream schema = new StringStream(configuration.getPdiSchema())) {
        pdiAssembler = new XmlPdiAssembler<File>(entityUri, entityName, schema) {

            @Override
            protected void doAdd(File value, Map<String, ContentInfo> ignored) {
                String name = value.getName();
                getBuilder().element("animal_name", name.substring(0, name.lastIndexOf('.'))).element("file_path", relativePath(value, rootPath));
            }
        };
    }
    DigitalObjectsExtraction<File> contentAssembler = file -> Collections.singleton(DigitalObject.fromFile(relativePath(file, rootPath), file)).iterator();
    // Assemble the SIP
    SipAssembler<File> sipAssembler = SipAssembler.forPdiAndContent(prototype, pdiAssembler, contentAssembler);
    FileGenerator<File> generator = new FileGenerator<>(sipAssembler, FileSupplier.fromTemporaryDirectory());
    File f1 = new File(rootPath, "ape.dat");
    File f2 = new File(rootPath, "bear.dat");
    File f3 = new File(rootPath, "cobra.dat");
    FileGenerationMetrics metrics = generator.generate(Arrays.asList(f1, f2, f3));
    File assembledSip = metrics.getFile();
    // Get an ArchiveClient instance to interact with InfoArchive.
    // In this case, we start with a blank installation, and create a sample holding from scratch that will contain
    // the SIP we've just assembled.
    // Use ArchiveClients.usingAlreadyConfiguredServer() instead if you already configured the server with application,
    // holding, etc.
    ArchiveClient archiveClient = ArchiveClients.configuringApplicationUsing(new YamlBasedApplicationConfigurer(configuration), newArchiveConnection(rootPath));
    // Ingest the SIP into InfoArchive
    try (InputStream sip = Files.newInputStream(assembledSip.toPath(), StandardOpenOption.READ)) {
        String aipId = archiveClient.ingestDirect(sip);
        System.out.println("  SIP ingested as AIP " + aipId);
    }
}
Also used : YamlBasedApplicationConfigurer(com.opentext.ia.sdk.server.configuration.yaml.YamlBasedApplicationConfigurer) XmlPdiAssembler(com.opentext.ia.sdk.sip.XmlPdiAssembler) Arrays(java.util.Arrays) ArchiveClients(com.opentext.ia.sdk.client.factory.ArchiveClients) PackagingInformation(com.opentext.ia.sdk.sip.PackagingInformation) FileGenerationMetrics(com.opentext.ia.sdk.sip.FileGenerationMetrics) FileSupplier(com.opentext.ia.sdk.support.io.FileSupplier) StringUtils(org.apache.commons.lang3.StringUtils) YamlConfiguration(com.opentext.ia.yaml.configuration.YamlConfiguration) InfoArchiveConnectionProperties(com.opentext.ia.sdk.server.configuration.InfoArchiveConnectionProperties) Map(java.util.Map) DigitalObjectsExtraction(com.opentext.ia.sdk.sip.DigitalObjectsExtraction) URI(java.net.URI) PropertiesBasedArchiveConnection(com.opentext.ia.sdk.server.configuration.PropertiesBasedArchiveConnection) Properties(java.util.Properties) DigitalObject(com.opentext.ia.sdk.sip.DigitalObject) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) ArchiveClient(com.opentext.ia.sdk.client.api.ArchiveClient) IOException(java.io.IOException) File(java.io.File) ArchiveConnection(com.opentext.ia.sdk.client.api.ArchiveConnection) ContentInfo(com.opentext.ia.sdk.sip.ContentInfo) SipAssembler(com.opentext.ia.sdk.sip.SipAssembler) StringStream(com.opentext.ia.sdk.support.io.StringStream) FileGenerator(com.opentext.ia.sdk.sip.FileGenerator) Paths(java.nio.file.Paths) Collections(java.util.Collections) InputStream(java.io.InputStream) FileGenerator(com.opentext.ia.sdk.sip.FileGenerator) InputStream(java.io.InputStream) YamlConfiguration(com.opentext.ia.yaml.configuration.YamlConfiguration) URI(java.net.URI) StringStream(com.opentext.ia.sdk.support.io.StringStream) YamlBasedApplicationConfigurer(com.opentext.ia.sdk.server.configuration.yaml.YamlBasedApplicationConfigurer) FileGenerationMetrics(com.opentext.ia.sdk.sip.FileGenerationMetrics) ContentInfo(com.opentext.ia.sdk.sip.ContentInfo) File(java.io.File) ArchiveClient(com.opentext.ia.sdk.client.api.ArchiveClient) PackagingInformation(com.opentext.ia.sdk.sip.PackagingInformation)

Example 5 with XmlPdiAssembler

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

the class AssemblePdi method run.

private void run() throws IOException {
    System.out.println("\nSample 5: Assembling just PDI, not the whole SIP");
    // Assembler for PDI, where the domain object is of type String
    PdiAssembler<String> pdiAssembler = new XmlPdiAssembler<String>(NAMESPACE, "message") {

        @Override
        protected void doAdd(String domainObject, Map<String, ContentInfo> ignored) {
            getBuilder().element("text", domainObject);
        }
    };
    // Collect the PDI XML in an in-memory buffer. For large PDIs, use a FileBuffer instead.
    DataBuffer dataBuffer = new MemoryBuffer();
    pdiAssembler.start(dataBuffer);
    try {
        // Assemble the PDI by adding domain objects to it.
        // In this sample, the domain objects are the strings "foo" and "bar".
        pdiAssembler.add(wrapDomainObject("foo"));
        pdiAssembler.add(wrapDomainObject("bar"));
    } finally {
        pdiAssembler.end();
    }
    try (InputStream pdi = dataBuffer.openForReading()) {
        System.out.println(IOUtils.toString(pdi, StandardCharsets.UTF_8));
    }
}
Also used : MemoryBuffer(com.opentext.ia.sdk.support.io.MemoryBuffer) XmlPdiAssembler(com.opentext.ia.sdk.sip.XmlPdiAssembler) InputStream(java.io.InputStream) Map(java.util.Map) DataBuffer(com.opentext.ia.sdk.support.io.DataBuffer)

Aggregations

XmlPdiAssembler (com.opentext.ia.sdk.sip.XmlPdiAssembler)7 Map (java.util.Map)7 InputStream (java.io.InputStream)5 DataBuffer (com.opentext.ia.sdk.support.io.DataBuffer)4 MemoryBuffer (com.opentext.ia.sdk.support.io.MemoryBuffer)4 PackagingInformation (com.opentext.ia.sdk.sip.PackagingInformation)3 File (java.io.File)3 URI (java.net.URI)3 ContentInfo (com.opentext.ia.sdk.sip.ContentInfo)2 DigitalObject (com.opentext.ia.sdk.sip.DigitalObject)2 DigitalObjectsExtraction (com.opentext.ia.sdk.sip.DigitalObjectsExtraction)2 FileGenerator (com.opentext.ia.sdk.sip.FileGenerator)2 SipAssembler (com.opentext.ia.sdk.sip.SipAssembler)2 SipMetrics (com.opentext.ia.sdk.sip.SipMetrics)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Collections (java.util.Collections)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1