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

[Notification Template API] API Implementation #629

Merged
merged 23 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
286247b
Notification Template API implementation
RushanNanayakkara Jun 4, 2024
f9ab533
Merge remote-tracking branch 'upstream/master' into ft_template_api
RushanNanayakkara Sep 8, 2024
7978df6
Merge remote-tracking branch 'upstream/master' into ft_template_api
RushanNanayakkara Sep 24, 2024
6df089f
Add system template endpoints
RushanNanayakkara Sep 26, 2024
ea76fd8
Add empty line to end of swagger definition
RushanNanayakkara Sep 27, 2024
885735a
Remove ignoring wrong template type
RushanNanayakkara Sep 28, 2024
3235e66
Add bulk delete endpoints
RushanNanayakkara Sep 28, 2024
4bff0ad
Change get all functions to return SimpleTemplates and add get all fu…
RushanNanayakkara Sep 29, 2024
cc2d188
Allow partial deletion for system template types.
RushanNanayakkara Sep 29, 2024
8d1686a
Merge remote-tracking branch 'upstream/master' into ft_template_api
RushanNanayakkara Sep 29, 2024
5ffcd2e
Add reset template type endpoints and minor fixes in template definit…
RushanNanayakkara Oct 5, 2024
fdf4687
Merge remote-tracking branch 'upstream/master' into ft_template_api
RushanNanayakkara Oct 5, 2024
b39ae00
merge upstream/master
RushanNanayakkara Oct 15, 2024
a9d3c1f
Add resetTemplateType function common to all channels.
RushanNanayakkara Oct 15, 2024
c1f3065
Add unauthorized deletion error mapping.
RushanNanayakkara Oct 18, 2024
080ae50
Code refactoring
RushanNanayakkara Oct 18, 2024
8c09b82
Fix invalid status code for system resource deletion
RushanNanayakkara Oct 18, 2024
544ac6d
Apply style suggestions from code review
RushanNanayakkara Oct 21, 2024
88287e9
add valid licence header
RushanNanayakkara Oct 22, 2024
d4df259
Addressing comments
RushanNanayakkara Oct 22, 2024
9d0c5aa
Merge upstream/master
RushanNanayakkara Oct 22, 2024
94789ca
Add notification template api module to pom
RushanNanayakkara Oct 22, 2024
2a1b58f
Bump pom versions
RushanNanayakkara Oct 23, 2024
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
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
~
~ WSO2 LLC. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<artifactId>org.wso2.carbon.identity.api.server.notification.template</artifactId>
<version>1.2.241-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.identity.api.server.notification.template.common</artifactId>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.event.handler.notification</groupId>
<artifactId>org.wso2.carbon.email.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.core.Response.Status;

import static org.wso2.carbon.email.mgt.constants.TemplateMgtConstants.ErrorCodes.ERROR_ADDING_TEMPLATE;
import static org.wso2.carbon.email.mgt.constants.TemplateMgtConstants.ErrorCodes.ERROR_RESOLVING_MAIN_APPLICATION;
import static org.wso2.carbon.email.mgt.constants.TemplateMgtConstants.ErrorCodes
.ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED;
import static org.wso2.carbon.email.mgt.constants.TemplateMgtConstants.ErrorCodes.TEMPLATE_ALREADY_EXISTS;
import static org.wso2.carbon.email.mgt.constants.TemplateMgtConstants.ErrorCodes.TEMPLATE_NOT_FOUND;
import static org.wso2.carbon.email.mgt.constants.TemplateMgtConstants.ErrorCodes.TEMPLATE_TYPE_ALREADY_EXISTS;
import static org.wso2.carbon.email.mgt.constants.TemplateMgtConstants.ErrorCodes.TEMPLATE_TYPE_NOT_FOUND;

