From 05ec7dcccf7fca80fe209fb72def03d56021562e Mon Sep 17 00:00:00 2001 From: Darshana Gunawardana Date: Thu, 21 Nov 2024 11:19:10 +0530 Subject: [PATCH] Store notification content as binary to support unicode --- .../email/mgt/constants/I18nMgtConstants.java | 4 +- .../email/mgt/constants/SQLConstants.java | 20 ++++----- .../mgt/internal/I18nMgtServiceComponent.java | 4 -- .../store/dao/AppNotificationTemplateDAO.java | 37 +++++++++------- .../store/dao/OrgNotificationTemplateDAO.java | 37 +++++++++------- .../carbon/email/mgt/util/I18nEmailUtil.java | 44 +++++++++++++++++++ pom.xml | 2 +- 7 files changed, 96 insertions(+), 52 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java index e2fcd390..3ea9acaa 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java @@ -157,9 +157,7 @@ public static class NotificationTableColumns { public static final String TENANT_ID = "TENANT_ID"; public static final String TEMPLATE_KEY = "TEMPLATE_KEY"; public static final String LOCALE = "LOCALE"; - public static final String SUBJECT = "SUBJECT"; - public static final String BODY = "BODY"; - public static final String FOOTER = "FOOTER"; + public static final String CONTENT = "CONTENT"; public static final String CONTENT_TYPE = "CONTENT_TYPE"; public static final String TYPE_ID = "TYPE_ID"; public static final String APP_ID = "APP_ID"; diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/SQLConstants.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/SQLConstants.java index 97852514..2fb08ba2 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/SQLConstants.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/SQLConstants.java @@ -43,22 +43,22 @@ public class SQLConstants { // sql constants for org notification template public static final String INSERT_ORG_NOTIFICATION_TEMPLATE_SQL = "INSERT INTO IDN_NOTIFICATION_ORG_TEMPLATE " + - "(TEMPLATE_KEY, LOCALE, SUBJECT, BODY, FOOTER, CONTENT_TYPE, TYPE_ID, TENANT_ID) " + - "VALUES (:TEMPLATE_KEY;, :LOCALE;, :SUBJECT;, :BODY;, :FOOTER;, :CONTENT_TYPE;, (" + + "(TEMPLATE_KEY, LOCALE, CONTENT, CONTENT_TYPE, TYPE_ID, TENANT_ID) " + + "VALUES (:TEMPLATE_KEY;, :LOCALE;, :CONTENT;, :CONTENT_TYPE;, (" + GET_NOTIFICATION_TYPE_ID_SQL + "), :TENANT_ID;)"; public static final String GET_ORG_NOTIFICATION_TEMPLATE_SQL = - "SELECT SUBJECT, BODY, FOOTER, CONTENT_TYPE FROM IDN_NOTIFICATION_ORG_TEMPLATE " + + "SELECT CONTENT, CONTENT_TYPE FROM IDN_NOTIFICATION_ORG_TEMPLATE " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND TENANT_ID = :TENANT_ID;"; public static final String IS_ORG_NOTIFICATION_TEMPLATE_EXISTS_SQL = "SELECT ID FROM IDN_NOTIFICATION_ORG_TEMPLATE " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = :TYPE_ID; AND TENANT_ID = :TENANT_ID;"; public static final String LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL = - "SELECT SUBJECT, BODY, FOOTER, CONTENT_TYPE, LOCALE FROM IDN_NOTIFICATION_ORG_TEMPLATE " + + "SELECT CONTENT, CONTENT_TYPE, LOCALE FROM IDN_NOTIFICATION_ORG_TEMPLATE " + "WHERE TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND TENANT_ID = :TENANT_ID;"; public static final String UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL = "UPDATE IDN_NOTIFICATION_ORG_TEMPLATE " + - "SET SUBJECT = :SUBJECT;, BODY = :BODY;, FOOTER = :FOOTER;, CONTENT_TYPE = :CONTENT_TYPE; " + + "SET CONTENT = :CONTENT;, CONTENT_TYPE = :CONTENT_TYPE; " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND TENANT_ID = :TENANT_ID;"; public static final String DELETE_ORG_NOTIFICATION_TEMPLATE_SQL = @@ -71,11 +71,11 @@ public class SQLConstants { // sql constants for app notification template public static final String INSERT_APP_NOTIFICATION_TEMPLATE_SQL = "INSERT INTO IDN_NOTIFICATION_APP_TEMPLATE " + - "(TEMPLATE_KEY, LOCALE, SUBJECT, BODY, FOOTER, CONTENT_TYPE, TYPE_ID, APP_ID, TENANT_ID) " + - "VALUES (:TEMPLATE_KEY;, :LOCALE;, :SUBJECT;, :BODY;, :FOOTER;, :CONTENT_TYPE;, (" + + "(TEMPLATE_KEY, LOCALE, CONTENT, CONTENT_TYPE, TYPE_ID, APP_ID, TENANT_ID) " + + "VALUES (:TEMPLATE_KEY;, :LOCALE;, :CONTENT;, :CONTENT_TYPE;, (" + GET_NOTIFICATION_TYPE_ID_SQL + "), :APP_ID;, :TENANT_ID;)"; public static final String GET_APP_NOTIFICATION_TEMPLATE_SQL = - "SELECT SUBJECT, BODY, FOOTER, CONTENT_TYPE FROM IDN_NOTIFICATION_APP_TEMPLATE " + + "SELECT CONTENT, CONTENT_TYPE FROM IDN_NOTIFICATION_APP_TEMPLATE " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; public static final String IS_APP_NOTIFICATION_TEMPLATE_EXISTS_SQL = @@ -83,12 +83,12 @@ public class SQLConstants { "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = :TYPE_ID; AND APP_ID = :APP_ID; " + "AND TENANT_ID = :TENANT_ID;"; public static final String LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL = - "SELECT SUBJECT, BODY, FOOTER, CONTENT_TYPE, LOCALE FROM IDN_NOTIFICATION_APP_TEMPLATE " + + "SELECT CONTENT, CONTENT_TYPE, LOCALE FROM IDN_NOTIFICATION_APP_TEMPLATE " + "WHERE TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; public static final String UPDATE_APP_NOTIFICATION_TEMPLATE_SQL = "UPDATE IDN_NOTIFICATION_APP_TEMPLATE " + - "SET SUBJECT = :SUBJECT;, BODY = :BODY;, FOOTER = :FOOTER;, CONTENT_TYPE = :CONTENT_TYPE; " + + "SET CONTENT = :CONTENT;, CONTENT_TYPE = :CONTENT_TYPE; " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; public static final String DELETE_APP_NOTIFICATION_TEMPLATE_SQL = diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java index 93b356bb..1644bc4a 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java @@ -25,25 +25,21 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.ComponentContext; -import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.email.mgt.EmailTemplateManager; import org.wso2.carbon.email.mgt.EmailTemplateManagerImpl; import org.wso2.carbon.email.mgt.NotificationTemplateManagerImpl; import org.wso2.carbon.email.mgt.SMSProviderPayloadTemplateManager; import org.wso2.carbon.email.mgt.SMSProviderPayloadTemplateManagerImpl; import org.wso2.carbon.email.mgt.constants.I18nMgtConstants; -import org.wso2.carbon.email.mgt.exceptions.I18nEmailMgtException; import org.wso2.carbon.email.mgt.model.SMSProviderTemplate; import org.wso2.carbon.identity.application.mgt.ApplicationManagementService; import org.wso2.carbon.identity.core.persistence.registry.RegistryResourceMgtService; -import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerException; import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.governance.model.NotificationTemplate; import org.wso2.carbon.identity.governance.service.notification.NotificationChannels; import org.wso2.carbon.identity.governance.service.notification.NotificationTemplateManager; import org.wso2.carbon.identity.organization.management.service.OrganizationManager; import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.stratos.common.listeners.TenantMgtListener; import org.wso2.carbon.user.core.service.RealmService; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java index 3c020e43..6df89ced 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java @@ -24,17 +24,18 @@ import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerServerException; import org.wso2.carbon.identity.governance.model.NotificationTemplate; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.List; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.APP_ID; -import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.BODY; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CHANNEL; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CONTENT; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CONTENT_TYPE; -import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.FOOTER; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.LOCALE; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TEMPLATE_KEY; -import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.SUBJECT; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TENANT_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_KEY; @@ -47,6 +48,8 @@ import static org.wso2.carbon.email.mgt.constants.SQLConstants.IS_APP_NOTIFICATION_TEMPLATE_EXISTS_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.UPDATE_APP_NOTIFICATION_TEMPLATE_SQL; +import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.getContentByteArray; +import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.setContent; /** * This class is to perform CRUD operations for Application NotificationTemplates. @@ -61,13 +64,13 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S String channelName = notificationTemplate.getNotificationChannel(); NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate(); - try { + byte[] contentByteArray = getContentByteArray(notificationTemplate); + int contentLength = contentByteArray.length; + try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) { namedJdbcTemplate.executeInsert(INSERT_APP_NOTIFICATION_TEMPLATE_SQL, (preparedStatement -> { preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase()); preparedStatement.setString(LOCALE, locale); - preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); - preparedStatement.setString(BODY, notificationTemplate.getBody()); - preparedStatement.setString(FOOTER, notificationTemplate.getFooter()); + preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType()); preparedStatement.setString(TYPE_KEY, displayName.toLowerCase()); preparedStatement.setString(CHANNEL, channelName); @@ -80,6 +83,8 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S String.format("Error while adding %s template %s of type %s to application %s in %s tenant.", channelName, locale, displayName, applicationUuid, tenantId); throw new NotificationTemplateManagerServerException(error, e); + } catch (IOException e) { + throw new NotificationTemplateManagerServerException("Error while processing content stream.", e); } } @@ -94,9 +99,7 @@ public NotificationTemplate getNotificationTemplate(String locale, String templa notificationTemplate = namedJdbcTemplate.fetchSingleRecord(GET_APP_NOTIFICATION_TEMPLATE_SQL, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); - notificationTemplateResult.setBody(resultSet.getString(BODY)); - notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE)); notificationTemplateResult.setLocale(locale); notificationTemplateResult.setType(templateType); @@ -170,9 +173,7 @@ public List listNotificationTemplates(String templateType, notificationTemplates = namedJdbcTemplate.executeQuery(LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); - notificationTemplateResult.setBody(resultSet.getString(BODY)); - notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE)); notificationTemplateResult.setLocale(resultSet.getString(LOCALE)); notificationTemplateResult.setType(templateType.toLowerCase()); @@ -204,12 +205,12 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate String channelName = notificationTemplate.getNotificationChannel(); NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate(); - try { + byte[] contentByteArray = getContentByteArray(notificationTemplate); + int contentLength = contentByteArray.length; + try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) { namedJdbcTemplate.executeUpdate(UPDATE_APP_NOTIFICATION_TEMPLATE_SQL, preparedStatement -> { - preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); - preparedStatement.setString(BODY, notificationTemplate.getBody()); - preparedStatement.setString(FOOTER, notificationTemplate.getFooter()); + preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType()); preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase()); preparedStatement.setString(TYPE_KEY, displayName.toLowerCase()); @@ -223,6 +224,8 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate String.format("Error while updating %s template %s of type %s from application %s in %s tenant.", channelName, locale, displayName, applicationUuid, tenantId); throw new NotificationTemplateManagerServerException(error, e); + } catch (IOException e) { + throw new NotificationTemplateManagerServerException("Error while processing content stream.", e); } } diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java index 2fc880b8..fa8d0d01 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java @@ -24,16 +24,17 @@ import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerServerException; import org.wso2.carbon.identity.governance.model.NotificationTemplate; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.List; -import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.BODY; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CHANNEL; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CONTENT; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CONTENT_TYPE; -import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.FOOTER; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.LOCALE; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TEMPLATE_KEY; -import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.SUBJECT; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TENANT_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_KEY; @@ -45,6 +46,8 @@ import static org.wso2.carbon.email.mgt.constants.SQLConstants.IS_ORG_NOTIFICATION_TEMPLATE_EXISTS_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL; +import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.getContentByteArray; +import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.setContent; /** * This class is to perform CRUD operations for Org NotificationTemplates. @@ -59,13 +62,13 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, i String channelName = notificationTemplate.getNotificationChannel(); NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate(); - try { + byte[] contentByteArray = getContentByteArray(notificationTemplate); + int contentLength = contentByteArray.length; + try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) { namedJdbcTemplate.executeInsert(INSERT_ORG_NOTIFICATION_TEMPLATE_SQL, (preparedStatement -> { preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase()); preparedStatement.setString(LOCALE, locale); - preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); - preparedStatement.setString(BODY, notificationTemplate.getBody()); - preparedStatement.setString(FOOTER, notificationTemplate.getFooter()); + preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType()); preparedStatement.setString(TYPE_KEY, displayName.toLowerCase()); preparedStatement.setString(CHANNEL, channelName); @@ -77,6 +80,8 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, i String.format("Error while adding %s template %s of type %s to %s tenant.", channelName, locale, displayName, tenantId); throw new NotificationTemplateManagerServerException(error, e); + } catch (IOException e) { + throw new NotificationTemplateManagerServerException("Error while processing content stream.", e); } } @@ -91,9 +96,7 @@ public NotificationTemplate getNotificationTemplate(String locale, String templa notificationTemplate = namedJdbcTemplate.fetchSingleRecord(GET_ORG_NOTIFICATION_TEMPLATE_SQL, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); - notificationTemplateResult.setBody(resultSet.getString(BODY)); - notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE)); notificationTemplateResult.setLocale(locale); notificationTemplateResult.setType(templateType); @@ -163,9 +166,7 @@ public List listNotificationTemplates(String templateType, notificationTemplates = namedJdbcTemplate.executeQuery(LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); - notificationTemplateResult.setBody(resultSet.getString(BODY)); - notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE)); notificationTemplateResult.setLocale(resultSet.getString(LOCALE)); notificationTemplateResult.setType(templateType.toLowerCase()); @@ -196,12 +197,12 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate String channelName = notificationTemplate.getNotificationChannel(); NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate(); - try { + byte[] contentByteArray = getContentByteArray(notificationTemplate); + int contentLength = contentByteArray.length; + try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) { namedJdbcTemplate.executeUpdate(UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL, preparedStatement -> { - preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); - preparedStatement.setString(BODY, notificationTemplate.getBody()); - preparedStatement.setString(FOOTER, notificationTemplate.getFooter()); + preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType()); preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase()); preparedStatement.setString(TYPE_KEY, displayName.toLowerCase()); @@ -214,6 +215,8 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate String.format("Error while updating %s template %s of type %s from %s tenant.", channelName, locale, displayName, tenantId); throw new NotificationTemplateManagerServerException(error, e); + } catch (IOException e) { + throw new NotificationTemplateManagerServerException("Error while processing content stream.", e); } } diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/util/I18nEmailUtil.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/util/I18nEmailUtil.java index 0ada4150..787772af 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/util/I18nEmailUtil.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/util/I18nEmailUtil.java @@ -39,9 +39,13 @@ import org.wso2.carbon.registry.core.ResourceImpl; import org.wso2.carbon.registry.core.exceptions.RegistryException; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -281,4 +285,44 @@ public static String getNotificationLocale() { ? IdentityUtil.getProperty(I18nMgtConstants.NOTIFICATION_DEFAULT_LOCALE) : I18nMgtConstants.DEFAULT_NOTIFICATION_LOCALE; } + + /** + * Get the notification template subject, body & footer contents as a byte array. + * + * @param notificationTemplate the notification template to get the content + * @return the byte array of the content + */ + public static byte[] getContentByteArray(NotificationTemplate notificationTemplate) { + + String[] templateContent = new String[]{notificationTemplate.getSubject(), notificationTemplate.getBody(), + notificationTemplate.getFooter()}; + String content = new Gson().toJson(templateContent); + return content.getBytes(StandardCharsets.UTF_8); + } + + /** + * Read the content stream and set the subject, body & footer of the notification template. + * + * @param contentStream the inputStream of the content + * @param notificationTemplateResult the notification template to set the content + * @throws SQLException + */ + public static void setContent(InputStream contentStream, NotificationTemplate notificationTemplateResult) throws + SQLException { + + try { + byte[] contentByteArray = contentStream.readAllBytes(); + String content = new String(contentByteArray, StandardCharsets.UTF_8); + String[] templateContent = new Gson().fromJson(content, String[].class); + if (templateContent != null && templateContent.length == 3) { + notificationTemplateResult.setSubject(templateContent[0]); + notificationTemplateResult.setBody(templateContent[1]); + notificationTemplateResult.setFooter(templateContent[2]); + } else { + throw new SQLException("Invalid content data."); + } + } catch (IOException e) { + throw new SQLException("Error while reading content data.", e); + } + } } diff --git a/pom.xml b/pom.xml index a457e05b..8c86b8b5 100644 --- a/pom.xml +++ b/pom.xml @@ -518,7 +518,7 @@ [4.7.11, 5.0.0) - 2.1.0 + 2.2.2 [2.1.0,3.0.0)