Search in sources :

Example 1 with PIPResourceFinder

use of org.wso2.carbon.identity.entitlement.pip.PIPResourceFinder in project carbon-identity-framework by wso2.

the class CarbonResourceFinder method findChildResources.

@Override
public ResourceFinderResult findChildResources(AttributeValue parentResourceId, EvaluationCtx context) {
    ResourceFinderResult resourceFinderResult = null;
    Set<AttributeValue> resources = null;
    String dataType = parentResourceId.getType().toString();
    for (PIPResourceFinder finder : resourceFinders) {
        try {
            Set<String> resourceNames = null;
            if (isResourceCachingEnabled && !finder.overrideDefaultCache()) {
                IdentityCacheKey cacheKey = null;
                String key = PDPConstants.RESOURCE_CHILDREN + parentResourceId.encode() + domToString(context.getRequestRoot());
                cacheKey = new IdentityCacheKey(tenantId, key);
                IdentityCacheEntry cacheEntry = (IdentityCacheEntry) resourceCache.getValueFromCache(cacheKey);
                if (cacheEntry != null) {
                    String cacheEntryString = cacheEntry.getCacheEntry();
                    String[] attributes = cacheEntryString.split(PDPConstants.ATTRIBUTE_SEPARATOR);
                    if (attributes != null && attributes.length > 0) {
                        List<String> list = Arrays.asList(attributes);
                        resourceNames = new HashSet<String>(list);
                    }
                    if (log.isDebugEnabled()) {
                        log.debug("Carbon Resource Cache Hit");
                    }
                } else {
                    resourceNames = finder.findChildResources(parentResourceId.encode(), context);
                    if (log.isDebugEnabled()) {
                        log.debug("Carbon Resource Cache Miss");
                    }
                    String cacheEntryString = "";
                    if (resourceNames != null && resourceNames.size() > 0) {
                        for (String attribute : resourceNames) {
                            if (cacheEntryString.equals("")) {
                                cacheEntryString = attribute;
                            } else {
                                cacheEntryString = cacheEntryString + PDPConstants.ATTRIBUTE_SEPARATOR + attribute;
                            }
                        }
                    }
                    cacheEntry = new IdentityCacheEntry(cacheEntryString);
                    resourceCache.addToCache(cacheKey, cacheEntry);
                }
            } else {
                resourceNames = finder.findChildResources(parentResourceId.encode(), context);
            }
            if (resourceNames != null && !resourceNames.isEmpty()) {
                resources = new HashSet<AttributeValue>();
                for (String resourceName : resourceNames) {
                    resources.add(EntitlementUtil.getAttributeValue(resourceName, dataType));
                }
            }
        } catch (EntitlementException e) {
            log.error("Error while finding child resources", e);
        } catch (TransformerException e) {
            log.error("Error while finding child resources", e);
        } catch (Exception e) {
            log.error("Error while finding child resources", e);
        }
    }
    if (resources != null) {
        resourceFinderResult = new ResourceFinderResult(resources);
    } else {
        resourceFinderResult = new ResourceFinderResult();
    }
    return resourceFinderResult;
}
Also used : AttributeValue(org.wso2.balana.attr.AttributeValue) TransformerException(javax.xml.transform.TransformerException) EntitlementException(org.wso2.carbon.identity.entitlement.EntitlementException) IdentityCacheKey(org.wso2.carbon.identity.entitlement.cache.IdentityCacheKey) EntitlementException(org.wso2.carbon.identity.entitlement.EntitlementException) IdentityCacheEntry(org.wso2.carbon.identity.entitlement.cache.IdentityCacheEntry) ResourceFinderResult(org.wso2.balana.finder.ResourceFinderResult) TransformerException(javax.xml.transform.TransformerException)

Example 2 with PIPResourceFinder

use of org.wso2.carbon.identity.entitlement.pip.PIPResourceFinder in project carbon-identity-framework by wso2.

the class CarbonResourceFinder method findDescendantResources.

