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.onap.policy.pap.xacml.rest.controller;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.json.JSONObject;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.EnforcingType;
40 import org.onap.policy.utils.PolicyUtils;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.http.MediaType;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.servlet.ModelAndView;
47 import com.fasterxml.jackson.databind.DeserializationFeature;
48 import com.fasterxml.jackson.databind.JsonNode;
49 import com.fasterxml.jackson.databind.ObjectMapper;
52 public class EnforcerDictionaryController {
54 private static final Logger LOGGER = FlexLogger.getLogger(EnforcerDictionaryController.class);
57 CommonClassDao commonClassDao;
59 @RequestMapping(value={"/get_EnforcerTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
60 public void getEnforcerDictionaryEntityData(HttpServletResponse response){
62 Map<String, Object> model = new HashMap<>();
63 ObjectMapper mapper = new ObjectMapper();
64 List<Object> list = commonClassDao.getData(EnforcingType.class);
65 List<String> dictList = new ArrayList<>();
66 for(int i = 0; i < list.size(); i++){
67 EnforcingType dict = (EnforcingType) list.get(i);
68 dictList.add(dict.getEnforcingType());
70 model.put("enforcerDictionaryDatas", mapper.writeValueAsString(dictList));
71 org.onap.policy.pap.xacml.rest.util.JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
72 JSONObject j = new JSONObject(msg);
73 response.getWriter().write(j.toString());
76 LOGGER.error("Exception Occured"+e);
80 @RequestMapping(value={"/enforcer_dictionary/save_enforcerType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
81 public ModelAndView saveEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
83 ObjectMapper mapper = new ObjectMapper();
84 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
85 JsonNode root = mapper.readTree(request.getReader());
86 EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("enforcerDictionaryData").toString(), EnforcingType.class);
87 if(enforcingType.getId() == 0){
88 commonClassDao.save(enforcingType);
90 commonClassDao.update(enforcingType);
92 response.setCharacterEncoding("UTF-8");
93 response.setContentType("application / json");
94 request.setCharacterEncoding("UTF-8");
96 PrintWriter out = response.getWriter();
97 String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
98 JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
100 out.write(j.toString());
105 response.setCharacterEncoding("UTF-8");
106 request.setCharacterEncoding("UTF-8");
107 PrintWriter out = response.getWriter();
108 out.write(PolicyUtils.CATCH_EXCEPTION);
114 @RequestMapping(value={"/enforcer_dictionary/remove_enforcer"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
115 public ModelAndView removeEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
117 ObjectMapper mapper = new ObjectMapper();
118 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
119 JsonNode root = mapper.readTree(request.getReader());
120 EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("data").toString(), EnforcingType.class);
121 commonClassDao.delete(enforcingType);
122 response.setCharacterEncoding("UTF-8");
123 response.setContentType("application / json");
124 request.setCharacterEncoding("UTF-8");
126 PrintWriter out = response.getWriter();
128 String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
129 JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
130 out.write(j.toString());
135 System.out.println(e);
136 response.setCharacterEncoding("UTF-8");
137 request.setCharacterEncoding("UTF-8");
138 PrintWriter out = response.getWriter();
139 out.write(PolicyUtils.CATCH_EXCEPTION);