PageRenderTime 68ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/components/entitlement/org.wso2.carbon.identity.entitlement.ui/src/main/java/org/wso2/carbon/identity/entitlement/ui/util/PolicyEditorUtil.java

https://github.com/Pushpalanka/carbon-identity
Java | 2959 lines | 2051 code | 447 blank | 461 comment | 746 complexity | e4b6f0779032c6e417550d9aed8b6459 MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
  3. *
  4. * WSO2 Inc. licenses this file to you under the Apache License,
  5. * Version 2.0 (the "License"); you may not use this file except
  6. * in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing,
  12. * software distributed under the License is distributed on an
  13. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. * KIND, either express or implied. See the License for the
  15. * specific language governing permissions and limitations
  16. * under the License.
  17. */
  18. package org.wso2.carbon.identity.entitlement.ui.util;
  19. import org.apache.axiom.om.OMElement;
  20. import org.apache.axiom.om.util.AXIOMUtil;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.Element;
  25. import org.wso2.balana.utils.Constants.PolicyConstants;
  26. import org.wso2.balana.utils.exception.PolicyBuilderException;
  27. import org.wso2.balana.utils.policy.PolicyBuilder;
  28. import org.wso2.balana.utils.policy.dto.*;
  29. import org.wso2.carbon.identity.entitlement.common.EntitlementConstants;
  30. import org.wso2.carbon.identity.entitlement.common.PolicyEditorEngine;
  31. import org.wso2.carbon.identity.entitlement.common.PolicyEditorException;
  32. import org.wso2.carbon.identity.entitlement.common.dto.PolicyEditorDataHolder;
  33. import org.wso2.carbon.identity.entitlement.ui.EntitlementPolicyConstants;
  34. import org.wso2.carbon.identity.entitlement.ui.EntitlementPolicyCreationException;
  35. import org.wso2.carbon.identity.entitlement.ui.PolicyEditorConstants;
  36. import org.wso2.carbon.identity.entitlement.ui.dto.*;
  37. import javax.xml.namespace.QName;
  38. import javax.xml.stream.XMLStreamException;
  39. import java.util.*;
  40. /**
  41. * Util class that helps to create the XACML policy which is defined by the XACML basic policy editor
  42. */
  43. public class PolicyEditorUtil {
  44. private static Log log = LogFactory.getLog(PolicyEditorUtil.class);
  45. /**
  46. * map of apply element w.r.t identifier
  47. */
  48. private static Map<String, ApplyElementDTO> applyElementMap = new HashMap<String, ApplyElementDTO>();
  49. /**
  50. * Create XACML policy with the simplest input attributes
  51. *
  52. * @param policyEditorDTO
  53. * @return
  54. * @throws PolicyEditorException
  55. */
  56. public static String createSOAPolicy(SimplePolicyEditorDTO policyEditorDTO) throws PolicyEditorException {
  57. BasicPolicyDTO basicPolicyDTO = new BasicPolicyDTO();
  58. BasicTargetDTO basicTargetDTO = null;
  59. List<BasicRuleDTO> ruleElementDTOs = new ArrayList<BasicRuleDTO>();
  60. PolicyEditorDataHolder holder = PolicyEditorEngine.getInstance().
  61. getPolicyEditorData(EntitlementConstants.PolicyEditor.RBAC);
  62. //create policy element
  63. basicPolicyDTO.setPolicyId(policyEditorDTO.getPolicyId());
  64. // setting rule combining algorithm
  65. basicPolicyDTO.setRuleAlgorithm(PolicyConstants.RuleCombiningAlog.FIRST_APPLICABLE_ID);
  66. basicPolicyDTO.setDescription(policyEditorDTO.getDescription());
  67. if (PolicyEditorConstants.SOA_CATEGORY_USER.equals(policyEditorDTO.getAppliedCategory())) {
  68. if (policyEditorDTO.getUserAttributeValue() != null &&
  69. !PolicyEditorConstants.FunctionIdentifier.ANY.
  70. equals(policyEditorDTO.getUserAttributeValue().trim())) {
  71. basicTargetDTO = new BasicTargetDTO();
  72. String selectedDataType = null;
  73. if (policyEditorDTO.getUserAttributeId() == null) {
  74. basicTargetDTO.setSubjectId(PolicyEditorConstants.SUBJECT_ID_DEFAULT);
  75. } else {
  76. basicTargetDTO.setSubjectId(holder.getAttributeIdUri(policyEditorDTO.getUserAttributeId()));
  77. if ((selectedDataType = holder.getDataTypeUriForAttribute(policyEditorDTO.getUserAttributeId())) != null) {
  78. basicTargetDTO.setSubjectDataType(selectedDataType);
  79. }
  80. }
  81. if (basicTargetDTO.getSubjectDataType() == null) {
  82. basicTargetDTO.setSubjectDataType(PolicyConstants.DataType.STRING);
  83. }
  84. String function = findFunction(policyEditorDTO.getUserAttributeValue(),
  85. basicTargetDTO.getSubjectDataType());
  86. String value = findAttributeValue(policyEditorDTO.getUserAttributeValue());
  87. basicTargetDTO.setSubjectList(value);
  88. basicTargetDTO.setFunctionOnSubjects(function);
  89. }
  90. List<SimplePolicyEditorElementDTO> elementDTOs = policyEditorDTO.getSimplePolicyEditorElementDTOs();
  91. if (elementDTOs != null) {
  92. int ruleNo = 1;
  93. for (SimplePolicyEditorElementDTO dto : elementDTOs) {
  94. BasicRuleDTO ruleElementDTO = new BasicRuleDTO();
  95. if (dto.getResourceValue() != null && dto.getResourceValue().trim().length() > 0 &&
  96. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getResourceValue().trim())) {
  97. addResourceElement(ruleElementDTO, dto);
  98. }
  99. if (dto.getActionValue() != null && dto.getActionValue().trim().length() > 0 &&
  100. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getActionValue().trim())) {
  101. addActionElement(ruleElementDTO, dto);
  102. }
  103. if (dto.getEnvironmentValue() != null && dto.getEnvironmentValue().trim().length() > 0 &&
  104. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getEnvironmentValue().trim())) {
  105. addEnvironmentElement(ruleElementDTO, dto);
  106. }
  107. ruleElementDTO.setRuleEffect(PolicyEditorConstants.RULE_EFFECT_PERMIT);
  108. ruleElementDTO.setRuleId("Rule-" + ruleNo);
  109. ruleElementDTOs.add(ruleElementDTO);
  110. ruleNo++;
  111. }
  112. BasicRuleDTO ruleElementDTO = new BasicRuleDTO();
  113. ruleElementDTO.setRuleId("Deny-Rule");
  114. ruleElementDTO.setRuleEffect(PolicyEditorConstants.RULE_EFFECT_DENY);
  115. ruleElementDTOs.add(ruleElementDTO);
  116. }
  117. } else if (PolicyEditorConstants.SOA_CATEGORY_RESOURCE.equals(policyEditorDTO.getAppliedCategory())) {
  118. if (policyEditorDTO.getResourceValue() != null &&
  119. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(policyEditorDTO.getResourceValue().trim())) {
  120. basicTargetDTO = new BasicTargetDTO();
  121. basicTargetDTO.setResourceId(PolicyEditorConstants.RESOURCE_ID_DEFAULT);
  122. basicTargetDTO.setResourceDataType(PolicyConstants.DataType.STRING);
  123. String function = findFunction(policyEditorDTO.getResourceValue(),
  124. basicTargetDTO.getResourceDataType());
  125. String value = findAttributeValue(policyEditorDTO.getResourceValue());
  126. basicTargetDTO.setResourceList(value);
  127. basicTargetDTO.setFunctionOnResources(function);
  128. }
  129. List<SimplePolicyEditorElementDTO> elementDTOs = policyEditorDTO.getSimplePolicyEditorElementDTOs();
  130. if (elementDTOs != null) {
  131. int ruleNo = 1;
  132. for (SimplePolicyEditorElementDTO dto : elementDTOs) {
  133. BasicRuleDTO ruleElementDTO = new BasicRuleDTO();
  134. if (dto.getResourceValue() != null && dto.getResourceValue().trim().length() > 0 &&
  135. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getResourceValue().trim())) {
  136. addResourceElement(ruleElementDTO, dto);
  137. }
  138. if (dto.getUserAttributeValue() != null && dto.getUserAttributeValue().trim().length() > 0 &&
  139. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getUserAttributeValue().trim())) {
  140. addSubjectElement(ruleElementDTO, dto);
  141. }
  142. if (dto.getActionValue() != null && dto.getActionValue().trim().length() > 0 &&
  143. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getActionValue().trim())) {
  144. addActionElement(ruleElementDTO, dto);
  145. }
  146. if (dto.getEnvironmentValue() != null && dto.getEnvironmentValue().trim().length() > 0 &&
  147. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getEnvironmentValue().trim())) {
  148. addEnvironmentElement(ruleElementDTO, dto);
  149. }
  150. ruleElementDTO.setRuleEffect(PolicyEditorConstants.RULE_EFFECT_PERMIT);
  151. ruleElementDTO.setRuleId("Rule-" + ruleNo);
  152. ruleElementDTOs.add(ruleElementDTO);
  153. ruleNo++;
  154. }
  155. BasicRuleDTO ruleElementDTO = new BasicRuleDTO();
  156. ruleElementDTO.setRuleId("Deny-Rule");
  157. ruleElementDTO.setRuleEffect(PolicyEditorConstants.RULE_EFFECT_DENY);
  158. ruleElementDTOs.add(ruleElementDTO);
  159. }
  160. } else if (PolicyEditorConstants.SOA_CATEGORY_ACTION.equals(policyEditorDTO.getAppliedCategory())) {
  161. if (policyEditorDTO.getActionValue() != null &&
  162. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(policyEditorDTO.getActionValue().trim())) {
  163. basicTargetDTO = new BasicTargetDTO();
  164. basicTargetDTO.setActionId(PolicyEditorConstants.ACTION_ID_DEFAULT);
  165. basicTargetDTO.setActionDataType(PolicyConstants.DataType.STRING);
  166. String function = findFunction(policyEditorDTO.getActionValue(),
  167. basicTargetDTO.getActionDataType());
  168. String value = findAttributeValue(policyEditorDTO.getActionValue());
  169. basicTargetDTO.setActionList(value);
  170. basicTargetDTO.setFunctionOnActions(function);
  171. }
  172. List<SimplePolicyEditorElementDTO> elementDTOs = policyEditorDTO.getSimplePolicyEditorElementDTOs();
  173. if (elementDTOs != null) {
  174. int ruleNo = 1;
  175. for (SimplePolicyEditorElementDTO dto : elementDTOs) {
  176. BasicRuleDTO ruleElementDTO = new BasicRuleDTO();
  177. if (dto.getResourceValue() != null && dto.getResourceValue().trim().length() > 0 &&
  178. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getResourceValue().trim())) {
  179. addResourceElement(ruleElementDTO, dto);
  180. }
  181. if (dto.getUserAttributeValue() != null && dto.getUserAttributeValue().trim().length() > 0 &&
  182. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getUserAttributeValue().trim())) {
  183. addSubjectElement(ruleElementDTO, dto);
  184. }
  185. if (dto.getEnvironmentValue() != null && dto.getEnvironmentValue().trim().length() > 0 &&
  186. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getEnvironmentValue().trim())) {
  187. addEnvironmentElement(ruleElementDTO, dto);
  188. }
  189. ruleElementDTO.setRuleEffect(PolicyEditorConstants.RULE_EFFECT_PERMIT);
  190. ruleElementDTO.setRuleId("Rule-" + ruleNo);
  191. ruleElementDTOs.add(ruleElementDTO);
  192. ruleNo++;
  193. }
  194. BasicRuleDTO ruleElementDTO = new BasicRuleDTO();
  195. ruleElementDTO.setRuleId("Deny-Rule");
  196. ruleElementDTO.setRuleEffect(PolicyEditorConstants.RULE_EFFECT_DENY);
  197. ruleElementDTOs.add(ruleElementDTO);
  198. }
  199. } else if (PolicyEditorConstants.SOA_CATEGORY_ENVIRONMENT.equals(policyEditorDTO.getAppliedCategory())) {
  200. if (policyEditorDTO.getEnvironmentValue() != null &&
  201. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(policyEditorDTO.getEnvironmentValue().trim())) {
  202. basicTargetDTO = new BasicTargetDTO();
  203. String selectedDataType = null;
  204. if (policyEditorDTO.getEnvironmentId() == null) {
  205. basicTargetDTO.setEnvironmentId(PolicyEditorConstants.ENVIRONMENT_ID_DEFAULT);
  206. } else {
  207. basicTargetDTO.setEnvironmentId(holder.getAttributeIdUri(policyEditorDTO.getEnvironmentId()));
  208. if ((selectedDataType = holder.getDataTypeUriForAttribute(policyEditorDTO.getEnvironmentId())) != null) {
  209. basicTargetDTO.setEnvironmentDataType(selectedDataType);
  210. }
  211. }
  212. if (basicTargetDTO.getEnvironmentDataType() == null) {
  213. basicTargetDTO.setEnvironmentDataType(PolicyConstants.DataType.STRING);
  214. }
  215. String function = findFunction(policyEditorDTO.getEnvironmentValue(),
  216. basicTargetDTO.getEnvironmentDataType());
  217. String value = findAttributeValue(policyEditorDTO.getEnvironmentValue());
  218. basicTargetDTO.setEnvironmentList(value);
  219. basicTargetDTO.setFunctionOnEnvironment(function);
  220. }
  221. List<SimplePolicyEditorElementDTO> elementDTOs = policyEditorDTO.getSimplePolicyEditorElementDTOs();
  222. if (elementDTOs != null) {
  223. int ruleNo = 1;
  224. for (SimplePolicyEditorElementDTO dto : elementDTOs) {
  225. BasicRuleDTO ruleElementDTO = new BasicRuleDTO();
  226. if (dto.getResourceValue() != null && dto.getResourceValue().trim().length() > 0 &&
  227. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getResourceValue().trim())) {
  228. addResourceElement(ruleElementDTO, dto);
  229. }
  230. if (dto.getUserAttributeValue() != null && dto.getUserAttributeValue().trim().length() > 0 &&
  231. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getUserAttributeValue().trim())) {
  232. addSubjectElement(ruleElementDTO, dto);
  233. }
  234. if (dto.getActionValue() != null && dto.getActionValue().trim().length() > 0 &&
  235. !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getActionValue().trim())) {
  236. addActionElement(ruleElementDTO, dto);
  237. }
  238. ruleElementDTO.setRuleEffect(PolicyEditorConstants.RULE_EFFECT_PERMIT);
  239. ruleElementDTO.setRuleId("Rule-" + ruleNo);
  240. ruleElementDTOs.add(ruleElementDTO);
  241. ruleNo++;
  242. }
  243. BasicRuleDTO ruleElementDTO = new BasicRuleDTO();
  244. ruleElementDTO.setRuleId("Deny-Rule");
  245. ruleElementDTO.setRuleEffect(PolicyEditorConstants.RULE_EFFECT_DENY);
  246. ruleElementDTOs.add(ruleElementDTO);
  247. }
  248. }
  249. if (basicTargetDTO != null) {
  250. basicPolicyDTO.setTargetDTO(basicTargetDTO);
  251. }
  252. if (ruleElementDTOs.size() > 0) {
  253. basicPolicyDTO.setBasicRuleDTOs(ruleElementDTOs);
  254. }
  255. try {
  256. return PolicyBuilder.getInstance().build(basicPolicyDTO);
  257. } catch (PolicyBuilderException e) {
  258. log.error(e);
  259. throw new PolicyEditorException("Error while building policy");
  260. }
  261. }
  262. /**
  263. * Helper method to create SOA policy
  264. *
  265. * @param ruleElementDTO
  266. * @param editorElementDTO
  267. */
  268. private static void addResourceElement(BasicRuleDTO ruleElementDTO,
  269. SimplePolicyEditorElementDTO editorElementDTO) {
  270. ruleElementDTO.setResourceId(PolicyEditorConstants.RESOURCE_ID_DEFAULT);
  271. ruleElementDTO.setResourceDataType(PolicyConstants.DataType.STRING);
  272. String function = findFunction(editorElementDTO.getResourceValue(),
  273. ruleElementDTO.getResourceDataType());
  274. String value = findAttributeValue(editorElementDTO.getResourceValue());
  275. ruleElementDTO.setResourceList(value);
  276. ruleElementDTO.setFunctionOnResources(function);
  277. }
  278. /**
  279. * Helper method to create SOA policy
  280. *
  281. * @param ruleElementDTO
  282. * @param editorElementDTO
  283. */
  284. private static void addSubjectElement(BasicRuleDTO ruleElementDTO,
  285. SimplePolicyEditorElementDTO editorElementDTO) {
  286. String selectedDataType = null;
  287. PolicyEditorDataHolder holder = PolicyEditorEngine.getInstance().
  288. getPolicyEditorData(EntitlementConstants.PolicyEditor.RBAC);
  289. if (editorElementDTO.getUserAttributeId() == null) {
  290. ruleElementDTO.setSubjectId(PolicyEditorConstants.SUBJECT_ID_DEFAULT);
  291. } else {
  292. ruleElementDTO.setSubjectId(holder.getAttributeIdUri(editorElementDTO.getUserAttributeId()));
  293. if ((selectedDataType = holder.getDataTypeUriForAttribute(editorElementDTO.getUserAttributeId())) != null) {
  294. ruleElementDTO.setSubjectDataType(selectedDataType);
  295. }
  296. }
  297. if (ruleElementDTO.getSubjectDataType() == null) {
  298. ruleElementDTO.setSubjectDataType(PolicyConstants.DataType.STRING);
  299. }
  300. String function = findFunction(editorElementDTO.getUserAttributeValue(),
  301. ruleElementDTO.getSubjectDataType());
  302. String value = findAttributeValue(editorElementDTO.getUserAttributeValue());
  303. ruleElementDTO.setSubjectList(value);
  304. ruleElementDTO.setFunctionOnSubjects(function);
  305. }
  306. /**
  307. * Helper method to create SOA policy
  308. *
  309. * @param ruleElementDTO
  310. * @param editorElementDTO
  311. */
  312. private static void addActionElement(BasicRuleDTO ruleElementDTO,
  313. SimplePolicyEditorElementDTO editorElementDTO) {
  314. ruleElementDTO.setActionId(PolicyEditorConstants.ACTION_ID_DEFAULT);
  315. ruleElementDTO.setActionDataType(PolicyConstants.DataType.STRING);
  316. String function = findFunction(editorElementDTO.getActionValue(),
  317. ruleElementDTO.getActionDataType());
  318. String value = findAttributeValue(editorElementDTO.getActionValue());
  319. ruleElementDTO.setActionList(value);
  320. ruleElementDTO.setFunctionOnActions(function);
  321. }
  322. /**
  323. * Helper method to create SOA policy
  324. *
  325. * @param ruleElementDTO
  326. * @param editorElementDTO
  327. */
  328. private static void addEnvironmentElement(BasicRuleDTO ruleElementDTO,
  329. SimplePolicyEditorElementDTO editorElementDTO) {
  330. String selectedDataType = null;
  331. PolicyEditorDataHolder holder = PolicyEditorEngine.getInstance().
  332. getPolicyEditorData(EntitlementConstants.PolicyEditor.RBAC);
  333. if (editorElementDTO.getEnvironmentId() == null) {
  334. ruleElementDTO.setEnvironmentId(PolicyEditorConstants.ENVIRONMENT_ID_DEFAULT);
  335. } else {
  336. ruleElementDTO.setEnvironmentId(holder.getAttributeIdUri(editorElementDTO.getEnvironmentId()));
  337. if ((selectedDataType = holder.getDataTypeUriForAttribute(editorElementDTO.getEnvironmentId())) != null) {
  338. ruleElementDTO.setEnvironmentDataType(selectedDataType);
  339. }
  340. }
  341. if (ruleElementDTO.getEnvironmentDataType() == null) {
  342. ruleElementDTO.setEnvironmentDataType(PolicyConstants.DataType.STRING);
  343. }
  344. String function = findFunction(editorElementDTO.getEnvironmentValue(),
  345. ruleElementDTO.getEnvironmentDataType());
  346. String value = findAttributeValue(editorElementDTO.getEnvironmentValue());
  347. ruleElementDTO.setEnvironmentDataType(ruleElementDTO.getEnvironmentDataType());
  348. ruleElementDTO.setEnvironmentList(value);
  349. ruleElementDTO.setFunctionOnEnvironment(function);
  350. }
  351. /**
  352. * Helper method to create SOA policy
  353. *
  354. * @param value
  355. * @param dataType
  356. * @return
  357. */
  358. private static String findFunction(String value, String dataType) {
  359. if (value == null) {
  360. return PolicyConstants.Functions.FUNCTION_EQUAL;
  361. }
  362. value = value.replace("&gt;", ">");
  363. value = value.replace("&lt;", "<");
  364. // only time range finction are valid for following data types
  365. if (PolicyConstants.DataType.DATE.equals(dataType) ||
  366. PolicyConstants.DataType.INT.equals(dataType) ||
  367. PolicyConstants.DataType.TIME.equals(dataType) ||
  368. PolicyConstants.DataType.DATE_TIME.equals(dataType) ||
  369. PolicyConstants.DataType.DOUBLE.equals(dataType) ||
  370. PolicyConstants.DataType.STRING.equals(dataType)) {
  371. if (value.startsWith(PolicyEditorConstants.FunctionIdentifier.EQUAL_RANGE)) {
  372. if (value.contains(PolicyEditorConstants.FunctionIdentifier.RANGE_CLOSE)) {
  373. return PolicyConstants.Functions.FUNCTION_GREATER_EQUAL_AND_LESS;
  374. } else {
  375. return PolicyConstants.Functions.FUNCTION_GREATER_EQUAL_AND_LESS_EQUAL;
  376. }
  377. }
  378. if (value.startsWith(PolicyEditorConstants.FunctionIdentifier.RANGE)) {
  379. if (value.contains(PolicyEditorConstants.FunctionIdentifier.EQUAL_RANGE_CLOSE)) {
  380. return PolicyConstants.Functions.FUNCTION_GREATER_AND_LESS_EQUAL;
  381. } else {
  382. return PolicyConstants.Functions.FUNCTION_GREATER_AND_LESS;
  383. }
  384. }
  385. if (value.startsWith(PolicyEditorConstants.FunctionIdentifier.GREATER)) {
  386. return PolicyConstants.Functions.FUNCTION_GREATER;
  387. } else if (value.startsWith(PolicyEditorConstants.FunctionIdentifier.GREATER_EQUAL)) {
  388. return PolicyConstants.Functions.FUNCTION_GREATER_EQUAL;
  389. } else if (value.startsWith(PolicyEditorConstants.FunctionIdentifier.LESS)) {
  390. return PolicyConstants.Functions.FUNCTION_LESS;
  391. } else if (value.startsWith(PolicyEditorConstants.FunctionIdentifier.LESS_EQUAL)) {
  392. return PolicyConstants.Functions.FUNCTION_LESS_EQUAL;
  393. }
  394. }
  395. if (value.startsWith(PolicyEditorConstants.FunctionIdentifier.REGEX)) {
  396. return PolicyConstants.Functions.FUNCTION_EQUAL_MATCH_REGEXP;
  397. }
  398. if (value.contains(PolicyEditorConstants.FunctionIdentifier.OR)) {
  399. return PolicyConstants.Functions.FUNCTION_AT_LEAST_ONE;
  400. }
  401. if (value.contains(PolicyEditorConstants.FunctionIdentifier.AND)) {
  402. return PolicyConstants.Functions.FUNCTION_SET_EQUALS;
  403. }
  404. return PolicyConstants.Functions.FUNCTION_EQUAL;
  405. }
  406. /**
  407. * Helper method to create SOA policy
  408. *
  409. * @param value
  410. * @return
  411. */
  412. private static String findAttributeValue(String value) {
  413. if (value == null) {
  414. return null;
  415. }
  416. value = value.replace("&gt;", ">");
  417. value = value.replace("&lt;", "<");
  418. if (value.startsWith(PolicyEditorConstants.FunctionIdentifier.EQUAL_RANGE) ||
  419. value.startsWith(PolicyEditorConstants.FunctionIdentifier.RANGE) ||
  420. value.startsWith(PolicyEditorConstants.FunctionIdentifier.REGEX)) {
  421. return value.substring(1, value.length() - 1).trim();
  422. } else if (value.startsWith(PolicyEditorConstants.FunctionIdentifier.GREATER) ||
  423. value.startsWith(PolicyEditorConstants.FunctionIdentifier.LESS)) {
  424. return value.substring(1).trim();
  425. } else if (value.startsWith(PolicyEditorConstants.FunctionIdentifier.GREATER_EQUAL) ||
  426. value.startsWith(PolicyEditorConstants.FunctionIdentifier.LESS_EQUAL)) {
  427. return value.substring(2).trim();
  428. }
  429. if (value.contains(PolicyEditorConstants.FunctionIdentifier.AND)) {
  430. value = value.replace(PolicyEditorConstants.FunctionIdentifier.AND,
  431. PolicyEditorConstants.ATTRIBUTE_SEPARATOR);
  432. }
  433. if (value.contains(PolicyEditorConstants.FunctionIdentifier.OR)) {
  434. value = value.replace(PolicyEditorConstants.FunctionIdentifier.OR,
  435. PolicyEditorConstants.ATTRIBUTE_SEPARATOR);
  436. }
  437. return value.trim();
  438. }
  439. // TODO for what?
  440. // public static String createRules(List<SimplePolicyEditorElementDTO> elementDTOs, Document doc)
  441. // throws PolicyEditorException {
  442. //
  443. // List<BasicRuleDTO> ruleElementDTOs = new ArrayList<BasicRuleDTO>();
  444. // if(elementDTOs != null){
  445. // int ruleNo = 1;
  446. // for(SimplePolicyEditorElementDTO dto : elementDTOs){
  447. // BasicRuleDTO ruleElementDTO = new BasicRuleDTO();
  448. //
  449. // if(dto.getResourceValue() != null && dto.getResourceValue().trim().length() > 0 &&
  450. // !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getResourceValue().trim())){
  451. // ruleElementDTO.setResourceDataType(PolicyEditorConstants.DataType.STRING);
  452. // ruleElementDTO.setResourceId(PolicyEditorConstants.RESOURCE_ID_DEFAULT);
  453. // ruleElementDTO.setResourceList(dto.getResourceValue());
  454. // ruleElementDTO.setFunctionOnResources(getBasicPolicyEditorFunction(dto.
  455. // getFunctionOnResources()));
  456. // }
  457. //
  458. // if(dto.getUserAttributeValue() != null && dto.getUserAttributeValue().trim().length() > 0 &&
  459. // !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getUserAttributeValue().trim())){
  460. // ruleElementDTO.setSubjectDataType(PolicyEditorConstants.DataType.STRING);
  461. // ruleElementDTO.setSubjectId(dto.getUserAttributeId());
  462. // ruleElementDTO.setSubjectList(dto.getUserAttributeValue());
  463. // ruleElementDTO.setFunctionOnSubjects(getBasicPolicyEditorFunction(dto.
  464. // getFunctionOnUsers()));
  465. // }
  466. //
  467. // if(dto.getActionValue() != null && dto.getActionValue().trim().length() > 0 &&
  468. // !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getActionValue().trim())){
  469. // ruleElementDTO.setActionDataType(PolicyEditorConstants.DataType.STRING);
  470. // ruleElementDTO.setActionList(dto.getActionValue());
  471. // ruleElementDTO.setActionId(PolicyEditorConstants.ACTION_ID_DEFAULT);
  472. // ruleElementDTO.setFunctionOnActions(getBasicPolicyEditorFunction(dto.
  473. // getFunctionOnActions()));
  474. // }
  475. //
  476. // if(dto.getEnvironmentValue() != null && dto.getEnvironmentValue().trim().length() > 0 &&
  477. // !PolicyEditorConstants.FunctionIdentifier.ANY.equals(dto.getEnvironmentValue().trim())){
  478. // ruleElementDTO.setEnvironmentId(dto.getEnvironmentId());
  479. // ruleElementDTO.setEnvironmentList(dto.getEnvironmentValue());
  480. // ruleElementDTO.setEnvironmentDataType(PolicyEditorConstants.DataType.STRING);
  481. // ruleElementDTO.setFunctionOnEnvironment(getBasicPolicyEditorFunction(dto.
  482. // getFunctionOnEnvironments()));
  483. // }
  484. //
  485. // if(dto.getOperationType() != null && PolicyEditorConstants.PreFunctions.CAN_DO.
  486. // equals(dto.getOperationType().trim())){
  487. // ruleElementDTO.setRuleEffect(PolicyEditorConstants.RULE_EFFECT_PERMIT);
  488. // } else {
  489. // ruleElementDTO.setRuleEffect(PolicyEditorConstants.RULE_EFFECT_DENY);
  490. // }
  491. // ruleElementDTO.setRuleId("Rule-" + System.currentTimeMillis() + "-" + ruleNo);
  492. // ruleElementDTOs.add(ruleElementDTO);
  493. // ruleNo ++;
  494. // }
  495. // }
  496. //
  497. // if(ruleElementDTOs.size() > 0){
  498. // for(BasicRuleDTO dto : ruleElementDTOs){
  499. // Element rule = null;
  500. // try {
  501. // rule = BasicPolicyHelper.createRuleElement(dto, doc);
  502. // } catch (PolicyBuilderException e) {
  503. // throw new PolicyEditorException("Error while creating rule element");
  504. // }
  505. // doc.appendChild(rule);
  506. // }
  507. // }
  508. //
  509. // return PolicyCreatorUtil.getStringFromDocument(doc);
  510. // }
  511. /**
  512. * Creates DOM representation of the XACML rule element.
  513. *
  514. * @param ruleDTO RuleDTO
  515. * @return
  516. * @throws PolicyEditorException throws
  517. */
  518. public static RuleElementDTO createRuleElementDTO(RuleDTO ruleDTO) throws PolicyEditorException {
  519. RuleElementDTO ruleElementDTO = new RuleElementDTO();
  520. ruleElementDTO.setRuleId(ruleDTO.getRuleId());
  521. ruleElementDTO.setRuleEffect(ruleDTO.getRuleEffect());
  522. TargetDTO targetDTO = ruleDTO.getTargetDTO();
  523. List<ExtendAttributeDTO> dynamicAttributeDTOs = ruleDTO.getAttributeDTOs();
  524. List<ObligationDTO> obligationDTOs = ruleDTO.getObligationDTOs();
  525. if (dynamicAttributeDTOs != null && dynamicAttributeDTOs.size() > 0) {
  526. Map<String, ExtendAttributeDTO> dtoMap = new HashMap<String, ExtendAttributeDTO>();
  527. //1st creating map of dynamic attribute elements
  528. for (ExtendAttributeDTO dto : dynamicAttributeDTOs) {
  529. dtoMap.put("${" + dto.getId().trim() + "}", dto);
  530. }
  531. //creating map of apply element with identifier
  532. for (ExtendAttributeDTO dto : dynamicAttributeDTOs) {
  533. ApplyElementDTO applyElementDTO = createApplyElement(dto, dtoMap);
  534. if (applyElementDTO == null) {
  535. continue;
  536. }
  537. applyElementMap.put("${" + dto.getId().trim() + "}", applyElementDTO);
  538. }
  539. }
  540. if (targetDTO != null && targetDTO.getRowDTOList() != null && targetDTO.getRowDTOList().size() > 0) {
  541. TargetElementDTO targetElementDTO = createTargetElementDTO(ruleDTO.getTargetDTO());
  542. if (targetElementDTO != null) {
  543. ruleElementDTO.setTargetElementDTO(targetElementDTO);
  544. }
  545. }
  546. if (ruleDTO.getRowDTOList() != null && ruleDTO.getRowDTOList().size() > 0) {
  547. ConditionElementDT0 conditionElementDT0 = createConditionDTO(ruleDTO.getRowDTOList());
  548. if (conditionElementDT0 != null) {
  549. ruleElementDTO.setConditionElementDT0(conditionElementDT0);
  550. }
  551. }
  552. if (obligationDTOs != null && obligationDTOs.size() > 0) {
  553. for (ObligationDTO obligationDTO : obligationDTOs) {
  554. ObligationElementDTO elementDTO = createObligationElement(obligationDTO);
  555. if (elementDTO != null) {
  556. ruleElementDTO.addObligationElementDTO(elementDTO);
  557. }
  558. }
  559. }
  560. return ruleElementDTO;
  561. }
  562. /**
  563. * creates DOM representation of the XACML obligation/advice element.
  564. *
  565. * @param obligationDTOs List of ObligationDTO
  566. * @return
  567. * @throws PolicyEditorException throws
  568. */
  569. public static List<ObligationElementDTO> createObligation(List<ObligationDTO> obligationDTOs)
  570. throws PolicyEditorException {
  571. List<ObligationElementDTO> obligationElementDTOs = new ArrayList<ObligationElementDTO>();
  572. List<Element> returnList = new ArrayList<Element>();
  573. if (obligationDTOs != null) {
  574. for (ObligationDTO obligationDTO : obligationDTOs) {
  575. ObligationElementDTO elementDTO = createObligationElement(obligationDTO);
  576. if (elementDTO != null) {
  577. obligationElementDTOs.add(elementDTO);
  578. }
  579. }
  580. }
  581. return obligationElementDTOs;
  582. }
  583. /**
  584. * @param dynamicAttributeDTO
  585. * @param map
  586. * @return
  587. */
  588. private static ApplyElementDTO createApplyElement(ExtendAttributeDTO dynamicAttributeDTO,
  589. Map<String, ExtendAttributeDTO> map) {
  590. if (PolicyEditorConstants.DYNAMIC_SELECTOR_CATEGORY.equals(dynamicAttributeDTO.getSelector())) {
  591. String category = dynamicAttributeDTO.getCategory();
  592. String attributeId = dynamicAttributeDTO.getAttributeId();
  593. String attributeDataType = dynamicAttributeDTO.getDataType();
  594. if (category != null && category.trim().length() > 0 && attributeDataType != null &&
  595. attributeDataType.trim().length() > 0) {
  596. AttributeDesignatorDTO designatorDTO = new AttributeDesignatorDTO();
  597. designatorDTO.setCategory(category);
  598. designatorDTO.setAttributeId(attributeId);
  599. designatorDTO.setDataType(attributeDataType);
  600. designatorDTO.setMustBePresent("true");
  601. ApplyElementDTO applyElementDTO = new ApplyElementDTO();
  602. applyElementDTO.setAttributeDesignators(designatorDTO);
  603. applyElementDTO.setFunctionId(processFunction("bag", attributeDataType));
  604. return applyElementDTO;
  605. }
  606. } else {
  607. String function = dynamicAttributeDTO.getFunction();
  608. String attributeValue = dynamicAttributeDTO.getAttributeValue();
  609. String attributeId = dynamicAttributeDTO.getAttributeId();
  610. String attributeDataType = dynamicAttributeDTO.getDataType();
  611. if (attributeValue != null && function != null) {
  612. String[] values = attributeValue.split(",");
  613. if (values != null && values.length > 0) {
  614. if (function.contains("concatenate")) {
  615. ApplyElementDTO applyElementDTO = new ApplyElementDTO();
  616. applyElementDTO.setFunctionId(processFunction(function, attributeDataType, "2.0"));
  617. // there can be any number of inputs
  618. for (String value : values) {
  619. if (map.containsKey(value)) {
  620. applyElementDTO.setApplyElement(createApplyElement(map.get(value), map));
  621. } else {
  622. AttributeValueElementDTO valueElementDTO = new AttributeValueElementDTO();
  623. valueElementDTO.setAttributeDataType(attributeDataType);
  624. valueElementDTO.setAttributeValue(value);
  625. applyElementDTO.setAttributeValueElementDTO(valueElementDTO);
  626. }
  627. }
  628. return applyElementDTO;
  629. }
  630. }
  631. }
  632. }
  633. return null;
  634. }
  635. private static ObligationElementDTO createObligationElement(ObligationDTO obligationDTO) {
  636. String id = obligationDTO.getObligationId();
  637. String effect = obligationDTO.getEffect();
  638. String type = obligationDTO.getType();
  639. if (id != null && id.trim().length() > 0 && effect != null) {
  640. ObligationElementDTO elementDTO = new ObligationElementDTO();
  641. elementDTO.setId(id);
  642. elementDTO.setEffect(effect);
  643. if ("Advice".equals(type)) {
  644. elementDTO.setType(ObligationElementDTO.ADVICE);
  645. } else {
  646. elementDTO.setType(ObligationElementDTO.OBLIGATION);
  647. }
  648. String attributeValue = obligationDTO.getAttributeValue();
  649. String attributeDataType = obligationDTO.getAttributeValueDataType();
  650. String resultingAttributeId = obligationDTO.getResultAttributeId();
  651. if (attributeValue != null && attributeValue.trim().length() > 0 &&
  652. resultingAttributeId != null && resultingAttributeId.trim().length() > 0) {
  653. AttributeAssignmentElementDTO assignmentElementDTO = new
  654. AttributeAssignmentElementDTO();
  655. assignmentElementDTO.setAttributeId(resultingAttributeId);
  656. if (attributeValue.contains(",")) {
  657. String[] values = attributeValue.split(",");
  658. ApplyElementDTO applyElementDTO = new ApplyElementDTO();
  659. applyElementDTO.setFunctionId(processFunction("bag", attributeDataType));
  660. for (String value : values) {
  661. if (applyElementMap.containsKey(value)) {
  662. applyElementDTO.setApplyElement(applyElementMap.get(value));
  663. } else {
  664. AttributeValueElementDTO valueElementDTO = new AttributeValueElementDTO();
  665. valueElementDTO.setAttributeDataType(attributeDataType);
  666. valueElementDTO.setAttributeValue(value);
  667. applyElementDTO.setAttributeValueElementDTO(valueElementDTO);
  668. }
  669. }
  670. assignmentElementDTO.setApplyElementDTO(applyElementDTO);
  671. } else {
  672. if (applyElementMap.containsKey(attributeValue)) {
  673. assignmentElementDTO.setApplyElementDTO(applyElementMap.get(attributeValue));
  674. } else {
  675. AttributeValueElementDTO valueElementDTO = new AttributeValueElementDTO();
  676. valueElementDTO.setAttributeDataType(attributeDataType);
  677. valueElementDTO.setAttributeValue(attributeValue);
  678. assignmentElementDTO.setValueElementDTO(valueElementDTO);
  679. }
  680. }
  681. elementDTO.addAssignmentElementDTO(assignmentElementDTO);
  682. }
  683. return elementDTO;
  684. }
  685. return null;
  686. }
  687. /**
  688. * Creates <code>ConditionElementDT0</code> Object that represents the XACML Condition element
  689. *
  690. * @param rowDTOs
  691. * @return
  692. * @throws PolicyEditorException
  693. */
  694. public static ConditionElementDT0 createConditionDTO(List<RowDTO> rowDTOs) throws PolicyEditorException {
  695. ConditionElementDT0 rootApplyDTO = new ConditionElementDT0();
  696. ArrayList<RowDTO> temp = new ArrayList<RowDTO>();
  697. Set<ArrayList<RowDTO>> listSet = new HashSet<ArrayList<RowDTO>>();
  698. for (int i = 0; i < rowDTOs.size(); i++) {
  699. if (i == 0) {
  700. temp.add(rowDTOs.get(0));
  701. continue;
  702. }
  703. String combineFunction = rowDTOs.get(i - 1).getCombineFunction();
  704. if (PolicyEditorConstants.COMBINE_FUNCTION_AND.equals(combineFunction)) {
  705. temp.add(rowDTOs.get(i));
  706. }
  707. if (PolicyEditorConstants.COMBINE_FUNCTION_OR.equals(combineFunction)) {
  708. listSet.add(temp);
  709. temp = new ArrayList<RowDTO>();
  710. temp.add(rowDTOs.get(i));
  711. }
  712. }
  713. listSet.add(temp);
  714. if (listSet.size() > 1) {
  715. ApplyElementDTO orApplyDTO = new ApplyElementDTO();
  716. orApplyDTO.setFunctionId(processFunction("or"));
  717. for (ArrayList<RowDTO> rowDTOArrayList : listSet) {
  718. if (rowDTOArrayList.size() > 1) {
  719. ApplyElementDTO andApplyDTO = new ApplyElementDTO();
  720. andApplyDTO.setFunctionId(processFunction("and"));
  721. for (RowDTO rowDTO : rowDTOArrayList) {
  722. ApplyElementDTO applyElementDTO = createApplyElement(rowDTO);
  723. andApplyDTO.setApplyElement(applyElementDTO);
  724. }
  725. orApplyDTO.setApplyElement(andApplyDTO);
  726. } else if (rowDTOArrayList.size() == 1) {
  727. RowDTO rowDTO = rowDTOArrayList.get(0);
  728. ApplyElementDTO andApplyDTO = createApplyElement(rowDTO);
  729. orApplyDTO.setApplyElement(andApplyDTO);
  730. }
  731. }
  732. rootApplyDTO.setApplyElement(orApplyDTO);
  733. } else if (listSet.size() == 1) {
  734. ArrayList<RowDTO> rowDTOArrayList = listSet.iterator().next();
  735. if (rowDTOArrayList.size() > 1) {
  736. ApplyElementDTO andApplyDTO = new ApplyElementDTO();
  737. andApplyDTO.setFunctionId(processFunction("and"));
  738. for (RowDTO rowDTO : rowDTOArrayList) {
  739. ApplyElementDTO applyElementDTO = createApplyElement(rowDTO);
  740. andApplyDTO.setApplyElement(applyElementDTO);
  741. }
  742. rootApplyDTO.setApplyElement(andApplyDTO);
  743. } else if (rowDTOArrayList.size() == 1) {
  744. RowDTO rowDTO = rowDTOArrayList.get(0);
  745. ApplyElementDTO andApplyDTO = createApplyElement(rowDTO);
  746. rootApplyDTO.setApplyElement(andApplyDTO);
  747. }
  748. }
  749. return rootApplyDTO;
  750. }
  751. /**
  752. * Creates <code>ApplyElementDTO</code> Object that represents the XACML Apply element
  753. *
  754. * @param rowDTO
  755. * @return
  756. * @throws PolicyEditorException
  757. */
  758. public static ApplyElementDTO createApplyElement(RowDTO rowDTO) throws PolicyEditorException {
  759. String preFunction = rowDTO.getPreFunction();
  760. String function = rowDTO.getFunction();
  761. String dataType = rowDTO.getAttributeDataType();
  762. String attributeValue = rowDTO.getAttributeValue();
  763. if (function == null || function.trim().length() < 1) {
  764. throw new PolicyEditorException("Can not create Apply element:" +
  765. "Missing required function Id");
  766. }
  767. if (attributeValue == null || attributeValue.trim().length() < 1) {
  768. throw new PolicyEditorException("Can not create Apply element:" +
  769. "Missing required attribute value");
  770. }
  771. ApplyElementDTO applyElementDTO = null;
  772. AttributeDesignatorDTO designatorDTO = new AttributeDesignatorDTO();
  773. designatorDTO.setCategory(rowDTO.getCategory());
  774. designatorDTO.setAttributeId(rowDTO.getAttributeId());
  775. designatorDTO.setDataType(dataType);
  776. designatorDTO.setMustBePresent("true");
  777. if (rowDTO.getFunction().contains("less") || rowDTO.getFunction().contains("greater")) {
  778. applyElementDTO = processGreaterLessThanFunctions(function, dataType, attributeValue,
  779. designatorDTO);
  780. } else if (PolicyConstants.Functions.FUNCTION_EQUAL.equals(rowDTO.getFunction())) {
  781. applyElementDTO = processEqualFunctions(function, dataType, attributeValue, designatorDTO);
  782. } else {
  783. applyElementDTO = processBagFunction(function, dataType, attributeValue, designatorDTO);
  784. }
  785. if (PolicyConstants.PreFunctions.PRE_FUNCTION_NOT.equals(preFunction)) {
  786. ApplyElementDTO notApplyElementDTO = new ApplyElementDTO();
  787. notApplyElementDTO.setFunctionId(processFunction("not"));
  788. notApplyElementDTO.setApplyElement(applyElementDTO);
  789. applyElementDTO = notApplyElementDTO;
  790. }
  791. return applyElementDTO;
  792. }
  793. /**
  794. * Creates <code>TargetElementDTO</code> Object that represents the XACML Target element
  795. *
  796. * @param targetDTO
  797. * @return
  798. */
  799. public static TargetElementDTO createTargetElementDTO(TargetDTO targetDTO) {
  800. AllOfElementDTO allOfElementDTO = new AllOfElementDTO();
  801. AnyOfElementDTO anyOfElementDTO = new AnyOfElementDTO();
  802. TargetElementDTO targetElementDTO = new TargetElementDTO();
  803. List<RowDTO> rowDTOs = targetDTO.getRowDTOList();
  804. ArrayList<RowDTO> tempRowDTOs = new ArrayList<RowDTO>();
  805. // pre function processing
  806. for (RowDTO rowDTO : rowDTOs) {
  807. if (PolicyEditorConstants.PreFunctions.PRE_FUNCTION_ARE.equals(rowDTO.getPreFunction())) {
  808. String[] attributeValues = rowDTO.getAttributeValue().split(PolicyEditorConstants.ATTRIBUTE_SEPARATOR);
  809. allOfElementDTO = new AllOfElementDTO();
  810. for (int j = 0; j < attributeValues.length; j++) {
  811. RowDTO newDto = new RowDTO(rowDTO);
  812. newDto.setAttributeValue(attributeValues[j]);
  813. if (j != attributeValues.length - 1) {
  814. newDto.setCombineFunction(PolicyEditorConstants.COMBINE_FUNCTION_AND);
  815. }
  816. tempRowDTOs.add(newDto);
  817. }
  818. } else {
  819. tempRowDTOs.add(rowDTO);
  820. }
  821. }
  822. if (tempRowDTOs.size() > 0) {
  823. for (int i = 0; i < tempRowDTOs.size(); i++) {
  824. if (i == 0) {
  825. MatchElementDTO matchElementDTO = createTargetMatch(tempRowDTOs.get(0));
  826. if (matchElementDTO != null) {
  827. allOfElementDTO.addMatchElementDTO(matchElementDTO);
  828. }
  829. continue;
  830. }
  831. String combineFunction = tempRowDTOs.get(i - 1).getCombineFunction();
  832. if (PolicyEditorConstants.COMBINE_FUNCTION_AND.equals(combineFunction)) {
  833. MatchElementDTO matchElementDTO = createTargetMatch(tempRowDTOs.get(i));
  834. if (matchElementDTO != null) {
  835. allOfElementDTO.addMatchElementDTO(matchElementDTO);
  836. }
  837. }
  838. if (PolicyEditorConstants.COMBINE_FUNCTION_OR.equals(combineFunction)) {
  839. anyOfElementDTO.addAllOfElementDTO(allOfElementDTO);
  840. allOfElementDTO = new AllOfElementDTO();
  841. MatchElementDTO matchElementDTO = createTargetMatch(tempRowDTOs.get(i));
  842. if (matchElementDTO != null) {
  843. allOfElementDTO.addMatchElementDTO(matchElementDTO);
  844. }
  845. }
  846. }
  847. anyOfElementDTO.addAllOfElementDTO(allOfElementDTO);
  848. targetElementDTO.addAnyOfElementDTO(anyOfElementDTO);
  849. }
  850. return targetElementDTO;
  851. }
  852. /**
  853. * process Bag functions
  854. *
  855. * @param function
  856. * @param dataType
  857. * @param attributeValue
  858. * @param designatorDTO
  859. * @return
  860. */
  861. public static ApplyElementDTO processBagFunction(String function, String dataType,
  862. String attributeValue, AttributeDesignatorDTO designatorDTO) {
  863. if (PolicyConstants.Functions.FUNCTION_IS_IN.equals(function)) {
  864. ApplyElementDTO applyElementDTO = new ApplyElementDTO();
  865. applyElementDTO.setFunctionId(processFunction("is-in", dataType));
  866. if (applyElementMap.containsKey(attributeValue)) {
  867. applyElementDTO.setApplyElement(applyElementMap.get(attributeValue));
  868. } else {
  869. AttributeValueElementDTO valueElementDTO = new AttributeValueElementDTO();
  870. valueElementDTO.setAttributeDataType(dataType);
  871. valueElementDTO.setAttributeValue(attributeValue);
  872. applyElementDTO.setAttributeValueElementDTO(valueElementDTO);
  873. }
  874. applyElementDTO.setAttributeDesignators(designatorDTO);
  875. return applyElementDTO;
  876. } else if (PolicyConstants.Functions.FUNCTION_AT_LEAST_ONE.equals(function) ||
  877. PolicyConstants.Functions.FUNCTION_SET_EQUALS.equals(function)) {
  878. ApplyElementDTO applyElementDTO = new ApplyElementDTO();
  879. if (PolicyConstants.Functions.FUNCTION_AT_LEAST_ONE.equals(function)) {
  880. applyElementDTO.setFunctionId(processFunction("at-least-one-member-of", dataType));
  881. } else {
  882. applyElementDTO.setFunctionId(processFunction("set-equals", dataType));
  883. }
  884. String[] values = attributeValue.split(PolicyEditorConstants.ATTRIBUTE_SEPARATOR);
  885. ApplyElementDTO applyBagElementDTO = new ApplyElementDTO();
  886. applyBagElementDTO.setFunctionId(processFunction("bag", dataType));
  887. for (String value : values) {
  888. if (applyElementMap.containsKey(value)) {
  889. applyBagElementDTO.setApplyElement(applyElementMap.get(value));
  890. } else {
  891. AttributeValueElementDTO valueElementDTO = new AttributeValueElementDTO();
  892. valueElementDTO.setAttributeDataType(dataType);
  893. valueElementDTO.setAttributeValue(value);
  894. applyBagElementDTO.setAttributeValueElementDTO(valueElementDTO);
  895. }
  896. }
  897. applyElementDTO.setAttributeDesignators(designatorDTO);
  898. applyElementDTO.setApplyElement(applyBagElementDTO);
  899. return applyElementDTO;
  900. }
  901. return null;
  902. }
  903. /**
  904. * Process equal function
  905. *
  906. * @param function
  907. * @param dataType

Large files files are truncated, but you can click here to view the full file