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=========================================================
21 package org.openecomp.policy.pap.xacml.rest.controller;
23 import java.io.PrintWriter;
24 import java.util.HashMap;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import org.json.JSONObject;
31 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
32 import org.openecomp.policy.rest.dao.EnforcerPolicyDao;
33 import org.openecomp.policy.rest.jpa.EnforcingType;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.MediaType;
36 import org.springframework.stereotype.Controller;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.servlet.ModelAndView;
40 import com.fasterxml.jackson.databind.DeserializationFeature;
41 import com.fasterxml.jackson.databind.JsonNode;
42 import com.fasterxml.jackson.databind.ObjectMapper;
45 public class EnforcerDictionaryController {
48 EnforcerPolicyDao enforcerPolicyDao;
50 @RequestMapping(value={"/get_EnforcerTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
51 public void getEnforcerDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
53 Map<String, Object> model = new HashMap<String, Object>();
54 ObjectMapper mapper = new ObjectMapper();
55 model.put("enforcerDictionaryDatas", mapper.writeValueAsString(enforcerPolicyDao.getEnforcingTypeData()));
56 org.openecomp.policy.pap.xacml.rest.util.JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
57 JSONObject j = new JSONObject(msg);
58 response.getWriter().write(j.toString());
65 @RequestMapping(value={"/enforcer_dictionary/save_enforcerType.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
66 public ModelAndView saveEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
68 ObjectMapper mapper = new ObjectMapper();
69 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
70 JsonNode root = mapper.readTree(request.getReader());
71 EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("enforcerDictionaryData").toString(), EnforcingType.class);
72 if(enforcingType.getId() == 0){
73 enforcerPolicyDao.Save(enforcingType);
75 enforcerPolicyDao.update(enforcingType);
77 response.setCharacterEncoding("UTF-8");
78 response.setContentType("application / json");
79 request.setCharacterEncoding("UTF-8");
81 PrintWriter out = response.getWriter();
82 String responseString = mapper.writeValueAsString(this.enforcerPolicyDao.getEnforcingTypeData());
83 JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
85 out.write(j.toString());
90 response.setCharacterEncoding("UTF-8");
91 request.setCharacterEncoding("UTF-8");
92 PrintWriter out = response.getWriter();
93 out.write(e.getMessage());
98 @RequestMapping(value={"/enforcer_dictionary/remove_enforcer.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
99 public ModelAndView removeEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
101 ObjectMapper mapper = new ObjectMapper();
102 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
103 JsonNode root = mapper.readTree(request.getReader());
104 EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("data").toString(), EnforcingType.class);
105 enforcerPolicyDao.delete(enforcingType);
106 response.setCharacterEncoding("UTF-8");
107 response.setContentType("application / json");
108 request.setCharacterEncoding("UTF-8");
110 PrintWriter out = response.getWriter();
112 String responseString = mapper.writeValueAsString(this.enforcerPolicyDao.getEnforcingTypeData());
113 JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
114 out.write(j.toString());
119 System.out.println(e);
120 response.setCharacterEncoding("UTF-8");
121 request.setCharacterEncoding("UTF-8");
122 PrintWriter out = response.getWriter();
123 out.write(e.getMessage());