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;
27 import java.util.List;
29 import javax.script.SimpleBindings;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
33 import org.onap.policy.common.logging.eelf.PolicyLogger;
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
36 import org.onap.policy.rest.dao.CommonClassDao;
37 import org.onap.policy.rest.jpa.PolicyVersion;
38 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
39 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
44 public class PushPolicyHandler {
45 private static final Logger logger = FlexLogger.getLogger(PushPolicyHandler.class);
47 private static CommonClassDao commonClassDao;
50 public PushPolicyHandler(CommonClassDao commonClassDao) {
51 PushPolicyHandler.commonClassDao = commonClassDao;
54 public PushPolicyHandler() {
55 // Default Constructor
61 public void getActiveVersion(HttpServletRequest request, HttpServletResponse response) {
62 String policyScope = request.getParameter("policyScope").replace(".", File.separator);
63 String filePrefix = request.getParameter("filePrefix");
64 String policyName = request.getParameter("policyName");
66 String pvName = policyScope + File.separator + filePrefix + policyName;
67 int activeVersion = 0;
69 // Get the Active Version to use in the ID
70 String query = "Select p from PolicyVersion p where p.policyName=:pname";
71 SimpleBindings params = new SimpleBindings();
72 params.put("pname", pvName);
74 @SuppressWarnings("rawtypes")
75 List result = commonClassDao.getDataByQuery(query, params);
76 PolicyVersion versionEntity = null;
77 if (!result.isEmpty()) {
78 versionEntity = (PolicyVersion) result.get(0);
79 commonClassDao.save(versionEntity);
80 activeVersion = versionEntity.getActiveVersion();
82 PolicyLogger.debug("No PolicyVersion using policyName found");
85 if (String.valueOf(activeVersion) != null || !String.valueOf(activeVersion).equalsIgnoreCase("")) {
86 response.setStatus(HttpServletResponse.SC_OK);
87 response.addHeader("version", String.valueOf(activeVersion));
89 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
94 * Get Selected URI path.
96 public void getSelectedURI(HttpServletRequest request, HttpServletResponse response) {
97 String gitPath = request.getParameter("gitPath");
98 File file = new File(gitPath);
99 PolicyLogger.debug("The fileItem is : " + file.toString());
100 URI selectedURI = file.toURI();
101 String uri = selectedURI.toString();
102 if (!uri.equalsIgnoreCase("")) {
103 response.setStatus(HttpServletResponse.SC_OK);
104 response.addHeader("selectedURI", uri);
106 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
110 public boolean preSafetyCheck(StdPDPPolicy policy, String configHome) {
114 public boolean preSafetyCheck(OnapPDPGroup policy, String configHome) {
118 public static PushPolicyHandler getInstance() {
120 Class<?> pushPolicyHandler = Class.forName(
121 XACMLProperties.getProperty("pushPolicy.impl.className", PushPolicyHandler.class.getName()));
122 PushPolicyHandler instance = (PushPolicyHandler) pushPolicyHandler.newInstance();
124 } catch (Exception e) {
125 logger.error(e.getMessage(), e);