Skip to content

Commit 3b8a98b

Browse files
authored
add support to link a tld to a child IBDO for inheriting parameters (#1016)
1 parent 1e9b231 commit 3b8a98b

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

src/main/java/emissary/core/BaseDataObject.java

+28
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ public class BaseDataObject implements Serializable, Cloneable, Remote, IBaseDat
193193
protected SeekableByteChannelFactory seekableByteChannelFactory;
194194

195195

196+
protected final IBaseDataObject tld;
197+
196198
protected enum DataState {
197199
NO_DATA, CHANNEL_ONLY, BYTE_ARRAY_ONLY, BYTE_ARRAY_AND_CHANNEL
198200
}
@@ -238,6 +240,7 @@ protected DataState getDataState() {
238240
public BaseDataObject() {
239241
this.theData = null;
240242
setCreationTimestamp(Instant.now());
243+
tld = null;
241244
}
242245

243246
/**
@@ -251,6 +254,7 @@ public BaseDataObject(final byte[] newData, final String name) {
251254
setData(newData);
252255
setFilename(name);
253256
setCreationTimestamp(Instant.now());
257+
tld = null;
254258
}
255259

256260
/**
@@ -275,6 +279,24 @@ public BaseDataObject(final byte[] newData, final String name, final String form
275279
}
276280
}
277281

282+
public BaseDataObject(final byte[] newData, final String name, @Nullable final String form, IBaseDataObject tld) {
283+
setData(newData);
284+
setFilename(name);
285+
setCreationTimestamp(Instant.now());
286+
if (form != null) {
287+
pushCurrentForm(form);
288+
}
289+
this.tld = tld;
290+
}
291+
292+
public BaseDataObject(final byte[] newData, final String name, @Nullable final String form, @Nullable final String fileType,
293+
IBaseDataObject tld) {
294+
this(newData, name, form, tld);
295+
if (fileType != null) {
296+
this.setFileType(fileType);
297+
}
298+
}
299+
278300
/**
279301
* Set the header byte array WARNING: this implementation uses the passed in array directly, no copy is made so the
280302
* caller should not reuse the array.
@@ -1462,4 +1484,10 @@ public String getTransactionId() {
14621484
public void setTransactionId(String transactionId) {
14631485
this.transactionId = transactionId;
14641486
}
1487+
1488+
@Override
1489+
public IBaseDataObject getTld() {
1490+
return tld;
1491+
}
1492+
14651493
}

src/main/java/emissary/core/DataObjectFactory.java

+30
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ public static IBaseDataObject getInstance(final byte[] payload, final String fil
114114
return getInstance(payload, filename, fileTypeAndForm, fileTypeAndForm);
115115
}
116116

117+
/**
118+
* Get an instance of the configured DataObject impl with filename, form, and file type set, and top level document
119+
*
120+
* @param payload the payload data
121+
* @param filename the filename
122+
* @param fileTypeAndForm the form and filetype to set on the IBDO
123+
* @param tld The top level document
124+
* @return an IBDO with the payload, filename, top level document set with the file type and form set to the same value
125+
*/
126+
public static IBaseDataObject getInstance(final byte[] payload, final String filename, final String fileTypeAndForm, IBaseDataObject tld) {
127+
final Object o = Factory.create(clazz, payload, filename, fileTypeAndForm, tld);
128+
return (IBaseDataObject) o;
129+
}
130+
117131
/**
118132
* Get an instance of the configured DataObject impl with filename, form, and file type set
119133
*
@@ -128,6 +142,22 @@ public static IBaseDataObject getInstance(final byte[] payload, final String fil
128142
return (IBaseDataObject) o;
129143
}
130144

145+
/**
146+
* Get an instance of the configured DataObject impl with filename, form, file type, and top level document set
147+
*
148+
* @param payload the payload data
149+
* @param filename the filename
150+
* @param form the form to set on the IBDO
151+
* @param fileType the file type to set on the IBDO
152+
* @param tld The top level document
153+
* @return an IBDO with the payload, filename, file type, form, and top level document set
154+
*/
155+
public static IBaseDataObject getInstance(final byte[] payload, final String filename, final String form, final String fileType,
156+
IBaseDataObject tld) {
157+
final Object o = Factory.create(clazz, payload, filename, form, fileType, tld);
158+
return (IBaseDataObject) o;
159+
}
160+
131161
/* IExtractedRecord */
132162

133163
/**

src/main/java/emissary/core/IBaseDataObject.java

+8
Original file line numberDiff line numberDiff line change
@@ -1011,4 +1011,12 @@ default String getParameterAsConcatString(final String key, final String sep) {
10111011
* @param transactionId the unique identifier of the transaction
10121012
*/
10131013
void setTransactionId(String transactionId);
1014+
1015+
/**
1016+
* Return the top level document or null if there is none for this IBaseDataObject
1017+
*
1018+
* @return The TLD IBaseDataObject
1019+
*/
1020+
IBaseDataObject getTld();
1021+
10141022
}

src/test/java/emissary/core/BaseDataObjectTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.lang.reflect.Field;
2828
import java.nio.ByteBuffer;
2929
import java.nio.channels.SeekableByteChannel;
30+
import java.nio.charset.StandardCharsets;
3031
import java.time.Instant;
3132
import java.util.ArrayList;
3233
import java.util.Arrays;
@@ -89,6 +90,25 @@ void testConstructors() {
8990
final BaseDataObject b3 = new BaseDataObject("test".getBytes(), "filename.txt", null);
9091
assertEquals("", b3.currentForm(), "Current form with null in ctor");
9192
assertNotNull(b3.getCreationTimestamp());
93+
94+
final BaseDataObject tld = new BaseDataObject();
95+
final byte[] data = "content".getBytes(StandardCharsets.UTF_8);
96+
final String fileName = "aChild";
97+
final String form = "UNKNOWN";
98+
final BaseDataObject b4 = new BaseDataObject(data, fileName, form, tld);
99+
assertEquals(fileName, b4.getFilename());
100+
assertEquals(form, b4.currentForm());
101+
assertNotNull(b4.getCreationTimestamp());
102+
assertEquals(tld, b4.getTld());
103+
104+
final String fileType = "TEXT";
105+
BaseDataObject b5 = new BaseDataObject(data, fileName, form, fileType, tld);
106+
assertEquals(fileName, b5.getFilename());
107+
assertEquals(form, b5.currentForm());
108+
assertEquals(fileType, b5.getFileType());
109+
assertNotNull(b5.getCreationTimestamp());
110+
assertEquals(tld, b5.getTld());
111+
92112
}
93113

94114
@Test

src/test/java/emissary/core/DataObjectFactoryTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.junit.jupiter.api.BeforeEach;
77
import org.junit.jupiter.api.Test;
88

9+
import java.nio.charset.StandardCharsets;
10+
911
import static org.junit.jupiter.api.Assertions.assertEquals;
1012
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
1113
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -106,6 +108,27 @@ void testFormAndFileType() {
106108
assertSame(testPayload, extract.data());
107109
}
108110

111+
@Test
112+
void testTLD() {
113+
BaseDataObject tld = new BaseDataObject();
114+
final byte[] data = "content".getBytes(StandardCharsets.UTF_8);
115+
final String fileName = "aChild";
116+
final String form = "UNKNOWN";
117+
IBaseDataObject ibdo = DataObjectFactory.getInstance(data, fileName, form, tld);
118+
assertEquals(fileName, ibdo.getFilename());
119+
assertEquals(form, ibdo.currentForm());
120+
assertNotNull(ibdo.getCreationTimestamp());
121+
assertEquals(tld, ibdo.getTld());
122+
123+
final String fileType = "TEXT";
124+
ibdo = DataObjectFactory.getInstance(data, fileName, form, fileType, tld);
125+
assertEquals(fileName, ibdo.getFilename());
126+
assertEquals(form, ibdo.currentForm());
127+
assertEquals(fileType, ibdo.getFileType());
128+
assertNotNull(ibdo.getCreationTimestamp());
129+
assertEquals(tld, ibdo.getTld());
130+
}
131+
109132
@SuppressWarnings("unused")
110133
public static class MyDataObject extends BaseDataObject {
111134
private static final long serialVersionUID = -2254597461746556210L;

0 commit comments

Comments
 (0)