Skip to content

Commit

Permalink
add support to link a tld to a child IBDO for inheriting parameters (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rpg36 authored Jan 8, 2025
1 parent 1e9b231 commit 3b8a98b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/emissary/core/BaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -238,6 +240,7 @@ protected DataState getDataState() {
public BaseDataObject() {
this.theData = null;
setCreationTimestamp(Instant.now());
tld = null;
}

/**
Expand All @@ -251,6 +254,7 @@ public BaseDataObject(final byte[] newData, final String name) {
setData(newData);
setFilename(name);
setCreationTimestamp(Instant.now());
tld = null;
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -1462,4 +1484,10 @@ public String getTransactionId() {
public void setTransactionId(String transactionId) {
this.transactionId = transactionId;
}

@Override
public IBaseDataObject getTld() {
return tld;
}

}
30 changes: 30 additions & 0 deletions src/main/java/emissary/core/DataObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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 */

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/emissary/core/IBaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
20 changes: 20 additions & 0 deletions src/test/java/emissary/core/BaseDataObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/emissary/core/DataObjectFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3b8a98b

Please sign in to comment.