Java Code Examples for org.wso2.carbon.registry.core.Resource#getProperty()

The following examples show how to use org.wso2.carbon.registry.core.Resource#getProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RegistryDataManager.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Encrypt the registry properties by new algorithm and update
 *
 * @param registry
 * @param resource
 * @param properties
 * @throws RegistryException
 * @throws CryptoException
 */
private void updateRegistryProperties(Registry registry, String resource, List<String> properties)
        throws RegistryException, CryptoException {
    if (registry == null || StringUtils.isEmpty(resource) || CollectionUtils.isEmpty(properties)) {
        return;
    }
    if (registry.resourceExists(resource)) {
        try {
            registry.beginTransaction();
            Resource resourceObj = registry.get(resource);
            for (String encryptedPropertyName : properties) {
                String oldValue = resourceObj.getProperty(encryptedPropertyName);
                String newValue = Utility.getNewEncryptedValue(oldValue);
                if (StringUtils.isNotEmpty(newValue)) {
                    resourceObj.setProperty(encryptedPropertyName, newValue);
                }
            }
            registry.put(resource, resourceObj);
            registry.commitTransaction();
        } catch (RegistryException e) {
            registry.rollbackTransaction();
            log.error("Unable to update the registry resource", e);
            throw e;
        }
    }
}
 
Example 2
Source File: SecurityUIUtil.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
public static String getUrl() throws Exception {

        if (url == null) {
            ServiceHolder serviceHodler = ServiceHolder.getInstance();
            RegistryService regService = serviceHodler.getRegistryService();
            Registry systemRegistry = regService.getConfigSystemRegistry();
            Resource resource = systemRegistry.get("/carbon/connection/props");
            String servicePath = resource.getProperty("service-path");
            String contextRoot = resource.getProperty("context-root");

            String host = resource.getProperty("host-name");
            contextRoot = StringUtils.equals("/", contextRoot) ? "" : contextRoot;

            host = (host == null) ? "localhost" : host;
            String port = System.getProperty("carbon.https.port");
            StringBuilder urlValue = new StringBuilder();
            url = (urlValue.append("https://").append(host).append(":").append(port).append("/").append(contextRoot).append(servicePath).append("/")).toString();
        }

        return url;
    }
 
Example 3
Source File: STSAdminServiceImpl.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
private void removeTrustedService(String groupName, String serviceName, String trustedService)
        throws SecurityConfigException {
    Registry registry;
    String resourcePath;
    Resource resource;
    try {
        resourcePath = RegistryResources.SERVICE_GROUPS + groupName
                + RegistryResources.SERVICES + serviceName + "/trustedServices";
        registry = getConfigSystemRegistry(); //TODO: Multitenancy
        if (registry != null && registry.resourceExists(resourcePath)) {
            resource = registry.get(resourcePath);
            if (resource.getProperty(trustedService) != null) {
                resource.removeProperty(trustedService);
            }
            registry.put(resourcePath, resource);
        }
    } catch (Exception e) {
        log.error("Error occured while removing trusted service for STS", e);
        throw new SecurityConfigException("Error occured while adding trusted service for STS",
                e);
    }
}
 
Example 4
Source File: SecurityUIUtil.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public static String getUrl() throws Exception {

        if (url == null) {
            ServiceHolder serviceHodler = ServiceHolder.getInstance();
            RegistryService regService = serviceHodler.getRegistryService();
            Registry systemRegistry = regService.getConfigSystemRegistry();
            Resource resource = systemRegistry.get("/carbon/connection/props");
            String servicePath = resource.getProperty("service-path");
            String contextRoot = resource.getProperty("context-root");

            String host = resource.getProperty("host-name");
            contextRoot = StringUtils.equals("/", contextRoot) ? "" : contextRoot;

            host = (host == null) ? "localhost" : host;
            String port = System.getProperty("carbon.https.port");
            StringBuilder urlValue = new StringBuilder();
            url = (urlValue.append("https://").append(host).append(":").append(port).append("/")
                    .append(contextRoot).append(servicePath).append("/")).toString();
        }

        return url;
    }
 
Example 5
Source File: CloudServicesUtil.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
public static boolean isCloudServiceActive(String cloudServiceName,
                                           int tenantId, UserRegistry govRegistry)
                                                                                  throws Exception {
    // The cloud manager is always active
    if (StratosConstants.CLOUD_MANAGER_SERVICE.equals(cloudServiceName)) {
        return true;
    }

    String cloudServiceInfoPath = StratosConstants.CLOUD_SERVICE_INFO_STORE_PATH +
                                  RegistryConstants.PATH_SEPARATOR + tenantId +
                                  RegistryConstants.PATH_SEPARATOR + cloudServiceName;
    Resource cloudServiceInfoResource;
    if (govRegistry.resourceExists(cloudServiceInfoPath)) {
        cloudServiceInfoResource = govRegistry.get(cloudServiceInfoPath);
        String isActiveStr =
                             cloudServiceInfoResource.getProperty(
                                                     StratosConstants.CLOUD_SERVICE_IS_ACTIVE_PROP_KEY);
        return "true".equals(isActiveStr);
    }
    return false;
}
 
