1
1
package org .idmef ;
2
2
3
+ import com .fasterxml .jackson .annotation .JsonAnyGetter ;
4
+ import com .fasterxml .jackson .databind .JsonNode ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
6
+ import com .fasterxml .jackson .databind .SerializationFeature ;
7
+ import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
8
+
9
+ import java .io .IOException ;
3
10
import java .lang .reflect .Array ;
4
- import java .util .ArrayList ;
5
- import java .util .HashMap ;
6
- import java .util .List ;
7
- import java .util .Map ;
11
+ import java .util .*;
12
+
13
+ /*
14
+ TODO:
15
+ - transform a JsonNode to a Map
16
+ - for embedded objects, lookup object class using property name (Analyzer -> org.idmef.Analyzer)
17
+ - for array of objects, use the array property name to lookup object class
18
+ */
19
+
8
20
9
- class IDMEFObject extends HashMap <String , Object > {
21
+ /**
22
+ * IDMEF base object.
23
+ *
24
+ * This implementation provides property setting/getting.
25
+ *
26
+ * Current implementation does not check property keys in put method. Property keys and values are checked
27
+ * when calling validate() method.
28
+ */
29
+ @ JsonDeserialize (using = IDMEFObjectDeserializer .class )
30
+ public class IDMEFObject {
31
+
32
+ private LinkedHashMap <String , Object > properties ;
10
33
11
34
/**
12
35
* Construct an empty IDMEFObject.
13
- *
14
36
*/
15
- IDMEFObject () {
37
+ public IDMEFObject ()
38
+ {
39
+ properties = new LinkedHashMap <>();
16
40
}
17
41
18
- /**
19
- * Construct an IDMEFObject from a map.
20
- *
21
- * @param map the map
22
- */
23
- IDMEFObject (Map <String , Object > map ) {
24
- putAll (map );
42
+ private static Object convertField (JsonNode value ) throws IDMEFException {
43
+ if (value .isInt ())
44
+ return value .asInt ();
45
+ else if (value .isTextual ())
46
+ return value .textValue ();
47
+ else if (value .isObject ())
48
+ return new IDMEFObject (value );
49
+ else if (value .isArray ()) {
50
+ List <Object > l = new ArrayList <>();
51
+
52
+ for (int i = 0 ; i < value .size (); i ++)
53
+ l .add (convertField (value .get (i )));
54
+
55
+ return l ;
56
+ } else
57
+ throw new IDMEFException ("Unhandled node type: " + value .getClass ().getName ());
58
+ }
59
+
60
+ IDMEFObject (JsonNode node ) throws IDMEFException {
61
+ this ();
62
+
63
+ Iterator <Map .Entry <String , JsonNode >> fields = node .fields ();
64
+
65
+ while (fields .hasNext ()) {
66
+ Map .Entry <String , JsonNode > field = fields .next ();
67
+
68
+ put (field .getKey (), convertField (field .getValue ()));
69
+ }
70
+ }
71
+
72
+ @ JsonAnyGetter
73
+ public Map <String , Object > getProperties () {
74
+ return properties ;
75
+ }
76
+
77
+ public Object get (String key ) {
78
+ return properties .get (key );
25
79
}
26
80
27
81
/**
28
82
* Set a property of the Message. If value is an array, transform it to a List.
29
83
*
30
84
* @param key the property key
31
85
* @param value the property value
32
- * @return the value that was set
86
+ * @return the real value that was set
33
87
*/
34
88
public Object put (String key , Object value ) {
35
89
Object adaptedValue = value ;
@@ -43,8 +97,48 @@ public Object put(String key, Object value) {
43
97
adaptedValue = l ;
44
98
}
45
99
46
- super .put (key , adaptedValue );
100
+ properties .put (key , adaptedValue );
47
101
48
102
return adaptedValue ;
49
103
}
104
+
105
+ @ Override
106
+ public boolean equals (Object 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 );
142
+ }
143
+
50
144
}
0 commit comments