Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Commit bb7bbbd

Browse files
committed
Remove Message class, clean code and move validation to a separate class (does not validate yet)
1 parent eb5371f commit bb7bbbd

File tree

7 files changed

+124
-164
lines changed

7 files changed

+124
-164
lines changed

src/main/java/org/idmef/IDMEFObject.java

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import com.fasterxml.jackson.annotation.JsonAnyGetter;
44
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.SerializationFeature;
57
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
68

9+
import java.io.IOException;
710
import java.lang.reflect.Array;
811
import java.util.*;
912

@@ -28,34 +31,12 @@ public class IDMEFObject {
2831

2932
private LinkedHashMap<String, Object> properties;
3033

31-
/**
32-
* Construct an empty IDMEFObject.
33-
*/
34-
public IDMEFObject(IDMEFObject parent, String propertyName)
35-
{
36-
properties = new LinkedHashMap<>();
37-
38-
if (parent != null)
39-
parent.put(propertyName, this);
40-
}
41-
4234
/**
4335
* Construct an empty IDMEFObject.
4436
*/
4537
public IDMEFObject()
4638
{
47-
this(null, "");
48-
}
49-
50-
/**
51-
* Construct an IDMEFObject from a Map.
52-
*
53-
* @param map the Map
54-
*/
55-
IDMEFObject(Map<String, Object> map) {
56-
this();
57-
58-
properties.putAll(map);
39+
properties = new LinkedHashMap<>();
5940
}
6041

6142
private static Object convertField(JsonNode value) throws IDMEFException {
@@ -76,24 +57,18 @@ else if (value.isArray()) {
7657
throw new IDMEFException("Unhandled node type: " + value.getClass().getName());
7758
}
7859

79-
private void putFields(JsonNode node) throws IDMEFException {
60+
IDMEFObject(JsonNode node) throws IDMEFException {
61+
this();
62+
8063
Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
8164

8265
while (fields.hasNext()) {
8366
Map.Entry<String, JsonNode> field = fields.next();
84-
String key = field.getKey();
85-
JsonNode value = field.getValue();
8667

87-
put(key, convertField(value));
68+
put(field.getKey(), convertField(field.getValue()));
8869
}
8970
}
9071

91-
IDMEFObject(JsonNode node) throws IDMEFException {
92-
this();
93-
94-
putFields(node);
95-
}
96-
9772
@JsonAnyGetter
9873
public Map<String, Object> getProperties() {
9974
return properties;
@@ -129,6 +104,41 @@ public Object put(String key, Object value) {
129104

130105
@Override
131106
public boolean equals(Object obj) {
132-
return properties.equals(obj);
107+
if (! (obj instanceof IDMEFObject))
108+
return false;
109+
110+
IDMEFObject idmefObject = (IDMEFObject) obj;
111+
112+
return properties.equals(idmefObject.properties);
113+
}
114+
115+
/**
116+
* Serialize a Message to JSON bytes.
117+
*
118+
* <b>Note: The method first validates the Message.</b>
119+
*
120+
* @return the JSON bytes
121+
* @throws IDMEFException if the Message is not valid
122+
*/
123+
public byte[] serialize() throws /* IDMEFException,*/ IOException {
124+
//validate();
125+
126+
ObjectMapper objectMapper = new ObjectMapper();
127+
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
128+
129+
return objectMapper.writeValueAsBytes(this);
130+
}
131+
132+
/**
133+
* Deserialize JSON bytes to a Message
134+
*
135+
* @param json the JSON bytes
136+
* @return a Message object with content filled from JSON
137+
*/
138+
public static IDMEFObject deserialize(byte[] json) throws IOException {
139+
ObjectMapper objectMapper = new ObjectMapper();
140+
141+
return objectMapper.readValue(json, IDMEFObject.class);
133142
}
143+
134144
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.idmef;
2+
3+
import net.jimblackler.jsonschemafriend.*;
4+
5+
import java.net.URL;
6+
7+
public class IDMEFValidator {
8+
private static final String SCHEMA_RESOURCE_PATH = "/IDMEFv2.schema";
9+
10+
/**
11+
* Constructs an empty Message.
12+
*/
13+
public IDMEFValidator() { }
14+
15+
/**
16+
* Validate the Message content w.r.t. current IDMEF JSON schema.
17+
*
18+
* @throws IDMEFException if the Message is not valid.
19+
*/
20+
public void validate(IDMEFObject idmefObject) throws IDMEFException {
21+
URL r = IDMEFValidator.class.getResource(SCHEMA_RESOURCE_PATH);
22+
if (r == null)
23+
throw new IDMEFException("Cannot locate schema resource");
24+
25+
Schema schema;
26+
27+
SchemaStore schemaStore = new SchemaStore();
28+
try {
29+
schema = schemaStore.loadSchema(r);
30+
} catch (GenerationException e) {
31+
throw new IDMEFException("error loading schema:" + e.getMessage());
32+
}
33+
34+
Validator validator = new Validator();
35+
36+
try {
37+
validator.validate(schema, idmefObject);
38+
} catch (ValidationException e) {
39+
throw new IDMEFException("error validating:" + e.getMessage());
40+
}
41+
}
42+
43+
}

src/main/java/org/idmef/Message.java

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/test/java/TestDeserialize.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
11
import org.idmef.IDMEFObject;
2-
import org.idmef.Message;
32
import org.junit.jupiter.api.Test;
43

54
import static org.junit.jupiter.api.Assertions.assertEquals;
65
import static org.junit.jupiter.api.Assertions.fail;
76

87
public class TestDeserialize {
98

10-
private static IDMEFObject deserialize(String json) {
9+
private static void deserializeAndCheck(String json, IDMEFObject expected) {
1110
IDMEFObject msg = null;
1211

1312
try {
14-
msg = Message.deserialize(json.getBytes());
13+
msg = IDMEFObject.deserialize(json.getBytes());
1514
} catch (Exception e) {
1615
fail(e.getMessage());
1716
}
1817

19-
return msg;
18+
String[] propertiesToCheck = {"ID", "CreateTime", "Version"};
19+
20+
for (String property: propertiesToCheck) {
21+
assertEquals(msg.get(property), expected.get(property));
22+
}
2023
}
2124

2225
@Test
2326
void testDeserializeMessage1() {
24-
IDMEFObject msg = deserialize(Util.string1());
25-
26-
System.err.println("Message: " + msg.getProperties().getClass().getName());
27-
System.err.println("Analyzer: " + msg.get("Analyzer").getClass().getName());
28-
29-
assertEquals(msg, Util.message1());
27+
deserializeAndCheck(Util.string1(), Util.message1());
3028
}
3129

3230
@Test
3331
void testDeserializeMessage2() {
34-
IDMEFObject msg = deserialize(Util.string2());
35-
36-
System.err.println("Message: " + msg.getProperties().getClass().getName());
37-
System.err.println("Sensor: " + msg.get("Sensor").getClass().getName());
38-
39-
assertEquals(msg, Util.message2());
32+
deserializeAndCheck(Util.string2(), Util.message2());
4033
}
4134
}

src/test/java/TestSerialize.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import org.idmef.Message;
1+
import org.idmef.IDMEFObject;
22
import org.junit.jupiter.api.Test;
33

44
import java.io.FileOutputStream;
@@ -7,7 +7,7 @@
77

88
public class TestSerialize {
99

10-
private static void serialize(Message msg, String outFileName) {
10+
private static void serialize(IDMEFObject msg, String outFileName) {
1111
try {
1212
byte[] b = msg.serialize();
1313

src/test/java/TestValidate.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
import org.idmef.Message;
1+
import org.idmef.IDMEFObject;
2+
import org.idmef.IDMEFValidator;
23
import org.junit.jupiter.api.Test;
34

45
import static org.junit.jupiter.api.Assertions.fail;
56

67
public class TestValidate {
78

8-
@Test
9-
void testValidateMessage1() {
10-
Message m = Util.message1();
9+
private static void validate(IDMEFObject idmefObject) {
10+
IDMEFValidator validator = new IDMEFValidator();
1111

1212
try {
13-
m.validate();
13+
validator.validate(idmefObject);
1414
} catch (Exception e) {
1515
fail(e.getMessage());
1616
}
1717
}
1818

19+
@Test
20+
void testValidateMessage1() {
21+
validate(Util.message1());
22+
}
23+
1924
}

0 commit comments

Comments
 (0)