Skip to content

Commit

Permalink
Add system template endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
RushanNanayakkara committed Sep 26, 2024
1 parent 7978df6 commit 6df089f
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<artifactId>org.wso2.carbon.identity.api.server.notification.template</artifactId>
<version>1.2.196-SNAPSHOT</version>
<version>1.2.228-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.api.server.notification.template.common;

import org.wso2.carbon.email.mgt.constants.TemplateMgtConstants;

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.core.Response.Status;
Expand All @@ -42,8 +44,8 @@ public class Constants {
public static final String APP_TEMPLATES_PATH = "/app-templates";
public static final String ORG_TEMPLATES_PATH = "/org-templates";
public static final String PATH_SEPARATOR = "/";
public static final String NOTIFICATION_CHANNEL_EMAIL = "email";
public static final String NOTIFICATION_CHANNEL_SMS = "sms";
public static final String NOTIFICATION_CHANNEL_EMAIL = "EMAIL";
public static final String NOTIFICATION_CHANNEL_SMS = "SMS";

// ERROR MESSAGES
private static final Map<String, ErrorMessage> NTM_ERROR_CODE_MAP = new HashMap<>();
Expand Down Expand Up @@ -94,7 +96,10 @@ public enum ErrorMessage {
"Server encountered an error while deleting the email template."),
ERROR_ERROR_DELETING_SMS_TEMPLATE("60010", Status.INTERNAL_SERVER_ERROR,
"Unable to delete the SMS template.",
"Server encountered an error while deleting the SMS template.");
"Server encountered an error while deleting the SMS template."),
ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED("60011", Status.FORBIDDEN,
"System resource deletion not allowed.",
"System resources are not eligible for deletion.");

private final String message;
private final Status httpStatus;
Expand Down Expand Up @@ -143,6 +148,8 @@ public String toString() {
NTM_ERROR_CODE_MAP.put(TEMPLATE_NOT_FOUND, ErrorMessage.ERROR_TEMPLATE_NOT_FOUND);
NTM_ERROR_CODE_MAP.put(ERROR_ADDING_TEMPLATE, ErrorMessage.ERROR_ERROR_ADDING_TEMPLATE);
NTM_ERROR_CODE_MAP.put(ERROR_UPDATING_TEMPLATE, ErrorMessage.ERROR_ERROR_UPDATING_TEMPLATE);
NTM_ERROR_CODE_MAP.put(TemplateMgtConstants.ErrorCodes.ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED,
ErrorMessage.ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED);
}

