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

Commit e66d0f6

Browse files
committed
Add objects contruction from Map for unserialization; not yet fully functional
1 parent 7de60ff commit e66d0f6

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

src/main/java/org/idmef/Analyzer.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.idmef;
22

3+
import java.util.Map;
4+
35
/**
46
* IDMEF Analyzer object.
57
*
@@ -16,4 +18,12 @@ public class Analyzer extends IDMEFObject {
1618
public Analyzer() {
1719
}
1820

21+
/**
22+
* Construct an Analyzer from a map.
23+
*
24+
* @param map the map
25+
*/
26+
public Analyzer(Map<String, Object> map) {
27+
super(map);
28+
}
1929
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,26 @@
44
import java.util.ArrayList;
55
import java.util.HashMap;
66
import java.util.List;
7+
import java.util.Map;
78

89
class IDMEFObject extends HashMap<String, Object> {
910

11+
/**
12+
* Construct an empty IDMEFObject.
13+
*
14+
*/
15+
IDMEFObject() {
16+
}
17+
18+
/**
19+
* Construct an IDMEFObject from a map.
20+
*
21+
* @param map the map
22+
*/
23+
IDMEFObject(Map<String, Object> map) {
24+
putAll(map);
25+
}
26+
1027
/**
1128
* Set a property of the Message. If value is an array, transform it to a List.
1229
*

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ public class Message extends IDMEFObject {
2626
public Message() {
2727
}
2828

29+
/**
30+
* Construct a Message from a map.
31+
*
32+
* @param map the map
33+
*/
34+
public Message(Map<String, Object> map) {
35+
super(map);
36+
}
37+
2938
/**
3039
* Validate the Message content w.r.t. current IDMEF JSON schema.
3140
*
@@ -76,14 +85,11 @@ public byte[] serialize() throws IDMEFException, IOException {
7685
* @param json the JSON bytes
7786
* @return a Message object with content filled from JSON
7887
*/
79-
public static Map<String, Object> unserialize(byte[] json) throws IOException {
88+
public static Message unserialize(byte[] json) throws IOException {
8089
ObjectMapper objectMapper = new ObjectMapper();
8190

8291
Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String,Object>>(){});
8392

84-
for (Map.Entry<String,Object> entry : map.entrySet()) {
85-
System.out.println("Item: " + entry.getKey() + ", Price: " + entry.getValue());
86-
}
87-
return map;
93+
return new Message(map);
8894
}
8995
}

src/test/java/TestUnserialize.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import org.idmef.Message;
22
import org.junit.jupiter.api.Test;
33

4-
import java.util.Map;
5-
64
import static org.junit.jupiter.api.Assertions.fail;
75

86
public class TestUnserialize {
@@ -12,7 +10,7 @@ void testUnserializeMessage1() {
1210
String s = Util.string1();
1311

1412
try {
15-
Map<String, Object> m = Message.unserialize(s.getBytes());
13+
Message m = Message.unserialize(s.getBytes());
1614
} catch (Exception e) {
1715
fail(e.getMessage());
1816
}

0 commit comments

Comments
 (0)