Skip to content

Commit

Permalink
add ExtractedRecord object that cannot have extracted records (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-mlb authored Oct 4, 2024
1 parent 699f623 commit a58a9c3
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 7 deletions.
84 changes: 79 additions & 5 deletions src/main/java/emissary/core/DataObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
*/
public class DataObjectFactory {

private static final Logger logger = LoggerFactory.getLogger(DataObjectFactory.class);

public static final String DEFAULT_CLASS = BaseDataObject.class.getName();
public static final String DEFAULT_EXTRACT_CLASS = ExtractedRecord.class.getName();

private static String clazz;

private static final Logger logger = LoggerFactory.getLogger(DataObjectFactory.class);
private static String extractedClazz;

/*
* Initialize our implementation details
Expand All @@ -27,9 +29,11 @@ public class DataObjectFactory {
try {
final Configurator c = ConfigUtil.getConfigInfo(AgentPool.class);
clazz = c.findStringEntry("payload.class", DEFAULT_CLASS);
extractedClazz = c.findStringEntry("payload.extracted.class", DEFAULT_EXTRACT_CLASS);
} catch (IOException ioe) {
logger.warn("Unable to configure DataObjectFactory", ioe);
clazz = DEFAULT_CLASS;
extractedClazz = DEFAULT_EXTRACT_CLASS;
}
}

Expand All @@ -42,18 +46,42 @@ private DataObjectFactory() {

/**
* Override implementation details
*
* @param clazz {@link IBaseDataObject} implementation
*/
public static void setImplementingClass(final String clazz) {
DataObjectFactory.clazz = clazz;
}

/**
* Get the name of the impl we are using
*
* @return {@link IBaseDataObject} implementation
*/
public static String getImplementingClass() {
return clazz;
}

/**
* Override implementation details
*
* @param clazz {@link IBaseDataObject} implementation
*/
public static void setImplementingExtractClass(final String clazz) {
DataObjectFactory.extractedClazz = clazz;
}

/**
* Get the name of the impl we are using
*
* @return {@link IBaseDataObject} implementation
*/
public static String getImplementingExtractClass() {
return extractedClazz;
}

/* IBaseDataObject */

/**
* Get an instance of the configured DataObject impl
*/
Expand All @@ -64,7 +92,7 @@ public static IBaseDataObject getInstance() {

/**
* Get an instance of the configured DataObject impl with pretty much arbitrary arguments to the constructor
*
*
* @param args the arguments to the BaseDataObject constructor
*/
public static IBaseDataObject getInstance(final Object... args) {
Expand All @@ -74,7 +102,7 @@ public static IBaseDataObject getInstance(final Object... args) {

/**
* Get an instance of the configured DataObject impl with filename, form, and file type set
*
*
* @param payload the payload data
* @param filename the filename
* @param fileTypeAndForm the form and filetype to set on the IBDO
Expand All @@ -86,7 +114,7 @@ public static IBaseDataObject getInstance(final byte[] payload, final String fil

/**
* Get an instance of the configured DataObject impl with filename, form, and file type set
*
*
* @param payload the payload data
* @param filename the filename
* @param form the form to set on the IBDO
Expand All @@ -97,4 +125,50 @@ public static IBaseDataObject getInstance(final byte[] payload, final String fil
final Object o = Factory.create(clazz, payload, filename, form, fileType);
return (IBaseDataObject) o;
}

/* IExtractedRecord */

/**
* Get an instance of the configured ExtractedObject impl
*/
public static IExtractedRecord getExtractInstance() {
final Object o = Factory.create(extractedClazz);
return (IExtractedRecord) o;
}

/**
* Get an instance of the configured ExtractedObject impl with pretty much arbitrary arguments to the constructor
*
* @param args the arguments to the IExtractedRecord constructor
*/
public static IExtractedRecord getExtractInstance(final Object... args) {
final Object o = Factory.create(extractedClazz, args);
return (IExtractedRecord) o;
}

/**
* Get an instance of the configured ExtractedObject impl with filename, form, and file type set
*
* @param payload the payload data
* @param filename the filename
* @param fileTypeAndForm the form and filetype to set on the IBDO
* @return an IBDO with the payload, filename, set with the file type and form set to the same value
*/
public static IExtractedRecord getExtractInstance(final byte[] payload, final String filename, final String fileTypeAndForm) {
return getExtractInstance(payload, filename, fileTypeAndForm, fileTypeAndForm);
}

/**
* Get an instance of the configured ExtractedObject impl with filename, form, and file type 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
* @return an IBDO with the payload, filename, file type, and form set
*/
public static IExtractedRecord getExtractInstance(final byte[] payload, final String filename, final String form, final String fileType) {
final Object o = Factory.create(extractedClazz, payload, filename, form, fileType);
return (IExtractedRecord) o;
}
}
79 changes: 79 additions & 0 deletions src/main/java/emissary/core/ExtractedRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package emissary.core;

import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;

public class ExtractedRecord extends BaseDataObject implements IExtractedRecord {

private static final long serialVersionUID = -6716572014928830135L;

public ExtractedRecord() {
super();
configure();
}

public ExtractedRecord(byte[] newData, String name) {
super(newData, name);
configure();
}

@SuppressWarnings("unused")
public ExtractedRecord(byte[] newData, String name, @Nullable String form) {
super(newData, name, form);
configure();
}

@SuppressWarnings("unused")
public ExtractedRecord(byte[] newData, String name, String form, @Nullable String fileType) {
super(newData, name, form, fileType);
configure();
}

private void configure() {
this.extractedRecords = Collections.emptyList();
}

@Override
@Deprecated(forRemoval = true)
public List<IBaseDataObject> getExtractedRecords() {
return null;
}

@Override
@Deprecated(forRemoval = true)
public void setExtractedRecords(final List<? extends IBaseDataObject> extractedRecords) {
throw new UnsupportedOperationException();
}

@Override
@Deprecated(forRemoval = true)
public void addExtractedRecord(final IBaseDataObject extractedRecord) {
throw new UnsupportedOperationException();
}

@Override
@Deprecated(forRemoval = true)
public void addExtractedRecords(final List<? extends IBaseDataObject> extractedRecords) {
throw new UnsupportedOperationException();
}

@Override
@Deprecated(forRemoval = true)
public boolean hasExtractedRecords() {
return false;
}

@Override
@Deprecated(forRemoval = true)
public void clearExtractedRecords() {
throw new UnsupportedOperationException();
}

@Override
@Deprecated(forRemoval = true)
public int getExtractedRecordCount() {
return 0;
}

}
4 changes: 4 additions & 0 deletions src/main/java/emissary/core/IExtractedRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package emissary.core;

public interface IExtractedRecord extends IBaseDataObject {
}
48 changes: 46 additions & 2 deletions src/test/java/emissary/core/DataObjectFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@

class DataObjectFactoryTest extends UnitTest {
private String defaultPayloadClass;
private String defaultPayloadExtractClass;

@Override
@BeforeEach
public void setUp() throws Exception {
this.defaultPayloadClass = DataObjectFactory.getImplementingClass();
this.defaultPayloadExtractClass = DataObjectFactory.getImplementingExtractClass();
}

@Override
@AfterEach
public void tearDown() throws Exception {
super.tearDown();
DataObjectFactory.setImplementingClass(this.defaultPayloadClass);
DataObjectFactory.setImplementingExtractClass(this.defaultPayloadExtractClass);
}

/**
Expand All @@ -35,6 +38,10 @@ void testFactory() {
final IBaseDataObject d = DataObjectFactory.getInstance();
assertNotNull(d, "DataObject created");
assertInstanceOf(IBaseDataObject.class, d, "Proper class hierarchy");

final IExtractedRecord e = DataObjectFactory.getExtractInstance();
assertNotNull(e, "DataObject created");
assertInstanceOf(ExtractedRecord.class, e, "Proper class hierarchy");
}

@Test
Expand All @@ -43,9 +50,16 @@ void testSetImpl() {
assertEquals(MyDataObject.class.getName(), DataObjectFactory.getImplementingClass(), "Impl class set");
final IBaseDataObject d = DataObjectFactory.getInstance();
assertNotNull(d, "DataObject created");
assertNotNull(d, "Proper class hierarchy");
assertInstanceOf(BaseDataObject.class, d, "Proper class hierarchy");
assertInstanceOf(MyDataObject.class, d, "Proper class hierarchy");

DataObjectFactory.setImplementingExtractClass(MyExtractObject.class.getName());
assertEquals(MyExtractObject.class.getName(), DataObjectFactory.getImplementingExtractClass(), "Impl class set");
final IExtractedRecord e = DataObjectFactory.getExtractInstance();
assertNotNull(e, "DataObject created");
assertInstanceOf(BaseDataObject.class, e, "Proper class hierarchy");
assertInstanceOf(ExtractedRecord.class, e, "Proper class hierarchy");
assertInstanceOf(MyExtractObject.class, e, "Proper class hierarchy");
}

@Test
Expand All @@ -54,6 +68,10 @@ void testWithArgs() {
DataObjectFactory.setImplementingClass(MyDataObject.class.getName());
final IBaseDataObject d = DataObjectFactory.getInstance(args);
assertNotNull(d, "DataObject created");

DataObjectFactory.setImplementingExtractClass(MyExtractObject.class.getName());
final IExtractedRecord e = DataObjectFactory.getExtractInstance(args);
assertNotNull(e, "DataObject created");
}

@Test
Expand All @@ -64,6 +82,12 @@ void testWtihFullSet() {
assertEquals("form", ibdo.currentForm());
assertEquals("type", ibdo.getFileType());
assertSame(testPayload, ibdo.data());

IExtractedRecord extract = DataObjectFactory.getExtractInstance(testPayload, "filename", "form", "type");
assertEquals("filename", extract.getFilename());
assertEquals("form", extract.currentForm());
assertEquals("type", extract.getFileType());
assertSame(testPayload, extract.data());
}

@Test
Expand All @@ -74,10 +98,17 @@ void testFormAndFileType() {
assertEquals("formAndFileType", ibdo.currentForm());
assertEquals("formAndFileType", ibdo.getFileType());
assertSame(testPayload, ibdo.data());

IExtractedRecord extract = DataObjectFactory.getExtractInstance(testPayload, "filename", "formAndFileType");
assertEquals("filename", extract.getFilename());
assertEquals("formAndFileType", extract.currentForm());
assertEquals("formAndFileType", extract.getFileType());
assertSame(testPayload, extract.data());
}

@SuppressWarnings("unused")
public static class MyDataObject extends BaseDataObject {
static final long serialVersionUID = -2254597461746556210L;
private static final long serialVersionUID = -2254597461746556210L;

public MyDataObject() {
super();
Expand All @@ -87,4 +118,17 @@ public MyDataObject(final byte[] data, final String name) {
super(data, name);
}
}

@SuppressWarnings("unused")
public static class MyExtractObject extends ExtractedRecord {
private static final long serialVersionUID = -579253286374306668L;

public MyExtractObject() {
super();
}

public MyExtractObject(final byte[] data, final String name) {
super(data, name);
}
}
}
Loading

0 comments on commit a58a9c3

Please sign in to comment.