use of com.opentext.ia.sdk.sip.DigitalObjectsExtraction 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());
}
use of com.opentext.ia.sdk.sip.DigitalObjectsExtraction 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);
}
}
Aggregations