@Override
public ResourceFinderResult findDescendantResources(AttributeValue parentResourceId, EvaluationCtx context) {
    ResourceFinderResult resourceFinderResult = null;
    Set<AttributeValue> resources = null;
    String dataType = parentResourceId.getType().toString();
    for (PIPResourceFinder finder : resourceFinders) {
        try {
            Set<String> resourceNames = null;
            if (isResourceCachingEnabled && !finder.overrideDefaultCache()) {
                IdentityCacheKey cacheKey = null;
                String key = PDPConstants.RESOURCE_DESCENDANTS + parentResourceId.encode() + domToString(context.getRequestRoot());
                cacheKey = new IdentityCacheKey(tenantId, key);
                IdentityCacheEntry cacheEntry = (IdentityCacheEntry) resourceCache.getValueFromCache(cacheKey);
                if (cacheEntry != null) {
                    String[] values = cacheEntry.getCacheEntryArray();
                    resourceNames = new HashSet<String>(Arrays.asList(values));
                    if (log.isDebugEnabled()) {
                        log.debug("Carbon Resource Cache Hit");
                    }
                }
                if (resourceNames != null) {
                    resourceNames = finder.findDescendantResources(parentResourceId.encode(), context);
                    if (log.isDebugEnabled()) {
                        log.debug("Carbon Resource Cache Miss");
                    }
                    cacheEntry = new IdentityCacheEntry(resourceNames.toArray(new String[resourceNames.size()]));
                    resourceCache.addToCache(cacheKey, cacheEntry);
                }
            } else {
                resourceNames = finder.findDescendantResources(parentResourceId.encode(), context);
            }
            if (resourceNames != null && !resourceNames.isEmpty()) {
                resources = new HashSet<AttributeValue>();
                for (String resourceName : resourceNames) {
                    resources.add(EntitlementUtil.getAttributeValue(resourceName, dataType));
                }
            }
        } catch (EntitlementException e) {
            log.error("Error while finding descendant resources", e);
        } catch (TransformerException e) {
            log.error("Error while finding descendant resources", e);
        } catch (Exception e) {
            log.error("Error while finding descendant resources", e);
        }
    }
    if (resources != null) {
        resourceFinderResult = new ResourceFinderResult(resources);
    } else {
        resourceFinderResult = new ResourceFinderResult();
    }
    return resourceFinderResult;
}
Also used : AttributeValue(org.wso2.balana.attr.AttributeValue) TransformerException(javax.xml.transform.TransformerException) EntitlementException(org.wso2.carbon.identity.entitlement.EntitlementException) IdentityCacheKey(org.wso2.carbon.identity.entitlement.cache.IdentityCacheKey) EntitlementException(org.wso2.carbon.identity.entitlement.EntitlementException) IdentityCacheEntry(org.wso2.carbon.identity.entitlement.cache.IdentityCacheEntry) ResourceFinderResult(org.wso2.balana.finder.ResourceFinderResult) TransformerException(javax.xml.transform.TransformerException)

Example 3 with PIPResourceFinder

use of org.wso2.carbon.identity.entitlement.pip.PIPResourceFinder in project carbon-identity-framework by wso2.

the class EntitlementExtensionBuilder method populateResourceFinders.

/**
 * @param properties
 * @param holder
 * @throws Exception
 */
private void populateResourceFinders(Properties properties, EntitlementConfigHolder holder) throws Exception {
    int i = 1;
    PIPResourceFinder resource = null;
    while (properties.getProperty("PIP.ResourceFinders.Finder." + i) != null) {
        String className = properties.getProperty("PIP.ResourceFinders.Finder." + i++);
        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        resource = (PIPResourceFinder) clazz.newInstance();
        int j = 1;
        Properties resourceProps = new Properties();
        while (properties.getProperty(className + "." + j) != null) {
            String[] props = properties.getProperty(className + "." + j++).split(",");
            resourceProps.put(props[0], props[1]);
        }
        resource.init(resourceProps);
        holder.addResourceFinders(resource, resourceProps);
    }
}
Also used : Properties(java.util.Properties) PIPResourceFinder(org.wso2.carbon.identity.entitlement.pip.PIPResourceFinder)

Example 4 with PIPResourceFinder

use of org.wso2.carbon.identity.entitlement.pip.PIPResourceFinder in project carbon-identity-framework by wso2.

the class EntitlementAdminService method refreshResourceFinder.

/**
 * Refreshes the supported resource id of a given resource finder module
 *
 * @param resourceFinder Canonical name of the resource finder class.
 * @throws EntitlementException throws if fails to  refresh
 */
