Skip to content

Commit

Permalink
Notification Template API implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RushanNanayakkara committed Jul 10, 2024
1 parent e6c87bf commit f27a82b
Show file tree
Hide file tree
Showing 26 changed files with 4,977 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2024, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ Licensed 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.196-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,152 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* 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_UPDATING_TEMPLATE;
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 PATH_SEPARATOR = "/";
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<>();

/**
* 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.");

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_UPDATING_TEMPLATE, ErrorMessage.ERROR_ERROR_UPDATING_TEMPLATE);
}

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,28 @@
package org.wso2.carbon.identity.api.server.notification.template.common;


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;
}

/**
* Set TemplateManager osgi service.
* @param notificationTemplateManager TemplateManager
*/
public static void setNotificationTemplateManager(NotificationTemplateManager notificationTemplateManager) {
TemplatesServiceHolder.notificationTemplateManager = notificationTemplateManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* 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.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("service.name", "NotificationTemplateManager");
NotificationTemplateManager taskOperationService = (NotificationTemplateManager) PrivilegedCarbonContext.
getThreadLocalCarbonContext().getOSGiService(NotificationTemplateManager.class, serviceProperties);
if (taskOperationService != null) {
this.notificationTemplateManager = taskOperationService;
} else {
throw new Exception("Unable to retrieve NotificationTemplateManager service.");
}
}
return this.notificationTemplateManager;
}
}
Loading

0 comments on commit f27a82b

Please sign in to comment.