From 7cb282a7b9760a3618168d291a7f22920d84aab5 Mon Sep 17 00:00:00 2001
From: RIHTARSIC Joze <joze.rihtarsic@ext.ec.europa.eu>
Date: Thu, 22 Aug 2024 10:01:30 +0200
Subject: [PATCH] [URGENT] fix mail alerting for new users

---
 smp-angular/src/assets/i18n/en.json           |  2 +-
 smp-angular/src/assets/i18n/ro.json           |  2 +-
 .../smp/utils/StringNamedSubstitutor.java     | 38 +++++++++++++------
 3 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/smp-angular/src/assets/i18n/en.json b/smp-angular/src/assets/i18n/en.json
index e7d518b67..8be9a6cef 100644
--- a/smp-angular/src/assets/i18n/en.json
+++ b/smp-angular/src/assets/i18n/en.json
@@ -473,7 +473,7 @@
 
   "login.button.login": "Login",
   "login.button.password.reset": "Request reset password",
-  "login.button.sso.login": "Request reset password",
+  "login.button.sso.login": "SSO login",
   "login.dialog.password.expiration.dialog.description": "Your password is about to expire on {{expirationDate}}! Please change the password before the expiration date!",
   "login.dialog.password.expiration.dialog.title": "Warning! Your password is about to expire",
   "login.error.generic.error": "Default error ({{errorStatus}}) occurred during login.",
diff --git a/smp-angular/src/assets/i18n/ro.json b/smp-angular/src/assets/i18n/ro.json
index ca2b94b06..17c97748a 100644
--- a/smp-angular/src/assets/i18n/ro.json
+++ b/smp-angular/src/assets/i18n/ro.json
@@ -473,7 +473,7 @@
 
   "login.button.login": "Autentifica",
   "login.button.password.reset": "Solicitati resetarea parolei",
-  "login.button.sso.login": "Solicitati resetarea parolei",
+  "login.button.sso.login": "SSO Autentifica",
   "login.dialog.password.expiration.dialog.description": "Parola dvs. este pe cale sa expire la {{expirationDate}}! Va rugam sa va schimbati parola inainte de data expirarii!",
   "login.dialog.password.expiration.dialog.title": "Avertizare! Parola dvs. este pe cale sa expire",
   "login.error.generic.error": "A aparut o eroare generica ({{errorStatus}}) in timpul conectarii.",
diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/StringNamedSubstitutor.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/StringNamedSubstitutor.java
index c8a13ae24..1c87fb419 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/StringNamedSubstitutor.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/StringNamedSubstitutor.java
@@ -18,13 +18,15 @@
  */
 package eu.europa.ec.edelivery.smp.utils;
 
-import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 
 import java.io.*;
 import java.nio.charset.Charset;
+import java.util.HashMap;
 import java.util.Map;
-import java.util.stream.Collectors;
+
+import static org.apache.commons.lang3.StringUtils.lowerCase;
+import static org.apache.commons.lang3.StringUtils.trim;
 
 /**
  * This class is used to substitute named variables in the string with key value pairs from the map.
@@ -56,12 +58,13 @@ public class StringNamedSubstitutor {
         LOG.debug("Using default charset: [{}]", charset);
         return resolve(string, config, charset);
     }
+
     /**
      * Substitute named variables in the string with key value pairs from the map.
      * The variables are in the form of ${name} and are case-insensitive and can contain only letters, digits, _ and .
      *
-     * @param string the string to resolve
-     * @param config the config to use
+     * @param string  the string to resolve
+     * @param config  the config to use
      * @param charset the character of the input stream
      * @return the resolved string
      */
@@ -79,7 +82,7 @@ public class StringNamedSubstitutor {
      * The input stream is ready with default charset.
      *
      * @param templateIS the InputStream to resolve
-     * @param config the map of property names and its values
+     * @param config     the map of property names and its values
      * @return the resolved string
      * @throws IOException if an I/O error occurs
      */
@@ -99,10 +102,9 @@ public class StringNamedSubstitutor {
      * @return the resolved string
      */
     public static String resolve(InputStream templateIS, Map<String, Object> config, String charset) throws IOException {
-        Map<String, Object> lowerCaseMap = config.entrySet().stream()
-                .collect(Collectors.toMap(e -> StringUtils.lowerCase(e.getKey()), Map.Entry::getValue));
+
         try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
-            resolve(templateIS, lowerCaseMap, byteArrayOutputStream, charset);
+            resolve(templateIS, config, byteArrayOutputStream, charset);
             return byteArrayOutputStream.toString();
         }
     }
@@ -119,9 +121,7 @@ public class StringNamedSubstitutor {
      */
     public static void resolve(InputStream templateIS, Map<String, Object> config,
                                OutputStream outputStream, String charset) throws IOException {
-        Map<String, Object> lowerCaseMap = config.entrySet().stream()
-                .collect(Collectors.toMap(e -> StringUtils.lowerCase(e.getKey()), Map.Entry::getValue));
-
+        Map<String, Object> lowerCaseMap = normalizeData(config);
         try (BufferedReader template = new BufferedReader(new InputStreamReader(templateIS, charset));
              Writer writer = new OutputStreamWriter(outputStream, charset)) {
             int read;
@@ -136,7 +136,7 @@ public class StringNamedSubstitutor {
                 if (name == null) {
                     writer.write(START_NAME);
                 } else {
-                    String key = StringUtils.lowerCase(name);
+                    String key = lowerCase(name);
                     Object objValue = lowerCaseMap.get(key);
                     String value = objValue != null ? String.valueOf(lowerCaseMap.get(key)) : null;
 
@@ -152,6 +152,20 @@ public class StringNamedSubstitutor {
         }
     }
 
+    /**
+     * Normalize the data model to trim and lower case the keys.
+     *
+     * @param dataModel the data model
+     * @return the normalized data model
+     */
+    private static Map<String, Object> normalizeData(Map<String, Object> dataModel) {
+        Map<String, Object> lowerCaseMap = new HashMap<>();
+        // Note: do not use stream with Collectors.toMap because it throws NPE if value is null
+        dataModel.forEach((key, value) ->
+                lowerCaseMap.put(lowerCase(trim(key)), value));
+        return lowerCaseMap;
+    }
+
     private static boolean isStartSequence(BufferedReader reader) throws IOException {
         reader.mark(START_NAME.length());
         int read = reader.read();
-- 
GitLab