/**
* Constants related to notification templates.
*/
public class Constants {

public static final String NOTIFICATION_TEMPLATES_ERROR_CODE_PREFIX = "NTM-";
public static final String NOTIFICATION_TEMPLATES_API_BASE_PATH_EMAIL = "/email";
public static final String NOTIFICATION_TEMPLATES_API_BASE_PATH_SMS = "/sms";
public static final String NOTIFICATION_TEMPLATES_API_PATH = "/notification";
public static final String TEMPLATE_TYPES_PATH = "/template-types";
public static final String APP_TEMPLATES_PATH = "/app-templates";
public static final String ORG_TEMPLATES_PATH = "/org-templates";
public static final String SYSTEM_TEMPLATES_PATH = "/system-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_TEMPLATE_OWNER_APP = "APP";
public static final String NOTIFICATION_TEMPLATE_OWNER_ORG = "ORG";
public static final String NOTIFICATION_TEMPLATE_OWNER_SYSTEM = "SYSTEM";

// ERROR MESSAGES
private static final Map<String, ErrorMessage> NTM_ERROR_CODE_MAP = new HashMap<>();

/**
* Enum for error messages.
*/
public enum ErrorMessage {

ERROR_TEMPLATE_TYPE_ALREADY_EXISTS("65001", Status.CONFLICT,
"Template Type already exists in the system.",
"A template type for the provided template display name already exists " +
"in the system."),
ERROR_TEMPLATE_TYPE_NOT_FOUND("65002", Status.NOT_FOUND,
"Template Type does not exist.",
"Specified template type does not exist in the system."),
ERROR_TEMPLATE_ALREADY_EXISTS("65003", Status.CONFLICT,
"Template already exists in the system.",
"A template for the provided template id already exists in the system."),
ERROR_TEMPLATE_NOT_FOUND("65004", Status.NOT_FOUND,
"Template does not exist.",
"Specified template does not exist in the system."),
ERROR_ERROR_RETRIEVING_TEMPLATE_TYPES("60002", Status.INTERNAL_SERVER_ERROR,
"Unable to retrieve template types.",
"Server encountered an error while retrieving template types."),
ERROR_ERROR_RETRIEVING_TEMPLATE_TYPE("60003", Status.INTERNAL_SERVER_ERROR,
"Unable to retrieve the template type.",
"Server encountered an error while retrieving the template " +
"type identified by the given template-type-id."),
ERROR_ERROR_ADDING_TEMPLATE_TYPE("60004", Status.INTERNAL_SERVER_ERROR,
"Unable to add the template type.",
"Server encountered an error while adding template type."),
ERROR_ERROR_DELETING_TEMPLATE_TYPE("60005", Status.INTERNAL_SERVER_ERROR,
"Unable to delete the template type.",
"Server encountered an error while deleting the template type."),
ERROR_ERROR_ADDING_TEMPLATE("60006", Status.INTERNAL_SERVER_ERROR,
"Unable to add the template.",
"Server encountered an error while adding the template to the system."),
ERROR_ERROR_UPDATING_TEMPLATE("60007", Status.INTERNAL_SERVER_ERROR,
"Unable to update the template.",
"Server encountered an error while updating the template."),
ERROR_ERROR_RETRIEVING_TEMPLATE("60008", Status.INTERNAL_SERVER_ERROR,
"Unable to retrieve the template.",
"Server encountered an error while retrieving the template " +
"identified by the given template-type-id and the template-id."),
ERROR_ERROR_DELETING_EMAIL_TEMPLATE("60009", Status.INTERNAL_SERVER_ERROR,
"Unable to delete the email template.",
"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."),
ERROR_ERROR_RETRIEVING_TEMPLATES("60012", Status.INTERNAL_SERVER_ERROR,
"Unable to retrieve templates.",
"Server encountered an error while retrieving templates."),
ERROR_ERROR_RESETTING_TEMPLATE_TYPE("60013", Status.INTERNAL_SERVER_ERROR,
"Unable to reset the template type.",
"Server encountered an error while deleting the templates of the template type."),
ERROR_ERROR_RESOLVING_MAIN_APPLICATION("60014", Status.INTERNAL_SERVER_ERROR,
"Unable to resolve the main application.",
"Server encountered an error while resolving the main application."),
ERROR_ERROR_INVALID_NOTIFICATION_CHANNEL("60015", Status.BAD_REQUEST,
"Invalid notification channel.",
"Notification channel can only be either 'EMAIL' or 'SMS'."),
ERROR_ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED("60016", Status.FORBIDDEN,
"System resource deletion not allowed.",
"System resources cannot be deleted.");

private final String message;
private final Status httpStatus;
private final String code;
private final String description;

ErrorMessage(String code, Status httpStatus, String message, String description) {

this.code = code;
this.httpStatus = httpStatus;
this.message = message;
this.description = description;
}

public String getCode() {

return NOTIFICATION_TEMPLATES_ERROR_CODE_PREFIX + code;
}

public String getMessage() {

return message;
}

public String getDescription() {

return description;
}

public Status getHttpStatus() {

return httpStatus;
}

@Override
public String toString() {

return code + " | " + message;
}
}

static {
NTM_ERROR_CODE_MAP.put(TEMPLATE_TYPE_ALREADY_EXISTS, ErrorMessage.ERROR_TEMPLATE_TYPE_ALREADY_EXISTS);
NTM_ERROR_CODE_MAP.put(TEMPLATE_TYPE_NOT_FOUND, ErrorMessage.ERROR_TEMPLATE_TYPE_NOT_FOUND);
NTM_ERROR_CODE_MAP.put(TEMPLATE_ALREADY_EXISTS, ErrorMessage.ERROR_TEMPLATE_ALREADY_EXISTS);
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_RESOLVING_MAIN_APPLICATION, ErrorMessage.ERROR_ERROR_RESOLVING_MAIN_APPLICATION);
NTM_ERROR_CODE_MAP.put(ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED,
ErrorMessage.ERROR_ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED);
}

