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.openecomp.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.openecomp.policy.common.logging.eelf.MessageCodes;
32 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
33 import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet;
34 import org.openecomp.policy.rest.jpa.PolicyVersion;
35 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
36 import org.openecomp.policy.xacml.std.pap.StdPDPPolicy;
38 import com.att.research.xacml.util.XACMLProperties;
40 public class PushPolicyHandler {
45 public void getActiveVersion(HttpServletRequest request, HttpServletResponse response) {
46 EntityManager em = null;
47 if(XACMLPapServlet.getEmf()!=null){
48 em = (EntityManager) XACMLPapServlet.getEmf().createEntityManager();
51 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Error creating entity manager with persistence unit: " + XACMLPapServlet.getPersistenceUnit());
54 String policyScope = request.getParameter("policyScope");
55 String filePrefix = request.getParameter("filePrefix");
56 String policyName = request.getParameter("policyName");
58 String pvName = policyScope + File.separator + filePrefix + policyName;
59 int activeVersion = 0;
61 //Get the Active Version to use in the ID
62 em.getTransaction().begin();
63 Query query = em.createQuery("Select p from PolicyVersion p where p.policyName=:pname");
64 query.setParameter("pname", pvName);
66 @SuppressWarnings("rawtypes")
67 List result = query.getResultList();
68 PolicyVersion versionEntity = null;
69 if (!result.isEmpty()) {
70 versionEntity = (PolicyVersion) result.get(0);
71 em.persist(versionEntity);
72 activeVersion = versionEntity.getActiveVersion();
73 em.getTransaction().commit();
75 PolicyLogger.debug("No PolicyVersion using policyName found");
80 if (String.valueOf(activeVersion)!=null || !String.valueOf(activeVersion).equalsIgnoreCase("")) {
81 response.setStatus(HttpServletResponse.SC_OK);
82 response.addHeader("version", String.valueOf(activeVersion));
84 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
89 * Get Selected URI path.
91 public void getSelectedURI(HttpServletRequest request, HttpServletResponse response) {
92 String gitPath = request.getParameter("gitPath");
93 File file = new File(gitPath);
94 PolicyLogger.debug("The fileItem is : " + file.toString());
95 URI selectedURI = file.toURI();
96 String uri = selectedURI.toString();
97 if (!uri.equalsIgnoreCase("")) {
98 response.setStatus(HttpServletResponse.SC_OK);
99 response.addHeader("selectedURI", uri);
101 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
105 public boolean preSafetyCheck(StdPDPPolicy policy, String configHome){
109 public boolean preSafetyCheck(EcompPDPGroup policy, String configHome){
113 public static PushPolicyHandler getInstance() {
115 Class<?> pushPolicyHandler = Class.forName(XACMLProperties.getProperty("pushPolicy.impl.className", PushPolicyHandler.class.getName()));
116 PushPolicyHandler instance = (PushPolicyHandler) pushPolicyHandler.newInstance();
118 } catch (Exception e) {
119 PolicyLogger.error(e.getMessage());