Resolved Fortify System Information Leak issues
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / EnforcerDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.controller;
22
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;
29 import java.util.Map;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
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;
46
47 import com.fasterxml.jackson.databind.DeserializationFeature;
48 import com.fasterxml.jackson.databind.JsonNode;
49 import com.fasterxml.jackson.databind.ObjectMapper;
50
51 @Controller
52 public class EnforcerDictionaryController {
53
54         private static final Logger LOGGER      = FlexLogger.getLogger(EnforcerDictionaryController.class);
55         
56         @Autowired
57         CommonClassDao commonClassDao;
58
59         @RequestMapping(value={"/get_EnforcerTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
60         public void getEnforcerDictionaryEntityData(HttpServletResponse response){
61                 try{
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());
69                         }
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());
74                 }
75                 catch (Exception e){
76                         LOGGER.error("Exception Occured"+e);
77                 }
78         }
79         
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{
82                 try {
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);
89                         }else{
90                                 commonClassDao.update(enforcingType); 
91                         } 
92                         response.setCharacterEncoding("UTF-8");
93                         response.setContentType("application / json");
94                         request.setCharacterEncoding("UTF-8");
95
96                         PrintWriter out = response.getWriter();
97                         String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
98                         JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
99
100                         out.write(j.toString());
101
102                         return null;
103                 }
104                 catch (Exception e){
105                         response.setCharacterEncoding("UTF-8");
106                         request.setCharacterEncoding("UTF-8");
107                         PrintWriter out = response.getWriter();
108                         out.write(PolicyUtils.CATCH_EXCEPTION);
109                         LOGGER.error(e);
110                 }
111                 return null;
112         }
113
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 {
116                 try{
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");
125
126                         PrintWriter out = response.getWriter();
127
128                         String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
129                         JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
130                         out.write(j.toString());
131
132                         return null;
133                 }
134                 catch (Exception e){
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);
140                 }
141                 return null;
142         }
143 }