Example 6
Source File: DocumentIndexer.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch api status and access control details to document artifacts
 *
 * @param registry
 * @param documentResource
 * @param fields
 * @throws RegistryException
 * @throws APIManagementException
 */
private void fetchRequiredDetailsFromAssociatedAPI(Registry registry, Resource documentResource,
        Map<String, List<String>> fields) throws RegistryException, APIManagementException {
    Association apiAssociations[] = registry
            .getAssociations(documentResource.getPath(), APIConstants.DOCUMENTATION_ASSOCIATION);

    //a document can have one api association
    Association apiAssociation = apiAssociations[0];
    String apiPath = apiAssociation.getSourcePath();

    if (registry.resourceExists(apiPath)) {
        Resource apiResource = registry.get(apiPath);
        GenericArtifactManager apiArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiResource.getUUID());
        String apiStatus = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_STATUS).toLowerCase();
        String publisherRoles = apiResource.getProperty(APIConstants.PUBLISHER_ROLES);
        fields.put(APIConstants.API_OVERVIEW_STATUS, Arrays.asList(apiStatus));
        fields.put(APIConstants.PUBLISHER_ROLES, Arrays.asList(publisherRoles));
    } else {
        log.warn("API does not exist at " + apiPath);
    }
}
 
Example 7
Source File: Utils.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * gets no of verified user challenges
 *
 * @param userDTO bean class that contains user and tenant Information
 * @return no of verified challenges
 * @throws IdentityException if fails
 */
public static int getVerifiedChallenges(UserDTO userDTO) throws IdentityException {

    int noOfChallenges = 0;

    try {
        UserRegistry registry = IdentityMgtServiceComponent.getRegistryService().
                getConfigSystemRegistry(MultitenantConstants.SUPER_TENANT_ID);
        String identityKeyMgtPath = IdentityMgtConstants.IDENTITY_MANAGEMENT_CHALLENGES +
                RegistryConstants.PATH_SEPARATOR + userDTO.getUserId() +
                RegistryConstants.PATH_SEPARATOR + userDTO.getUserId();

        Resource resource;
        if (registry.resourceExists(identityKeyMgtPath)) {
            resource = registry.get(identityKeyMgtPath);
            String property = resource.getProperty(IdentityMgtConstants.VERIFIED_CHALLENGES);
            if (property != null) {
                return Integer.parseInt(property);
            }
        }
    } catch (RegistryException e) {
        log.error("Error while processing userKey", e);
    }

    return noOfChallenges;
}
 
Example 8
Source File: DefaultPolicyDataStore.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public PolicyStoreDTO[] getPolicyData() {

    Registry registry = EntitlementServiceComponent.
            getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId());
    List<PolicyStoreDTO> policyStoreDTOs = new ArrayList<PolicyStoreDTO>();
    try {
        if (registry.resourceExists(policyDataCollection)) {
            Collection collection = (Collection) registry.get(policyDataCollection);
            String[] paths = collection.getChildren();
            for (String path : paths) {
                if (registry.resourceExists(path)) {
                    PolicyStoreDTO dataDTO = new PolicyStoreDTO();
                    Resource resource = registry.get(path);
                    String order = resource.getProperty("order");
                    String active = resource.getProperty("active");
                    String id = path.substring(path.lastIndexOf(RegistryConstants.PATH_SEPARATOR) + 1);
                    dataDTO.setPolicyId(id);
                    if (order != null && order.trim().length() > 0) {
                        dataDTO.setPolicyOrder(Integer.parseInt(order));
                    }
                    dataDTO.setActive(Boolean.parseBoolean(active));
                    policyStoreDTOs.add(dataDTO);
                }
            }
        }
    } catch (RegistryException e) {
        if (log.isDebugEnabled()) {
            log.debug(e);
        }
    }
    return policyStoreDTOs.toArray(new PolicyStoreDTO[policyStoreDTOs.size()]);
}
 
Example 9
Source File: RegistryManager.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public String getTenantDomainForHost(String hostName) throws Exception {
    Resource resource = getMappingFromRegistry(hostName);
    if (resource != null) {
        return resource.getProperty(UrlMapperConstants.HostProperties.TENANT_DOMAIN);
    }
    return null;

}
 
