More sonar cleanup and line consolidation
[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, 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Bell Canada
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controller;
23
24 import com.fasterxml.jackson.databind.DeserializationFeature;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27
28 import java.io.IOException;
29 import java.io.PrintWriter;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.json.JSONObject;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.GlobalRoleSettings;
41 import org.onap.policy.utils.PolicyUtils;
42 import org.onap.portalsdk.core.controller.RestrictedBaseController;
43 import org.onap.portalsdk.core.web.support.JsonMessage;
44 import org.onap.portalsdk.core.web.support.UserUtils;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.http.MediaType;
47 import org.springframework.stereotype.Controller;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.servlet.ModelAndView;
50
51 @Controller
52 @RequestMapping({"/"})
53 public class AdminTabController extends RestrictedBaseController {
54
55     private static final Logger LOGGER = FlexLogger.getLogger(AdminTabController.class);
56     private static final String CHARACTER_ENCODING = "UTF-8";
57
58     private static CommonClassDao commonClassDao;
59
60     public AdminTabController() {
61         // default constructor
62     }
63
64     @Autowired
65     private AdminTabController(CommonClassDao commonClassDao) {
66         AdminTabController.commonClassDao = commonClassDao;
67     }
68
69     public static CommonClassDao getCommonClassDao() {
70         return commonClassDao;
71     }
72
73     public static void setCommonClassDao(CommonClassDao commonClassDao) {
74         AdminTabController.commonClassDao = commonClassDao;
75     }
76
77     /**
78      * getAdminTabEntityData.
79      *
80      * @param request HttpServletRequest
81      * @param response HttpServletResponse
82      */
83     @RequestMapping(
84             value = {"/get_LockDownData"},
85             method = {org.springframework.web.bind.annotation.RequestMethod.GET},
86             produces = MediaType.APPLICATION_JSON_VALUE)
87     public void getAdminTabEntityData(HttpServletRequest request, HttpServletResponse response) {
88         try {
89             Map<String, Object> model = new HashMap<>();
90             ObjectMapper mapper = new ObjectMapper();
91             model.put("lockdowndata", mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class)));
92             response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
93         } catch (Exception e) {
94             LOGGER.error("Exception Occured" + e);
95         }
96     }
97
98     /**
99      * saveAdminTabLockdownValue.
100      *
101      * @param request HttpServletRequest
102      * @param response HttpServletResponse
103      * @return ModelAndView object
104      * @throws IOException IOException
105      */
106     @RequestMapping(
107             value = {"/adminTabController/save_LockDownValue.htm"},
108             method = {org.springframework.web.bind.annotation.RequestMethod.POST})
109     public ModelAndView saveAdminTabLockdownValue(HttpServletRequest request, HttpServletResponse response)
110             throws IOException {
111         try {
112             ObjectMapper mapper = new ObjectMapper();
113             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
114             String userId = UserUtils.getUserSession(request).getOrgUserId();
115             LOGGER.info(
116                     "********************Logging UserID for Application Lockdown Function**************************");
117             LOGGER.info("UserId:  " + userId);
118             LOGGER.info(
119                     "**********************************************************************************************");
120             JsonNode root = mapper.readTree(request.getReader());
121             GlobalRoleSettings globalRole =
122                     mapper.readValue(root.get("lockdowndata").toString(), GlobalRoleSettings.class);
123             globalRole.setRole("super-admin");
124             commonClassDao.update(globalRole);
125
126             response.setCharacterEncoding(CHARACTER_ENCODING);
127             response.setContentType("application / json");
128             request.setCharacterEncoding(CHARACTER_ENCODING);
129
130             String responseString = mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class));
131
132             response.getWriter().write(new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString
133                     + "}").toString());
134
135             return null;
136         } catch (Exception e) {
137             LOGGER.error("Exception Occured" + e);
138             response.setCharacterEncoding(CHARACTER_ENCODING);
139             request.setCharacterEncoding(CHARACTER_ENCODING);
140             response.getWriter().write(PolicyUtils.CATCH_EXCEPTION);
141         }
142         return null;
143     }
144 }