Skip to content

Commit

Permalink
transferred utils to respective classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachin-Mamoru committed Jul 20, 2024
1 parent 6e511b9 commit 0b6f6ee
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@

package org.wso2.identity.webhook.common.event.handler;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.wso2.carbon.identity.event.IdentityEventServerException;
import org.wso2.identity.webhook.common.event.handler.builder.LoginEventPayloadBuilder;
import org.wso2.identity.webhook.common.event.handler.constant.Constants;
import org.wso2.identity.webhook.common.event.handler.internal.EventHookHandlerDataHolder;
import org.wso2.identity.webhook.common.event.handler.model.EventAttribute;
import org.wso2.identity.webhook.common.event.handler.model.EventData;
import org.wso2.identity.webhook.common.event.handler.model.ResourceConfig;
import org.wso2.identity.webhook.common.event.handler.util.EventHookHandlerUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -145,7 +150,7 @@ private EventAttribute extractEventAttribute(Resources publisherConfigResource,

for (Attribute attribute : publisherConfigResource.getResources().get(0).getAttributes()) {
if (isMatchingEventAttribute(attribute, eventName)) {
return EventHookHandlerUtils.buildEventAttributeFromJSONString(attribute.getValue());
return buildEventAttributeFromJSONString(attribute.getValue());
}
}
}
Expand All @@ -167,4 +172,49 @@ private ComplexCondition createPublisherConfigFilterCondition() {
conditionList.add(new PrimitiveCondition(Constants.RESOURCE_NAME, EQUALS, Constants.EVENT_PUBLISHER_CONFIG_RESOURCE_NAME));
return new ComplexCondition(ConditionType.ComplexOperator.AND, conditionList);
}

/**
* This method constructs the EventAttribute object from the json string.
*
* @param jsonString JSON string.
* @return EventAttribute object.
*/
private EventAttribute buildEventAttributeFromJSONString(String jsonString) throws IdentityEventException {

JSONObject eventJSON = getJSONObject(jsonString);
EventAttribute eventAttribute = new EventAttribute();
try {
if (eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY) instanceof Boolean) {
eventAttribute.setPublishEnabled(
(Boolean) eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY));
} else {
eventAttribute.setPublishEnabled(Boolean.parseBoolean(
(String) eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY)));
}
JSONObject propertiesJSON =
(JSONObject) eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PROPERTIES_KEY);
eventAttribute.setProperties(new ResourceConfig(propertiesJSON));

return eventAttribute;
} catch (ClassCastException e) {
throw new IdentityEventException("Error while casting event attribute from JSON string", e);
}
}

