Fixes for sonar critical issues
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / AdminTabController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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.controller;
22
23
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.json.JSONObject;
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.policy.common.logging.flexlogger.Logger;
35 import org.onap.policy.rest.dao.CommonClassDao;
36 import org.onap.policy.rest.jpa.GlobalRoleSettings;
37 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
38 import org.openecomp.portalsdk.core.web.support.JsonMessage;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.MediaType;
41 import org.springframework.stereotype.Controller;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.servlet.ModelAndView;
44
45 import com.fasterxml.jackson.databind.DeserializationFeature;
46 import com.fasterxml.jackson.databind.JsonNode;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48
49 @Controller
50 @RequestMapping({"/"})
51 public class AdminTabController extends RestrictedBaseController{
52
53         private static final Logger LOGGER      = FlexLogger.getLogger(AdminTabController.class);
54         
55         private static CommonClassDao commonClassDao;
56         
57         public static CommonClassDao getCommonClassDao() {
58                 return commonClassDao;
59         }
60
61         public static void setCommonClassDao(CommonClassDao commonClassDao) {
62                 AdminTabController.commonClassDao = commonClassDao;
63         }
64         
65         @Autowired
66         private AdminTabController(CommonClassDao commonClassDao){
67                 AdminTabController.commonClassDao = commonClassDao;
68         }
69                 
70         public AdminTabController() {
71                 //default constructor
72         }
73
74         @RequestMapping(value={"/get_LockDownData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
75         public void getAdminTabEntityData(HttpServletRequest request, HttpServletResponse response){
76                 try{
77                         Map<String, Object> model = new HashMap<>();
78                         ObjectMapper mapper = new ObjectMapper();
79                         model.put("lockdowndata", mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class)));
80                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
81                         JSONObject j = new JSONObject(msg);
82                         response.getWriter().write(j.toString());
83                 }
84                 catch (Exception e){
85                         LOGGER.error("Exception Occured"+e);
86                 }
87         }
88         
89         @RequestMapping(value={"/adminTabController/save_LockDownValue.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
90         public ModelAndView saveAdminTabLockdownValue(HttpServletRequest request, HttpServletResponse response) throws IOException{
91                 try {
92                         ObjectMapper mapper = new ObjectMapper();
93                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
94                         JsonNode root = mapper.readTree(request.getReader());
95                         GlobalRoleSettings globalRole = mapper.readValue(root.get("lockdowndata").toString(), GlobalRoleSettings.class);
96                         globalRole.setRole("super-admin");
97                         commonClassDao.update(globalRole);
98                         
99                         response.setCharacterEncoding("UTF-8");
100                         response.setContentType("application / json");
101                         request.setCharacterEncoding("UTF-8");
102
103                         PrintWriter out = response.getWriter();
104                         String responseString = mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class));
105                         JSONObject j = new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}");
106
107                         out.write(j.toString());
108
109                         return null;
110                 }
111                 catch (Exception e){
112                         LOGGER.error("Exception Occured"+e);
113                         response.setCharacterEncoding("UTF-8");
114                         request.setCharacterEncoding("UTF-8");
115                         PrintWriter out = response.getWriter();
116                         out.write(e.getMessage());
117                 }
118                 return null;
119         }
120 }