Search in sources :

Example 21 with Resource

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resource in project carbon-identity-framework by wso2.

the class PAPPolicyStoreReader method readMetaDataPolicyDTO.

/**
 * Reads Light Weight PolicyDTO with Attribute meta data for given policy id
 *
 * @param policyId policy id
 * @return PolicyDTO but don not contains XACML policy
 * @throws EntitlementException throws, if fails
 */
public PolicyDTO readMetaDataPolicyDTO(String policyId) throws EntitlementException {
    Resource resource = null;
    PolicyDTO dto = null;
    resource = store.getPolicy(policyId, PDPConstants.ENTITLEMENT_POLICY_PAP);
    if (resource == null) {
        return null;
    }
    dto = new PolicyDTO();
    dto.setPolicyId(policyId);
    dto.setActive(Boolean.parseBoolean(resource.getProperty(PDPConstants.ACTIVE_POLICY)));
    String policyOrder = resource.getProperty(PDPConstants.POLICY_ORDER);
    if (policyOrder != null) {
        dto.setPolicyOrder(Integer.parseInt(policyOrder));
    } else {
        dto.setPolicyOrder(0);
    }
    String version = resource.getProperty(PDPConstants.POLICY_VERSION);
    if (version != null) {
        dto.setVersion(version);
    }
    String lastModifiedTime = resource.getProperty(PDPConstants.LAST_MODIFIED_TIME);
    if (lastModifiedTime != null) {
        dto.setLastModifiedTime(lastModifiedTime);
    }
    String lastModifiedUser = resource.getProperty(PDPConstants.LAST_MODIFIED_USER);
    if (lastModifiedUser != null) {
        dto.setLastModifiedUser(lastModifiedUser);
    }
    dto.setPolicyType(resource.getProperty(PDPConstants.POLICY_TYPE));
    String policyReferences = resource.getProperty(PDPConstants.POLICY_REFERENCE);
    if (policyReferences != null && policyReferences.trim().length() > 0) {
        dto.setPolicyIdReferences(policyReferences.split(PDPConstants.ATTRIBUTE_SEPARATOR));
    }
    String policySetReferences = resource.getProperty(PDPConstants.POLICY_SET_REFERENCE);
    if (policySetReferences != null && policySetReferences.trim().length() > 0) {
        dto.setPolicySetIdReferences(policySetReferences.split(PDPConstants.ATTRIBUTE_SEPARATOR));
    }
    dto.setPolicyEditor(resource.getProperty(PDPConstants.POLICY_EDITOR_TYPE));
    String basicPolicyEditorMetaDataAmount = resource.getProperty(PDPConstants.BASIC_POLICY_EDITOR_META_DATA_AMOUNT);
    if (basicPolicyEditorMetaDataAmount != null) {
        int amount = Integer.parseInt(basicPolicyEditorMetaDataAmount);
        String[] basicPolicyEditorMetaData = new String[amount];
        for (int i = 0; i < amount; i++) {
            basicPolicyEditorMetaData[i] = resource.getProperty(PDPConstants.BASIC_POLICY_EDITOR_META_DATA + i);
        }
        dto.setPolicyEditorData(basicPolicyEditorMetaData);
    }
    PolicyAttributeBuilder policyAttributeBuilder = new PolicyAttributeBuilder();
    dto.setAttributeDTOs(policyAttributeBuilder.getPolicyMetaDataFromRegistryProperties(resource.getProperties()));
    return dto;
}
Also used : PolicyDTO(org.wso2.carbon.identity.entitlement.dto.PolicyDTO) PolicyAttributeBuilder(org.wso2.carbon.identity.entitlement.policy.PolicyAttributeBuilder) Resource(org.wso2.carbon.registry.core.Resource)

Example 22 with Resource

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resource 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 23 with Resource

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resource in project carbon-identity-framework by wso2.

the class CarbonEntitlementDataFinder method getChildResources.

/**
 * This helps to find resources un a recursive manner
 *
 * @param node           attribute value node
 * @param parentResource parent resource Name
 * @return child resource set
 * @throws RegistryException throws
 */
