From eab3d48bed25bed5ecbea29cd1003c13dec1e985 Mon Sep 17 00:00:00 2001 From: blavenie <benoit.lavenier@e-is.pro> Date: Fri, 19 May 2017 19:03:11 +0200 Subject: [PATCH] [fix ]User event WS: id was not serialized - fix #12 --- .../websocket/WebsocketUserEventEndPoint.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/websocket/WebsocketUserEventEndPoint.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/websocket/WebsocketUserEventEndPoint.java index 552300ca..6f2c8983 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/websocket/WebsocketUserEventEndPoint.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/websocket/WebsocketUserEventEndPoint.java @@ -38,6 +38,8 @@ package org.duniter.elasticsearch.user.websocket; limitations under the License. */ +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; import org.duniter.core.client.model.bma.Constants; import org.duniter.core.util.StringUtils; import org.duniter.elasticsearch.user.PluginSettings; @@ -59,6 +61,7 @@ import java.util.regex.Pattern; public class WebsocketUserEventEndPoint implements UserEventService.UserEventListener { public static Locale defaultLocale; + public static ObjectMapper mapper; public static class Init { @@ -67,6 +70,10 @@ public class WebsocketUserEventEndPoint implements UserEventService.UserEventLis webSocketServer.addEndPoint(WebsocketUserEventEndPoint.class); defaultLocale = pluginSettings.getI18nLocale(); if (defaultLocale == null) defaultLocale = new Locale("en", "GB"); + + // Define a static mapper + mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); } } @@ -100,7 +107,18 @@ public class WebsocketUserEventEndPoint implements UserEventService.UserEventLis @Override public void onEvent(UserEvent event) { - session.getAsyncRemote().sendText(event.toJson(locale)); + try { + UserEvent copy = new UserEvent(event); + copy.setMessage(copy.getLocalizedMessage(locale)); + String json = mapper.writeValueAsString(copy); + + // Force to serialized 'id' (skip @JsonIgnore) - fix #12 + json = "{\"id\":\""+event.getId()+"\"," + json.substring(1); + + session.getAsyncRemote().sendText(json); + } catch(IOException e) { + // silent + } } @Override -- GitLab