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.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import org.json.JSONObject;
33 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
34 import org.openecomp.policy.common.logging.flexlogger.Logger;
35 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
36 import org.openecomp.policy.rest.dao.CommonClassDao;
37 import org.openecomp.policy.rest.jpa.EnforcingType;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.MediaType;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.servlet.ModelAndView;
44 import com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
49 public class EnforcerDictionaryController {
51 private static final Logger LOGGER = FlexLogger.getLogger(EnforcerDictionaryController.class);
54 CommonClassDao commonClassDao;
56 @RequestMapping(value={"/get_EnforcerTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
57 public void getEnforcerDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
59 Map<String, Object> model = new HashMap<>();
60 ObjectMapper mapper = new ObjectMapper();
61 List<Object> list = commonClassDao.getData(EnforcingType.class);
62 List<String> dictList = new ArrayList<>();
63 for(int i = 0; i < list.size(); i++){
64 EnforcingType dict = (EnforcingType) list.get(i);
65 dictList.add(dict.getEnforcingType());
67 model.put("enforcerDictionaryDatas", mapper.writeValueAsString(dictList));
68 org.openecomp.policy.pap.xacml.rest.util.JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
69 JSONObject j = new JSONObject(msg);
70 response.getWriter().write(j.toString());
73 LOGGER.error("Exception Occured"+e);
77 @RequestMapping(value={"/enforcer_dictionary/save_enforcerType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
78 public ModelAndView saveEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
80 ObjectMapper mapper = new ObjectMapper();
81 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
82 JsonNode root = mapper.readTree(request.getReader());
83 EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("enforcerDictionaryData").toString(), EnforcingType.class);
84 if(enforcingType.getId() == 0){
85 commonClassDao.save(enforcingType);
87 commonClassDao.update(enforcingType);
89 response.setCharacterEncoding("UTF-8");
90 response.setContentType("application / json");
91 request.setCharacterEncoding("UTF-8");
93 PrintWriter out = response.getWriter();
94 String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
95 JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
97 out.write(j.toString());
102 response.setCharacterEncoding("UTF-8");
103 request.setCharacterEncoding("UTF-8");
104 PrintWriter out = response.getWriter();
105 out.write(e.getMessage());
110 @RequestMapping(value={"/enforcer_dictionary/remove_enforcer"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
111 public ModelAndView removeEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
113 ObjectMapper mapper = new ObjectMapper();
114 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
115 JsonNode root = mapper.readTree(request.getReader());
116 EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("data").toString(), EnforcingType.class);
117 commonClassDao.delete(enforcingType);
118 response.setCharacterEncoding("UTF-8");
119 response.setContentType("application / json");
120 request.setCharacterEncoding("UTF-8");
122 PrintWriter out = response.getWriter();
124 String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
125 JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
126 out.write(j.toString());
131 System.out.println(e);
132 response.setCharacterEncoding("UTF-8");
133 request.setCharacterEncoding("UTF-8");
134 PrintWriter out = response.getWriter();
135 out.write(e.getMessage());