public static ErrorMessage getNTMMappedErrorMessage(String errorCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<artifactId>org.wso2.carbon.identity.api.server.notification.template</artifactId>
<version>1.2.196-SNAPSHOT</version>
<version>1.2.228-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public interface NotificationApiService {

public Response getAppTemplatesListOfSMSTemplateType(String templateTypeId, String appUuid);

public Response getDefaultEmailTemplate(String templateTypeId, String locale);

public Response getDefaultSMSTemplate(String templateTypeId, String locale);

public Response getEmailTemplateType(String templateTypeId);

public Response getOrgEmailTemplate(String templateTypeId, String locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/**
* Service class for application email templates.
*/
public class ApplicationTemplatesService {
public class TemplatesService {

/**
* Adds a new organization email template to the given template type. Template ID should not exist in the system.
Expand Down Expand Up @@ -274,6 +274,60 @@ public SMSTemplateWithID getSMSTemplate(String templateTypeId, String templateId
}
}

/**
* Retrieves the default email template of the given template type and locale.
*
* @param templateTypeId Template type ID.
* @param templateId Template ID.
* @return Default email template.
*/
public EmailTemplateWithID getSystemEmailTemplate(String templateTypeId, String templateId) {

try {
String templateTypeDisplayName = Util.decodeTemplateTypeId(templateTypeId);
NotificationTemplate internalTemplate = TemplatesServiceHolder.getNotificationTemplateManager().
getSystemNotificationTemplate(Constants.NOTIFICATION_CHANNEL_EMAIL,
templateTypeDisplayName, templateId);
// NotificationTemplateManager sends the default template if no matching template found.
// We need to check for the locale specifically.
if (!internalTemplate.getLocale().equals(templateId)) {
throw Util.handleError(Constants.ErrorMessage.ERROR_TEMPLATE_NOT_FOUND);
} else {
return Util.buildEmailTemplateWithID(internalTemplate);
}
} catch (NotificationTemplateManagerException e) {
throw Util.handleNotificationTemplateManagerException(e,
Constants.ErrorMessage.ERROR_ERROR_RETRIEVING_TEMPLATE);
}
}

/**
* Retrieves the default SMS template of the given template type and locale.
*
* @param templateTypeId Template type ID.
* @param templateId Template ID.
* @return Default SMS template.
*/
public SMSTemplateWithID getSystemSmsTemplate(String templateTypeId, String templateId) {

try {
String templateTypeDisplayName = Util.decodeTemplateTypeId(templateTypeId);
NotificationTemplate internalTemplate = TemplatesServiceHolder.getNotificationTemplateManager().
getSystemNotificationTemplate(Constants.NOTIFICATION_CHANNEL_SMS,
templateTypeDisplayName, templateId);
// NotificationTemplateManager sends the default template if no matching template found.
// We need to check for the locale specifically.
if (!internalTemplate.getLocale().equals(templateId)) {
throw Util.handleError(Constants.ErrorMessage.ERROR_TEMPLATE_NOT_FOUND);
} else {
return Util.buildSMSTemplateWithID(internalTemplate);
}
} catch (NotificationTemplateManagerException e) {
throw Util.handleNotificationTemplateManagerException(e,
Constants.ErrorMessage.ERROR_ERROR_RETRIEVING_TEMPLATE);
}
}

/**
* Updates the organization email template of the given template type and locale.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.wso2.carbon.identity.api.server.notification.template.common.Constants;
import org.wso2.carbon.identity.rest.api.server.notification.template.v1.NotificationApiService;
import org.wso2.carbon.identity.rest.api.server.notification.template.v1.core.ApplicationTemplatesService;
import org.wso2.carbon.identity.rest.api.server.notification.template.v1.core.TemplateTypeService;
import org.wso2.carbon.identity.rest.api.server.notification.template.v1.core.TemplatesService;
import org.wso2.carbon.identity.rest.api.server.notification.template.v1.model.EmailTemplate;
import org.wso2.carbon.identity.rest.api.server.notification.template.v1.model.EmailTemplateWithID;
import org.wso2.carbon.identity.rest.api.server.notification.template.v1.model.SMSTemplate;
Expand Down Expand Up @@ -54,15 +54,15 @@
public class NotificationApiServiceImpl implements NotificationApiService {

@Autowired
private ApplicationTemplatesService applicationTemplatesService;
private TemplatesService templatesService;
@Autowired
private TemplateTypeService templateTypeService;

@Override
public Response addAppEmailTemplate(String templateTypeId, String appUuid,
EmailTemplateWithID emailTemplateWithID) {

SimpleTemplate simpleEmailTemplate = applicationTemplatesService.addEmailTemplate(templateTypeId,
SimpleTemplate simpleEmailTemplate = templatesService.addEmailTemplate(templateTypeId,
emailTemplateWithID, appUuid);
URI headerLocation = buildURIForHeader(V1_API_PATH_COMPONENT + NOTIFICATION_TEMPLATES_API_PATH
+ NOTIFICATION_TEMPLATES_API_BASE_PATH_EMAIL + TEMPLATE_TYPES_PATH + PATH_SEPARATOR
Expand All @@ -74,7 +74,7 @@ public Response addAppEmailTemplate(String templateTypeId, String appUuid,
@Override
public Response addAppSMSTemplate(String templateTypeId, String appUuid, SMSTemplateWithID smSTemplateWithID) {

SimpleTemplate simpleSMSTemplate = applicationTemplatesService.addSMSTemplate(templateTypeId,
SimpleTemplate simpleSMSTemplate = templatesService.addSMSTemplate(templateTypeId,
smSTemplateWithID, appUuid);
URI headerLocation = buildURIForHeader(V1_API_PATH_COMPONENT + NOTIFICATION_TEMPLATES_API_PATH
+ NOTIFICATION_TEMPLATES_API_BASE_PATH_SMS + TEMPLATE_TYPES_PATH + PATH_SEPARATOR
Expand All @@ -98,7 +98,7 @@ public Response addEmailTemplateType(TemplateTypeOverview templateTypeOverview)
@Override
public Response addOrgEmailTemplate(String templateTypeId, EmailTemplateWithID emailTemplateWithID) {

SimpleTemplate simpleEmailTemplate = applicationTemplatesService.addEmailTemplate(templateTypeId,
SimpleTemplate simpleEmailTemplate = templatesService.addEmailTemplate(templateTypeId,
emailTemplateWithID);
URI headerLocation = buildURIForHeader(
V1_API_PATH_COMPONENT + NOTIFICATION_TEMPLATES_API_PATH
Expand All @@ -110,7 +110,7 @@ public Response addOrgEmailTemplate(String templateTypeId, EmailTemplateWithID e
@Override
public Response addOrgSMSTemplate(String templateTypeId, SMSTemplateWithID smSTemplateWithID) {

SimpleTemplate simpleSMSTemplate = applicationTemplatesService.addSMSTemplate(templateTypeId,
SimpleTemplate simpleSMSTemplate = templatesService.addSMSTemplate(templateTypeId,
smSTemplateWithID);
URI headerLocation = buildURIForHeader(
V1_API_PATH_COMPONENT + NOTIFICATION_TEMPLATES_API_PATH
Expand All @@ -134,14 +134,14 @@ public Response addSMSTemplateType(TemplateTypeOverview templateTypeOverview) {
@Override
public Response deleteAppEmailTemplate(String templateTypeId, String appUuid, String locale) {

applicationTemplatesService.deleteEmailTemplate(templateTypeId, locale, appUuid);
templatesService.deleteEmailTemplate(templateTypeId, locale, appUuid);
return Response.noContent().build();
}

@Override
public Response deleteAppSMSTemplate(String templateTypeId, String appUuid, String locale) {

applicationTemplatesService.deleteSMSTemplate(templateTypeId, locale, appUuid);
templatesService.deleteSMSTemplate(templateTypeId, locale, appUuid);
return Response.noContent().build();
}

Expand All @@ -155,14 +155,14 @@ public Response deleteEmailTemplateType(String templateTypeId) {
@Override
public Response deleteOrgEmailTemplate(String templateTypeId, String locale) {

applicationTemplatesService.deleteEmailTemplate(templateTypeId, locale);
templatesService.deleteEmailTemplate(templateTypeId, locale);
return Response.noContent().build();
}

@Override
public Response deleteOrgSMSTemplate(String templateTypeId, String locale) {

applicationTemplatesService.deleteSMSTemplate(templateTypeId, locale);
templatesService.deleteSMSTemplate(templateTypeId, locale);
return Response.noContent().build();
}

Expand Down Expand Up @@ -190,29 +190,29 @@ public Response getAllSMSTemplateTypes() {
@Override
public Response getAppEmailTemplate(String templateTypeId, String appUuid, String locale) {

return Response.ok().entity(applicationTemplatesService.getEmailTemplate(templateTypeId, locale, appUuid))
return Response.ok().entity(templatesService.getEmailTemplate(templateTypeId, locale, appUuid))
.build();
}

@Override
public Response getAppSMSTemplate(String templateTypeId, String appUuid, String locale) {

return Response.ok().entity(applicationTemplatesService.getSMSTemplate(templateTypeId, locale, appUuid))
return Response.ok().entity(templatesService.getSMSTemplate(templateTypeId, locale, appUuid))
.build();
}

@Override
public Response getAppTemplatesListOfEmailTemplateType(String templateTypeId, String appUuid) {

return Response.ok().entity(
applicationTemplatesService.getTemplatesListOfEmailTemplateType(templateTypeId, appUuid)).build();
templatesService.getTemplatesListOfEmailTemplateType(templateTypeId, appUuid)).build();
}

@Override
public Response getAppTemplatesListOfSMSTemplateType(String templateTypeId, String appUuid) {

return Response.ok().entity(
applicationTemplatesService.getTemplatesListOfSMSTemplateType(templateTypeId, appUuid)).build();
templatesService.getTemplatesListOfSMSTemplateType(templateTypeId, appUuid)).build();
}

@Override
Expand All @@ -225,26 +225,26 @@ public Response getEmailTemplateType(String templateTypeId) {
@Override
public Response getOrgEmailTemplate(String templateTypeId, String locale) {

return Response.ok().entity(applicationTemplatesService.getEmailTemplate(templateTypeId, locale)).build();
return Response.ok().entity(templatesService.getEmailTemplate(templateTypeId, locale)).build();
}

@Override
public Response getOrgSMSTemplate(String templateTypeId, String locale) {

return Response.ok().entity(applicationTemplatesService.getSMSTemplate(templateTypeId, locale)).build();
return Response.ok().entity(templatesService.getSMSTemplate(templateTypeId, locale)).build();
}

@Override
public Response getOrgTemplatesListOfEmailTemplateType(String templateTypeId) {

return Response.ok().entity(applicationTemplatesService.getTemplatesListOfEmailTemplateType(templateTypeId))
return Response.ok().entity(templatesService.getTemplatesListOfEmailTemplateType(templateTypeId))
.build();
}

@Override
public Response getOrgTemplatesListOfSMSTemplateType(String templateTypeId) {

return Response.ok().entity(applicationTemplatesService.getTemplatesListOfSMSTemplateType(templateTypeId))
return Response.ok().entity(templatesService.getTemplatesListOfSMSTemplateType(templateTypeId))
.build();
}

Expand All @@ -255,34 +255,46 @@ public Response getSMSTemplateType(String templateTypeId) {
templateTypeId)).build();
}

@Override
public Response getDefaultEmailTemplate(String templateTypeId, String locale) {

return Response.ok().entity(templatesService.getSystemEmailTemplate(templateTypeId, locale)).build();
}

@Override
public Response getDefaultSMSTemplate(String templateTypeId, String locale) {

return Response.ok().entity(templatesService.getSystemSmsTemplate(templateTypeId, locale)).build();
}

@Override
public Response updateAppEmailTemplate(String templateTypeId, String appUuid, String locale,
EmailTemplate emailTemplate) {

applicationTemplatesService.updateEmailTemplate(templateTypeId, locale, emailTemplate, appUuid);
templatesService.updateEmailTemplate(templateTypeId, locale, emailTemplate, appUuid);
return Response.ok().build();
}

@Override
public Response updateAppSMSTemplate(String templateTypeId, String appUuid, String locale,
SMSTemplate smsTemplate) {

applicationTemplatesService.updateSMSTemplate(templateTypeId, locale, smsTemplate, appUuid);
templatesService.updateSMSTemplate(templateTypeId, locale, smsTemplate, appUuid);
return Response.ok().build();
}

@Override
public Response updateOrgEmailTemplate(String templateTypeId, String locale,
EmailTemplate emailTemplate) {

applicationTemplatesService.updateEmailTemplate(templateTypeId, locale, emailTemplate);
templatesService.updateEmailTemplate(templateTypeId, locale, emailTemplate);
return Response.ok().build();
}

@Override
public Response updateOrgSMSTemplate(String templateTypeId, String locale, SMSTemplate smsTemplate) {

applicationTemplatesService.updateSMSTemplate(templateTypeId, locale, smsTemplate);
templatesService.updateSMSTemplate(templateTypeId, locale, smsTemplate);
return Response.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="org.wso2.carbon.identity.rest.api.server.notification.template.v1.core.ApplicationTemplatesService"/>
<bean class="org.wso2.carbon.identity.rest.api.server.notification.template.v1.core.TemplatesService"/>
<bean class="org.wso2.carbon.identity.rest.api.server.notification.template.v1.core.TemplateTypeService"/>
<bean class="org.wso2.carbon.identity.rest.api.server.notification.template.v1.impl.NotificationApiServiceImpl"/>
<bean id="templatesServiceHolderBean" class="org.wso2.carbon.identity.api.server.notification.template.common.TemplatesServiceHolder">
Expand Down
Loading

0 comments on commit 6df089f

Please sign in to comment.