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.pap.xacml.rest.util.JsonMessage;
34 import org.openecomp.policy.rest.dao.CommonClassDao;
35 import org.openecomp.policy.rest.jpa.EnforcingType;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.MediaType;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.servlet.ModelAndView;
42 import com.fasterxml.jackson.databind.DeserializationFeature;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.ObjectMapper;
47 public class EnforcerDictionaryController {
50 CommonClassDao commonClassDao;
52 @RequestMapping(value={"/get_EnforcerTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
53 public void getEnforcerDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
55 Map<String, Object> model = new HashMap<String, Object>();
56 ObjectMapper mapper = new ObjectMapper();
57 List<Object> list = commonClassDao.getData(EnforcingType.class);
58 List<String> dictList = new ArrayList<String>();
59 for(int i = 0; i < list.size(); i++){
60 EnforcingType dict = (EnforcingType) list.get(i);
61 dictList.add(dict.getEnforcingType());
63 model.put("enforcerDictionaryDatas", mapper.writeValueAsString(dictList));
64 org.openecomp.policy.pap.xacml.rest.util.JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
65 JSONObject j = new JSONObject(msg);
66 response.getWriter().write(j.toString());
73 @RequestMapping(value={"/enforcer_dictionary/save_enforcerType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
74 public ModelAndView saveEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
76 ObjectMapper mapper = new ObjectMapper();
77 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
78 JsonNode root = mapper.readTree(request.getReader());
79 EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("enforcerDictionaryData").toString(), EnforcingType.class);
80 if(enforcingType.getId() == 0){
81 commonClassDao.save(enforcingType);
83 commonClassDao.update(enforcingType);
85 response.setCharacterEncoding("UTF-8");
86 response.setContentType("application / json");
87 request.setCharacterEncoding("UTF-8");
89 PrintWriter out = response.getWriter();
90 String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
91 JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
93 out.write(j.toString());
98 response.setCharacterEncoding("UTF-8");
99 request.setCharacterEncoding("UTF-8");
100 PrintWriter out = response.getWriter();
101 out.write(e.getMessage());
106 @RequestMapping(value={"/enforcer_dictionary/remove_enforcer"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
107 public ModelAndView removeEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
109 ObjectMapper mapper = new ObjectMapper();
110 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
111 JsonNode root = mapper.readTree(request.getReader());
112 EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("data").toString(), EnforcingType.class);
113 commonClassDao.delete(enforcingType);
114 response.setCharacterEncoding("UTF-8");
115 response.setContentType("application / json");
116 request.setCharacterEncoding("UTF-8");
118 PrintWriter out = response.getWriter();
120 String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
121 JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
122 out.write(j.toString());
127 System.out.println(e);
128 response.setCharacterEncoding("UTF-8");
129 request.setCharacterEncoding("UTF-8");
130 PrintWriter out = response.getWriter();
131 out.write(e.getMessage());