Example 10
Source File: RegistrySubscriptionManager.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void unSubscribe(String subscriptionID) throws EventBrokerException {
    try {
        UserRegistry userRegistry =
                this.registryService.getGovernanceSystemRegistry(EventBrokerHolder.getInstance()
                                                                         .getTenantId());
        String fullPath = this.indexStoragePath;
        if (userRegistry.resourceExists(fullPath)) {
            Resource topicIndexResource = userRegistry.get(fullPath);

            String topicName = topicIndexResource.getProperty(subscriptionID);
            // delete the subscriptions resource
            // if the registry is read only there can be situations where the the subscriptions
            // is not saved to registry and hence the topic name
            if (topicName != null) {
                String resourcePath =  getResourcePath(subscriptionID, topicName);
                if (userRegistry.resourceExists(resourcePath)) {
                    userRegistry.delete(resourcePath);
                }
                String jMSResourcePath =  getJMSSubResourcePath(subscriptionID, topicName);
                if (userRegistry.resourceExists(jMSResourcePath)) {
                    userRegistry.delete(jMSResourcePath);
                }
            }

            topicIndexResource.removeProperty(subscriptionID);

            userRegistry.put(fullPath, topicIndexResource);
        }

    } catch (RegistryException e) {
        throw new EventBrokerException("Cannot access the registry ", e);
    }
}
 
Example 11
Source File: RegistryManager.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public String getAppNameFromRegistry(String hostName) throws Exception{
    Resource resource = getMappingFromRegistry(hostName);
    if (resource != null) {
        return resource.getProperty(UrlMapperConstants.HostProperties.APP_NAME);
    }
    return null;
}
 
Example 12
Source File: OpenIDAdminDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected OpenIDAdminDO resourceToObject(Resource resource) {
    OpenIDAdminDO opdo = null;
    if (resource != null) {
        opdo = new OpenIDAdminDO();
        String subDomain = resource.getProperty(IdentityRegistryResources.SUB_DOMAIN);
        String openIDPattern = resource.getProperty(IdentityRegistryResources.OPENID_PATTERN);
        opdo.setSubDomain(subDomain);
        opdo.setTenantOpenIDPattern(openIDPattern);
    }
    return opdo;
}
 
Example 13
Source File: OpenIDUserDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Get all OpenIDs of an user
 *
 * @param username
 * @return
 */
public String[] getOpenIDsForUser(String username) {
    String[] openIDSet = new String[0];
    Resource openIDResource = null;

    try {
        if (registry.resourceExists(RegistryConstants.PROFILES_PATH + username)) {
            Association[] openIDAssociations = registry.getAssociations(
                    RegistryConstants.PROFILES_PATH + username,
                    IdentityRegistryResources.ASSOCIATION_USER_OPENID);
            openIDSet = new String[openIDAssociations.length];

            int i = 0;

            for (Association association : openIDAssociations) {
                String openIDAssociation = association.getDestinationPath().trim();
                String openID = "";
                if (registry.resourceExists(openIDAssociation)) {
                    openIDResource = registry.get(openIDAssociation);
                    openID = openIDResource.getProperty(IdentityRegistryResources.PROP_OPENID);
                }
                openIDSet[i] = openID;
                i++;
            }
        }
    } catch (RegistryException e) {
        log.error("Error retrieving user information from registry.", e);
    }
    return openIDSet;
}
 
Example 14
Source File: OpenIDAdminDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected OpenIDAdminDO resourceToObject(Resource resource) {
    OpenIDAdminDO opdo = null;
    if (resource != null) {
        opdo = new OpenIDAdminDO();
        String subDomain = resource.getProperty(IdentityRegistryResources.SUB_DOMAIN);
        String openIDPattern = resource.getProperty(IdentityRegistryResources.OPENID_PATTERN);
        opdo.setSubDomain(subDomain);
        opdo.setTenantOpenIDPattern(openIDPattern);
    }
    return opdo;
}
 
Example 15
Source File: PAPPolicyStoreReader.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Reads PolicyDTO for given registry resource
 *
 * @param resource Registry resource
 * @return PolicyDTO
 * @throws EntitlementException throws, if fails
 */
