use of org.wso2.carbon.identity.entitlement.policy.PolicyAttributeBuilder in project carbon-identity-framework by wso2.
the class PAPPolicyStore method addOrUpdatePolicy.
/**
* @param policy
* @throws EntitlementException
*/
public void addOrUpdatePolicy(PolicyDTO policy, String policyId, String policyPath) throws EntitlementException {
String path = null;
Resource resource = null;
boolean newPolicy = false;
OMElement omElement = null;
if (log.isDebugEnabled()) {
log.debug("Creating or updating entitlement policy");
}
if (policy == null || policyId == null) {
log.error("Error while creating or updating entitlement policy: " + "Policy DTO or Policy Id can not be null");
throw new EntitlementException("Invalid Entitlement Policy. Policy or policyId can not be Null");
}
try {
path = policyPath + policyId;
if (registry.resourceExists(path)) {
resource = registry.get(path);
} else {
resource = registry.newResource();
}
Collection policyCollection;
if (registry.resourceExists(policyPath)) {
policyCollection = (Collection) registry.get(policyPath);
} else {
policyCollection = registry.newCollection();
}
if (policy.getPolicyOrder() > 0) {
String noOfPolicies = policyCollection.getProperty(PDPConstants.MAX_POLICY_ORDER);
if (noOfPolicies != null && Integer.parseInt(noOfPolicies) < policy.getPolicyOrder()) {
policyCollection.setProperty(PDPConstants.MAX_POLICY_ORDER, Integer.toString(policy.getPolicyOrder()));
registry.put(policyPath, policyCollection);
}
resource.setProperty(PDPConstants.POLICY_ORDER, Integer.toString(policy.getPolicyOrder()));
} else {
String previousOrder = resource.getProperty(PDPConstants.POLICY_ORDER);
if (previousOrder == null) {
if (policyCollection != null) {
int policyOrder = 1;
String noOfPolicies = policyCollection.getProperty(PDPConstants.MAX_POLICY_ORDER);
if (noOfPolicies != null) {
policyOrder = policyOrder + Integer.parseInt(noOfPolicies);
}
policyCollection.setProperty(PDPConstants.MAX_POLICY_ORDER, Integer.toString(policyOrder));
resource.setProperty(PDPConstants.POLICY_ORDER, Integer.toString(policyOrder));
}
registry.put(policyPath, policyCollection);
}
}
if (StringUtils.isNotBlank(policy.getPolicy())) {
resource.setContent(policy.getPolicy());
newPolicy = true;
PolicyAttributeBuilder policyAttributeBuilder = new PolicyAttributeBuilder(policy.getPolicy());
Properties properties = policyAttributeBuilder.getPolicyMetaDataFromPolicy();
Properties resourceProperties = new Properties();
for (Object o : properties.keySet()) {
String key = o.toString();
resourceProperties.put(key, Collections.singletonList(properties.get(key)));
}
resource.setProperties(resourceProperties);
}
resource.setProperty(PDPConstants.ACTIVE_POLICY, Boolean.toString(policy.isActive()));
resource.setProperty(PDPConstants.PROMOTED_POLICY, Boolean.toString(policy.isPromote()));
if (policy.getVersion() != null) {
resource.setProperty(PDPConstants.POLICY_VERSION, policy.getVersion());
}
resource.setProperty(PDPConstants.LAST_MODIFIED_TIME, Long.toString(System.currentTimeMillis()));
resource.setProperty(PDPConstants.LAST_MODIFIED_USER, CarbonContext.getThreadLocalCarbonContext().getUsername());
if (policy.getPolicyType() != null && policy.getPolicyType().trim().length() > 0) {
resource.setProperty(PDPConstants.POLICY_TYPE, policy.getPolicyType());
} else {
try {
if (newPolicy) {
omElement = AXIOMUtil.stringToOM(policy.getPolicy());
resource.setProperty(PDPConstants.POLICY_TYPE, omElement.getLocalName());
}
} catch (XMLStreamException e) {
policy.setPolicyType(PDPConstants.POLICY_ELEMENT);
log.warn("Policy Type can not be found. Default type is set");
}
}
if (omElement != null) {
Iterator iterator1 = omElement.getChildrenWithLocalName(PDPConstants.POLICY_REFERENCE);
if (iterator1 != null) {
String policyReferences = "";
while (iterator1.hasNext()) {
OMElement policyReference = (OMElement) iterator1.next();
if (!"".equals(policyReferences)) {
policyReferences = policyReferences + PDPConstants.ATTRIBUTE_SEPARATOR + policyReference.getText();
} else {
policyReferences = policyReference.getText();
}
}
resource.setProperty(PDPConstants.POLICY_REFERENCE, policyReferences);
}
Iterator iterator2 = omElement.getChildrenWithLocalName(PDPConstants.POLICY_SET_REFERENCE);
if (iterator2 != null) {
String policySetReferences = "";
while (iterator1.hasNext()) {
OMElement policySetReference = (OMElement) iterator2.next();
if (!"".equals(policySetReferences)) {
policySetReferences = policySetReferences + PDPConstants.ATTRIBUTE_SEPARATOR + policySetReference.getText();
} else {
policySetReferences = policySetReference.getText();
}
}
resource.setProperty(PDPConstants.POLICY_SET_REFERENCE, policySetReferences);
}
}
// before writing basic policy editor meta data as properties,
// delete any properties related to them
String policyEditor = resource.getProperty(PDPConstants.POLICY_EDITOR_TYPE);
if (newPolicy && policyEditor != null) {
resource.removeProperty(PDPConstants.POLICY_EDITOR_TYPE);
}
// write policy meta data that is used for basic policy editor
if (policy.getPolicyEditor() != null && policy.getPolicyEditor().trim().length() > 0) {
resource.setProperty(PDPConstants.POLICY_EDITOR_TYPE, policy.getPolicyEditor().trim());
}
String[] policyMetaData = policy.getPolicyEditorData();
if (policyMetaData != null && policyMetaData.length > 0) {
String BasicPolicyEditorMetaDataAmount = resource.getProperty(PDPConstants.BASIC_POLICY_EDITOR_META_DATA_AMOUNT);
if (newPolicy && BasicPolicyEditorMetaDataAmount != null) {
int amount = Integer.parseInt(BasicPolicyEditorMetaDataAmount);
for (int i = 0; i < amount; i++) {
resource.removeProperty(PDPConstants.BASIC_POLICY_EDITOR_META_DATA + i);
}
resource.removeProperty(PDPConstants.BASIC_POLICY_EDITOR_META_DATA_AMOUNT);
}
int i = 0;
for (String policyData : policyMetaData) {
if (policyData != null && !"".equals(policyData)) {
resource.setProperty(PDPConstants.BASIC_POLICY_EDITOR_META_DATA + i, policyData);
}
i++;
}
resource.setProperty(PDPConstants.BASIC_POLICY_EDITOR_META_DATA_AMOUNT, Integer.toString(i));
}
registry.put(path, resource);
} catch (RegistryException e) {
log.error("Error while adding or updating entitlement policy " + policyId + " in policy store", e);
throw new EntitlementException("Error while adding or updating entitlement policy in policy store");
}
}
use of org.wso2.carbon.identity.entitlement.policy.PolicyAttributeBuilder in project carbon-identity-framework by wso2.
the class PAPPolicyStoreReader method readPolicyDTO.
/**
* 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");
}
}
use of org.wso2.carbon.identity.entitlement.policy.PolicyAttributeBuilder in project carbon-identity-framework by wso2.
the class PAPPolicyStoreReader method readPolicyDTO.
/**
* 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");
}
}
use of org.wso2.carbon.identity.entitlement.policy.PolicyAttributeBuilder 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;
}
use of org.wso2.carbon.identity.entitlement.policy.PolicyAttributeBuilder 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);
}
}
Aggregations