diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff371f5e4..e58b858e2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,8 +46,8 @@ jobs: - 13 - 14 - 15 -# - 16 -# - 17 + - 16 + - 17 steps: - name: Git checkout uses: actions/checkout@v2 diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 7515174f5..5ca111bff 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1,5 @@ +- [cygnus-ngsi] Upgrade mockito-core dep from 5.5.0 to 5.10.0 +- [cygnus-ngsi-ld] Upgrade mockito-all 1.9.5 to mockito-core 5.10.0, httpcore from 4.3.1 to 4.4.16 and maven dependencies in general +- [cygnus-ngsi] Upgrade Java version from 1.11 to 1.17 in Dockerfile (#2297, #2304) +- [cygnus-common][cygnus-ngsi][cygnus-ngsi-ld] Upgrade in source code to support Java 17 build - [cygnus-common] [Arcgis] All NGSI DateTime are translated to Millis from Epoch (esriFieldTypeDate) (#2339) diff --git a/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/NameMappingsHandlers.java b/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/NameMappingsHandlers.java index 2dfdfbcdb..433e38748 100644 --- a/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/NameMappingsHandlers.java +++ b/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/NameMappingsHandlers.java @@ -28,13 +28,16 @@ import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; +import java.util.regex.Pattern; +import com.google.gson.GsonBuilder; /** * * @author frb */ public final class NameMappingsHandlers { - + private static final CygnusLogger LOGGER = new CygnusLogger(NameMappingsHandlers.class); /** @@ -69,7 +72,9 @@ private static NameMappings parseNameMappings(String jsonStr) { } // if // Parse the Json string - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); try { nameMappings = gson.fromJson(jsonStr, NameMappings.class); diff --git a/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/PatternTypeAdapter.java b/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/PatternTypeAdapter.java new file mode 100644 index 000000000..bebfe95d8 --- /dev/null +++ b/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/PatternTypeAdapter.java @@ -0,0 +1,44 @@ +/** + * Copyright 2024 Telefonica Investigación y Desarrollo, S.A.U + * + * This file is part of fiware-cygnus (FI-WARE project). + * + * fiware-cygnus is free software: you can redistribute it and/or modify it under the terms of the GNU Affero + * General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * fiware-cygnus is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License along with fiware-cygnus. If not, see + * http://www.gnu.org/licenses/. + * + * For those usages not covered by the GNU Affero General Public License please contact with iot_support at tid dot es + */ + +package com.telefonica.iot.cygnus.management; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; +import java.util.regex.Pattern; + +public class PatternTypeAdapter extends TypeAdapter { + + @Override + public void write(JsonWriter out, Pattern pattern) throws IOException { + if (pattern == null) { + out.nullValue(); + return; + } + out.value(pattern.pattern()); + } + + @Override + public Pattern read(JsonReader in) throws IOException { + String patternString = in.nextString(); + return Pattern.compile(patternString); + } +} diff --git a/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/SubscriptionsHandlers.java b/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/SubscriptionsHandlers.java index 6ca598026..cd5156793 100644 --- a/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/SubscriptionsHandlers.java +++ b/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/SubscriptionsHandlers.java @@ -18,6 +18,7 @@ package com.telefonica.iot.cygnus.management; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; import com.telefonica.iot.cygnus.backends.http.JsonResponse; import com.telefonica.iot.cygnus.backends.orion.OrionBackendImpl; @@ -26,8 +27,10 @@ import com.telefonica.iot.cygnus.containers.OrionEndpoint; import com.telefonica.iot.cygnus.log.CygnusLogger; import com.telefonica.iot.cygnus.utils.CommonConstants; +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; import java.io.BufferedReader; import java.io.IOException; +import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.simple.JSONObject; @@ -117,7 +120,9 @@ public static void get(HttpServletRequest request, HttpServletResponse response) // Get the endpoint string and parse it String endpointStr = readPayload(request); - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); OrionEndpoint endpoint; try { @@ -276,7 +281,9 @@ public static void post(HttpServletRequest request, HttpServletResponse response private static void postV1(String payload, String fiwareService, String fiwareServicePath, HttpServletResponse response) throws IOException { // Parse the payload - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); CygnusSubscriptionV1 cygnusSubscriptionv1; try { @@ -341,7 +348,9 @@ private static void postV1(String payload, String fiwareService, String fiwareSe private static void postV2(String payload, String fiwareService, String fiwareServicePath, HttpServletResponse response) throws IOException { // Parse the payload - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); CygnusSubscriptionV2 cygnusSubscriptionv2; try { diff --git a/cygnus-ngsi-ld/pom.xml b/cygnus-ngsi-ld/pom.xml index 40ef56834..53b3cee5e 100644 --- a/cygnus-ngsi-ld/pom.xml +++ b/cygnus-ngsi-ld/pom.xml @@ -17,9 +17,9 @@ - org.mockito - mockito-all - 1.9.5 + org.mockito + mockito-core + 5.10.0 test @@ -32,7 +32,7 @@ org.apache.httpcomponents httpcore - 4.3.1 + 4.4.16 @@ -54,7 +54,7 @@ org.apache.commons commons-lang3 - 3.4 + 3.12.0 @@ -80,7 +80,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.5.1 + 3.8.0 1.8 1.8 @@ -91,7 +91,7 @@ org.apache.maven.plugins maven-assembly-plugin - 2.6 + 3.5.0 jar-with-dependencies @@ -101,7 +101,7 @@ org.apache.maven.plugins maven-assembly-plugin - 2.6 + 3.5.0 src/main/assembly/assemblyCompile.xml @@ -121,7 +121,7 @@ org.codehaus.mojo exec-maven-plugin - 1.5.0 + 3.1.0 compile @@ -142,7 +142,7 @@ org.apache.maven.plugins maven-site-plugin - 3.5.1 + 3.8.2 @@ -156,7 +156,7 @@ org.apache.maven.plugins maven-surefire-report-plugin - 2.16 + 3.0.0 true @@ -164,7 +164,7 @@ org.apache.maven.plugins maven-checkstyle-plugin - 2.12 + 3.2.1 telefonica_checkstyle.xml false @@ -174,7 +174,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.9 + 3.5.0 true diff --git a/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/containers/NotifyContextRequestLD.java b/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/containers/NotifyContextRequestLD.java index 266390ef2..b5ea9a9a3 100644 --- a/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/containers/NotifyContextRequestLD.java +++ b/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/containers/NotifyContextRequestLD.java @@ -21,13 +21,14 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Map; - +import java.util.regex.Pattern; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; import org.json.JSONArray; import org.json.JSONObject; - +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; /** * @@ -141,7 +142,9 @@ public static class ContextElement { * Constructor for Gson, a Json parser. */ public ContextElement(String contextElement) { - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); String cer = contextElement; Map map = gson.fromJson(cer, new TypeToken>() { }.getType()); @@ -238,4 +241,4 @@ public String toString() { } // ContextElement -} \ No newline at end of file +} diff --git a/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandler.java b/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandler.java index c81094494..c4325eede 100644 --- a/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandler.java +++ b/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandler.java @@ -19,6 +19,7 @@ package com.telefonica.iot.cygnus.handlers; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; @@ -27,7 +28,9 @@ import com.telefonica.iot.cygnus.log.CygnusLogger; import com.telefonica.iot.cygnus.utils.CommonConstants; import com.telefonica.iot.cygnus.utils.CommonUtils; +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; import java.io.BufferedReader; +import java.util.regex.Pattern; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; @@ -271,7 +274,9 @@ public List getEvents(javax.servlet.http.HttpServletRequest request) thro // Parse the original data into a NotifyContextRequest object JSONObject content = new JSONObject(data); NotifyContextRequestLD notifyContextRequestLD = null; - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); try { notifyContextRequestLD = new NotifyContextRequestLD(content); diff --git a/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/sinks/NGSILDSink.java b/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/sinks/NGSILDSink.java index 519503949..d58af79f7 100644 --- a/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/sinks/NGSILDSink.java +++ b/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/sinks/NGSILDSink.java @@ -18,6 +18,7 @@ package com.telefonica.iot.cygnus.sinks; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.telefonica.iot.cygnus.containers.NotifyContextRequestLD.ContextElement; import com.telefonica.iot.cygnus.errors.*; import com.telefonica.iot.cygnus.interceptors.NGSILDEvent; @@ -25,6 +26,7 @@ import com.telefonica.iot.cygnus.sinks.Enums.DataModel; import com.telefonica.iot.cygnus.utils.CommonConstants; import com.telefonica.iot.cygnus.utils.NGSIConstants; +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; import org.apache.flume.*; import org.apache.flume.conf.Configurable; import org.apache.log4j.MDC; @@ -33,6 +35,7 @@ import java.util.Arrays; import java.util.Date; import java.util.Map; +import java.util.regex.Pattern; /** * @@ -497,7 +500,9 @@ private Status processNewBatches() { } else { // Event comes from file... original and mapped context elements must be re-created String[] contextElementsStr = (new String(event.getBody())).split(CommonConstants.CONCATENATOR); - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); ContextElement originalCE = null; ContextElement mappedCE = null; diff --git a/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/utils/NGSIUtilsForTests.java b/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/utils/NGSIUtilsForTests.java index 677b20492..5854d2012 100644 --- a/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/utils/NGSIUtilsForTests.java +++ b/cygnus-ngsi-ld/src/main/java/com/telefonica/iot/cygnus/utils/NGSIUtilsForTests.java @@ -18,11 +18,13 @@ package com.telefonica.iot.cygnus.utils; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.telefonica.iot.cygnus.containers.NameMappings; import com.telefonica.iot.cygnus.containers.NotifyContextRequestLD; import java.util.HashMap; - +import java.util.regex.Pattern; import com.telefonica.iot.cygnus.interceptors.NGSILDEvent; +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; import org.apache.flume.Context; import org.json.JSONObject; @@ -98,7 +100,9 @@ public static Context createContextForOrion(String orionHost, String orionPort, } // createContextForOrion public static NotifyContextRequestLD createJsonNotifyContextRequestLD(String jsonStr) throws Exception { - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); return gson.fromJson(jsonStr, NotifyContextRequestLD.class); } // createJsonNotifyContextRequest @@ -132,7 +136,9 @@ public static NGSILDEvent createNGSIEventLD(String originalCEStr, String mappedC * @throws java.lang.Exception */ public static NameMappings createJsonNameMappings(String jsonStr) throws Exception { - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); return gson.fromJson(jsonStr, NameMappings.class); } // createJsonNameMappings diff --git a/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandlerTest.java b/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandlerTest.java index cb79e5ea2..cb32fb73e 100644 --- a/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandlerTest.java +++ b/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandlerTest.java @@ -31,12 +31,15 @@ import javax.servlet.http.HttpServletRequest; import org.apache.flume.Context; import org.apache.flume.Event; -import org.apache.log4j.Level; -import org.apache.log4j.LogManager; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.LoggerConfig; import static org.junit.Assert.*; // this is required by "fail" like assertions import org.junit.Before; import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.junit.Test; import org.mockito.Mock; import static org.mockito.Mockito.when; @@ -48,7 +51,7 @@ * * @author frb */ -@RunWith(MockitoJUnitRunner.class) +@RunWith(MockitoJUnitRunner.Silent.class) public class NGSIRestHandlerTest { // Mocks @@ -178,7 +181,11 @@ public class NGSIRestHandlerTest { * Constructor. */ public NGSIRestHandlerTest() { - LogManager.getRootLogger().setLevel(Level.FATAL); + LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + Configuration config = ctx.getConfiguration(); + LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); + loggerConfig.setLevel(Level.FATAL); + ctx.updateLoggers(); } // NGSIRestHandlerTest /** @@ -628,4 +635,4 @@ private Context createContext(String notificationTarget, String defaultService, return context; } // createContext -} // NGSIRestHandlerTest \ No newline at end of file +} // NGSIRestHandlerTest diff --git a/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSICKANSinkTest.java b/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSICKANSinkTest.java index 9c5db6ec7..57d0312bb 100644 --- a/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSICKANSinkTest.java +++ b/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSICKANSinkTest.java @@ -22,7 +22,7 @@ import com.telefonica.iot.cygnus.sinks.Enums.DataModel; import static com.telefonica.iot.cygnus.utils.CommonUtilsForTests.getTestTraceHead; import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.apache.flume.Context; import org.apache.log4j.Level; import org.apache.log4j.LogManager; diff --git a/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSIPostgisSinkTest.java b/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSIPostgisSinkTest.java index dbe3ed3ae..2b8a8eba7 100644 --- a/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSIPostgisSinkTest.java +++ b/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSIPostgisSinkTest.java @@ -25,7 +25,7 @@ import org.apache.log4j.LogManager; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.junit.MockitoJUnitRunner; /** * diff --git a/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSIPostgreSQLSinkTest.java b/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSIPostgreSQLSinkTest.java index f5fe0ecaf..9dd2b15ec 100644 --- a/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSIPostgreSQLSinkTest.java +++ b/cygnus-ngsi-ld/src/test/java/com/telefonica/iot/cygnus/sinks/NGSIPostgreSQLSinkTest.java @@ -22,7 +22,7 @@ import org.apache.log4j.LogManager; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.junit.MockitoJUnitRunner; import static com.telefonica.iot.cygnus.utils.CommonUtilsForTests.getTestTraceHead; import static org.junit.Assert.assertEquals; diff --git a/cygnus-ngsi/pom.xml b/cygnus-ngsi/pom.xml index e9180d9a9..ff59622ad 100644 --- a/cygnus-ngsi/pom.xml +++ b/cygnus-ngsi/pom.xml @@ -20,7 +20,7 @@ org.mockito mockito-core - 5.5.0 + 5.10.0 test diff --git a/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandler.java b/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandler.java index defd98c49..fc88794d7 100644 --- a/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandler.java +++ b/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/handlers/NGSIRestHandler.java @@ -35,8 +35,9 @@ import java.util.List; import java.util.Locale; import java.util.Map; - +import java.util.regex.Pattern; import com.telefonica.iot.cygnus.utils.NotifyContextRequestNGSIv2Deserializer; +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; import org.apache.flume.Context; import org.apache.flume.Event; import org.apache.flume.source.http.HTTPBadRequestException; @@ -329,7 +330,9 @@ public List getEvents(javax.servlet.http.HttpServletRequest request) thro // Parse the original data into a NotifyContextRequest object NotifyContextRequest ncr = null; NotifyContextRequestNGSIv2 notifyContextRequestNGSIv2 = null; - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); try { if (ngsiVersion != null) { diff --git a/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/interceptors/NGSINameMappingsInterceptor.java b/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/interceptors/NGSINameMappingsInterceptor.java index 2d1366864..3262c7178 100644 --- a/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/interceptors/NGSINameMappingsInterceptor.java +++ b/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/interceptors/NGSINameMappingsInterceptor.java @@ -18,6 +18,7 @@ package com.telefonica.iot.cygnus.interceptors; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonIOException; import com.google.gson.JsonSyntaxException; import com.telefonica.iot.cygnus.containers.NameMappings; @@ -31,6 +32,7 @@ import com.telefonica.iot.cygnus.utils.CommonConstants; import com.telefonica.iot.cygnus.utils.JsonUtils; import com.telefonica.iot.cygnus.utils.NGSIConstants; +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; import java.io.File; import java.util.ArrayList; import java.util.List; @@ -42,6 +44,7 @@ import org.apache.logging.log4j.ThreadContext; import java.util.UUID; +import java.util.regex.Pattern; /** * @@ -272,7 +275,9 @@ protected void loadNameMappings(String jsonStr) { } // if // Parse the Json string - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); try { nameMappings = gson.fromJson(jsonStr, NameMappings.class); diff --git a/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/sinks/NGSIKafkaSink.java b/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/sinks/NGSIKafkaSink.java index d54771eae..333925b43 100644 --- a/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/sinks/NGSIKafkaSink.java +++ b/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/sinks/NGSIKafkaSink.java @@ -18,6 +18,7 @@ package com.telefonica.iot.cygnus.sinks; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.telefonica.iot.cygnus.backends.kafka.KafkaBackendImpl; import com.telefonica.iot.cygnus.containers.NotifyContextRequest.ContextElement; import com.telefonica.iot.cygnus.errors.CygnusBadConfiguration; @@ -29,7 +30,9 @@ import com.telefonica.iot.cygnus.log.CygnusLogger; import com.telefonica.iot.cygnus.utils.CommonConstants; import com.telefonica.iot.cygnus.utils.NGSICharsets; +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; import java.util.ArrayList; +import java.util.regex.Pattern; import org.I0Itec.zkclient.ZkClient; import org.apache.flume.Context; import org.apache.kafka.clients.producer.ProducerRecord; @@ -325,7 +328,9 @@ private String buildMessage(ContextElement contextElement, String fiwareService, String message = "{\"headers\":[{\"fiware-service\":\"" + fiwareService + "\"}," + "{\"fiware-servicePath\":\"" + fiwareServicePath + "\"}," + "{\"timestamp\":" + recvTimeTs + "}" + "],\"body\":"; - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); String contextElementResponseStr = gson.toJson(contextElement); message += contextElementResponseStr + "}"; return message; diff --git a/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/sinks/NGSISink.java b/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/sinks/NGSISink.java index 27651f56b..f0e59b823 100644 --- a/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/sinks/NGSISink.java +++ b/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/sinks/NGSISink.java @@ -19,6 +19,7 @@ package com.telefonica.iot.cygnus.sinks; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.telefonica.iot.cygnus.containers.NotifyContextRequest.ContextAttribute; import com.telefonica.iot.cygnus.containers.NotifyContextRequest.ContextElement; import com.telefonica.iot.cygnus.errors.CygnusBadConfiguration; @@ -45,9 +46,11 @@ import java.util.Map; import com.telefonica.iot.cygnus.utils.CommonConstants; import com.telefonica.iot.cygnus.utils.NGSIConstants; +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; +import java.util.regex.Pattern; import org.apache.flume.Channel; import org.apache.flume.Context; import org.apache.flume.Event; @@ -537,7 +540,9 @@ private Status processNewBatches() { } else { // Event comes from file... original and mapped context elements must be re-created String[] contextElementsStr = (new String(event.getBody())).split(CommonConstants.CONCATENATOR); - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); ContextElement originalCE = null; ContextElement mappedCE = null; diff --git a/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/utils/NGSIUtilsForTests.java b/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/utils/NGSIUtilsForTests.java index e6e89a9d5..0f6b0b00f 100644 --- a/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/utils/NGSIUtilsForTests.java +++ b/cygnus-ngsi/src/main/java/com/telefonica/iot/cygnus/utils/NGSIUtilsForTests.java @@ -25,6 +25,9 @@ import com.telefonica.iot.cygnus.containers.NameMappings; import com.telefonica.iot.cygnus.containers.NotifyContextRequest; import com.telefonica.iot.cygnus.interceptors.NGSIEvent; +import com.telefonica.iot.cygnus.management.PatternTypeAdapter; +import java.util.regex.Pattern; +import com.google.gson.GsonBuilder; /** * @@ -122,7 +125,10 @@ public static Context createContextForArcGis(String url, String username, * @throws java.lang.Exception */ public static NotifyContextRequest createJsonNotifyContextRequest(String jsonStr) throws Exception { - Gson gson = new Gson(); + // Parse the Json string + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); return gson.fromJson(jsonStr, NotifyContextRequest.class); } // createJsonNotifyContextRequest @@ -133,7 +139,9 @@ public static NotifyContextRequest createJsonNotifyContextRequest(String jsonStr * @throws java.lang.Exception */ public static NotifyContextRequest.StatusCode createJsonStatusCode(String jsonStr) throws Exception { - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); return gson.fromJson(jsonStr, NotifyContextRequest.StatusCode.class); } // createJsonStatusCode @@ -166,7 +174,9 @@ public static NGSIEvent createNGSIEvent(String originalCEStr, String mappedCEStr * @throws java.lang.Exception */ public static NameMappings createJsonNameMappings(String jsonStr) throws Exception { - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); return gson.fromJson(jsonStr, NameMappings.class); } // createJsonNameMappings @@ -193,7 +203,9 @@ public static NGSIEvent createInterceptedNGSIEvent(String originalCEStr, String * @throws java.lang.Exception */ public static NotifyContextRequest.ContextElement createJsonContextElement(String jsonStr) throws Exception { - Gson gson = new Gson(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) + .create(); return gson.fromJson(jsonStr, NotifyContextRequest.ContextElement.class); } // createJsonContextElement diff --git a/docker/cygnus-ngsi/Dockerfile b/docker/cygnus-ngsi/Dockerfile index 577c3b853..cf30c7d26 100644 --- a/docker/cygnus-ngsi/Dockerfile +++ b/docker/cygnus-ngsi/Dockerfile @@ -256,7 +256,7 @@ ENV FLUME_HOME "/opt/apache-flume" ENV FLUME_TGZ "apache-flume-${FLUME_VER}-bin.tar.gz" ENV FLUME_URL "https://archive.apache.org/dist/flume/${FLUME_VER}/${FLUME_TGZ}" -ENV JAVA_VERSION "1.11.0" +ENV JAVA_VERSION "1.17.0" COPY / ${CYGNUS_HOME} @@ -269,14 +269,8 @@ RUN \ apt-get -y update && \ apt-get -y upgrade && \ # Install dependencies - # Debian 12 comes with Java 17 but we have found some problems with it (see https://github.com/telefonicaid/fiware-cygnus/issues/2297) - # so we "downgrade" to Java 11 apt-get -y install curl git python3 && \ - curl -s --insecure -L 'http://security.debian.org/debian-security/pool/updates/main/o/openjdk-11/openjdk-11-jdk_11.0.22+7-1~deb11u1_amd64.deb' > openjdk-11-jdk_11.0.22+7-1~deb11u1_amd64.deb && \ - curl -s --insecure -L 'http://security.debian.org/debian-security/pool/updates/main/o/openjdk-11/openjdk-11-jre_11.0.22+7-1~deb11u1_amd64.deb' > openjdk-11-jre_11.0.22+7-1~deb11u1_amd64.deb && \ - curl -s --insecure -L 'http://security.debian.org/debian-security/pool/updates/main/o/openjdk-11/openjdk-11-jdk-headless_11.0.22+7-1~deb11u1_amd64.deb' > openjdk-11-jdk-headless_11.0.22+7-1~deb11u1_amd64.deb && \ - curl -s --insecure -L 'http://security.debian.org/debian-security/pool/updates/main/o/openjdk-11/openjdk-11-jre-headless_11.0.22+7-1~deb11u1_amd64.deb' > openjdk-11-jre-headless_11.0.22+7-1~deb11u1_amd64.deb && \ - apt-get -y install ./openjdk-11-jre-headless_11.0.22+7-1~deb11u1_amd64.deb ./openjdk-11-jre_11.0.22+7-1~deb11u1_amd64.deb ./openjdk-11-jdk-headless_11.0.22+7-1~deb11u1_amd64.deb ./openjdk-11-jdk_11.0.22+7-1~deb11u1_amd64.deb && \ + apt-get -y install openjdk-17-jdk openjdk-17-jdk-headless openjdk-17-jre openjdk-17-jre-headless && \ apt-get -y install maven && \ # Add Cygnus user adduser ${CYGNUS_USER} && \