public PolicyDTO readPolicyDTO(Resource resource) throws EntitlementException {
    String policy = null;
    String policyId = null;
    AbstractPolicy absPolicy = null;
    PolicyDTO dto = null;
    try {
        policy = new String((byte[]) resource.getContent(), Charset.forName("UTF-8"));
        absPolicy = PAPPolicyReader.getInstance(null).getPolicy(policy);
        policyId = absPolicy.getId().toASCIIString();
        dto = new PolicyDTO();
        dto.setPolicyId(policyId);
        dto.setPolicy(policy);
        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));
        }

        //read policy meta data that is used for basic policy editor
        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;
    } catch (RegistryException e) {
        log.error("Error while loading entitlement policy " + policyId + " from PAP policy store", e);
        throw new EntitlementException("Error while loading entitlement policy " + policyId +
                " from PAP policy store");
    }
}
 
Example 16
Source File: PAPPolicyStoreReader.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Reads PolicyDTO for given policy id
 *
 * @param policyId policy id
 * @return PolicyDTO
 * @throws EntitlementException throws, if fails
 */
public PolicyDTO readPolicyDTO(String policyId) throws EntitlementException {
    Resource resource = null;
    PolicyDTO dto = null;
    try {
        resource = store.getPolicy(policyId, PDPConstants.ENTITLEMENT_POLICY_PAP);
        if (resource == null) {
            log.error("Policy does not exist in the system with id " + policyId);
            throw new EntitlementException("Policy does not exist in the system with id " + policyId);
        }

        dto = new PolicyDTO();
        dto.setPolicyId(policyId);
        dto.setPolicy(new String((byte[]) resource.getContent(), Charset.forName("UTF-8")));
        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);
        }
        dto.setPolicyType(resource.getProperty(PDPConstants.POLICY_TYPE));
        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);
        }
        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));
        }
        //read policy meta data that is used for basic policy editor
        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;
    } catch (RegistryException e) {
        log.error("Error while loading entitlement policy " + policyId + " from PAP policy store", e);
        throw new EntitlementException("Error while loading entitlement policy " + policyId +
                " from PAP policy store");
    }
}
 
Example 17
Source File: PAPPolicyStoreReader.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Reads Light Weight PolicyDTO for given policy id
 *
 * @param policyId policy id
 * @return PolicyDTO but don not contains XACML policy and attribute meta data
 * @throws EntitlementException throws, if fails
 */
public PolicyDTO readLightPolicyDTO(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);
    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.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);
    }
    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));

    return dto;
}
 
Example 18
Source File: PAPPolicyStoreReader.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example 19
Source File: PAPPolicyStoreReader.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Reads PolicyDTO for given registry resource
 *
 * @param resource Registry resource
 * @return PolicyDTO
 * @throws EntitlementException throws, if fails
 */
public PolicyDTO readPolicyDTO(Resource resource) throws EntitlementException {
    String policy = null;
    String policyId = null;
    AbstractPolicy absPolicy = null;
    PolicyDTO dto = null;
    try {
        policy = new String((byte[]) resource.getContent(), Charset.forName("UTF-8"));
        absPolicy = PAPPolicyReader.getInstance(null).getPolicy(policy);
        policyId = absPolicy.getId().toASCIIString();
        dto = new PolicyDTO();
        dto.setPolicyId(policyId);
        dto.setPolicy(policy);
        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));
        }

        //read policy meta data that is used for basic policy editor
        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;
    } catch (RegistryException e) {
        log.error("Error while loading entitlement policy " + policyId + " from PAP policy store", e);
        throw new EntitlementException("Error while loading entitlement policy " + policyId +
                " from PAP policy store");
    }
}
 
Example 20
Source File: MetadataApiRegistry.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public boolean removePropertyFromApplication(String applicationId, String propertyKey)
        throws RegistryException, MetadataException {
    Registry registry = getRegistry();
    String resourcePath = mainResource + applicationId;
    Resource nodeResource;

    try {
        acquireWriteLock(applicationId);
        // We are using only super tenant registry to persist
        PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        if (registry.resourceExists(resourcePath)) {
            nodeResource = registry.get(resourcePath);
            if (nodeResource.getProperty(propertyKey) == null) {
                log.info(String.format("Registry property not found: [application-id] %s [key] %s ", applicationId,
                        propertyKey));
                return false;
            } else {
                nodeResource.removeProperty(propertyKey);
                registry.put(resourcePath, nodeResource);
            }
        } else {
            log.error("Registry resource not not found at " + resourcePath);
            return false;
        }

        log.info(String.format("Registry property removed: [application-id] %s, [key] %s", applicationId,
                propertyKey));
        return true;
    } catch (Exception e) {
        throw new MetadataException(
                String.format("Could not remove registry resource: [resource-path] %s, [key] %s", resourcePath,
                        propertyKey), e);
    } finally {
        try {
            releaseWriteLock(applicationId);
        } catch (MetadataException ignored) {
        }
    }
}