public void refreshResourceFinder(String resourceFinder) throws EntitlementException {
    Map<PIPResourceFinder, Properties> resourceFinders = EntitlementServiceComponent.getEntitlementConfig().getResourceFinders();
    if (resourceFinder != null && resourceFinders != null && !resourceFinders.isEmpty()) {
        for (Map.Entry<PIPResourceFinder, Properties> entry : resourceFinders.entrySet()) {
            if (resourceFinder.equals(entry.getKey().getClass().getName()) || resourceFinder.equals(entry.getKey().getModuleName())) {
                try {
                    entry.getKey().init(entry.getValue());
                    entry.getKey().clearCache();
                    CarbonAttributeFinder carbonAttributeFinder = EntitlementEngine.getInstance().getCarbonAttributeFinder();
                    carbonAttributeFinder.init();
                } catch (Exception e) {
                    throw new EntitlementException("Error while refreshing attribute finder - " + resourceFinder);
                }
                break;
            }
        }
    }
}
Also used : CarbonAttributeFinder(org.wso2.carbon.identity.entitlement.pip.CarbonAttributeFinder) Properties(java.util.Properties) Map(java.util.Map) PIPResourceFinder(org.wso2.carbon.identity.entitlement.pip.PIPResourceFinder)

Example 5 with PIPResourceFinder

use of org.wso2.carbon.identity.entitlement.pip.PIPResourceFinder in project carbon-identity-framework by wso2.

the class EntitlementAdminService method clearCarbonResourceCache.

/**
 * Clears the carbon resource cache
 *
 * @throws EntitlementException throws
 */
public void clearCarbonResourceCache() throws EntitlementException {
    CarbonResourceFinder finder = EntitlementEngine.getInstance().getCarbonResourceFinder();
    if (finder != null) {
        finder.clearAttributeCache();
        // we need invalidate decision cache as well.
        clearDecisionCache();
    } else {
        throw new EntitlementException("Can not clear attribute cache - Carbon Attribute Finder " + "is not initialized");
    }
    Map<PIPResourceFinder, Properties> resourceConfigs = EntitlementServiceComponent.getEntitlementConfig().getResourceFinders();
    if (resourceConfigs != null && !resourceConfigs.isEmpty()) {
        Set<PIPResourceFinder> resourceFinders = resourceConfigs.keySet();
        for (PIPResourceFinder pipResourceFinder : resourceFinders) {
            pipResourceFinder.clearCache();
        }
    }
}
Also used : CarbonResourceFinder(org.wso2.carbon.identity.entitlement.pip.CarbonResourceFinder) Properties(java.util.Properties) PIPResourceFinder(org.wso2.carbon.identity.entitlement.pip.PIPResourceFinder)

Aggregations

Properties (java.util.Properties)5 PIPResourceFinder (org.wso2.carbon.identity.entitlement.pip.PIPResourceFinder)5 Map (java.util.Map)3 TransformerException (javax.xml.transform.TransformerException)2 AttributeValue (org.wso2.balana.attr.AttributeValue)2 ResourceFinderResult (org.wso2.balana.finder.ResourceFinderResult)2 EntitlementException (org.wso2.carbon.identity.entitlement.EntitlementException)2 IdentityCacheEntry (org.wso2.carbon.identity.entitlement.cache.IdentityCacheEntry)2 IdentityCacheKey (org.wso2.carbon.identity.entitlement.cache.IdentityCacheKey)2 ArrayList (java.util.ArrayList)1 PDPDataHolder (org.wso2.carbon.identity.entitlement.dto.PDPDataHolder)1 PIPFinderDataHolder (org.wso2.carbon.identity.entitlement.dto.PIPFinderDataHolder)1 AbstractPIPAttributeFinder (org.wso2.carbon.identity.entitlement.pip.AbstractPIPAttributeFinder)1 CarbonAttributeFinder (org.wso2.carbon.identity.entitlement.pip.CarbonAttributeFinder)1 CarbonResourceFinder (org.wso2.carbon.identity.entitlement.pip.CarbonResourceFinder)1 PIPAttributeFinder (org.wso2.carbon.identity.entitlement.pip.PIPAttributeFinder)1 PolicyFinderModule (org.wso2.carbon.identity.entitlement.policy.finder.PolicyFinderModule)1