2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.onap.policy.pap.xacml.rest.handler;
24 import java.util.List;
26 import javax.persistence.EntityManager;
27 import javax.persistence.Query;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.onap.policy.common.logging.eelf.MessageCodes;
32 import org.onap.policy.common.logging.eelf.PolicyLogger;
33 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
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.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import com.att.research.xacml.util.XACMLProperties;
41 public class PushPolicyHandler {
42 private static final Logger logger = FlexLogger.getLogger(PushPolicyHandler.class);
46 public void getActiveVersion(HttpServletRequest request, HttpServletResponse response) {
47 EntityManager em = null;
48 if(XACMLPapServlet.getEmf()!=null){
49 em = (EntityManager) XACMLPapServlet.getEmf().createEntityManager();
52 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Error creating entity manager with persistence unit: " + XACMLPapServlet.getPersistenceUnit());
55 String policyScope = request.getParameter("policyScope");
56 String filePrefix = request.getParameter("filePrefix");
57 String policyName = request.getParameter("policyName");
59 String pvName = policyScope + File.separator + filePrefix + policyName;
60 int activeVersion = 0;
62 //Get the Active Version to use in the ID
63 em.getTransaction().begin();
64 Query query = em.createQuery("Select p from PolicyVersion p where p.policyName=:pname");
65 query.setParameter("pname", pvName);
67 @SuppressWarnings("rawtypes")
68 List result = query.getResultList();
69 PolicyVersion versionEntity = null;
70 if (!result.isEmpty()) {
71 versionEntity = (PolicyVersion) result.get(0);
72 em.persist(versionEntity);
73 activeVersion = versionEntity.getActiveVersion();
74 em.getTransaction().commit();
76 PolicyLogger.debug("No PolicyVersion using policyName found");
81 if (String.valueOf(activeVersion)!=null || !String.valueOf(activeVersion).equalsIgnoreCase("")) {
82 response.setStatus(HttpServletResponse.SC_OK);
83 response.addHeader("version", String.valueOf(activeVersion));
85 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
90 * Get Selected URI path.
92 public void getSelectedURI(HttpServletRequest request, HttpServletResponse response) {
93 String gitPath = request.getParameter("gitPath");
94 File file = new File(gitPath);
95 PolicyLogger.debug("The fileItem is : " + file.toString());
96 URI selectedURI = file.toURI();
97 String uri = selectedURI.toString();
98 if (!uri.equalsIgnoreCase("")) {
99 response.setStatus(HttpServletResponse.SC_OK);
100 response.addHeader("selectedURI", uri);
102 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
106 public boolean preSafetyCheck(StdPDPPolicy policy, String configHome){
110 public boolean preSafetyCheck(OnapPDPGroup policy, String configHome){
114 public static PushPolicyHandler getInstance() {
116 Class<?> pushPolicyHandler = Class.forName(XACMLProperties.getProperty("pushPolicy.impl.className", PushPolicyHandler.class.getName()));
117 PushPolicyHandler instance = (PushPolicyHandler) pushPolicyHandler.newInstance();
119 } catch (Exception e) {
120 logger.error(e.getMessage(),e);