Skip to content

Commit f530f76

Browse files
authored
fix: constant pool to jackson (#21080)
Use jackson in constant pool UidlWriter to handle response in jackson. Part of #20741
1 parent 6b68511 commit f530f76

22 files changed

+387
-329
lines changed

flow-polymer-template/src/test/java/com/vaadin/flow/templatemodel/TemplateModelTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,9 @@ public void testDoubleValue() {
671671

672672
Assert.assertEquals(null, model.getDouble());
673673

674-
model.setDouble(new Double(1.0D));
674+
model.setDouble(Double.valueOf(1.0D));
675675

676-
Assert.assertEquals(new Double(1.0D), model.getDouble());
676+
Assert.assertEquals(Double.valueOf(1.0D), model.getDouble());
677677
}
678678

679679
@Test
@@ -695,9 +695,9 @@ public void testIntegerValue() {
695695

696696
Assert.assertEquals(null, model.getInteger());
697697

698-
model.setInteger(new Integer(10));
698+
model.setInteger(Integer.valueOf(10));
699699

700-
Assert.assertEquals(new Integer(10), model.getInteger());
700+
Assert.assertEquals(Integer.valueOf(10), model.getInteger());
701701
}
702702

703703
@Test
@@ -1443,7 +1443,7 @@ public void emptyModelListShouldBeRepopulatedAfterDetach() {
14431443
assertTrue(
14441444
"Changes to empty list after attach and detach should be the same",
14451445
changesAfterDetach.get(0).toJson(null)
1446-
.jsEquals(changesAfterAttach.get(0).toJson(null)));
1446+
.equals(changesAfterAttach.get(0).toJson(null)));
14471447

14481448
assertTrue(
14491449
"After the empty model list is detached and created a change, no more changes are created",

flow-server/src/main/java/com/vaadin/flow/internal/ConstantPool.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
import java.util.HashSet;
2020
import java.util.Set;
2121

22-
import elemental.json.Json;
23-
import elemental.json.JsonObject;
22+
import com.fasterxml.jackson.databind.node.ObjectNode;
2423

2524
/**
2625
* Keeps track of {@link ConstantPoolKey} values that have already been sent to
@@ -77,8 +76,8 @@ public boolean hasNewConstants() {
7776
*
7877
* @return a JSON object describing all new constants
7978
*/
80-
public JsonObject dumpConstants() {
81-
JsonObject json = Json.createObject();
79+
public ObjectNode dumpConstants() {
80+
ObjectNode json = JacksonUtils.createObjectNode();
8281

8382
newKeys.forEach(key -> key.export(json));
8483
newKeys.clear();

flow-server/src/main/java/com/vaadin/flow/internal/ConstantPoolKey.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import java.nio.charset.StandardCharsets;
2121
import java.util.Base64;
2222

23+
import com.fasterxml.jackson.databind.JsonNode;
24+
import com.fasterxml.jackson.databind.node.ObjectNode;
25+
2326
import elemental.json.JsonObject;
24-
import elemental.json.JsonValue;
2527

2628
/**
2729
* Wraps a JSON value that should be stored in the {@link ConstantPool} shared
@@ -39,7 +41,7 @@
3941
* @since 1.0
4042
*/
4143
public class ConstantPoolKey implements Serializable {
42-
private final JsonValue json;
44+
private final JsonNode json;
4345
private String id;
4446

4547
/**
@@ -50,7 +52,7 @@ public class ConstantPoolKey implements Serializable {
5052
* @param json
5153
* the JSON constant, not <code>null</code>
5254
*/
53-
public ConstantPoolKey(JsonValue json) {
55+
public ConstantPoolKey(JsonNode json) {
5456
assert json != null;
5557
this.json = json;
5658
}
@@ -76,8 +78,22 @@ public String getId() {
7678
* the constant pool update that is to be sent to the client, not
7779
* <code>null</code>
7880
*/
81+
@Deprecated
7982
public void export(JsonObject clientConstantPoolUpdate) {
80-
clientConstantPoolUpdate.put(getId(), json);
83+
export(JacksonUtils.mapElemental((JsonObject) json));
84+
}
85+
86+
/**
87+
* Exports this key into a JSON object to send to the client. This method
88+
* should be called only by the {@link ConstantPool} instance that manages
89+
* this value. It may be called multiple times.
90+
*
91+
* @param clientConstantPoolUpdate
92+
* the constant pool update that is to be sent to the client, not
93+
* <code>null</code>
94+
*/
95+
public void export(ObjectNode clientConstantPoolUpdate) {
96+
clientConstantPoolUpdate.set(getId(), json);
8197
}
8298

8399
/**
@@ -88,8 +104,8 @@ public void export(JsonObject clientConstantPoolUpdate) {
88104
* the JSON to get a hash of, not <code>null</code>
89105
* @return the key uniquely identifying the given JSON value
90106
*/
91-
private static String calculateHash(JsonValue json) {
92-
byte[] digest = MessageDigestUtil.sha256(json.toJson());
107+
private static String calculateHash(JsonNode json) {
108+
byte[] digest = MessageDigestUtil.sha256(json.toString());
93109

94110
/*
95111
* Only use first 64 bits to keep id string short (1 in 100 000 000

flow-server/src/main/java/com/vaadin/flow/internal/JacksonUtils.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,32 @@ public static ValueNode nullNode() {
104104
return (ValueNode) objectMapper.nullNode();
105105
}
106106

107+
/**
108+
* Map JsonArray to ArrayNode.
109+
*
110+
* @param jsonArray
111+
* JsonArray to change
112+
* @return ArrayNode of elemental json array object or null for null
113+
* jsonArray
114+
*/
115+
public static ArrayNode mapElemental(JsonArray jsonArray) {
116+
if (jsonArray == null || jsonArray instanceof JsonNull) {
117+
return null;
118+
}
119+
try {
120+
return (ArrayNode) objectMapper.readTree(jsonArray.toJson());
121+
} catch (JsonProcessingException e) {
122+
throw new RuntimeException(e);
123+
}
124+
}
125+
107126
/**
108127
* Map JsonObject to ObjectNode.
109128
*
110129
* @param jsonObject
111130
* JsonObject to change
112-
* @return ObjectNode of elemental json object or null for null jsonObject
131+
* @return ObjectNode of elemental json object object or null for null
132+
* jsonObject
113133
*/
114134
public static ObjectNode mapElemental(JsonObject jsonObject) {
115135
if (jsonObject == null) {

flow-server/src/main/java/com/vaadin/flow/internal/change/NodeChange.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
import java.io.Serializable;
2020

21+
import com.fasterxml.jackson.databind.node.ObjectNode;
22+
2123
import com.vaadin.flow.internal.ConstantPool;
24+
import com.vaadin.flow.internal.JacksonUtils;
2225
import com.vaadin.flow.internal.StateNode;
2326
import com.vaadin.flow.shared.JsonConstants;
2427

@@ -64,14 +67,15 @@ public StateNode getNode() {
6467
*
6568
* @return a json representation of this change
6669
*/
67-
public JsonObject toJson(ConstantPool constantPool) {
70+
public ObjectNode toJson(ConstantPool constantPool) {
6871
JsonObject json = Json.createObject();
6972

7073
json.put(JsonConstants.CHANGE_NODE, node.getId());
7174

7275
populateJson(json, constantPool);
7376

74-
return json;
77+
// TODO: Use Jackson directly after updating *Change to jackson
78+
return JacksonUtils.mapElemental(json);
7579
}
7680

7781
/**

flow-server/src/main/java/com/vaadin/flow/internal/nodefeature/ElementListenerMap.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.function.Function;
3030
import java.util.stream.Stream;
3131

32+
import com.fasterxml.jackson.databind.JsonNode;
33+
import com.fasterxml.jackson.databind.node.ObjectNode;
3234
import org.slf4j.LoggerFactory;
3335

3436
import com.vaadin.flow.component.UI;
@@ -39,13 +41,11 @@
3941
import com.vaadin.flow.dom.DomListenerRegistration;
4042
import com.vaadin.flow.function.SerializableRunnable;
4143
import com.vaadin.flow.internal.ConstantPoolKey;
42-
import com.vaadin.flow.internal.JsonUtils;
44+
import com.vaadin.flow.internal.JacksonUtils;
4345
import com.vaadin.flow.internal.StateNode;
4446
import com.vaadin.flow.shared.JsonConstants;
4547

46-
import elemental.json.Json;
4748
import elemental.json.JsonObject;
48-
import elemental.json.JsonValue;
4949

5050
/**
5151
* Map of DOM events with server-side listeners. The key set of this map
@@ -81,24 +81,24 @@ public void addDebouncePhases(int timeout, Set<DebouncePhase> phases) {
8181
});
8282
}
8383

84-
public JsonValue toJson() {
84+
public JsonNode toJson() {
8585
if (debounceSettings.isEmpty()) {
86-
return Json.create(false);
86+
return JacksonUtils.createNode(false);
8787
} else if (debounceSettings.size() == 1
8888
&& debounceSettings.containsKey(Integer.valueOf(0))) {
8989
// Shorthand if only debounce is a dummy filter debounce
90-
return Json.create(true);
90+
return JacksonUtils.createNode(true);
9191
} else {
9292
// [[timeout1, phase1, phase2, ...], [timeout2, phase1, ...]]
9393
return debounceSettings.entrySet().stream()
9494
.map(entry -> Stream.concat(
95-
Stream.of(
96-
Json.create(entry.getKey().intValue())),
95+
Stream.of(JacksonUtils
96+
.createNode(entry.getKey().intValue())),
9797
entry.getValue().stream()
9898
.map(DebouncePhase::getIdentifier)
99-
.map(Json::create))
100-
.collect(JsonUtils.asArray()))
101-
.collect(JsonUtils.asArray());
99+
.map(JacksonUtils::createNode))
100+
.collect(JacksonUtils.asArray()))
101+
.collect(JacksonUtils.asArray());
102102
}
103103

104104
}
@@ -393,7 +393,7 @@ private Map<String, ExpressionSettings> collectEventExpressions(
393393
private void updateEventSettings(String eventType) {
394394
Map<String, ExpressionSettings> eventSettings = collectEventExpressions(
395395
eventType);
396-
JsonObject eventSettingsJson = JsonUtils.createObject(eventSettings,
396+
ObjectNode eventSettingsJson = JacksonUtils.createObject(eventSettings,
397397
ExpressionSettings::toJson);
398398

399399
ConstantPoolKey constantPoolKey = new ConstantPoolKey(

flow-server/src/main/java/com/vaadin/flow/internal/nodefeature/PolymerEventListenerMap.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
import java.util.Map;
2323

2424
import com.vaadin.flow.internal.ConstantPoolKey;
25-
import com.vaadin.flow.internal.JsonUtils;
25+
import com.vaadin.flow.internal.JacksonUtils;
2626
import com.vaadin.flow.internal.StateNode;
2727
import com.vaadin.flow.shared.Registration;
2828

29-
import elemental.json.Json;
30-
3129
/**
3230
* Map of PolymerTemplate events with server-side listeners. The key set of this
3331
* map describes the event types for which event date is present.
@@ -104,8 +102,8 @@ public Registration add(String methodName, String[] eventDataExpressions) {
104102

105103
private static ConstantPoolKey createConstantPoolKey(
106104
List<String> eventData) {
107-
return new ConstantPoolKey(eventData.stream().map(Json::create)
108-
.collect(JsonUtils.asArray()));
105+
return new ConstantPoolKey(eventData.stream()
106+
.map(JacksonUtils::createNode).collect(JacksonUtils.asArray()));
109107
}
110108

111109
private void removeListener(String eventType) {

flow-server/src/main/java/com/vaadin/flow/server/BootstrapHandler.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,7 @@ private List<Element> setupDocumentHead(Element head,
823823
* @return a JSON object with the initial UIDL message
824824
*/
825825
private ObjectNode getInitialUidl(UI ui) {
826-
ObjectNode json = JacksonUtils
827-
.mapElemental(new UidlWriter().createUidl(ui, false));
826+
ObjectNode json = new UidlWriter().createUidl(ui, false);
828827

829828
VaadinSession session = ui.getSession();
830829
if (session.getConfiguration().isXsrfProtectionEnabled()) {
@@ -1500,8 +1499,7 @@ protected static String readResource(String fileName) {
15001499
* @return a JSON object with the initial UIDL message
15011500
*/
15021501
protected static ObjectNode getInitialUidl(UI ui) {
1503-
ObjectNode json = JacksonUtils
1504-
.mapElemental(new UidlWriter().createUidl(ui, false));
1502+
ObjectNode json = new UidlWriter().createUidl(ui, false);
15051503

15061504
VaadinSession session = ui.getSession();
15071505
if (session.getConfiguration().isXsrfProtectionEnabled()) {

flow-server/src/main/java/com/vaadin/flow/server/communication/AtmospherePushConnection.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.concurrent.TimeUnit;
2626
import java.util.concurrent.TimeoutException;
2727

28+
import com.fasterxml.jackson.databind.JsonNode;
2829
import org.atmosphere.cpr.AtmosphereResource;
2930
import org.atmosphere.cpr.AtmosphereResource.TRANSPORT;
3031
import org.atmosphere.cpr.BroadcastFilterAdapter;
@@ -36,8 +37,6 @@
3637
import com.vaadin.flow.internal.UsageStatistics;
3738
import com.vaadin.flow.shared.communication.PushConstants;
3839

39-
import elemental.json.JsonObject;
40-
4140
/**
4241
* A {@link PushConnection} implementation using the Atmosphere push support
4342
* that is by default included in Vaadin.
@@ -204,9 +203,9 @@ public void push(boolean async) {
204203
} else {
205204
synchronized (lock) {
206205
try {
207-
JsonObject response = new UidlWriter().createUidl(getUI(),
206+
JsonNode response = new UidlWriter().createUidl(getUI(),
208207
async);
209-
sendMessage("for(;;);[" + response.toJson() + "]");
208+
sendMessage("for(;;);[" + response + "]");
210209
} catch (Exception e) {
211210
throw new RuntimeException("Push failed", e);
212211
}

flow-server/src/main/java/com/vaadin/flow/server/communication/MetadataWriter.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818

1919
import java.io.Serializable;
2020

21+
import com.fasterxml.jackson.databind.node.ObjectNode;
22+
2123
import com.vaadin.flow.component.UI;
24+
import com.vaadin.flow.internal.JacksonUtils;
2225
import com.vaadin.flow.server.SystemMessages;
2326
import com.vaadin.flow.server.VaadinSessionState;
2427
import com.vaadin.flow.shared.JsonConstants;
2528

26-
import elemental.json.Json;
27-
import elemental.json.JsonObject;
28-
2929
/**
3030
* Serializes miscellaneous metadata to JSON.
3131
* <p>
@@ -54,9 +54,9 @@ public class MetadataWriter implements Serializable {
5454
* @return JSON object with the metadata
5555
*
5656
*/
57-
public JsonObject createMetadata(UI ui, boolean repaintAll, boolean async,
57+
public ObjectNode createMetadata(UI ui, boolean repaintAll, boolean async,
5858
SystemMessages messages) {
59-
JsonObject meta = Json.createObject();
59+
ObjectNode meta = JacksonUtils.createObjectNode();
6060

6161
if (repaintAll) {
6262
meta.put("repaintAll", true);
@@ -86,11 +86,11 @@ public JsonObject createMetadata(UI ui, boolean repaintAll, boolean async,
8686
}
8787
int redirectInterval = newTimeoutInterval + 15;
8888

89-
JsonObject redirect = Json.createObject();
89+
ObjectNode redirect = JacksonUtils.createObjectNode();
9090
redirect.put("interval", redirectInterval);
9191
redirect.put("url", url);
9292

93-
meta.put("timedRedirect", redirect);
93+
meta.set("timedRedirect", redirect);
9494
}
9595
timeoutInterval = newTimeoutInterval;
9696
}

0 commit comments

Comments
 (0)