Listen to the events handling by WSO2 APIM

Write Simple Event Listener to Work with WSO2 API Manager

Yasas Rangika Mendis
3 min readJun 13, 2021
www.calxibe.com

Event Listener or Event Handler?

There is no formally defined difference between listeners and handlers. A listener is an object that subscribes for events from a source and a handler is an object that is responsible for handling certain events.

WSO2 API Manager provides ability to customize operations by registering an event listener for these operations. The listeners are executed at a fixed point in the certain operation, and the users are free to create a listener which implements their desired logic to be executed at these fixed points. Listener is an extension to extend the core functions of the API Manager.

Event handlers in API Manager can be used to trigger events and it is used to perform an operation based on the published events. For instance, an event handler can be used to send an email after a user addition.

In this post we are trying to add a default role to every newly registering user, by registering simple event listener. Every time when the user core method is called, this listener that we registered with that core method will be called and perform the task we assigned.

Listener is an extension to extend the user core functions.

Register an Event Listener to Add Specific Role to Every New User

In the user core there is a method called addUser(). When a user is created in WSO2 APIM Server, the addUser() method is called. We can register a listener before the actual execution of addUser() method and also, we can register a listener after the actual execution of addUser() method.

addUser() {     
preAddUser(); // you can implement this using listener
actualAddUser();
postAddUser(); // you can implement this using listener
}

In our scenario: when we add new user, there is a requirement to add Internal/subscriber role by-default to the user. For this requirement, we need to write some custom code before add new user. The following is the custom listener implementation for this. The doPreAddUser() method would be called before the actual user creation is done.

Likewise, you can add custom extensions to any method of the user core.

Registering the event handler

Register the event handler in the service component as follows.

Configuring the event handler

Add the event handler configuration to the <APIM_HOME>/repository/conf/deployment.toml file.

[[event_listener]]
id = "add-new-user-operation-event-listener"
type = "org.wso2.carbon.user.core.listener.UserOperationEventListener"
name = "org.wso2.custom.user.operation.event.listener.AddNewUserOperationEventListener"
order = 2

Make note of the following tips:

  1. The getExecutionOrderId() method can return any random value. This is important when there is more than one listener in the Identity Server and you need to consider the execution order of them. (Here, we have used 2 as the order id since this listener wanted be executed before all the other listeners[0 and 1 are already reserved for audit loggers]).
  2. All the methods return a boolean value. This value is mentioned regardless of whether you want to execute the next listener or not.

The following are the steps to configure the custom implementation.

  1. Listeners are registered as OSGI components. Therefore you need to register this class in an OSGI framework. You can go through this , which is the complete sample project of this sample scenario.
  2. Copy the OSGI bundle file in to the <APIM_HOME>/repository/components/dropins directory.
  3. Restart the server.

Testing the implementation

  • Login to the management console and create new user without adding Internal/subscriber role to the user.
View roles of a created user
View roles of a created user
  • Then click on the `View Roles` button to see the assigned user roles to the user.

Here, you will see the Internal/subscriber role has been added to the user.

--

--

Yasas Rangika Mendis

Software Engineer WSO2 Lanka Private Limited | Bachelor of Computer Science (Special)