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());
}
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;
}
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;
}
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;
}
}
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);
}
}
}
}
Aggregations