Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store notification content as binary to support unicode #280

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -71,24 +71,24 @@ 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 =
"SELECT ID FROM IDN_NOTIFICATION_APP_TEMPLATE " +
"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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -61,13 +64,13 @@
String channelName = notificationTemplate.getNotificationChannel();

NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate();
try {
byte[] contentByteArray = getContentByteArray(notificationTemplate);
int contentLength = contentByteArray.length;
try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) {

Check warning on line 69 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java#L67-L69

Added lines #L67 - L69 were not covered by tests
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);

Check warning on line 73 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java#L73

Added line #L73 was not covered by tests
preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType());
preparedStatement.setString(TYPE_KEY, displayName.toLowerCase());
preparedStatement.setString(CHANNEL, channelName);
Expand All @@ -80,6 +83,8 @@
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);

Check warning on line 87 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java#L86-L87

Added lines #L86 - L87 were not covered by tests
}
}

Expand All @@ -94,9 +99,7 @@
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);

Check warning on line 102 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java#L102

Added line #L102 was not covered by tests
notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE));
notificationTemplateResult.setLocale(locale);
notificationTemplateResult.setType(templateType);
Expand Down Expand Up @@ -170,9 +173,7 @@
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);

Check warning on line 176 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java#L176

Added line #L176 was not covered by tests
notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE));
notificationTemplateResult.setLocale(resultSet.getString(LOCALE));
notificationTemplateResult.setType(templateType.toLowerCase());
Expand Down Expand Up @@ -204,12 +205,12 @@
String channelName = notificationTemplate.getNotificationChannel();

NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate();
try {
byte[] contentByteArray = getContentByteArray(notificationTemplate);
int contentLength = contentByteArray.length;
try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) {

Check warning on line 210 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java#L208-L210

Added lines #L208 - L210 were not covered by tests
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);

Check warning on line 213 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java#L213

Added line #L213 was not covered by tests
preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType());
preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase());
preparedStatement.setString(TYPE_KEY, displayName.toLowerCase());
Expand All @@ -223,6 +224,8 @@
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);

Check warning on line 228 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java#L227-L228

Added lines #L227 - L228 were not covered by tests
}

}
Expand Down
Loading
Loading