Package org.wso2.carbon.identity.entitlement.policy

Source Code of org.wso2.carbon.identity.entitlement.policy.PolicyStoreReader

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.identity.entitlement.policy;

import com.sun.xacml.AbstractPolicy;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.entitlement.dto.PolicyDTO;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;

public class PolicyStoreReader {

    /**
     * The property which is used to specify the schema file to validate against (if any). Note that
     * this isn't used directly by <code>PolicyReader</code>, but is referenced by many classes that
     * use this class to load policies.
     */
    public static final String POLICY_SCHEMA_PROPERTY = "com.sun.xacml.PolicySchema";

    // the optional logger used for error reporting
    private static Log log = LogFactory.getLog(PolicyStoreReader.class);

    private PolicyStore store;

    /**
     *
     * @param store
     */
    public PolicyStoreReader(PolicyStore store) {
        this.store = store;
    }

    /**
     *
     * @param policyId
     * @return
     * @throws IdentityException
     */
    public synchronized AbstractPolicy readPolicy(String policyId) throws IdentityException {
        Resource resource = null;
        resource = store.getPolicy(policyId);
        return readPolicy(resource);
    }

    /**
     *
     * @return
     * @throws IdentityException
     */
    public synchronized AbstractPolicy[] readPolicies() throws IdentityException {
        Resource[] resources = null;
        AbstractPolicy[] policies = null;
        resources = store.getActivePolicies();

        if (resources == null) {
            return new AbstractPolicy[0];
        }
        policies = new AbstractPolicy[resources.length];

        for (int i = 0; i < resources.length; i++) {
            policies[i] = readPolicy(resources[i]);
        }

        return policies;
    }

    /**
     *
     * @param resource
     * @return
     * @throws IdentityException
     */
    private AbstractPolicy readPolicy(Resource resource) throws IdentityException {
        String policy = null;
        try {
            policy = new String((byte[]) resource.getContent());
            return PolicyReader.getInstance(null, null).getPolicy(policy);
        } catch (RegistryException e) {
            log.error("Error while loading entitilement policy", e);
            throw new IdentityException("Error while loading entitilement policy", e);
        }
    }

    /**
     *
     * @return
     * @throws IdentityException
     */
    public PolicyDTO[] readAllPolicyDTOs() throws IdentityException {
        Resource[] resources = null;
        PolicyDTO[] policies = null;
        resources = store.getAllPolicies();

        if (resources == null) {
            return new PolicyDTO[0];
        }
        policies = new PolicyDTO[resources.length];

        for (int i = 0; i < resources.length; i++) {
            policies[i] = readPolicyDTO(resources[i]);
        }

        return policies;
    }

    /**
     *
     * @param policyId
     * @return
     * @throws IdentityException
     */
    public PolicyDTO readPolicyDTO(String policyId) throws IdentityException {
        Resource resource = null;
        PolicyDTO dto = null;
        try {
            resource = store.getPolicy(policyId);
            if (resource == null) {
                return null;
            }
            dto = new PolicyDTO();
            dto.setPolicyId(policyId);
            dto.setPolicy(new String((byte[]) resource.getContent()));
            if ("true".equals(resource.getProperty("isActive"))) {
                dto.setActive(true);
            }
            dto.setPolicyType(resource.getProperty("policyType"));
            //read policy meta data that is used for basic policy editor
            dto.setPolicyEditor(resource.getProperty("policyEditor"));
            String policyMetaDataAmount = resource.getProperty("policyData");
            if(policyMetaDataAmount != null){
                int amount = Integer.parseInt(policyMetaDataAmount);
                String[] policyData = new String[amount];
                for(int i = 0; i < amount; i++){
                    policyData[i] = resource.getProperty("policyData" + i);
                }
                dto.setPolicyMetaData(policyData);
            }          
            return dto;
        } catch (RegistryException e) {
            log.error("Error while loading entitlement policy", e);
            throw new IdentityException("Error while loading entitlement policy", e);
        }
    }

    /**
     *
     * @param resource
     * @return
     * @throws IdentityException
     */
    private PolicyDTO readPolicyDTO(Resource resource) throws IdentityException {
        String policy = null;
        AbstractPolicy absPolicy = null;
        PolicyDTO dto = null;
        try {
            policy = new String((byte[]) resource.getContent());
            absPolicy = PolicyReader.getInstance(null, null).getPolicy(policy);
            dto = new PolicyDTO();
            dto.setPolicyId(absPolicy.getId().toASCIIString());
            dto.setPolicy(policy);
            if ("true".equals(resource.getProperty("isActive"))) {
                dto.setActive(true);
            }
            dto.setPolicyType(resource.getProperty("policyType"));
            //read policy meta data that is used for basic policy editor
            dto.setPolicyEditor(resource.getProperty("policyEditor"));           
            String policyMetaDataAmount = resource.getProperty("policyData");
            if(policyMetaDataAmount != null){
                int amount = Integer.parseInt(policyMetaDataAmount);
                String[] policyData = new String[amount];
                for(int i = 0; i < amount; i++){
                    policyData[i] = resource.getProperty("policyData" + i);
                }
                dto.setPolicyMetaData(policyData);
            }
            return dto;
           
        } catch (RegistryException e) {
            log.error("Error while loading entitlement policy", e);
            throw new IdentityException("Error while loading entitlement policy", e);
        }
    }

    /**
     * This reads the policy combining algorithm from registry resource property
     * @return policy combining algorithm as String
     * @throws IdentityException throws
     */
    public String readPolicyCombiningAlgorithm() throws IdentityException {

        Resource resource = null;
        try {
            resource = store.getEntitlementPolicyResources("globalPolicyCombiningAlgorithm");
            if(resource != null){
                return resource.getProperty("globalPolicyCombiningAlgorithm") ;
            }
        } catch (IdentityException e) {
            log.error("Error while loading policy combining algorithm", e);
            throw new IdentityException("Error while loading policy combining algorithm", e);
        }

        return null;
    }   
}
TOP

Related Classes of org.wso2.carbon.identity.entitlement.policy.PolicyStoreReader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.