diff --git a/src/main/java/emissary/core/BaseDataObject.java b/src/main/java/emissary/core/BaseDataObject.java index 69d752f613..0b8c2c79b3 100755 --- a/src/main/java/emissary/core/BaseDataObject.java +++ b/src/main/java/emissary/core/BaseDataObject.java @@ -193,6 +193,8 @@ public class BaseDataObject implements Serializable, Cloneable, Remote, IBaseDat protected SeekableByteChannelFactory seekableByteChannelFactory; + protected final IBaseDataObject tld; + protected enum DataState { NO_DATA, CHANNEL_ONLY, BYTE_ARRAY_ONLY, BYTE_ARRAY_AND_CHANNEL } @@ -238,6 +240,7 @@ protected DataState getDataState() { public BaseDataObject() { this.theData = null; setCreationTimestamp(Instant.now()); + tld = null; } /** @@ -251,6 +254,7 @@ public BaseDataObject(final byte[] newData, final String name) { setData(newData); setFilename(name); setCreationTimestamp(Instant.now()); + tld = null; } /** @@ -275,6 +279,24 @@ public BaseDataObject(final byte[] newData, final String name, final String form } } + public BaseDataObject(final byte[] newData, final String name, @Nullable final String form, IBaseDataObject tld) { + setData(newData); + setFilename(name); + setCreationTimestamp(Instant.now()); + if (form != null) { + pushCurrentForm(form); + } + this.tld = tld; + } + + public BaseDataObject(final byte[] newData, final String name, @Nullable final String form, @Nullable final String fileType, + IBaseDataObject tld) { + this(newData, name, form, tld); + if (fileType != null) { + this.setFileType(fileType); + } + } + /** * Set the header byte array WARNING: this implementation uses the passed in array directly, no copy is made so the * caller should not reuse the array. @@ -1462,4 +1484,10 @@ public String getTransactionId() { public void setTransactionId(String transactionId) { this.transactionId = transactionId; } + + @Override + public IBaseDataObject getTld() { + return tld; + } + } diff --git a/src/main/java/emissary/core/DataObjectFactory.java b/src/main/java/emissary/core/DataObjectFactory.java index 01a511e669..4845ff6c20 100755 --- a/src/main/java/emissary/core/DataObjectFactory.java +++ b/src/main/java/emissary/core/DataObjectFactory.java @@ -114,6 +114,20 @@ public static IBaseDataObject getInstance(final byte[] payload, final String fil return getInstance(payload, filename, fileTypeAndForm, fileTypeAndForm); } + /** + * Get an instance of the configured DataObject impl with filename, form, and file type set, and top level document + * + * @param payload the payload data + * @param filename the filename + * @param fileTypeAndForm the form and filetype to set on the IBDO + * @param tld The top level document + * @return an IBDO with the payload, filename, top level document set with the file type and form set to the same value + */ + public static IBaseDataObject getInstance(final byte[] payload, final String filename, final String fileTypeAndForm, IBaseDataObject tld) { + final Object o = Factory.create(clazz, payload, filename, fileTypeAndForm, tld); + return (IBaseDataObject) o; + } + /** * Get an instance of the configured DataObject impl with filename, form, and file type set * @@ -128,6 +142,22 @@ public static IBaseDataObject getInstance(final byte[] payload, final String fil return (IBaseDataObject) o; } + /** + * Get an instance of the configured DataObject impl with filename, form, file type, and top level document set + * + * @param payload the payload data + * @param filename the filename + * @param form the form to set on the IBDO + * @param fileType the file type to set on the IBDO + * @param tld The top level document + * @return an IBDO with the payload, filename, file type, form, and top level document set + */ + public static IBaseDataObject getInstance(final byte[] payload, final String filename, final String form, final String fileType, + IBaseDataObject tld) { + final Object o = Factory.create(clazz, payload, filename, form, fileType, tld); + return (IBaseDataObject) o; + } + /* IExtractedRecord */ /** diff --git a/src/main/java/emissary/core/IBaseDataObject.java b/src/main/java/emissary/core/IBaseDataObject.java index c973a495f3..5e07767310 100755 --- a/src/main/java/emissary/core/IBaseDataObject.java +++ b/src/main/java/emissary/core/IBaseDataObject.java @@ -1011,4 +1011,12 @@ default String getParameterAsConcatString(final String key, final String sep) { * @param transactionId the unique identifier of the transaction */ void setTransactionId(String transactionId); + + /** + * Return the top level document or null if there is none for this IBaseDataObject + * + * @return The TLD IBaseDataObject + */ + IBaseDataObject getTld(); + } diff --git a/src/test/java/emissary/core/BaseDataObjectTest.java b/src/test/java/emissary/core/BaseDataObjectTest.java index cd695b6615..5efdfc5435 100755 --- a/src/test/java/emissary/core/BaseDataObjectTest.java +++ b/src/test/java/emissary/core/BaseDataObjectTest.java @@ -27,6 +27,7 @@ import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; +import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; @@ -89,6 +90,25 @@ void testConstructors() { final BaseDataObject b3 = new BaseDataObject("test".getBytes(), "filename.txt", null); assertEquals("", b3.currentForm(), "Current form with null in ctor"); assertNotNull(b3.getCreationTimestamp()); + + final BaseDataObject tld = new BaseDataObject(); + final byte[] data = "content".getBytes(StandardCharsets.UTF_8); + final String fileName = "aChild"; + final String form = "UNKNOWN"; + final BaseDataObject b4 = new BaseDataObject(data, fileName, form, tld); + assertEquals(fileName, b4.getFilename()); + assertEquals(form, b4.currentForm()); + assertNotNull(b4.getCreationTimestamp()); + assertEquals(tld, b4.getTld()); + + final String fileType = "TEXT"; + BaseDataObject b5 = new BaseDataObject(data, fileName, form, fileType, tld); + assertEquals(fileName, b5.getFilename()); + assertEquals(form, b5.currentForm()); + assertEquals(fileType, b5.getFileType()); + assertNotNull(b5.getCreationTimestamp()); + assertEquals(tld, b5.getTld()); + } @Test diff --git a/src/test/java/emissary/core/DataObjectFactoryTest.java b/src/test/java/emissary/core/DataObjectFactoryTest.java index 3cce9775d4..f06f130aa4 100755 --- a/src/test/java/emissary/core/DataObjectFactoryTest.java +++ b/src/test/java/emissary/core/DataObjectFactoryTest.java @@ -6,6 +6,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.nio.charset.StandardCharsets; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -106,6 +108,27 @@ void testFormAndFileType() { assertSame(testPayload, extract.data()); } + @Test + void testTLD() { + BaseDataObject tld = new BaseDataObject(); + final byte[] data = "content".getBytes(StandardCharsets.UTF_8); + final String fileName = "aChild"; + final String form = "UNKNOWN"; + IBaseDataObject ibdo = DataObjectFactory.getInstance(data, fileName, form, tld); + assertEquals(fileName, ibdo.getFilename()); + assertEquals(form, ibdo.currentForm()); + assertNotNull(ibdo.getCreationTimestamp()); + assertEquals(tld, ibdo.getTld()); + + final String fileType = "TEXT"; + ibdo = DataObjectFactory.getInstance(data, fileName, form, fileType, tld); + assertEquals(fileName, ibdo.getFilename()); + assertEquals(form, ibdo.currentForm()); + assertEquals(fileType, ibdo.getFileType()); + assertNotNull(ibdo.getCreationTimestamp()); + assertEquals(tld, ibdo.getTld()); + } + @SuppressWarnings("unused") public static class MyDataObject extends BaseDataObject { private static final long serialVersionUID = -2254597461746556210L;