public static ErrorMessage getNTMMappedErrorMessage(String errorCode) {

return NTM_ERROR_CODE_MAP.get(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.api.server.notification.template.common;
RushanNanayakkara marked this conversation as resolved.
Show resolved Hide resolved


import org.wso2.carbon.identity.governance.service.notification.NotificationTemplateManager;

/**
* This class is used to hold the TemplateManager service.
*/
public class TemplatesServiceHolder {

private static NotificationTemplateManager notificationTemplateManager;

/**
* Get TemplateManager osgi service.
* @return TemplateManager
*/
public static NotificationTemplateManager getNotificationTemplateManager() {

return notificationTemplateManager;
RushanNanayakkara marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Set TemplateManager osgi service.
* @param notificationTemplateManager TemplateManager
*/
public static void setNotificationTemplateManager(NotificationTemplateManager notificationTemplateManager) {

TemplatesServiceHolder.notificationTemplateManager = notificationTemplateManager;
RushanNanayakkara marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.email.mgt.constants.I18nMgtConstants;
import org.wso2.carbon.identity.governance.service.notification.NotificationTemplateManager;

import java.util.Hashtable;

/**
* Factory Beans serves as a factory for creating other beans within the IOC container. This factory bean is used to
* instantiate the NotificationTemplateManager type of object inside the container.
*/
public class OSGIServiceFactory extends AbstractFactoryBean<NotificationTemplateManager> {

private NotificationTemplateManager notificationTemplateManager;

@Override
public Class<?> getObjectType() {
return Object.class;
}

@Override
protected NotificationTemplateManager createInstance() throws Exception {

if (this.notificationTemplateManager == null) {
Hashtable<String, String> serviceProperties = new Hashtable<>();
serviceProperties.put(I18nMgtConstants.SERVICE_PROPERTY_KEY_SERVICE_NAME,
I18nMgtConstants.SERVICE_PROPERTY_VAL_NOTIFICATION_TEMPLATE_MANAGER);
NotificationTemplateManager taskOperationService = (NotificationTemplateManager) PrivilegedCarbonContext.
getThreadLocalCarbonContext().getOSGiService(NotificationTemplateManager.class, serviceProperties);
if (taskOperationService != null) {
this.notificationTemplateManager = taskOperationService;
} else {
throw new Exception("Unable to retrieve NotificationTemplateManager service.");
RushanNanayakkara marked this conversation as resolved.
Show resolved Hide resolved
}
}
return this.notificationTemplateManager;
}
}
Loading
Loading