Search in sources :

Example 6 with IRuleHelper

use of org.jaffa.rules.rulemeta.IRuleHelper in project jaffa-framework by jaffa-projects.

the class CxfFunctionGuardInterceptorTest method testInterceptor.

@Test
public void testInterceptor() throws NoSuchMethodException, FrameworkException, ApplicationExceptions {
    // Mock the security manager
    PowerMockito.mockStatic(SecurityManager.class);
    PowerMockito.when(SecurityManager.checkFunctionAccess("Test")).thenReturn(true);
    // Mock the Rule helper factory and the rule it returns
    mockStatic(RuleHelperFactory.class);
    IRuleHelper ruleHelperFactory = mock(IRuleHelper.class);
    when(RuleHelperFactory.instance(Mockito.anyString())).thenReturn(ruleHelperFactory);
    // Mock the metadata repository
    mockStatic(MetaDataRepository.class);
    MetaDataRepository repository = mock(MetaDataRepository.class);
    when(MetaDataRepository.instance()).thenReturn(repository);
    // Create a real instance of an interceptor
    CxfFunctionGuardInterceptor interceptor = new CxfFunctionGuardInterceptor();
    // Successful access - matching method and name
    updateRules("Test", "Test", repository, ruleHelperFactory);
    interceptor.handleMessage(getMockMessage());
    // Failed access - matching method not valid name
    Fault fault = null;
    try {
        updateRules("Fail", "Test", repository, ruleHelperFactory);
        interceptor.handleMessage(getMockMessage());
    } catch (Fault f) {
        fault = f;
    }
    Assert.assertNotNull("Fault should have been thrown du to failed access", fault);
    // Successful access - not matching method, so rules not enforced
    updateRules("xxxxx", "NoMatch", repository, ruleHelperFactory);
    interceptor.handleMessage(getMockMessage());
}
Also used : CxfFunctionGuardInterceptor(org.jaffa.rules.interceptors.CxfFunctionGuardInterceptor) IRuleHelper(org.jaffa.rules.rulemeta.IRuleHelper) MetaDataRepository(org.jaffa.rules.meta.MetaDataRepository) Fault(org.apache.cxf.interceptor.Fault) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with IRuleHelper

use of org.jaffa.rules.rulemeta.IRuleHelper in project jaffa-framework by jaffa-projects.

the class MetaDataIntrospector method findPropertyRuleMap.

/**
 * Returns a Map of propertyName and first applicable rule for the className/ruleName combination.
 */
private Map<String, RuleMetaData> findPropertyRuleMap(String className, Object obj, String ruleName) {
    try {
        Map<String, List<RuleMetaData>> ruleMap = MetaDataRepository.instance().getPropertyRuleMap(className, ruleName);
        if (ruleMap != null) {
            Map<String, RuleMetaData> output = new LinkedHashMap<String, RuleMetaData>();
            IRuleHelper ruleHelper = RuleHelperFactory.instance(ruleName);
            for (Map.Entry<String, List<RuleMetaData>> me : ruleMap.entrySet()) {
                List<RuleMetaData> applicableRules = ruleHelper.getApplicableRules(className, obj, me.getValue(), false);
                if (applicableRules != null && applicableRules.size() > 0)
                    output.put(me.getKey(), applicableRules.get(0));
            }
            return output;
        }
    } catch (Exception e) {
        // do nothing
        if (log.isDebugEnabled())
            log.debug("Error in finding the applicable rules of type " + ruleName, e);
    }
    return null;
}
Also used : IRuleHelper(org.jaffa.rules.rulemeta.IRuleHelper) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with IRuleHelper

use of org.jaffa.rules.rulemeta.IRuleHelper in project jaffa-framework by jaffa-projects.

the class DataSecurity method getRules.

/**
 * Gets the data security rules for a class
 *
 * @param targetClassName Name of the class the rule applies to
 * @param targetObject    Object the rules applies to
 * @param ruleName        Name of the rule
 * @return List of rules that apply
 */
private static List<RuleMetaData> getRules(String targetClassName, Object targetObject, String ruleName) throws ApplicationExceptions, FrameworkException {
    if (targetClassName != null) {
        // NOTE: We can probably go up the inheritance hierarchy of the targetObject to determine the appropriate rules
        Map<String, List<RuleMetaData>> map = MetaDataRepository.instance().getPropertyRuleMap(targetClassName, ruleName);
        if (map != null) {
            IRuleHelper ruleHelper = RuleHelperFactory.instance(ruleName);
            Map<String, List<RuleMetaData>> newMap = null;
            for (Map.Entry<String, List<RuleMetaData>> me : map.entrySet()) {
                List<RuleMetaData> rules = ruleHelper.getApplicableRules(targetClassName, targetObject, me.getValue(), true);
                if (rules != null && rules.size() > 0) {
                    if (newMap == null)
                        newMap = new LinkedHashMap<String, List<RuleMetaData>>();
                    newMap.put(me.getKey(), rules);
                }
            }
            map = newMap;
            return map.get(null);
        }
    }
    return Collections.EMPTY_LIST;
}
Also used : IRuleHelper(org.jaffa.rules.rulemeta.IRuleHelper) List(java.util.List) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with IRuleHelper