/**
* This method converts the parsed JSON String into a JSONObject.
*
* @param jsonString JSON string.
* @return JSON object.
* @throws IdentityEventServerException If an error occurs while constructing the object.
*/
private JSONObject getJSONObject(String jsonString) throws IdentityEventServerException {

JSONParser jsonParser = new JSONParser();
try {
return (JSONObject) jsonParser.parse(jsonString);
} catch (ParseException | ClassCastException e) {
throw new IdentityEventServerException("Error while parsing JSON string", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

package org.wso2.identity.webhook.common.event.handler.internal;

import org.wso2.carbon.identity.event.IdentityEventConfigBuilder;
import org.wso2.carbon.identity.event.IdentityEventException;
import org.wso2.carbon.identity.event.IdentityEventServerException;
import org.wso2.carbon.identity.event.bean.ModuleConfiguration;
import org.wso2.identity.webhook.common.event.handler.LoginEventHookHandler;
import org.wso2.identity.webhook.common.event.handler.builder.LoginEventPayloadBuilder;
import org.apache.commons.logging.Log;
Expand All @@ -35,7 +38,6 @@
import org.wso2.carbon.identity.event.handler.AbstractEventHandler;
import org.wso2.identity.event.common.publisher.EventPublisherService;
import org.wso2.identity.webhook.common.event.handler.constant.Constants;
import org.wso2.identity.webhook.common.event.handler.util.EventHookHandlerUtils;

/**
* WSO2 Event Handler service component class.
Expand All @@ -52,12 +54,15 @@ protected void activate(ComponentContext context) {

try {
log.debug("Event Handler is activated.");
String isLoginEventHandlerEnabled = EventHookHandlerUtils
.getIdentityEventProperty(Constants.LOGIN_EVENT_HOOK_NAME, Constants.LOGIN_EVENT_HOOK_ENABLED);
String isLoginEventHandlerEnabled = getIdentityEventProperty(Constants.LOGIN_EVENT_HOOK_NAME,
Constants.LOGIN_EVENT_HOOK_ENABLED);
BundleContext bundleContext = context.getBundleContext();
if (isLoginEventHandlerEnabled.equalsIgnoreCase(Boolean.TRUE.toString())) {

if (isLoginEventHandlerEnabled != null && isLoginEventHandlerEnabled.equalsIgnoreCase(Boolean.TRUE.toString())) {
bundleContext.registerService(AbstractEventHandler.class.getName(),
new LoginEventHookHandler(), null);
} else if (isLoginEventHandlerEnabled == null) {
log.error("Login Event Handler enabled property is null.");
} else {
log.error("Login Event Handler is not enabled.");
}
Expand Down Expand Up @@ -126,4 +131,29 @@ protected void unsetEventPublisherService(EventPublisherService eventPublisherSe

EventHookHandlerDataHolder.getInstance().setEventPublisherService(null);
}

/**
* Get the identity property specified in identity-event.properties
*
* @param moduleName The name of the module which the property belongs to
* @param propertyName The name of the property which should be fetched
* @return The required property
*/
private String getIdentityEventProperty(String moduleName, String propertyName) throws IdentityEventServerException {

// Retrieving properties set in identity event properties
String propertyValue = null;
try {
ModuleConfiguration moduleConfiguration = IdentityEventConfigBuilder.getInstance()
.getModuleConfigurations(moduleName);

if (moduleConfiguration != null) {
propertyValue = moduleConfiguration.getModuleProperties().getProperty(propertyName);
}
} catch (IdentityEventException e) {
throw new IdentityEventServerException("An error occurred while retrieving module properties because " +
e.getMessage());
}
return propertyValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,14 @@
import org.wso2.carbon.identity.application.common.model.ClaimMapping;
import org.wso2.carbon.identity.core.ServiceURLBuilder;
import org.wso2.carbon.identity.core.URLBuilderException;
import org.wso2.carbon.identity.event.IdentityEventConfigBuilder;
import org.wso2.carbon.identity.event.IdentityEventException;
import org.wso2.carbon.identity.event.IdentityEventServerException;
import org.wso2.carbon.identity.event.bean.ModuleConfiguration;
import org.wso2.carbon.identity.event.event.Event;
import org.wso2.identity.event.common.publisher.model.EventContext;
import org.wso2.identity.event.common.publisher.model.EventPayload;
import org.wso2.identity.event.common.publisher.model.SecurityEventTokenPayload;
import org.wso2.identity.webhook.common.event.handler.constant.Constants;
import org.wso2.identity.webhook.common.event.handler.internal.EventHookHandlerDataHolder;
import org.wso2.identity.webhook.common.event.handler.model.EventAttribute;
import org.wso2.identity.webhook.common.event.handler.model.EventData;
import org.wso2.identity.webhook.common.event.handler.model.ResourceConfig;

Expand All @@ -68,31 +65,6 @@ public class EventHookHandlerUtils {
private static volatile ResourceConfig eventSchema = null;
private static final Object lock = new Object();

/**
* Get the identity property specified in identity-event.properties
*
* @param moduleName The name of the module which the property belongs to
* @param propertyName The name of the property which should be fetched
* @return The required property
*/
public static String getIdentityEventProperty(String moduleName, String propertyName) throws IdentityEventServerException {

// Retrieving properties set in identity event properties
String propertyValue = null;
try {
ModuleConfiguration moduleConfiguration = IdentityEventConfigBuilder.getInstance()
.getModuleConfigurations(moduleName);

if (moduleConfiguration != null) {
propertyValue = moduleConfiguration.getModuleProperties().getProperty(propertyName);
}
} catch (IdentityEventException e) {
throw new IdentityEventServerException("An error occurred while retrieving module properties because " +
e.getMessage());
}
return propertyValue;
}

/**
* Retrieve event uri.
*
Expand Down Expand Up @@ -123,7 +95,7 @@ public static String getEventUri(String eventKey) throws IdentityEventServerExce
* @return Resource config object.
* @throws IdentityEventServerException If an error occurs.
*/
public static ResourceConfig getEventConfig(String eventName) throws IdentityEventServerException {
private static ResourceConfig getEventConfig(String eventName) throws IdentityEventServerException {

JSONObject eventsConfigObject = (JSONObject) getEventsSchemaResourceFile().getConfigs()
.get(Constants.EVENT_SCHEMA_EVENTS_KEY);
Expand All @@ -136,51 +108,6 @@ public static ResourceConfig getEventConfig(String eventName) throws IdentityEve
}
}

/**
* This method constructs the EventAttribute object from the json string.
*
* @param jsonString JSON string.
* @return EventAttribute object.
*/
public static EventAttribute buildEventAttributeFromJSONString(String jsonString) throws IdentityEventException {

JSONObject eventJSON = getJSONObject(jsonString);
EventAttribute eventAttribute = new EventAttribute();
try {
if (eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY) instanceof Boolean) {
eventAttribute.setPublishEnabled(
(Boolean) eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY));
} else {
eventAttribute.setPublishEnabled(Boolean.parseBoolean(
(String) eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY)));
}
JSONObject propertiesJSON =
(JSONObject) eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PROPERTIES_KEY);
eventAttribute.setProperties(new ResourceConfig(propertiesJSON));

return eventAttribute;
} catch (ClassCastException e) {
throw new IdentityEventException("Error while casting event attribute from JSON string", e);
}
}

/**
* This method converts the parsed JSON String into a JSONObject.
*
* @param jsonString JSON string.
* @return JSON object.
* @throws IdentityEventServerException If an error occurs while constructing the object.
*/
private static JSONObject getJSONObject(String jsonString) throws IdentityEventServerException {

JSONParser jsonParser = new JSONParser();
try {
return (JSONObject) jsonParser.parse(jsonString);
} catch (ParseException | ClassCastException e) {
throw new IdentityEventServerException("Error while parsing JSON string", e);
}
}

/**
* This method reads the event schema resource file and returns the config object.
*
Expand Down

0 comments on commit 0b6f6ee

Please sign in to comment.