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

/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
  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
  908. * @param attributeValue
  909. * @param designatorDTO
  910. * @return
  911. */
  912. public static ApplyElementDTO processEqualFunctions(String function, String dataType,
  913. String attributeValue, AttributeDesignatorDTO designatorDTO) {
  914. if (PolicyConstants.Functions.FUNCTION_EQUAL.equals(function)) {
  915. ApplyElementDTO applyElementDTO = new ApplyElementDTO();
  916. if (PolicyEditorConstants.DataType.DAY_TIME_DURATION.equals(dataType) ||
  917. PolicyEditorConstants.DataType.YEAR_MONTH_DURATION.equals(dataType)) {
  918. applyElementDTO.setFunctionId(processFunction("equal", dataType, "3.0"));
  919. } else {
  920. applyElementDTO.setFunctionId(processFunction("equal", dataType));
  921. }
  922. ApplyElementDTO oneAndOnlyApplyElement = new ApplyElementDTO();
  923. oneAndOnlyApplyElement.setFunctionId(processFunction("one-and-only", dataType));
  924. oneAndOnlyApplyElement.setAttributeDesignators(designatorDTO);
  925. if (applyElementMap.containsKey(attributeValue)) {
  926. applyElementDTO.setApplyElement(applyElementMap.get(attributeValue));
  927. } else {
  928. AttributeValueElementDTO valueElementDTO = new AttributeValueElementDTO();
  929. valueElementDTO.setAttributeDataType(dataType);
  930. valueElementDTO.setAttributeValue(attributeValue);
  931. applyElementDTO.setAttributeValueElementDTO(valueElementDTO);
  932. }
  933. applyElementDTO.setApplyElement(oneAndOnlyApplyElement);
  934. return applyElementDTO;
  935. }
  936. return null;
  937. }
  938. /**
  939. * Process less than and greater than functions
  940. *
  941. * @param function
  942. * @param dataType
  943. * @param attributeValue
  944. * @param designatorDTO
  945. * @return
  946. * @throws PolicyEditorException
  947. */
  948. public static ApplyElementDTO processGreaterLessThanFunctions(String function, String dataType,
  949. String attributeValue, AttributeDesignatorDTO designatorDTO)
  950. throws PolicyEditorException {
  951. String[] values = attributeValue.split(PolicyEditorConstants.ATTRIBUTE_SEPARATOR);
  952. if (PolicyConstants.Functions.FUNCTION_GREATER_EQUAL_AND_LESS_EQUAL.equals(function) ||
  953. PolicyConstants.Functions.FUNCTION_GREATER_AND_LESS_EQUAL.equals(function) ||
  954. PolicyConstants.Functions.FUNCTION_GREATER_EQUAL_AND_LESS.equals(function) ||
  955. PolicyConstants.Functions.FUNCTION_GREATER_AND_LESS.equals(function)) {
  956. String leftValue;
  957. String rightValue;
  958. if (values.length == 2) {
  959. leftValue = values[0].trim();
  960. rightValue = values[1].trim();
  961. } else {
  962. throw new PolicyEditorException("Can not create Apply element:" +
  963. "Missing required attribute values for function : " + function);
  964. }
  965. ApplyElementDTO andApplyElement = new ApplyElementDTO();
  966. andApplyElement.setFunctionId(processFunction("and"));
  967. ApplyElementDTO greaterThanApplyElement = new ApplyElementDTO();
  968. if (PolicyConstants.Functions.FUNCTION_GREATER_AND_LESS.equals(function) ||
  969. PolicyConstants.Functions.FUNCTION_GREATER_AND_LESS_EQUAL.equals(function)) {
  970. greaterThanApplyElement.setFunctionId(processFunction("greater-than", dataType));
  971. } else {
  972. greaterThanApplyElement.setFunctionId(processFunction("greater-than-or-equal", dataType));
  973. }
  974. ApplyElementDTO lessThanApplyElement = new ApplyElementDTO();
  975. if (PolicyConstants.Functions.FUNCTION_GREATER_AND_LESS.equals(function) ||
  976. PolicyConstants.Functions.FUNCTION_GREATER_EQUAL_AND_LESS.equals(function)) {
  977. lessThanApplyElement.setFunctionId(processFunction("less-than", dataType));
  978. } else {
  979. lessThanApplyElement.setFunctionId(processFunction("less-than-or-equal", dataType));
  980. }
  981. ApplyElementDTO oneAndOnlyApplyElement = new ApplyElementDTO();
  982. oneAndOnlyApplyElement.setFunctionId(processFunction("one-and-only", dataType));
  983. oneAndOnlyApplyElement.setAttributeDesignators(designatorDTO);
  984. AttributeValueElementDTO leftValueElementDTO = new AttributeValueElementDTO();
  985. leftValueElementDTO.setAttributeDataType(dataType);
  986. leftValueElementDTO.setAttributeValue(leftValue);
  987. AttributeValueElementDTO rightValueElementDTO = new AttributeValueElementDTO();
  988. rightValueElementDTO.setAttributeDataType(dataType);
  989. rightValueElementDTO.setAttributeValue(rightValue);
  990. greaterThanApplyElement.setApplyElement(oneAndOnlyApplyElement);
  991. greaterThanApplyElement.setAttributeValueElementDTO(leftValueElementDTO);
  992. lessThanApplyElement.setApplyElement(oneAndOnlyApplyElement);
  993. lessThanApplyElement.setAttributeValueElementDTO(rightValueElementDTO);
  994. andApplyElement.setApplyElement(greaterThanApplyElement);
  995. andApplyElement.setApplyElement(lessThanApplyElement);
  996. return andApplyElement;
  997. } else {
  998. ApplyElementDTO applyElementDTO = new ApplyElementDTO();
  999. if (PolicyConstants.Functions.FUNCTION_GREATER.equals(function)) {
  1000. applyElementDTO.setFunctionId(processFunction("greater-than", dataType));
  1001. } else if (PolicyConstants.Functions.FUNCTION_GREATER_EQUAL.equals(function)) {
  1002. applyElementDTO.setFunctionId(processFunction("greater-than-or-equal", dataType));
  1003. } else if (PolicyConstants.Functions.FUNCTION_LESS.equals(function)) {
  1004. applyElementDTO.setFunctionId(processFunction("less-than", dataType));
  1005. } else if (PolicyConstants.Functions.FUNCTION_LESS_EQUAL.equals(function)) {
  1006. applyElementDTO.setFunctionId(processFunction("less-than-or-equal", dataType));
  1007. } else {
  1008. throw new PolicyEditorException("Can not create Apply element:" +
  1009. "Invalid function : " + function);
  1010. }
  1011. ApplyElementDTO oneAndOnlyApplyElement = new ApplyElementDTO();
  1012. oneAndOnlyApplyElement.setFunctionId(processFunction("one-and-only", dataType));
  1013. oneAndOnlyApplyElement.setAttributeDesignators(designatorDTO);
  1014. AttributeValueElementDTO valueElementDTO = new AttributeValueElementDTO();
  1015. valueElementDTO.setAttributeDataType(dataType);
  1016. valueElementDTO.setAttributeValue(values[0]);
  1017. applyElementDTO.setApplyElement(oneAndOnlyApplyElement);
  1018. applyElementDTO.setAttributeValueElementDTO(valueElementDTO);
  1019. return applyElementDTO;
  1020. }
  1021. }
  1022. /**
  1023. * Helper method to create full XACML function URI
  1024. *
  1025. * @param function
  1026. * @param type
  1027. * @param version
  1028. * @return
  1029. */
  1030. private static String processFunction(String function, String type, String version) {
  1031. return "urn:oasis:names:tc:xacml:" + version + ":function:" + getDataTypePrefix(type) +
  1032. "-" + function;
  1033. }
  1034. /**
  1035. * Helper method to create full XACML function URI
  1036. *
  1037. * @param function
  1038. * @return
  1039. */
  1040. private static String processFunction(String function) {
  1041. return "urn:oasis:names:tc:xacml:1.0:function:" + function;
  1042. }
  1043. /**
  1044. * Helper method to create full XACML function URI
  1045. *
  1046. * @param function
  1047. * @param type
  1048. * @return
  1049. */
  1050. private static String processFunction(String function, String type) {
  1051. return "urn:oasis:names:tc:xacml:1.0:function:" + getDataTypePrefix(type) + "-" + function;
  1052. }
  1053. //
  1054. // /**
  1055. // * Helper method to check whether attribute value is pre-defined one
  1056. // *
  1057. // * @param value
  1058. // * @return
  1059. // */
  1060. // private static boolean isPreDefinedValue(String value){
  1061. //
  1062. // if(value != null && applyElementMap != null && applyElementMap.size() > 0){
  1063. // value = value.trim();
  1064. // if(value.startsWith("${") && value.endsWith("}")){
  1065. // value = value.substring(value.indexOf("{") + 1, value.indexOf("}"));
  1066. // return applyElementMap.containsKey(value);
  1067. // }
  1068. // }
  1069. //
  1070. // return false;
  1071. // }
  1072. //
  1073. // /**
  1074. // * Helper method to check whether attribute value is pre-defined one
  1075. // *
  1076. // * @param value
  1077. // * @param map
  1078. // * @return
  1079. // */
  1080. // private static boolean isPreDefinedValue(String value, Map<String, ExtendAttributeDTO> map){
  1081. //
  1082. // if(value != null && map != null && map.size() > 0){
  1083. // value = value.trim();
  1084. // if(value.startsWith("${") && value.endsWith("}")){
  1085. // value = value.substring(value.indexOf("{") + 1, value.indexOf("}"));
  1086. // return map.containsKey(value);
  1087. // }
  1088. // }
  1089. //
  1090. // return false;
  1091. // }
  1092. /**
  1093. * Helper method to create full XACML function URI
  1094. *
  1095. * @param dataTypeUri
  1096. * @return
  1097. */
  1098. private static String getDataTypePrefix(String dataTypeUri) {
  1099. if (dataTypeUri != null) {
  1100. if (dataTypeUri.contains("#")) {
  1101. return dataTypeUri.substring(dataTypeUri.indexOf("#") + 1);
  1102. } else if (dataTypeUri.contains(":")) {
  1103. String[] stringArray = dataTypeUri.split(":");
  1104. if (stringArray != null && stringArray.length > 0) {
  1105. return stringArray[stringArray.length - 1];
  1106. }
  1107. }
  1108. }
  1109. return dataTypeUri;
  1110. }
  1111. /**
  1112. * Creates match element
  1113. *
  1114. * @param rowDTO
  1115. * @return
  1116. */
  1117. public static MatchElementDTO createTargetMatch(RowDTO rowDTO) {
  1118. String category = rowDTO.getCategory();
  1119. String functionId = rowDTO.getFunction();
  1120. String attributeValue = rowDTO.getAttributeValue();
  1121. String attributeId = rowDTO.getAttributeId();
  1122. String dataType = rowDTO.getAttributeDataType();
  1123. MatchElementDTO matchElementDTO;
  1124. if (functionId != null && functionId.trim().length() > 0 && attributeValue != null &&
  1125. attributeValue.trim().length() > 0 && category != null &&
  1126. category.trim().length() > 0 && attributeId != null &&
  1127. attributeId.trim().length() > 0 && dataType != null &&
  1128. dataType.trim().length() > 0) {
  1129. functionId = processFunction(functionId, dataType);
  1130. matchElementDTO = new MatchElementDTO();
  1131. AttributeValueElementDTO attributeValueElementDTO = new AttributeValueElementDTO();
  1132. attributeValueElementDTO.setAttributeDataType(dataType);
  1133. attributeValueElementDTO.setAttributeValue(attributeValue.trim());
  1134. AttributeDesignatorDTO attributeDesignatorDTO = new AttributeDesignatorDTO();
  1135. attributeDesignatorDTO.setDataType(dataType);
  1136. attributeDesignatorDTO.setAttributeId(attributeId);
  1137. attributeDesignatorDTO.setCategory(category);
  1138. matchElementDTO.setMatchId(functionId);
  1139. matchElementDTO.setAttributeValueElementDTO(attributeValueElementDTO);
  1140. matchElementDTO.setAttributeDesignatorDTO(attributeDesignatorDTO);
  1141. } else {
  1142. return null; // TODO
  1143. }
  1144. return matchElementDTO;
  1145. }
  1146. /**
  1147. * This method creates a match element (such as subject,action,resource or environment) of the XACML policy
  1148. *
  1149. * @param matchElementDTO match element data object
  1150. * @param doc XML document
  1151. * @return match Element
  1152. * @throws PolicyEditorException if any error occurs
  1153. */
  1154. public static Element createMatchElement(MatchElementDTO matchElementDTO, Document doc)
  1155. throws PolicyEditorException {
  1156. Element matchElement;
  1157. if (matchElementDTO.getMatchId() != null && matchElementDTO.getMatchId().trim().length() > 0) {
  1158. matchElement = doc.createElement(PolicyEditorConstants.MATCH_ELEMENT);
  1159. matchElement.setAttribute(PolicyEditorConstants.MATCH_ID,
  1160. matchElementDTO.getMatchId());
  1161. if (matchElementDTO.getAttributeValueElementDTO() != null) {
  1162. Element attributeValueElement = createAttributeValueElement(matchElementDTO.
  1163. getAttributeValueElementDTO(), doc);
  1164. matchElement.appendChild(attributeValueElement);
  1165. }
  1166. if (matchElementDTO.getAttributeDesignatorDTO() != null) {
  1167. Element attributeDesignatorElement = createAttributeDesignatorElement(matchElementDTO.
  1168. getAttributeDesignatorDTO(), doc);
  1169. matchElement.appendChild(attributeDesignatorElement);
  1170. } else if (matchElementDTO.getAttributeSelectorDTO() != null) {
  1171. Element attributeSelectorElement = createAttributeSelectorElement(matchElementDTO.
  1172. getAttributeSelectorDTO(), doc);
  1173. matchElement.appendChild(attributeSelectorElement);
  1174. }
  1175. } else {
  1176. throw new PolicyEditorException("Can not create Match element:" +
  1177. " Required Attributes are missing");
  1178. }
  1179. return matchElement;
  1180. }
  1181. /**
  1182. * This method creates attribute value DOM element
  1183. *
  1184. * @param attributeValueElementDTO attribute value element data object
  1185. * @param doc XML document
  1186. * @return attribute value element as DOM
  1187. */
  1188. public static Element createAttributeValueElement(AttributeValueElementDTO
  1189. attributeValueElementDTO, Document doc) {
  1190. Element attributeValueElement = doc.createElement(EntitlementPolicyConstants.ATTRIBUTE_VALUE);
  1191. if (attributeValueElementDTO.getAttributeValue() != null && attributeValueElementDTO.
  1192. getAttributeValue().trim().length() > 0) {
  1193. attributeValueElement.setTextContent(attributeValueElementDTO.getAttributeValue().trim());
  1194. if (attributeValueElementDTO.getAttributeDataType() != null && attributeValueElementDTO.
  1195. getAttributeDataType().trim().length() > 0) {
  1196. attributeValueElement.setAttribute(EntitlementPolicyConstants.DATA_TYPE,
  1197. attributeValueElementDTO.getAttributeDataType());
  1198. } else {
  1199. attributeValueElement.setAttribute(EntitlementPolicyConstants.DATA_TYPE,
  1200. EntitlementPolicyConstants.STRING_DATA_TYPE);
  1201. }
  1202. }
  1203. return attributeValueElement;
  1204. }
  1205. /**
  1206. * This method creates attribute designator DOM element
  1207. *
  1208. * @param attributeDesignatorDTO attribute designator data object
  1209. * @param doc XML document
  1210. * @return attribute designator element as DOM
  1211. * @throws PolicyEditorException throws if missing required data
  1212. */
  1213. public static Element createAttributeDesignatorElement(AttributeDesignatorDTO
  1214. attributeDesignatorDTO, Document doc) throws PolicyEditorException {
  1215. Element attributeDesignatorElement;
  1216. if (attributeDesignatorDTO != null && doc != null) {
  1217. String category = attributeDesignatorDTO.getCategory();
  1218. String attributeId = attributeDesignatorDTO.getAttributeId();
  1219. String dataType = attributeDesignatorDTO.getDataType();
  1220. String mustBe = attributeDesignatorDTO.getMustBePresent();
  1221. if (category != null && category.trim().length() > 0 && attributeId != null &&
  1222. attributeId.trim().length() > 0 && dataType != null && dataType.trim().length() > 0 &&
  1223. mustBe != null && mustBe.trim().length() > 0) {
  1224. attributeDesignatorElement = doc.
  1225. createElement(PolicyEditorConstants.ATTRIBUTE_DESIGNATOR);
  1226. attributeDesignatorElement.setAttribute(PolicyEditorConstants.ATTRIBUTE_ID,
  1227. attributeId);
  1228. attributeDesignatorElement.setAttribute(PolicyEditorConstants.CATEGORY, category);
  1229. attributeDesignatorElement.setAttribute(PolicyEditorConstants.DATA_TYPE, dataType);
  1230. attributeDesignatorElement.setAttribute(PolicyEditorConstants.MUST_BE_PRESENT, mustBe);
  1231. if (attributeDesignatorDTO.getIssuer() != null && attributeDesignatorDTO.getIssuer().
  1232. trim().length() > 0) {
  1233. attributeDesignatorElement.setAttribute(EntitlementPolicyConstants.ISSUER,
  1234. attributeDesignatorDTO.getIssuer());
  1235. }
  1236. } else {
  1237. throw new PolicyEditorException("Can not create AttributeDesignator element:" +
  1238. " Required Attributes are missing");
  1239. }
  1240. } else {
  1241. throw new PolicyEditorException("Can not create AttributeDesignator element:" +
  1242. " A Null object is received");
  1243. }
  1244. return attributeDesignatorElement;
  1245. }
  1246. /**
  1247. * This method creates attribute selector DOM element
  1248. *
  1249. * @param attributeSelectorDTO attribute selector data object
  1250. * @param doc xML document
  1251. * @return attribute selector element as DOM
  1252. */
  1253. public static Element createAttributeSelectorElement(AttributeSelectorDTO attributeSelectorDTO,
  1254. Document doc) {
  1255. Element attributeSelectorElement = doc.createElement(EntitlementPolicyConstants.
  1256. ATTRIBUTE_SELECTOR);
  1257. if (attributeSelectorDTO.getAttributeSelectorRequestContextPath() != null &&
  1258. attributeSelectorDTO.getAttributeSelectorRequestContextPath().trim().length() > 0) {
  1259. attributeSelectorElement.setAttribute(EntitlementPolicyConstants.REQUEST_CONTEXT_PATH,
  1260. EntitlementPolicyConstants.ATTRIBUTE_NAMESPACE + attributeSelectorDTO.
  1261. getAttributeSelectorRequestContextPath());
  1262. if (attributeSelectorDTO.getAttributeSelectorDataType() != null &&
  1263. attributeSelectorDTO.getAttributeSelectorDataType().trim().length() > 0) {
  1264. attributeSelectorElement.setAttribute(EntitlementPolicyConstants.DATA_TYPE,
  1265. attributeSelectorDTO.getAttributeSelectorDataType());
  1266. } else {
  1267. attributeSelectorElement.setAttribute(EntitlementPolicyConstants.DATA_TYPE,
  1268. EntitlementPolicyConstants.STRING_DATA_TYPE);
  1269. }
  1270. if (attributeSelectorDTO.getAttributeSelectorMustBePresent() != null &&
  1271. attributeSelectorDTO.getAttributeSelectorMustBePresent().trim().length() > 0) {
  1272. attributeSelectorElement.setAttribute(EntitlementPolicyConstants.MUST_BE_PRESENT,
  1273. attributeSelectorDTO.getAttributeSelectorMustBePresent());
  1274. }
  1275. }
  1276. return attributeSelectorElement;
  1277. }
  1278. /**
  1279. * Modifies the user data that are got from policy editor. If there are null values for required
  1280. * things, replace them with default values
  1281. */
  1282. public static String[] processPolicySetData(PolicySetDTO policyDTO) {
  1283. TargetDTO targetDTO = policyDTO.getTargetDTO();
  1284. List<ObligationDTO> obligationDTOs = policyDTO.getObligations();
  1285. List<PolicyRefIdDTO> policyRefIdDTOs = policyDTO.getPolicyRefIdDTOs();
  1286. String policyOrder = policyDTO.getPolicyOrder();
  1287. PolicyEditorDataHolder holder = PolicyEditorEngine.getInstance().
  1288. getPolicyEditorData(EntitlementConstants.PolicyEditor.SET);
  1289. List<String> policyMetaDataList = new ArrayList<String>();
  1290. List<PolicyRefIdDTO> arrangedRefIdDTOs = new ArrayList<PolicyRefIdDTO>();
  1291. if (policyOrder != null && policyOrder.trim().length() > 0) {
  1292. String[] ruleIds = policyOrder.
  1293. split(EntitlementPolicyConstants.ATTRIBUTE_SEPARATOR);
  1294. for (String ruleId : ruleIds) {
  1295. for (PolicyRefIdDTO dto : policyRefIdDTOs) {
  1296. if (ruleId.equals(dto.getId())) {
  1297. arrangedRefIdDTOs.add(dto);
  1298. }
  1299. }
  1300. }
  1301. policyRefIdDTOs = arrangedRefIdDTOs;
  1302. }
  1303. createMetaDataFromPolicySet("policy", policyDTO, policyMetaDataList);
  1304. String algorithm = policyDTO.getPolicyCombiningAlgId();
  1305. if (algorithm != null && algorithm.trim().length() > 0) {
  1306. policyDTO.setPolicyCombiningAlgId(holder.getPolicyAlgorithmUri(algorithm));
  1307. } else {
  1308. policyDTO.setPolicyCombiningAlgId(holder.getDefaultPolicyAlgorithm());
  1309. }
  1310. if (targetDTO != null && targetDTO.getRowDTOList() != null) {
  1311. List<RowDTO> newRowDTOs = new ArrayList<RowDTO>();
  1312. for (RowDTO rowDTO : targetDTO.getRowDTOList()) {
  1313. createMetaDataFromRowDTO("target", rowDTO, policyMetaDataList);
  1314. String category = rowDTO.getCategory();
  1315. if (category == null) {
  1316. continue;
  1317. }
  1318. String attributeValue = rowDTO.getAttributeValue();
  1319. if (attributeValue == null || attributeValue.trim().length() < 1) {
  1320. continue;
  1321. }
  1322. rowDTO.setCategory(holder.getCategoryUri(category));
  1323. if (rowDTO.getAttributeDataType() == null ||
  1324. rowDTO.getAttributeDataType().trim().length() < 1 ||
  1325. rowDTO.getAttributeDataType().trim().equals("null")) {
  1326. if (holder.getDefaultDataType() != null) {
  1327. rowDTO.setAttributeDataType(holder.getDefaultDataType());
  1328. } else {
  1329. rowDTO.setAttributeDataType(PolicyEditorConstants.DataType.STRING);
  1330. }
  1331. } else {
  1332. if (holder.getDataTypeUri(rowDTO.getAttributeDataType()) != null) {
  1333. rowDTO.setAttributeDataType(holder.getDataTypeUri(rowDTO.getAttributeDataType()));
  1334. }
  1335. }
  1336. String attributeId = rowDTO.getAttributeId();
  1337. if (attributeId == null || attributeId.trim().length() < 1 ||
  1338. attributeId.trim().equals("null")) {
  1339. attributeId = holder.getCategoryDefaultAttributeId(category);
  1340. }
  1341. rowDTO.setAttributeId(holder.getAttributeIdUri(attributeId));
  1342. rowDTO.setFunction(holder.getFunctionUri(rowDTO.getFunction()));
  1343. rowDTO.setPreFunction(holder.getPreFunctionUri(rowDTO.getPreFunction()));
  1344. newRowDTOs.add(rowDTO);
  1345. }
  1346. targetDTO.setRowDTOList(newRowDTOs);
  1347. policyDTO.setTargetDTO(targetDTO);
  1348. }
  1349. if (policyRefIdDTOs != null) {
  1350. policyDTO.setPolicyRefIdDTOs(policyRefIdDTOs);
  1351. for (PolicyRefIdDTO dto : policyRefIdDTOs) {
  1352. createMetaDataFromReference("reference", dto, policyMetaDataList);
  1353. }
  1354. }
  1355. if (obligationDTOs != null) {
  1356. for (ObligationDTO dto : obligationDTOs) {
  1357. createMetaDataFromObligation("obligation", dto, policyMetaDataList);
  1358. if (dto.getAttributeValueDataType() == null ||
  1359. dto.getAttributeValueDataType().trim().length() == 0 ||
  1360. dto.getAttributeValueDataType().trim().equals("null")) {
  1361. dto.setAttributeValueDataType(PolicyEditorConstants.DataType.STRING);
  1362. }
  1363. if (dto.getResultAttributeId() == null ||
  1364. dto.getResultAttributeId().trim().length() == 0 ||
  1365. dto.getResultAttributeId().trim().equals("null")) {
  1366. // setting obligation id
  1367. dto.setResultAttributeId(dto.getObligationId());
  1368. }
  1369. }
  1370. policyDTO.setObligations(obligationDTOs);
  1371. }
  1372. return policyMetaDataList.toArray(new String[policyMetaDataList.size()]);
  1373. }
  1374. /**
  1375. * Modifies the user data that are got from policy editor. If there are null values for required
  1376. * things, replace them with default values
  1377. */
  1378. public static String[] processPolicyData(PolicyDTO policyDTO) {
  1379. TargetDTO targetDTO = policyDTO.getTargetDTO();
  1380. List<RuleDTO> ruleDTOs = policyDTO.getRuleDTOs();
  1381. List<ObligationDTO> obligationDTOs = policyDTO.getObligationDTOs();
  1382. String ruleElementOrder = policyDTO.getRuleOrder();
  1383. PolicyEditorDataHolder holder = PolicyEditorEngine.getInstance().
  1384. getPolicyEditorData(EntitlementConstants.PolicyEditor.STANDARD);
  1385. List<String> policyMetaDataList = new ArrayList<String>();
  1386. List<RuleDTO> arrangedRules = new ArrayList<RuleDTO>();
  1387. if (ruleElementOrder != null && ruleElementOrder.trim().length() > 0) {
  1388. String[] ruleIds = ruleElementOrder.
  1389. split(EntitlementPolicyConstants.ATTRIBUTE_SEPARATOR);
  1390. for (String ruleId : ruleIds) {
  1391. for (RuleDTO ruleDTO : ruleDTOs) {
  1392. if (ruleId.equals(ruleDTO.getRuleId())) {
  1393. arrangedRules.add(ruleDTO);
  1394. }
  1395. }
  1396. }
  1397. ruleDTOs = arrangedRules;
  1398. }
  1399. createMetaDataFromPolicy("policy", policyDTO, policyMetaDataList);
  1400. String algorithm = policyDTO.getRuleAlgorithm();
  1401. if (algorithm != null && algorithm.trim().length() > 0) {
  1402. policyDTO.setRuleAlgorithm(holder.getRuleAlgorithmUri(algorithm));
  1403. } else {
  1404. policyDTO.setRuleAlgorithm(holder.getDefaultRuleAlgorithm());
  1405. }
  1406. if (targetDTO != null && targetDTO.getRowDTOList() != null) {
  1407. List<RowDTO> newRowDTOs = new ArrayList<RowDTO>();
  1408. for (RowDTO rowDTO : targetDTO.getRowDTOList()) {
  1409. createMetaDataFromRowDTO("target", rowDTO, policyMetaDataList);
  1410. String category = rowDTO.getCategory();
  1411. if (category == null) {
  1412. continue;
  1413. }
  1414. String attributeValue = rowDTO.getAttributeValue();
  1415. if (attributeValue == null || attributeValue.trim().length() < 1) {
  1416. continue;
  1417. }
  1418. rowDTO.setCategory(holder.getCategoryUri(category));
  1419. if (rowDTO.getAttributeDataType() == null ||
  1420. rowDTO.getAttributeDataType().trim().length() < 1 ||
  1421. rowDTO.getAttributeDataType().trim().equals("null")) {
  1422. if (holder.getDefaultDataType() != null) {
  1423. rowDTO.setAttributeDataType(holder.getDefaultDataType());
  1424. } else {
  1425. rowDTO.setAttributeDataType(PolicyEditorConstants.DataType.STRING);
  1426. }
  1427. } else {
  1428. if (holder.getDataTypeUri(rowDTO.getAttributeDataType()) != null) {
  1429. rowDTO.setAttributeDataType(holder.getDataTypeUri(rowDTO.getAttributeDataType()));
  1430. }
  1431. }
  1432. String attributeId = rowDTO.getAttributeId();
  1433. if (attributeId == null || attributeId.trim().length() < 1 ||
  1434. attributeId.trim().equals("null")) {
  1435. attributeId = holder.getCategoryDefaultAttributeId(category);
  1436. }
  1437. rowDTO.setAttributeId(holder.getAttributeIdUri(attributeId));
  1438. rowDTO.setFunction(holder.getFunctionUri(rowDTO.getFunction()));
  1439. rowDTO.setPreFunction(holder.getPreFunctionUri(rowDTO.getPreFunction()));
  1440. newRowDTOs.add(rowDTO);
  1441. }
  1442. targetDTO.setRowDTOList(newRowDTOs);
  1443. policyDTO.setTargetDTO(targetDTO);
  1444. }
  1445. if (ruleDTOs != null) {
  1446. for (RuleDTO ruleDTO : ruleDTOs) {
  1447. createMetaDataFromRule("rule", ruleDTO, policyMetaDataList);
  1448. List<RowDTO> newRowDTOs = new ArrayList<RowDTO>();
  1449. for (RowDTO rowDTO : ruleDTO.getRowDTOList()) {
  1450. createMetaDataFromRowDTO("ruleRow" + ruleDTO.getRuleId(), rowDTO, policyMetaDataList);
  1451. String category = rowDTO.getCategory();
  1452. if (category == null) {
  1453. continue;
  1454. }
  1455. String attributeValue = rowDTO.getAttributeValue();
  1456. if (attributeValue == null || attributeValue.trim().length() < 1) {
  1457. continue;
  1458. }
  1459. rowDTO.setCategory(holder.getCategoryUri(category));
  1460. if (rowDTO.getAttributeDataType() == null ||
  1461. rowDTO.getAttributeDataType().trim().length() < 1 ||
  1462. rowDTO.getAttributeDataType().trim().equals("null")) {
  1463. if (holder.getDefaultDataType() != null) {
  1464. rowDTO.setAttributeDataType(holder.getDefaultDataType());
  1465. } else {
  1466. rowDTO.setAttributeDataType(PolicyEditorConstants.DataType.STRING);
  1467. }
  1468. } else {
  1469. if (holder.getDataTypeUri(rowDTO.getAttributeDataType()) != null) {
  1470. rowDTO.setAttributeDataType(holder.getDataTypeUri(rowDTO.getAttributeDataType()));
  1471. }
  1472. }
  1473. String attributeId = rowDTO.getAttributeId();
  1474. if (attributeId == null || attributeId.trim().length() < 1 ||
  1475. attributeId.trim().equals("null")) {
  1476. attributeId = holder.getCategoryDefaultAttributeId(category);
  1477. }
  1478. rowDTO.setAttributeId(holder.getAttributeIdUri(attributeId));
  1479. rowDTO.setFunction(holder.getFunctionUri(rowDTO.getFunction()));
  1480. rowDTO.setPreFunction(holder.getPreFunctionUri(rowDTO.getPreFunction()));
  1481. newRowDTOs.add(rowDTO);
  1482. }
  1483. ruleDTO.setRowDTOList(newRowDTOs);
  1484. TargetDTO ruleTargetDTO = ruleDTO.getTargetDTO();
  1485. if (ruleTargetDTO == null) {
  1486. continue;
  1487. }
  1488. List<RowDTO> newTargetRowDTOs = new ArrayList<RowDTO>();
  1489. for (RowDTO rowDTO : ruleTargetDTO.getRowDTOList()) {
  1490. createMetaDataFromRowDTO("ruleTarget" + ruleDTO.getRuleId(), rowDTO, policyMetaDataList);
  1491. String category = rowDTO.getCategory();
  1492. if (category == null) {
  1493. continue;
  1494. }
  1495. String attributeValue = rowDTO.getAttributeValue();
  1496. if (attributeValue == null || attributeValue.trim().length() < 1) {
  1497. continue;
  1498. }
  1499. rowDTO.setCategory(holder.getCategoryUri(category));
  1500. if (rowDTO.getAttributeDataType() == null ||
  1501. rowDTO.getAttributeDataType().trim().length() < 1 ||
  1502. rowDTO.getAttributeDataType().trim().equals("null")) {
  1503. if (holder.getDefaultDataType() != null) {
  1504. rowDTO.setAttributeDataType(holder.getDefaultDataType());
  1505. } else {
  1506. rowDTO.setAttributeDataType(PolicyEditorConstants.DataType.STRING);
  1507. }
  1508. } else {
  1509. if (holder.getDataTypeUri(rowDTO.getAttributeDataType()) != null) {
  1510. rowDTO.setAttributeDataType(holder.getDataTypeUri(rowDTO.getAttributeDataType()));
  1511. }
  1512. }
  1513. String attributeId = rowDTO.getAttributeId();
  1514. if (attributeId == null || attributeId.trim().length() < 1 ||
  1515. attributeId.trim().equals("null")) {
  1516. attributeId = holder.getCategoryDefaultAttributeId(category);
  1517. }
  1518. rowDTO.setAttributeId(holder.getAttributeIdUri(attributeId));
  1519. rowDTO.setFunction(holder.getFunctionUri(rowDTO.getFunction()));
  1520. rowDTO.setPreFunction(holder.getPreFunctionUri(rowDTO.getPreFunction()));
  1521. newTargetRowDTOs.add(rowDTO);
  1522. }
  1523. ruleTargetDTO.setRowDTOList(newTargetRowDTOs);
  1524. List<ObligationDTO> ruleObligationDTOs = ruleDTO.getObligationDTOs();
  1525. if (ruleObligationDTOs != null) {
  1526. for (ObligationDTO dto : ruleObligationDTOs) {
  1527. createMetaDataFromObligation("ruleObligation" + ruleDTO.getRuleId(),
  1528. dto, policyMetaDataList);
  1529. if (dto.getAttributeValueDataType() == null ||
  1530. dto.getAttributeValueDataType().trim().length() < 1 ||
  1531. dto.getAttributeValueDataType().trim().equals("null")) {
  1532. dto.setAttributeValueDataType(PolicyEditorConstants.DataType.STRING);
  1533. }
  1534. if (dto.getResultAttributeId() == null ||
  1535. dto.getResultAttributeId().trim().length() == 0 ||
  1536. dto.getResultAttributeId().trim().equals("null")) {
  1537. // setting obligation id
  1538. dto.setResultAttributeId(dto.getObligationId());
  1539. }
  1540. }
  1541. ruleDTO.setObligationDTOs(ruleObligationDTOs);
  1542. }
  1543. ruleDTO.setTargetDTO(ruleTargetDTO);
  1544. }
  1545. policyDTO.setRuleDTOs(ruleDTOs);
  1546. }
  1547. if (obligationDTOs != null) {
  1548. for (ObligationDTO dto : obligationDTOs) {
  1549. createMetaDataFromObligation("obligation", dto, policyMetaDataList);
  1550. if (dto.getAttributeValueDataType() == null ||
  1551. dto.getAttributeValueDataType().trim().length() == 0 ||
  1552. dto.getAttributeValueDataType().trim().equals("null")) {
  1553. dto.setAttributeValueDataType(PolicyEditorConstants.DataType.STRING);
  1554. }
  1555. if (dto.getResultAttributeId() == null ||
  1556. dto.getResultAttributeId().trim().length() == 0 ||
  1557. dto.getResultAttributeId().trim().equals("null")) {
  1558. // setting obligation id
  1559. dto.setResultAttributeId(dto.getObligationId());
  1560. }
  1561. }
  1562. policyDTO.setObligationDTOs(obligationDTOs);
  1563. }
  1564. // for(ExtendAttributeDTO attributeDTO : ruleDTO.getAttributeDTOs()){
  1565. //
  1566. // String id = attributeDTO.getId();
  1567. // String selector = attributeDTO.getSelector();
  1568. // String category = null;
  1569. // String function = null;
  1570. //
  1571. // if(id == null){
  1572. // continue;
  1573. // }
  1574. //
  1575. // if(PolicyEditorConstants.DYNAMIC_SELECTOR_FUNCTION.equals(selector)){
  1576. //
  1577. // String attributeValue = attributeDTO.getAttributeValue();
  1578. // if(attributeValue == null || attributeValue.trim().length() < 1){
  1579. // continue;
  1580. // }
  1581. // function = attributeDTO.getFunction();
  1582. // if(function != null){
  1583. // function = function.replace("&gt;", ">");
  1584. // function = function.replace("&lt;", "<");
  1585. //
  1586. // if(ruleFunctionMap.get(function) != null){// TODO
  1587. // attributeDTO.setFunction(ruleFunctionMap.get(function));
  1588. // }
  1589. // }
  1590. //
  1591. // if(attributeDTO.getDataType() == null ||
  1592. // attributeDTO.getDataType().trim().length() < 1 ||
  1593. // attributeDTO.getDataType().trim().equals("null")) {
  1594. //
  1595. // if(category != null && defaultDataTypeMap.get(category) != null){
  1596. // attributeDTO.setDataType((defaultDataTypeMap.
  1597. // get(category).iterator().next()));
  1598. // } else {
  1599. // attributeDTO.setDataType(PolicyEditorConstants.DataType.STRING);
  1600. // }
  1601. // }
  1602. //
  1603. // } else {
  1604. //
  1605. // category = attributeDTO.getCategory();
  1606. //
  1607. // if(category == null || category.trim().length() < 1){
  1608. // continue;
  1609. // }
  1610. //
  1611. // if(categoryMap.get(category) != null){
  1612. // attributeDTO.setCategory(categoryMap.get(category));
  1613. // }
  1614. //
  1615. // if(attributeDTO.getDataType() == null ||
  1616. // attributeDTO.getDataType().trim().length() < 1 ||
  1617. // attributeDTO.getDataType().trim().equals("null")) {
  1618. //
  1619. // if(defaultDataTypeMap.get(category) != null){
  1620. // attributeDTO.setDataType((defaultDataTypeMap.
  1621. // get(category).iterator().next()));
  1622. // } else {
  1623. // attributeDTO.setDataType(PolicyEditorConstants.DataType.STRING);
  1624. // }
  1625. // }
  1626. //
  1627. // if(attributeDTO.getAttributeId() == null ||
  1628. // attributeDTO.getAttributeId().trim().length() < 1 ||
  1629. // attributeDTO.getAttributeId().trim().equals("null")) {
  1630. // if(defaultAttributeIdMap.get(category) != null){
  1631. // attributeDTO.setAttributeId((defaultAttributeIdMap.
  1632. // get(category).iterator().next()));
  1633. // }
  1634. // }
  1635. // }
  1636. //
  1637. //
  1638. // ExtendAttributeDTO odlRowDTO = new ExtendAttributeDTO(attributeDTO);
  1639. // odlRowDTO.setCategory(category);
  1640. // odlRowDTO.setFunction(function);
  1641. // createMetaDataFromDynamicAttribute("targetRule" + odlRowDTO.getId(), odlRowDTO,
  1642. // policyMetaDataList);
  1643. // //newDynamicAttributeDTOs.add(attributeDTO);
  1644. // }
  1645. return policyMetaDataList.toArray(new String[policyMetaDataList.size()]);
  1646. }
  1647. private static void createMetaDataFromPolicy(String prefix, PolicyDTO policyDTO, List<String> metaDataList) {
  1648. if (metaDataList != null) {
  1649. metaDataList.add(prefix + "|" + policyDTO.getPolicyId());
  1650. metaDataList.add(prefix + "|" + policyDTO.getRuleAlgorithm());
  1651. if (policyDTO.getDescription() == null) {
  1652. policyDTO.setDescription("");
  1653. }
  1654. metaDataList.add(prefix + "|" + policyDTO.getDescription());
  1655. metaDataList.add(prefix + "|" + policyDTO.getVersion());
  1656. }
  1657. }
  1658. private static void createMetaDataFromPolicySet(String prefix, PolicySetDTO policyDTO, List<String> metaDataList) {
  1659. if (metaDataList != null) {
  1660. metaDataList.add(prefix + "|" + policyDTO.getPolicySetId());
  1661. metaDataList.add(prefix + "|" + policyDTO.getPolicyCombiningAlgId());
  1662. if (policyDTO.getDescription() == null) {
  1663. policyDTO.setDescription("");
  1664. }
  1665. metaDataList.add(prefix + "|" + policyDTO.getDescription());
  1666. metaDataList.add(prefix + "|" + policyDTO.getVersion());
  1667. }
  1668. }
  1669. private static void createMetaDataFromRule(String prefix, RuleDTO ruleDTO, List<String> metaDataList) {
  1670. if (metaDataList != null) {
  1671. metaDataList.add(prefix + "|" + ruleDTO.getRuleId());
  1672. metaDataList.add(prefix + "|" + ruleDTO.getRuleEffect());
  1673. metaDataList.add(prefix + "|" + ruleDTO.getRuleDescription());
  1674. }
  1675. }
  1676. private static void createMetaDataFromRowDTO(String prefix, RowDTO rowDTO, List<String> metaDataList) {
  1677. if (metaDataList != null) {
  1678. metaDataList.add(prefix + "|" + rowDTO.getCategory());
  1679. metaDataList.add(prefix + "|" + rowDTO.getPreFunction());
  1680. metaDataList.add(prefix + "|" + rowDTO.getFunction());
  1681. metaDataList.add(prefix + "|" + rowDTO.getAttributeValue());
  1682. metaDataList.add(prefix + "|" + rowDTO.getAttributeId());
  1683. metaDataList.add(prefix + "|" + rowDTO.getAttributeDataType());
  1684. metaDataList.add(prefix + "|" + rowDTO.getCombineFunction());
  1685. }
  1686. }
  1687. private static void createMetaDataFromDynamicAttribute(String prefix, ExtendAttributeDTO dto,
  1688. List<String> metaDataList) {
  1689. if (metaDataList != null) {
  1690. metaDataList.add(prefix + "|" + dto.getCategory());
  1691. metaDataList.add(prefix + "|" + dto.getSelector());
  1692. metaDataList.add(prefix + "|" + dto.getFunction());
  1693. metaDataList.add(prefix + "|" + dto.getAttributeValue());
  1694. metaDataList.add(prefix + "|" + dto.getAttributeId());
  1695. metaDataList.add(prefix + "|" + dto.getDataType());
  1696. metaDataList.add(prefix + "|" + dto.getId());
  1697. }
  1698. }
  1699. private static void createMetaDataFromObligation(String prefix, ObligationDTO dto,
  1700. List<String> metaDataList) {
  1701. if (metaDataList != null) {
  1702. metaDataList.add(prefix + "|" + dto.getType());
  1703. metaDataList.add(prefix + "|" + dto.getObligationId());
  1704. metaDataList.add(prefix + "|" + dto.getEffect());
  1705. metaDataList.add(prefix + "|" + dto.getAttributeValue());
  1706. metaDataList.add(prefix + "|" + dto.getResultAttributeId());
  1707. metaDataList.add(prefix + "|" + dto.getAttributeValueDataType());
  1708. }
  1709. }
  1710. private static void createMetaDataFromReference(String prefix, PolicyRefIdDTO dto,
  1711. List<String> metaDataList) {
  1712. if (metaDataList != null) {
  1713. metaDataList.add(prefix + "|" + dto.getId());
  1714. metaDataList.add(prefix + "|" + dto.isPolicySet());
  1715. metaDataList.add(prefix + "|" + dto.isReferenceOnly());
  1716. }
  1717. }
  1718. public static String[] createBasicPolicyData(SimplePolicyEditorDTO policyEditorDTO) {
  1719. List<String> metaDataList = new ArrayList<String>();
  1720. metaDataList.add("policyId|" + policyEditorDTO.getPolicyId());
  1721. metaDataList.add("category|" + policyEditorDTO.getAppliedCategory());
  1722. metaDataList.add("policyDescription|" + policyEditorDTO.getDescription());
  1723. metaDataList.add("userAttributeId|" + policyEditorDTO.getUserAttributeId());
  1724. metaDataList.add("userAttributeValue|" + policyEditorDTO.getUserAttributeValue());
  1725. metaDataList.add("function|" + policyEditorDTO.getFunction());
  1726. metaDataList.add("actionValue|" + policyEditorDTO.getActionValue());
  1727. metaDataList.add("resourceValue|" + policyEditorDTO.getResourceValue());
  1728. metaDataList.add("category|" + policyEditorDTO.getAppliedCategory());
  1729. metaDataList.add("environmentValue|" + policyEditorDTO.getEnvironmentValue());
  1730. metaDataList.add("environmentId|" + policyEditorDTO.getEnvironmentId());
  1731. List<SimplePolicyEditorElementDTO> elementDTOs = policyEditorDTO.getSimplePolicyEditorElementDTOs();
  1732. if (elementDTOs != null && elementDTOs.size() > 0) {
  1733. for (int i = 0; i < elementDTOs.size(); i++) {
  1734. SimplePolicyEditorElementDTO dto = elementDTOs.get(i);
  1735. if (dto.getResourceValue() != null) {
  1736. metaDataList.add("resourceValue" + i + "|" + dto.getResourceValue());
  1737. } else {
  1738. metaDataList.add("resourceValue" + i);
  1739. }
  1740. if (dto.getEnvironmentValue() != null) {
  1741. metaDataList.add("environmentValue" + i + "|" + dto.getEnvironmentValue());
  1742. } else {
  1743. metaDataList.add("environmentValue" + i);
  1744. }
  1745. if (dto.getActionValue() != null) {
  1746. metaDataList.add("actionValue" + i + "|" + dto.getActionValue());
  1747. } else {
  1748. metaDataList.add("actionValue" + i);
  1749. }
  1750. if (dto.getOperationType() != null) {
  1751. metaDataList.add("operationValue" + i + "|" + dto.getOperationType());
  1752. } else {
  1753. metaDataList.add("operationValue" + i);
  1754. }
  1755. if (dto.getUserAttributeId() != null) {
  1756. metaDataList.add("userAttributeId" + i + "|" + dto.getUserAttributeId());
  1757. } else {
  1758. metaDataList.add("userAttributeId" + i);
  1759. }
  1760. if (dto.getUserAttributeValue() != null) {
  1761. metaDataList.add("userAttributeValue" + i + "|" + dto.getUserAttributeValue());
  1762. } else {
  1763. metaDataList.add("userAttributeValue" + i);
  1764. }
  1765. if (dto.getEnvironmentId() != null) {
  1766. metaDataList.add("environmentId" + i + "|" + dto.getEnvironmentId());
  1767. } else {
  1768. metaDataList.add("environmentId" + i);
  1769. }
  1770. if (dto.getFunctionOnResources() != null) {
  1771. metaDataList.add("functionOnResources" + i + "|" + dto.getFunctionOnResources());
  1772. } else {
  1773. metaDataList.add("functionOnResources" + i);
  1774. }
  1775. if (dto.getFunctionOnActions() != null) {
  1776. metaDataList.add("functionOnActions" + i + "|" + dto.getFunctionOnActions());
  1777. } else {
  1778. metaDataList.add("functionOnActions" + i);
  1779. }
  1780. if (dto.getFunctionOnUsers() != null) {
  1781. metaDataList.add("functionOnUsers" + i + "|" + dto.getFunctionOnUsers());
  1782. } else {
  1783. metaDataList.add("functionOnUsers" + i);
  1784. }
  1785. if (dto.getFunctionOnEnvironments() != null) {
  1786. metaDataList.add("functionOnEnvironments" + i + "|" + dto.getFunctionOnEnvironments());
  1787. } else {
  1788. metaDataList.add("functionOnEnvironments" + i);
  1789. }
  1790. }
  1791. }
  1792. return metaDataList.toArray(new String[metaDataList.size()]);
  1793. }
  1794. ////////////////////////////////////// Simple Policy Editor data ////////////////////////////////////
  1795. public static SimplePolicyEditorDTO createSimplePolicyEditorDTO(String[] policyEditorData) {
  1796. Map<String, String> metaDataMap = new HashMap<String, String>();
  1797. List<SimplePolicyEditorElementDTO> SimplePolicyEditorElementDTOs = new ArrayList<SimplePolicyEditorElementDTO>();
  1798. int i = 0;
  1799. if (policyEditorData != null) {
  1800. for (String data : policyEditorData) {
  1801. if (data.contains("|")) {
  1802. String identifier = data.substring(0, data.indexOf("|"));
  1803. String value = data.substring(data.indexOf("|") + 1);
  1804. metaDataMap.put(identifier, value);
  1805. }
  1806. i++;
  1807. }
  1808. }
  1809. SimplePolicyEditorDTO policyEditorDTO = new SimplePolicyEditorDTO();
  1810. policyEditorDTO.setPolicyId(metaDataMap.get("policyId"));
  1811. policyEditorDTO.setAppliedCategory(metaDataMap.get("policyId"));
  1812. policyEditorDTO.setFunction(metaDataMap.get("function"));
  1813. policyEditorDTO.setActionValue(metaDataMap.get("actionValue"));
  1814. policyEditorDTO.setDescription(metaDataMap.get("policyDescription"));
  1815. policyEditorDTO.setUserAttributeId(metaDataMap.get("userAttributeId"));
  1816. policyEditorDTO.setUserAttributeValue(metaDataMap.get("userAttributeValue"));
  1817. policyEditorDTO.setResourceValue(metaDataMap.get("resourceValue"));
  1818. policyEditorDTO.setEnvironmentValue(metaDataMap.get("environmentValue"));
  1819. policyEditorDTO.setEnvironmentId(metaDataMap.get("environmentId"));
  1820. policyEditorDTO.setAppliedCategory(metaDataMap.get("category"));
  1821. i = (i - 11) / 11;
  1822. for (int j = 0; j < i; j++) {
  1823. SimplePolicyEditorElementDTO elementDTO = new SimplePolicyEditorElementDTO();
  1824. elementDTO.setResourceValue(metaDataMap.get("resourceValue" + j));
  1825. elementDTO.setEnvironmentValue(metaDataMap.get("environmentValue" + j));
  1826. if (metaDataMap.get("actionValue" + j) != null) {
  1827. elementDTO.setActionValue(metaDataMap.get("actionValue" + j));
  1828. }
  1829. elementDTO.setOperationType(metaDataMap.get("operationValue" + j));
  1830. elementDTO.setUserAttributeId(metaDataMap.get("userAttributeId" + j));
  1831. elementDTO.setUserAttributeValue(metaDataMap.get("userAttributeValue" + j));
  1832. elementDTO.setEnvironmentId(metaDataMap.get("environmentId" + j));
  1833. elementDTO.setFunctionOnResources(metaDataMap.get("functionOnResources" + j));
  1834. elementDTO.setFunctionOnActions(metaDataMap.get("functionOnActions" + j));
  1835. elementDTO.setFunctionOnUsers(metaDataMap.get("functionOnUsers" + j));
  1836. elementDTO.setFunctionOnEnvironments(metaDataMap.get("functionOnEnvironments" + j));
  1837. SimplePolicyEditorElementDTOs.add(elementDTO);
  1838. }
  1839. if (SimplePolicyEditorElementDTOs.size() > 0) {
  1840. policyEditorDTO.setSimplePolicyEditorElementDTOs(SimplePolicyEditorElementDTOs);
  1841. }
  1842. return policyEditorDTO;
  1843. }
  1844. ///////////////////////////////// policy Set ///////////////////////////////////////////////////////
  1845. // public static PolicyElementDTO createPolicySetElementDTO(String policy)
  1846. // throws EntitlementPolicyCreationException {
  1847. //
  1848. // PolicySetDTO policyElementDTO = new PolicySetDTO();
  1849. // OMElement omElement;
  1850. // try {
  1851. // omElement = AXIOMUtil.stringToOM(policy);
  1852. // } catch (XMLStreamException e) {
  1853. // throw new EntitlementPolicyCreationException("Policy can not be converted to OMElement");
  1854. // }
  1855. //
  1856. // if (omElement != null) {
  1857. //
  1858. // policyElementDTO.setPolicySetId(omElement.
  1859. // getAttributeValue(new QName(EntitlementPolicyConstants.POLICY_SET_ID)));
  1860. //
  1861. // String ruleCombiningAlgorithm = omElement.
  1862. // getAttributeValue(new QName(EntitlementPolicyConstants.POLICY_ALGORITHM));
  1863. //
  1864. // try{
  1865. // policyElementDTO.setRuleCombiningAlgorithms(ruleCombiningAlgorithm.
  1866. // split(PolicyEditorConstants.RULE_ALGORITHM_IDENTIFIER_3)[1]);
  1867. // } catch (Exception ignore){
  1868. // policyElementDTO.setRuleCombiningAlgorithms(ruleCombiningAlgorithm.
  1869. // split(PolicyEditorConstants.RULE_ALGORITHM_IDENTIFIER_1)[1]);
  1870. // // if this is also fails, can not edit the policy
  1871. // }
  1872. //
  1873. // Iterator iterator = omElement.getChildrenWithLocalName(EntitlementPolicyConstants.
  1874. // DESCRIPTION_ELEMENT);
  1875. //
  1876. // if(iterator.hasNext()){
  1877. // OMElement descriptionElement = (OMElement) iterator.next();
  1878. // if(descriptionElement != null && descriptionElement.getText() != null){
  1879. // policyElementDTO.setPolicyDescription(descriptionElement.getText().trim());
  1880. // }
  1881. // }
  1882. //
  1883. // }
  1884. // return policyElementDTO;
  1885. // }
  1886. //////////////////////////////// Standard policy editor/////////////////////////////////////////////////////
  1887. public static PolicyElementDTO createPolicyElementDTO(String policy)
  1888. throws EntitlementPolicyCreationException {
  1889. PolicyElementDTO policyElementDTO = new PolicyElementDTO();
  1890. OMElement omElement;
  1891. try {
  1892. omElement = AXIOMUtil.stringToOM(policy);
  1893. } catch (XMLStreamException e) {
  1894. throw new EntitlementPolicyCreationException("Policy can not be converted to OMElement");
  1895. }
  1896. if (omElement != null) {
  1897. policyElementDTO.setPolicyName(omElement.
  1898. getAttributeValue(new QName(EntitlementPolicyConstants.POLICY_ID)));
  1899. String ruleCombiningAlgorithm = omElement.
  1900. getAttributeValue(new QName(EntitlementPolicyConstants.RULE_ALGORITHM));
  1901. try {
  1902. policyElementDTO.setRuleCombiningAlgorithms(ruleCombiningAlgorithm.
  1903. split(PolicyEditorConstants.RULE_ALGORITHM_IDENTIFIER_3)[1]);
  1904. } catch (Exception ignore) {
  1905. policyElementDTO.setRuleCombiningAlgorithms(ruleCombiningAlgorithm.
  1906. split(PolicyEditorConstants.RULE_ALGORITHM_IDENTIFIER_1)[1]);
  1907. // if this is also fails, can not edit the policy
  1908. }
  1909. Iterator iterator = omElement.getChildrenWithLocalName(EntitlementPolicyConstants.
  1910. DESCRIPTION_ELEMENT);
  1911. if (iterator.hasNext()) {
  1912. OMElement descriptionElement = (OMElement) iterator.next();
  1913. if (descriptionElement != null && descriptionElement.getText() != null) {
  1914. policyElementDTO.setPolicyDescription(descriptionElement.getText().trim());
  1915. }
  1916. }
  1917. }
  1918. return policyElementDTO;
  1919. }
  1920. public static List<RuleElementDTO> createRuleElementDTOs(String policy)
  1921. throws EntitlementPolicyCreationException {
  1922. List<RuleElementDTO> ruleElementDTOs = new ArrayList<RuleElementDTO>();
  1923. OMElement omElement;
  1924. try {
  1925. omElement = AXIOMUtil.stringToOM(policy);
  1926. } catch (XMLStreamException e) {
  1927. throw new EntitlementPolicyCreationException("Policy can not be converted to OMElement");
  1928. }
  1929. if (omElement != null) {
  1930. Iterator iterator2 = omElement.getChildrenWithLocalName(EntitlementPolicyConstants.
  1931. RULE_ELEMENT);
  1932. while (iterator2.hasNext()) {
  1933. OMElement ruleElement = (OMElement) iterator2.next();
  1934. ruleElementDTOs.add(createRuleDTO(ruleElement));
  1935. }
  1936. }
  1937. return ruleElementDTOs;
  1938. }
  1939. public static RuleElementDTO createRuleDTO(OMElement omElement) {
  1940. RuleElementDTO ruleElementDTO = new RuleElementDTO();
  1941. if (omElement != null) {
  1942. ruleElementDTO.setRuleId(omElement.
  1943. getAttributeValue(new QName(EntitlementPolicyConstants.RULE_ID)).trim());
  1944. ruleElementDTO.setRuleEffect(omElement.
  1945. getAttributeValue(new QName(EntitlementPolicyConstants.RULE_EFFECT)).trim());
  1946. Iterator iterator1 = omElement.
  1947. getChildrenWithLocalName(EntitlementPolicyConstants.DESCRIPTION_ELEMENT);
  1948. while (iterator1.hasNext()) {
  1949. OMElement descriptionElement = (OMElement) iterator1.next();
  1950. if (descriptionElement != null && descriptionElement.getText() != null) {
  1951. ruleElementDTO.setRuleDescription(descriptionElement.getText().trim());
  1952. }
  1953. }
  1954. }
  1955. return ruleElementDTO;
  1956. }
  1957. public static void processRuleRowPolicyEditorData(List<RuleDTO> rules, String[] policyEditorData) {
  1958. for (RuleDTO ruleDTO : rules) {
  1959. List<String> ruleList = new ArrayList<String>();
  1960. List<String> ruleTargetList = new ArrayList<String>();
  1961. List<String> obligationList = new ArrayList<String>();
  1962. for (String data : policyEditorData) {
  1963. if (data.contains("|")) {
  1964. String identifier = data.substring(0, data.indexOf("|"));
  1965. if (identifier.startsWith("ruleTarget")) {
  1966. String ruleId = identifier.substring(10);
  1967. if (ruleId != null && ruleId.contains(ruleDTO.getRuleId())) {
  1968. ruleTargetList.add(data.substring(data.indexOf("|") + 1));
  1969. }
  1970. } else if (identifier.startsWith("ruleObligation")) {
  1971. String ruleId = identifier.substring(14);
  1972. if (ruleId != null && ruleId.equals(ruleDTO.getRuleId())) {
  1973. obligationList.add(data.substring(data.indexOf("|") + 1));
  1974. }
  1975. } else if (identifier.startsWith("ruleRow")) {
  1976. String ruleId = identifier.substring(7);
  1977. if (ruleId != null && ruleId.equals(ruleDTO.getRuleId())) {
  1978. ruleList.add(data.substring(data.indexOf("|") + 1));
  1979. }
  1980. }
  1981. }
  1982. }
  1983. ruleDTO.setRowDTOList(createRowDTO(ruleList));
  1984. ruleDTO.getTargetDTO().setRowDTOList(createRowDTO(ruleTargetList));
  1985. ruleDTO.setObligationDTOs(createObligationDTO(obligationList));
  1986. ruleDTO.setCompletedRule(true);
  1987. }
  1988. }
  1989. public static void processTargetPolicyEditorData(TargetDTO targetDTO, String[] policyEditorData) {
  1990. List<String> targetList = new ArrayList<String>();
  1991. if (policyEditorData != null) {
  1992. for (String data : policyEditorData) {
  1993. if (data.contains("|")) {
  1994. String identifier = data.substring(0, data.indexOf("|"));
  1995. if (("target").equals(identifier)) {
  1996. targetList.add(data.substring(data.indexOf("|") + 1));
  1997. }
  1998. }
  1999. }
  2000. targetDTO.setRowDTOList(createRowDTO(targetList));
  2001. }
  2002. }
  2003. public static void processPolicyEditorData(PolicyElementDTO policyElementDTO, String[] policyEditorData) {
  2004. List<String> targetList = new ArrayList<String>();
  2005. if (policyEditorData != null) {
  2006. for (String data : policyEditorData) {
  2007. if (data.contains("|")) {
  2008. String identifier = data.substring(0, data.indexOf("|"));
  2009. if (("policy").equals(identifier)) {
  2010. targetList.add(data.substring(data.indexOf("|") + 1));
  2011. }
  2012. }
  2013. }
  2014. policyElementDTO.setPolicyName(targetList.get(0));
  2015. policyElementDTO.setRuleCombiningAlgorithms(targetList.get(1));
  2016. if (targetList.get(2) != null) {
  2017. policyElementDTO.setPolicyDescription(targetList.get(2));
  2018. }
  2019. policyElementDTO.setVersion(targetList.get(3));
  2020. }
  2021. }
  2022. public static void processObligationPolicyEditorData(List<ObligationDTO> obligationDTOs,
  2023. String[] policyEditorData) {
  2024. List<String> targetList = new ArrayList<String>();
  2025. if (policyEditorData != null) {
  2026. for (String data : policyEditorData) {
  2027. if (data.contains("|")) {
  2028. String identifier = data.substring(0, data.indexOf("|"));
  2029. if (("obligation").equals(identifier)) {
  2030. targetList.add(data.substring(data.indexOf("|") + 1));
  2031. }
  2032. }
  2033. }
  2034. obligationDTOs.addAll(createObligationDTO(targetList));
  2035. }
  2036. }
  2037. public static void processRulePolicyEditorData(List<RuleDTO> ruleDTOs,
  2038. String[] policyEditorData) {
  2039. List<String> targetList = new ArrayList<String>();
  2040. if (policyEditorData != null) {
  2041. for (String data : policyEditorData) {
  2042. if (data.contains("|")) {
  2043. String identifier = data.substring(0, data.indexOf("|"));
  2044. if (("rule").equals(identifier)) {
  2045. targetList.add(data.substring(data.indexOf("|") + 1));
  2046. }
  2047. }
  2048. }
  2049. ruleDTOs.addAll(createRuleDTO(targetList));
  2050. if (ruleDTOs.size() > 0) {
  2051. processRuleRowPolicyEditorData(ruleDTOs, policyEditorData);
  2052. }
  2053. }
  2054. }
  2055. public static void processReferencePolicyEditorData(List<PolicyRefIdDTO> policyRefIdDTOs,
  2056. String[] policyEditorData) {
  2057. List<String> targetList = new ArrayList<String>();
  2058. if (policyEditorData != null) {
  2059. for (String data : policyEditorData) {
  2060. if (data.contains("|")) {
  2061. String identifier = data.substring(0, data.indexOf("|"));
  2062. if (("reference").equals(identifier)) {
  2063. targetList.add(data.substring(data.indexOf("|") + 1));
  2064. }
  2065. }
  2066. }
  2067. policyRefIdDTOs.addAll(createReferenceDTO(targetList));
  2068. }
  2069. }
  2070. private static List<RowDTO> createRowDTO(List<String> list) {
  2071. List<RowDTO> rowDTOs = new ArrayList<RowDTO>();
  2072. for (int i = 0; i < list.size(); i = i + 7) {
  2073. List<String> newList = list.subList(i, i + 7);
  2074. if (newList != null) {
  2075. RowDTO rowDTO = new RowDTO();
  2076. rowDTO.setCategory(newList.get(0));
  2077. rowDTO.setPreFunction(newList.get(1));
  2078. rowDTO.setFunction(newList.get(2));
  2079. rowDTO.setAttributeValue(newList.get(3));
  2080. rowDTO.setAttributeId(newList.get(4));
  2081. rowDTO.setAttributeDataType(newList.get(5));
  2082. rowDTO.setCombineFunction(newList.get(6));
  2083. rowDTOs.add(rowDTO);
  2084. }
  2085. }
  2086. return rowDTOs;
  2087. }
  2088. private static List<ObligationDTO> createObligationDTO(List<String> list) {
  2089. List<ObligationDTO> rowDTOs = new ArrayList<ObligationDTO>();
  2090. for (int i = 0; i < list.size(); i = i + 6) {
  2091. List<String> newList = list.subList(i, i + 6);
  2092. if (newList != null) {
  2093. ObligationDTO rowDTO = new ObligationDTO();
  2094. rowDTO.setType(newList.get(0));
  2095. rowDTO.setObligationId(newList.get(1));
  2096. rowDTO.setEffect(newList.get(2));
  2097. rowDTO.setAttributeValue(newList.get(3));
  2098. rowDTO.setResultAttributeId(newList.get(4));
  2099. rowDTO.setAttributeValueDataType(newList.get(5));
  2100. rowDTOs.add(rowDTO);
  2101. }
  2102. }
  2103. return rowDTOs;
  2104. }
  2105. private static List<RuleDTO> createRuleDTO(List<String> list) {
  2106. List<RuleDTO> rowDTOs = new ArrayList<RuleDTO>();
  2107. for (int i = 0; i < list.size(); i = i + 3) {
  2108. List<String> newList = list.subList(i, i + 3);
  2109. if (newList != null) {
  2110. RuleDTO rowDTO = new RuleDTO();
  2111. rowDTO.setRuleId(newList.get(0));
  2112. rowDTO.setRuleEffect(newList.get(1));
  2113. rowDTO.setRuleDescription(newList.get(2));
  2114. rowDTOs.add(rowDTO);
  2115. }
  2116. }
  2117. return rowDTOs;
  2118. }
  2119. private static List<PolicyRefIdDTO> createReferenceDTO(List<String> list) {
  2120. List<PolicyRefIdDTO> rowDTOs = new ArrayList<PolicyRefIdDTO>();
  2121. for (int i = 0; i < list.size(); i = i + 3) {
  2122. List<String> newList = list.subList(i, i + 3);
  2123. if (newList != null) {
  2124. PolicyRefIdDTO rowDTO = new PolicyRefIdDTO();
  2125. rowDTO.setId(newList.get(0));
  2126. rowDTO.setPolicySet(Boolean.parseBoolean(newList.get(1)));
  2127. rowDTO.setReferenceOnly(Boolean.parseBoolean(newList.get(2)));
  2128. rowDTOs.add(rowDTO);
  2129. }
  2130. }
  2131. return rowDTOs;
  2132. }
  2133. ///////////////////////////////////////// Basic Policy Editor ///////////////////////////////////////
  2134. /**
  2135. * create policy meta data that helps to edit the policy using basic editor
  2136. *
  2137. * @param basicPolicyDTO BasicPolicyDTO
  2138. * @param ruleElementOrder String
  2139. * @return String Array to dent to back end
  2140. */
  2141. public static String[] generateBasicPolicyEditorData(BasicPolicyDTO basicPolicyDTO,
  2142. String ruleElementOrder) {
  2143. List<BasicRuleDTO> basicRuleDTOs = basicPolicyDTO.getBasicRuleDTOs();
  2144. BasicTargetDTO basicTargetDTO = basicPolicyDTO.getTargetDTO();
  2145. PolicyEditorDataHolder holder = PolicyEditorEngine.getInstance().
  2146. getPolicyEditorData(EntitlementConstants.PolicyEditor.BASIC);
  2147. List<BasicRuleDTO> arrangedRules = new ArrayList<BasicRuleDTO>();
  2148. if (ruleElementOrder != null && ruleElementOrder.trim().length() > 0) {
  2149. String[] ruleIds = ruleElementOrder.
  2150. split(EntitlementPolicyConstants.ATTRIBUTE_SEPARATOR);
  2151. for (String ruleId : ruleIds) {
  2152. for (BasicRuleDTO ruleDTO : basicRuleDTOs) {
  2153. if (ruleId.equals(ruleDTO.getRuleId())) {
  2154. arrangedRules.add(ruleDTO);
  2155. }
  2156. }
  2157. }
  2158. basicRuleDTOs = arrangedRules;
  2159. }
  2160. int ruleEditorDataConstant = EntitlementPolicyConstants.BASIC_POLICY_EDITOR_RULE_DATA_AMOUNT;
  2161. int targetEditorDataConstant = EntitlementPolicyConstants.BASIC_POLICY_EDITOR_TARGET_DATA_AMOUNT;
  2162. int i = 0;
  2163. String selectedDataType;
  2164. String[] policyEditorData;
  2165. if (basicRuleDTOs != null) {
  2166. policyEditorData = new String[targetEditorDataConstant +
  2167. (basicRuleDTOs.size() * ruleEditorDataConstant)];
  2168. } else {
  2169. policyEditorData = new String[targetEditorDataConstant];
  2170. }
  2171. policyEditorData[i++] = basicPolicyDTO.getPolicyId();
  2172. policyEditorData[i++] = basicPolicyDTO.getRuleAlgorithm();
  2173. String algorithm = basicPolicyDTO.getRuleAlgorithm();
  2174. if (algorithm != null && algorithm.trim().length() > 0) {
  2175. basicPolicyDTO.setRuleAlgorithm(holder.getRuleAlgorithmUri(algorithm));
  2176. } else {
  2177. basicPolicyDTO.setRuleAlgorithm(holder.getRuleAlgorithmUri(holder.getDefaultRuleAlgorithm()));
  2178. }
  2179. policyEditorData[i++] = basicPolicyDTO.getVersion();
  2180. policyEditorData[i++] = basicPolicyDTO.getDescription();
  2181. policyEditorData[i++] = basicTargetDTO.getFunctionOnResources();
  2182. policyEditorData[i++] = basicTargetDTO.getResourceList();
  2183. policyEditorData[i++] = basicTargetDTO.getResourceId();
  2184. String resourceId = basicTargetDTO.getResourceId();
  2185. policyEditorData[i++] = basicTargetDTO.getResourceDataType();
  2186. basicTargetDTO.setFunctionOnResources(holder.getFunctionUri(basicTargetDTO.getFunctionOnResources()));
  2187. basicTargetDTO.setResourceId(holder.getAttributeIdUri(resourceId));
  2188. if ((selectedDataType = holder.getDataTypeUriForAttribute(resourceId)) != null) {
  2189. basicTargetDTO.setResourceDataType(selectedDataType);
  2190. }
  2191. policyEditorData[i++] = basicTargetDTO.getFunctionOnSubjects();
  2192. policyEditorData[i++] = basicTargetDTO.getSubjectList();
  2193. policyEditorData[i++] = basicTargetDTO.getSubjectId();
  2194. policyEditorData[i++] = basicTargetDTO.getSubjectDataType();
  2195. String subjectId = basicTargetDTO.getSubjectId();
  2196. basicTargetDTO.setFunctionOnSubjects(holder.getFunctionUri(basicTargetDTO.getFunctionOnSubjects()));
  2197. basicTargetDTO.setSubjectId(holder.getAttributeIdUri(subjectId));
  2198. if ((selectedDataType = holder.getDataTypeUriForAttribute(subjectId)) != null) {
  2199. basicTargetDTO.setSubjectDataType(selectedDataType);
  2200. }
  2201. policyEditorData[i++] = basicTargetDTO.getFunctionOnActions();
  2202. policyEditorData[i++] = basicTargetDTO.getActionList();
  2203. policyEditorData[i++] = basicTargetDTO.getActionId();
  2204. String actionId = basicTargetDTO.getActionId();
  2205. policyEditorData[i++] = basicTargetDTO.getActionDataType();
  2206. basicTargetDTO.setFunctionOnActions(holder.getFunctionUri(basicTargetDTO.getFunctionOnActions()));
  2207. basicTargetDTO.setActionId(holder.getAttributeIdUri(actionId));
  2208. if ((selectedDataType = holder.getDataTypeUriForAttribute(actionId)) != null) {
  2209. basicTargetDTO.setActionDataType(selectedDataType);
  2210. }
  2211. policyEditorData[i++] = basicTargetDTO.getFunctionOnEnvironment();
  2212. policyEditorData[i++] = basicTargetDTO.getEnvironmentList();
  2213. policyEditorData[i++] = basicTargetDTO.getEnvironmentId();
  2214. policyEditorData[i++] = basicTargetDTO.getEnvironmentDataType();
  2215. String environmentId = basicTargetDTO.getEnvironmentId();
  2216. basicTargetDTO.setFunctionOnEnvironment(holder.getFunctionUri(basicTargetDTO.getFunctionOnEnvironment()));
  2217. basicTargetDTO.setEnvironmentId(holder.getAttributeIdUri(environmentId));
  2218. if ((selectedDataType = holder.getDataTypeUriForAttribute(environmentId)) != null) {
  2219. basicTargetDTO.setEnvironmentDataType(selectedDataType);
  2220. }
  2221. if (basicRuleDTOs != null && basicRuleDTOs.size() > 0) {
  2222. for (BasicRuleDTO basicRuleDTO : basicRuleDTOs) {
  2223. generateBasicPolicyEditorDataForRule(basicRuleDTO, policyEditorData, i);
  2224. i = i + ruleEditorDataConstant;
  2225. if (basicRuleDTO.getRuleId() == null || basicRuleDTO.getRuleId().trim().length() == 0) {
  2226. basicRuleDTO.setRuleId(UUID.randomUUID().toString());
  2227. }
  2228. if (basicRuleDTO.getRuleEffect() == null || basicRuleDTO.getRuleEffect().trim().length() == 0) {
  2229. basicRuleDTO.setRuleEffect(holder.getDefaultEffect());
  2230. }
  2231. }
  2232. }
  2233. if (holder.isAddLastRule()) {
  2234. if (basicRuleDTOs == null) {
  2235. basicRuleDTOs = new ArrayList<BasicRuleDTO>();
  2236. }
  2237. BasicRuleDTO basicRuleDTO = new BasicRuleDTO();
  2238. basicRuleDTO.setRuleId(UUID.randomUUID().toString());
  2239. if (holder.getLastRuleEffect() != null) {
  2240. basicRuleDTO.setRuleEffect(holder.getLastRuleEffect());
  2241. } else {
  2242. basicRuleDTO.setRuleEffect(holder.getDefaultEffect());
  2243. }
  2244. basicRuleDTOs.add(basicRuleDTO);
  2245. }
  2246. //as we have rearrage the rules
  2247. basicPolicyDTO.setBasicRuleDTOs(basicRuleDTOs);
  2248. return policyEditorData;
  2249. }
  2250. public static String[] generateBasicPolicyEditorDataForRule(BasicRuleDTO basicRuleDTO,
  2251. String[] policyEditorData, int currentArrayIndex) {
  2252. int i = currentArrayIndex;
  2253. String selectedDataType;
  2254. PolicyEditorDataHolder holder = PolicyEditorEngine.getInstance().
  2255. getPolicyEditorData(EntitlementConstants.PolicyEditor.BASIC);
  2256. policyEditorData[i++] = basicRuleDTO.getRuleId();
  2257. policyEditorData[i++] = basicRuleDTO.getRuleEffect();
  2258. policyEditorData[i++] = basicRuleDTO.getRuleDescription();
  2259. basicRuleDTO.setRuleEffect(holder.getRuleEffectUri(basicRuleDTO.getRuleEffect()));
  2260. policyEditorData[i++] = basicRuleDTO.getPreFunctionOnResources();
  2261. policyEditorData[i++] = basicRuleDTO.getFunctionOnResources();
  2262. policyEditorData[i++] = basicRuleDTO.getResourceList();
  2263. policyEditorData[i++] = basicRuleDTO.getResourceId();
  2264. String resourceId = basicRuleDTO.getResourceId();
  2265. policyEditorData[i++] = basicRuleDTO.getResourceDataType();
  2266. basicRuleDTO.setPreFunctionOnResources(holder.getPreFunctionUri(basicRuleDTO.getPreFunctionOnResources()));
  2267. basicRuleDTO.setFunctionOnResources(holder.getFunctionUri(basicRuleDTO.getFunctionOnResources()));
  2268. basicRuleDTO.setResourceId(holder.getAttributeIdUri(resourceId));
  2269. if ((selectedDataType = holder.getDataTypeUriForAttribute(resourceId)) != null) {
  2270. basicRuleDTO.setResourceDataType(selectedDataType);
  2271. }
  2272. policyEditorData[i++] = basicRuleDTO.getPreFunctionOnSubjects();
  2273. policyEditorData[i++] = basicRuleDTO.getFunctionOnSubjects();
  2274. policyEditorData[i++] = basicRuleDTO.getSubjectList();
  2275. policyEditorData[i++] = basicRuleDTO.getSubjectId();
  2276. policyEditorData[i++] = basicRuleDTO.getSubjectDataType();
  2277. String subjectId = basicRuleDTO.getSubjectId();
  2278. basicRuleDTO.setPreFunctionOnSubjects(holder.getPreFunctionUri(basicRuleDTO.getPreFunctionOnSubjects()));
  2279. basicRuleDTO.setFunctionOnSubjects(holder.getFunctionUri(basicRuleDTO.getFunctionOnSubjects()));
  2280. basicRuleDTO.setSubjectId(holder.getAttributeIdUri(subjectId));
  2281. if ((selectedDataType = holder.getDataTypeUriForAttribute(subjectId)) != null) {
  2282. basicRuleDTO.setSubjectDataType(selectedDataType);
  2283. }
  2284. policyEditorData[i++] = basicRuleDTO.getPreFunctionOnActions();
  2285. policyEditorData[i++] = basicRuleDTO.getFunctionOnActions();
  2286. policyEditorData[i++] = basicRuleDTO.getActionList();
  2287. policyEditorData[i++] = basicRuleDTO.getActionId();
  2288. String actionId = basicRuleDTO.getActionId();
  2289. policyEditorData[i++] = basicRuleDTO.getActionDataType();
  2290. basicRuleDTO.setPreFunctionOnActions(holder.getPreFunctionUri(basicRuleDTO.getPreFunctionOnActions()));
  2291. basicRuleDTO.setFunctionOnActions(holder.getFunctionUri(basicRuleDTO.getFunctionOnActions()));
  2292. basicRuleDTO.setActionId(holder.getAttributeIdUri(actionId));
  2293. if ((selectedDataType = holder.getDataTypeUriForAttribute(actionId)) != null) {
  2294. basicRuleDTO.setActionDataType(selectedDataType);
  2295. }
  2296. policyEditorData[i++] = basicRuleDTO.getPreFunctionOnEnvironment();
  2297. policyEditorData[i++] = basicRuleDTO.getFunctionOnEnvironment();
  2298. policyEditorData[i++] = basicRuleDTO.getEnvironmentList();
  2299. policyEditorData[i++] = basicRuleDTO.getEnvironmentId();
  2300. policyEditorData[i++] = basicRuleDTO.getEnvironmentDataType();
  2301. String environmentId = basicRuleDTO.getSubjectId();
  2302. basicRuleDTO.setPreFunctionOnEnvironment(holder.getPreFunctionUri(basicRuleDTO.getPreFunctionOnEnvironment()));
  2303. basicRuleDTO.setFunctionOnEnvironment(holder.getFunctionUri(basicRuleDTO.getFunctionOnEnvironment()));
  2304. basicRuleDTO.setEnvironmentId(holder.getAttributeIdUri(environmentId));
  2305. if ((selectedDataType = holder.getDataTypeUriForAttribute(environmentId)) != null) {
  2306. basicRuleDTO.setEnvironmentDataType(selectedDataType);
  2307. }
  2308. return policyEditorData;
  2309. }
  2310. public static BasicPolicyDTO createBasicPolicyDTO(String[] policyEditorData) {
  2311. BasicPolicyDTO basicPolicyDTO = new BasicPolicyDTO();
  2312. int i = 0;
  2313. if (policyEditorData[i] != null) {
  2314. basicPolicyDTO.setPolicyId(policyEditorData[i]);
  2315. }
  2316. i++;
  2317. if (policyEditorData[i] != null) {
  2318. basicPolicyDTO.setRuleAlgorithm(policyEditorData[i]);
  2319. }
  2320. i++;
  2321. if (policyEditorData[i] != null) {
  2322. basicPolicyDTO.setVersion(policyEditorData[i]);
  2323. }
  2324. i++;
  2325. if (policyEditorData[i] != null) {
  2326. basicPolicyDTO.setDescription(policyEditorData[i]);
  2327. }
  2328. i++;
  2329. BasicTargetDTO basicTargetDTO = new BasicTargetDTO();
  2330. if (policyEditorData[i] != null) {
  2331. basicTargetDTO.setFunctionOnResources(policyEditorData[i]);
  2332. }
  2333. i++;
  2334. if (policyEditorData[i] != null) {
  2335. basicTargetDTO.setResourceList(policyEditorData[i]);
  2336. }
  2337. i++;
  2338. if (policyEditorData[i] != null) {
  2339. basicTargetDTO.setResourceId(policyEditorData[i]);
  2340. }
  2341. i++;
  2342. if (policyEditorData[i] != null) {
  2343. basicTargetDTO.setResourceDataType(policyEditorData[i]);
  2344. }
  2345. i++;
  2346. if (policyEditorData[i] != null) {
  2347. basicTargetDTO.setFunctionOnSubjects(policyEditorData[i]);
  2348. }
  2349. i++;
  2350. if (policyEditorData[i] != null) {
  2351. basicTargetDTO.setSubjectList(policyEditorData[i]);
  2352. }
  2353. i++;
  2354. if (policyEditorData[i] != null) {
  2355. basicTargetDTO.setSubjectId(policyEditorData[i]);
  2356. }
  2357. i++;
  2358. if (policyEditorData[i] != null) {
  2359. basicTargetDTO.setSubjectDataType(policyEditorData[i]);
  2360. }
  2361. i++;
  2362. if (policyEditorData[i] != null) {
  2363. basicTargetDTO.setFunctionOnActions(policyEditorData[i]);
  2364. }
  2365. i++;
  2366. if (policyEditorData[i] != null) {
  2367. basicTargetDTO.setActionList(policyEditorData[i]);
  2368. }
  2369. i++;
  2370. if (policyEditorData[i] != null) {
  2371. basicTargetDTO.setActionId(policyEditorData[i]);
  2372. }
  2373. i++;
  2374. if (policyEditorData[i] != null) {
  2375. basicTargetDTO.setActionDataType(policyEditorData[i]);
  2376. }
  2377. i++;
  2378. if (policyEditorData[i] != null) {
  2379. basicTargetDTO.setFunctionOnEnvironment(policyEditorData[i]);
  2380. }
  2381. i++;
  2382. if (policyEditorData[i] != null) {
  2383. basicTargetDTO.setEnvironmentList(policyEditorData[i]);
  2384. }
  2385. i++;
  2386. if (policyEditorData[i] != null) {
  2387. basicTargetDTO.setEnvironmentId(policyEditorData[i]);
  2388. }
  2389. i++;
  2390. if (policyEditorData[i] != null) {
  2391. basicTargetDTO.setEnvironmentDataType(policyEditorData[i]);
  2392. }
  2393. i++;
  2394. basicPolicyDTO.setTargetDTO(basicTargetDTO);
  2395. List<BasicRuleDTO> basicRuleDTOs = createBasicRuleDTOs(policyEditorData, i);
  2396. if (basicRuleDTOs != null && basicRuleDTOs.size() > 0) {
  2397. basicPolicyDTO.setBasicRuleDTOs(basicRuleDTOs);
  2398. }
  2399. return basicPolicyDTO;
  2400. }
  2401. public static List<BasicRuleDTO> createBasicRuleDTOs(String[] policyEditorData, int nextIndex) {
  2402. List<BasicRuleDTO> basicRuleDTOs = new ArrayList<BasicRuleDTO>();
  2403. if (policyEditorData != null) {
  2404. while (true) {
  2405. if (policyEditorData.length == nextIndex) {
  2406. break;
  2407. }
  2408. BasicRuleDTO basicRuleDTO = createBasicRuleDTO(policyEditorData, nextIndex);
  2409. nextIndex = nextIndex + EntitlementPolicyConstants.BASIC_POLICY_EDITOR_RULE_DATA_AMOUNT;
  2410. basicRuleDTO.setCompletedRule(true);
  2411. basicRuleDTOs.add(basicRuleDTO);
  2412. }
  2413. }
  2414. return basicRuleDTOs;
  2415. }
  2416. public static BasicRuleDTO createBasicRuleDTO(String[] policyEditorDataForRule, int nextIndex) {
  2417. BasicRuleDTO basicRuleDTO = new BasicRuleDTO();
  2418. int i = nextIndex;
  2419. if (policyEditorDataForRule[i] != null) {
  2420. basicRuleDTO.setRuleId(policyEditorDataForRule[i]);
  2421. }
  2422. i++;
  2423. if (policyEditorDataForRule[i] != null) {
  2424. basicRuleDTO.setRuleEffect(policyEditorDataForRule[i]);
  2425. }
  2426. i++;
  2427. if (policyEditorDataForRule[i] != null) {
  2428. basicRuleDTO.setRuleDescription(policyEditorDataForRule[i]);
  2429. }
  2430. i++;
  2431. if (policyEditorDataForRule[i] != null) {
  2432. basicRuleDTO.setPreFunctionOnResources(policyEditorDataForRule[i]);
  2433. }
  2434. i++;
  2435. if (policyEditorDataForRule[i] != null) {
  2436. basicRuleDTO.setFunctionOnResources(policyEditorDataForRule[i]);
  2437. }
  2438. i++;
  2439. if (policyEditorDataForRule[i] != null) {
  2440. basicRuleDTO.setResourceList(policyEditorDataForRule[i]);
  2441. }
  2442. i++;
  2443. if (policyEditorDataForRule[i] != null) {
  2444. basicRuleDTO.setResourceId(policyEditorDataForRule[i]);
  2445. }
  2446. i++;
  2447. if (policyEditorDataForRule[i] != null) {
  2448. basicRuleDTO.setResourceDataType(policyEditorDataForRule[i]);
  2449. }
  2450. i++;
  2451. if (policyEditorDataForRule[i] != null) {
  2452. basicRuleDTO.setPreFunctionOnSubjects(policyEditorDataForRule[i]);
  2453. }
  2454. i++;
  2455. if (policyEditorDataForRule[i] != null) {
  2456. basicRuleDTO.setFunctionOnSubjects(policyEditorDataForRule[i]);
  2457. }
  2458. i++;
  2459. if (policyEditorDataForRule[i] != null) {
  2460. basicRuleDTO.setSubjectList(policyEditorDataForRule[i]);
  2461. }
  2462. i++;
  2463. if (policyEditorDataForRule[i] != null) {
  2464. basicRuleDTO.setSubjectId(policyEditorDataForRule[i]);
  2465. }
  2466. i++;
  2467. if (policyEditorDataForRule[i] != null) {
  2468. basicRuleDTO.setSubjectDataType(policyEditorDataForRule[i]);
  2469. }
  2470. i++;
  2471. if (policyEditorDataForRule[i] != null) {
  2472. basicRuleDTO.setPreFunctionOnActions(policyEditorDataForRule[i]);
  2473. }
  2474. i++;
  2475. if (policyEditorDataForRule[i] != null) {
  2476. basicRuleDTO.setFunctionOnActions(policyEditorDataForRule[i]);
  2477. }
  2478. i++;
  2479. if (policyEditorDataForRule[i] != null) {
  2480. basicRuleDTO.setActionList(policyEditorDataForRule[i]);
  2481. }
  2482. i++;
  2483. if (policyEditorDataForRule[i] != null) {
  2484. basicRuleDTO.setActionId(policyEditorDataForRule[i]);
  2485. }
  2486. i++;
  2487. if (policyEditorDataForRule[i] != null) {
  2488. basicRuleDTO.setActionDataType(policyEditorDataForRule[i]);
  2489. }
  2490. i++;
  2491. if (policyEditorDataForRule[i] != null) {
  2492. basicRuleDTO.setPreFunctionOnEnvironment(policyEditorDataForRule[i]);
  2493. }
  2494. i++;
  2495. if (policyEditorDataForRule[i] != null) {
  2496. basicRuleDTO.setFunctionOnEnvironment(policyEditorDataForRule[i]);
  2497. }
  2498. i++;
  2499. if (policyEditorDataForRule[i] != null) {
  2500. basicRuleDTO.setEnvironmentList(policyEditorDataForRule[i]);
  2501. }
  2502. i++;
  2503. if (policyEditorDataForRule[i] != null) {
  2504. basicRuleDTO.setEnvironmentId(policyEditorDataForRule[i]);
  2505. }
  2506. i++;
  2507. if (policyEditorDataForRule[i] != null) {
  2508. basicRuleDTO.setEnvironmentDataType(policyEditorDataForRule[i]);
  2509. }
  2510. return basicRuleDTO;
  2511. }
  2512. }