use of org.jaffa.rules.rulemeta.IRuleHelper in project jaffa-framework by jaffa-projects.

the class RuleActor method getPropertyRuleMap.

/**
 * Returns a Map containing a List of RuleMetaData instances per propertyName for the className+ruleName combination.
 * The class-level RuleMetaData instances defined for the className+ruleName combination will be added to the Map with propertyName null.
 * The className is obtained from the targetClass.
 *
 * @param targetClassName The target Class.
 * @param targetObject    The target Object.
 * @param ruleName        the rule to search for.
 * @return a Map containing a List of RuleMetaData instances per propertyName for the className+ruleName combination.
 * @throws ApplicationExceptions if any application exception occurs.
 * @throws FrameworkException    if any internal error occurs.
 */
protected Map<String, List<RuleMetaData>> getPropertyRuleMap(String targetClassName, Object targetObject, String ruleName) throws ApplicationExceptions, FrameworkException {
    if (targetClassName != null) {
        Map<String, List<RuleMetaData>> map = MetaDataRepository.instance().getPropertyRuleMap(targetClassName, ruleName);
        if (map != null) {
            IRuleHelper ruleHelper = RuleHelperFactory.instance(ruleName);
            Map<String, List<RuleMetaData>> newMap = null;
            for (Map.Entry<String, List<RuleMetaData>> me : map.entrySet()) {
                List<RuleMetaData> rules = ruleHelper.getApplicableRules(targetClassName, targetObject, me.getValue(), true);
                if (rules != null && rules.size() > 0) {
                    if (newMap == null) {
                        newMap = new LinkedHashMap<>();
                    }
                    newMap.put(me.getKey(), rules);
                }
            }
            map = newMap;
        }
        return map;
    } else {
        return null;
    }
}
Also used : IRuleHelper(org.jaffa.rules.rulemeta.IRuleHelper) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Example 10 with IRuleHelper

use of org.jaffa.rules.rulemeta.IRuleHelper in project jaffa-framework by jaffa-projects.

the class SOAEventBaseHandler method raiseSOAEvent.

/**
 * Check if the method being invoked on the handler is the trigger method defined in the rule meta data then raise the event
 *
 * @param invocationMethod method being invoked
 * @param target           the target object
 * @param uow              unit of work for the life cycle event handler
 * @throws ApplicationExceptions
 * @throws FrameworkException
 */
protected void raiseSOAEvent(String invocationMethod, Object target, UOW uow, Object[] args) throws ApplicationExceptions, FrameworkException {
    if (log.isDebugEnabled()) {
        log.debug("Handle Event : " + invocationMethod + "  for " + " (Target=" + shortClassName(target) + ")");
    }
    if (invocationMethod.equals(ruleMetaData.getParameter(RuleMetaData.PARAMETER_TRIGGER))) {
        // Need to check the condition
        List<RuleMetaData> rules = new ArrayList<>();
        rules.add(ruleMetaData);
        IRuleHelper ruleHelper = RuleHelperFactory.instance(ruleMetaData.getName());
        // Check if the rule is applicable for the target object (evaluates rule condition)
        List<RuleMetaData> applicableRules = ruleHelper.getApplicableRules(target.getClass().getName(), target, rules, true);
        // raise event for each applicableRule
        if (applicableRules != null) {
            for (RuleMetaData rule : applicableRules) {
                raiseSOAEvent(uow, target, rule, args);
            }
        }
    }
}
Also used : IRuleHelper(org.jaffa.rules.rulemeta.IRuleHelper) ArrayList(java.util.ArrayList) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Aggregations

IRuleHelper (org.jaffa.rules.rulemeta.IRuleHelper)10 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)6 Map (java.util.Map)6 RuleMetaData (org.jaffa.rules.meta.RuleMetaData)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Fault (org.apache.cxf.interceptor.Fault)1 CxfFunctionGuardInterceptor (org.jaffa.rules.interceptors.CxfFunctionGuardInterceptor)1 MetaDataRepository (org.jaffa.rules.meta.MetaDataRepository)1 AuditRuleHelper (org.jaffa.rules.rulemeta.AuditRuleHelper)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1