2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017,2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.xacml.rest.handler;
23 import com.att.research.xacml.util.XACMLProperties;
26 import java.util.List;
27 import javax.script.SimpleBindings;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import org.onap.policy.common.logging.eelf.PolicyLogger;
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
33 import org.onap.policy.rest.dao.CommonClassDao;
34 import org.onap.policy.rest.jpa.PolicyVersion;
35 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
36 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
41 public class PushPolicyHandler {
42 private static final Logger logger = FlexLogger.getLogger(PushPolicyHandler.class);
44 private static CommonClassDao commonClassDao;
47 public PushPolicyHandler(CommonClassDao commonClassDao) {
48 PushPolicyHandler.commonClassDao = commonClassDao;
51 public PushPolicyHandler() {
52 // Default Constructor
58 public void getActiveVersion(HttpServletRequest request, HttpServletResponse response) {
59 String policyScope = request.getParameter("policyScope").replace(".", File.separator);
60 String filePrefix = request.getParameter("filePrefix");
61 String policyName = request.getParameter("policyName");
63 String pvName = policyScope + File.separator + filePrefix + policyName;
64 int activeVersion = 0;
66 // Get the Active Version to use in the ID
67 String query = "Select p from PolicyVersion p where p.policyName=:pname";
68 SimpleBindings params = new SimpleBindings();
69 params.put("pname", pvName);
71 @SuppressWarnings("rawtypes")
72 List result = commonClassDao.getDataByQuery(query, params);
73 PolicyVersion versionEntity = null;
74 if (!result.isEmpty()) {
75 versionEntity = (PolicyVersion) result.get(0);
76 commonClassDao.save(versionEntity);
77 activeVersion = versionEntity.getActiveVersion();
79 PolicyLogger.debug("No PolicyVersion using policyName found");
82 if (String.valueOf(activeVersion) != null || !String.valueOf(activeVersion).equalsIgnoreCase("")) {
83 response.setStatus(HttpServletResponse.SC_OK);
84 response.addHeader("version", String.valueOf(activeVersion));
86 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
91 * Get Selected URI path.
93 public void getSelectedURI(HttpServletRequest request, HttpServletResponse response) {
94 String gitPath = request.getParameter("gitPath");
95 File file = new File(gitPath);
96 PolicyLogger.debug("The fileItem is : " + file.toString());
97 URI selectedURI = file.toURI();
98 String uri = selectedURI.toString();
99 if (!uri.equalsIgnoreCase("")) {
100 response.setStatus(HttpServletResponse.SC_OK);
101 response.addHeader("selectedURI", uri);
103 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
107 public boolean preSafetyCheck(StdPDPPolicy policy, String configHome){
111 public boolean preSafetyCheck(OnapPDPGroup policy, String configHome){
115 public static PushPolicyHandler getInstance() {
117 Class<?> pushPolicyHandler = Class.forName(XACMLProperties.getProperty("pushPolicy.impl.className", PushPolicyHandler.class.getName()));
118 PushPolicyHandler instance = (PushPolicyHandler) pushPolicyHandler.newInstance();
120 } catch (Exception e) {
121 logger.error(e.getMessage(),e);