diff --git a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/LoginEventHookHandler.java b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/LoginEventHookHandler.java index 673864f..bca8388 100644 --- a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/LoginEventHookHandler.java +++ b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/LoginEventHookHandler.java @@ -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; @@ -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()); } } } @@ -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); + } + } } diff --git a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/internal/EventHookHandlerServiceComponent.java b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/internal/EventHookHandlerServiceComponent.java index 3ec4977..4828c0e 100644 --- a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/internal/EventHookHandlerServiceComponent.java +++ b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/internal/EventHookHandlerServiceComponent.java @@ -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; @@ -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. @@ -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."); } @@ -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; + } } diff --git a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/util/EventHookHandlerUtils.java b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/util/EventHookHandlerUtils.java index f8e4bae..21da7b3 100644 --- a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/util/EventHookHandlerUtils.java +++ b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/util/EventHookHandlerUtils.java @@ -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; @@ -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. * @@ -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); @@ -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. *