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

Commit ede835a

Browse files
authored
Merge pull request #1 from teclib-idmef/features/jackson
Features/jackson
2 parents b9d9fa0 + 5c1cf07 commit ede835a

14 files changed

+380
-210
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.gradle/
22
/build/
33
/.idea/
4+
*.json

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repositories {
1313
}
1414

1515
dependencies {
16-
implementation 'net.jimblackler.jsonschemafriend:core:0.11.2'
16+
implementation 'com.networknt:json-schema-validator:1.0.64'
1717
implementation 'com.fasterxml.jackson.core:jackson-databind:2.0.1'
1818
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
1919
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'

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

-40
This file was deleted.

src/main/java/org/idmef/IDMEFException.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.idmef;
22

3-
public class IDMEFException extends Exception {
3+
import java.io.IOException;
4+
5+
public class IDMEFException extends IOException {
46
public IDMEFException(String message) {
57
super(message);
68
}
+110-16
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,89 @@
11
package org.idmef;
22

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;
310
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+
820

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;
1033

1134
/**
1235
* Construct an empty IDMEFObject.
13-
*
1436
*/
15-
IDMEFObject() {
37+
public IDMEFObject()
38+
{
39+
properties = new LinkedHashMap<>();
1640
}
1741

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);
2579
}
2680

2781
/**
2882
* Set a property of the Message. If value is an array, transform it to a List.
2983
*
3084
* @param key the property key
3185
* @param value the property value
32-
* @return the value that was set
86+
* @return the real value that was set
3387
*/
3488
public Object put(String key, Object value) {
3589
Object adaptedValue = value;
@@ -43,8 +97,48 @@ public Object put(String key, Object value) {
4397
adaptedValue = l;
4498
}
4599

46-
super.put(key, adaptedValue);
100+
properties.put(key, adaptedValue);
47101

48102
return adaptedValue;
49103
}
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+
50144
}

src/main/java/org/idmef/IDMEFObjectAdapter.java

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.idmef;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.DeserializationContext;
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
8+
9+
import java.io.IOException;
10+
11+
public class IDMEFObjectDeserializer extends StdDeserializer<IDMEFObject> {
12+
13+
public IDMEFObjectDeserializer() {
14+
this(null);
15+
}
16+
17+
public IDMEFObjectDeserializer(Class<?> vc) {
18+
super(vc);
19+
}
20+
21+
@Override
22+
public IDMEFObject deserialize(JsonParser jp, DeserializationContext context) throws IOException, JsonProcessingException {
23+
JsonNode jsonNode = jp.getCodec().readTree(jp);
24+
25+
if (!jsonNode.isObject()) {
26+
throw new IDMEFException("Invalid JsonNode type: " + jsonNode.getClass().getName());
27+
}
28+
29+
return new IDMEFObject(jsonNode);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.idmef;
2+
3+
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.networknt.schema.JsonSchema;
7+
import com.networknt.schema.JsonSchemaFactory;
8+
import com.networknt.schema.SpecVersion;
9+
import com.networknt.schema.ValidationMessage;
10+
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.util.Set;
14+
15+
public class IDMEFValidator {
16+
private static final String SCHEMA_RESOURCE_PATH = "/IDMEFv2.schema";
17+
18+
/**
19+
* Constructs a Validator.
20+
*/
21+
public IDMEFValidator() {
22+
}
23+
24+
private boolean validateJsonNode(JsonNode jsonNode) throws IDMEFException {
25+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
26+
InputStream is = IDMEFValidator.class.getResourceAsStream(SCHEMA_RESOURCE_PATH);
27+
if (is == null)
28+
throw new IDMEFException("cannot load schema");
29+
30+
JsonSchema schema = factory.getSchema(is);
31+
32+
Set<ValidationMessage> errors = schema.validate(jsonNode);
33+
34+
return errors.size() == 0;
35+
}
36+
37+
/**
38+
* Validate the object content w.r.t. current IDMEF JSON schema.
39+
*
40+
*/
41+
public boolean validate(IDMEFObject idmefObject) throws IDMEFException {
42+
ObjectMapper objectMapper = new ObjectMapper();
43+
JsonNode jsonNode = objectMapper.valueToTree(idmefObject);
44+
45+
return validateJsonNode(jsonNode);
46+
}
47+
48+
/**
49+
* Validate the bytes w.r.t. current IDMEF JSON schema.
50+
*
51+
*/
52+
public boolean validate(byte[] json) throws IOException {
53+
ObjectMapper objectMapper = new ObjectMapper();
54+
JsonNode jsonNode = objectMapper.readTree(json);
55+
56+
return validateJsonNode(jsonNode);
57+
}
58+
}

0 commit comments

Comments
 (0)