private EntitlementTreeNodeDTO getChildResources(EntitlementTreeNodeDTO node, String parentResource) throws RegistryException {
    if (registry.resourceExists(parentResource)) {
        String[] resourcePath = parentResource.split("/");
        EntitlementTreeNodeDTO childNode = new EntitlementTreeNodeDTO(resourcePath[resourcePath.length - 1]);
        node.addChildNode(childNode);
        Resource root = registry.get(parentResource);
        if (root instanceof Collection) {
            Collection collection = (Collection) root;
            String[] resources = collection.getChildren();
            for (String resource : resources) {
                getChildResources(childNode, resource);
            }
        }
    }
    return node;
}
Also used : EntitlementTreeNodeDTO(org.wso2.carbon.identity.entitlement.dto.EntitlementTreeNodeDTO) Resource(org.wso2.carbon.registry.api.Resource) Collection(org.wso2.carbon.registry.core.Collection)

Example 24 with Resource

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resource in project carbon-identity-framework by wso2.

the class RegistryPolicyReader method readPolicy.

/**
 * Reads given policy resource as PolicyDTO
 *
 * @param policyId policy id
 * @return PolicyDTO
 * @throws EntitlementException throws, if fails
 */
public PolicyDTO readPolicy(String policyId) throws EntitlementException {
    Resource resource = null;
    resource = getPolicyResource(policyId);
    if (resource == null) {
        return new PolicyDTO();
    }
    return readPolicy(resource);
}
Also used : PolicyDTO(org.wso2.carbon.identity.entitlement.dto.PolicyDTO) Resource(org.wso2.carbon.registry.core.Resource)

Example 25 with Resource

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resource in project carbon-identity-framework by wso2.

the class RegistryPolicyReader method readPolicy.

/**
 * Reads PolicyDTO for given registry resource
 *
 * @param resource Registry resource
 * @return PolicyDTO
 * @throws EntitlementException throws, if fails
 */
private PolicyDTO readPolicy(Resource resource) throws EntitlementException {
    String policy = null;
    AbstractPolicy absPolicy = null;
    PolicyDTO dto = null;
    try {
        if (resource.getContent() == null) {
            throw new EntitlementException("Error while loading entitlement policy. Policy content is null");
        }
        policy = new String((byte[]) resource.getContent(), Charset.forName("UTF-8"));
        absPolicy = PAPPolicyReader.getInstance(null).getPolicy(policy);
        dto = new PolicyDTO();
        dto.setPolicyId(absPolicy.getId().toASCIIString());
        dto.setPolicy(policy);
        String policyOrder = resource.getProperty("order");
        if (policyOrder != null) {
            dto.setPolicyOrder(Integer.parseInt(policyOrder));
        } else {
            dto.setPolicyOrder(0);
        }
        String policyActive = resource.getProperty("active");
        if (policyActive != null) {
            dto.setActive(Boolean.parseBoolean(policyActive));
        }
        PolicyAttributeBuilder policyAttributeBuilder = new PolicyAttributeBuilder();
        dto.setAttributeDTOs(policyAttributeBuilder.getPolicyMetaDataFromRegistryProperties(resource.getProperties()));
        return dto;
    } catch (RegistryException e) {
        log.error("Error while loading entitlement policy", e);
        throw new EntitlementException("Error while loading entitlement policy", e);
    }
}
Also used : EntitlementException(org.wso2.carbon.identity.entitlement.EntitlementException) PolicyDTO(org.wso2.carbon.identity.entitlement.dto.PolicyDTO) AbstractPolicy(org.wso2.balana.AbstractPolicy) PolicyAttributeBuilder(org.wso2.carbon.identity.entitlement.policy.PolicyAttributeBuilder) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

Resource (org.wso2.carbon.registry.core.Resource)341 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)267 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)224 HashMap (java.util.HashMap)196 ArrayList (java.util.ArrayList)164 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)153 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)142 Test (org.testng.annotations.Test)130 Registry (org.wso2.carbon.registry.core.Registry)125 Test (org.junit.Test)115 IOException (java.io.IOException)113 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)111 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)109 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)90 Map (java.util.Map)86 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)86 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)79 InputStream (java.io.InputStream)78 Collection (org.wso2.carbon.registry.core.Collection)77 API (org.wso2.carbon.apimgt.api.model.API)73