e9190968dda00d2ddba366735de759c732a2578f
[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.util.HashMap;
30 import java.util.Map;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.json.JSONObject;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.GlobalRoleSettings;
40 import org.onap.policy.utils.PolicyUtils;
41 import org.onap.portalsdk.core.controller.RestrictedBaseController;
42 import org.onap.portalsdk.core.web.support.JsonMessage;
43 import org.onap.portalsdk.core.web.support.UserUtils;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.MediaType;
46 import org.springframework.stereotype.Controller;
47 import org.springframework.web.bind.annotation.RequestMapping;
48 import org.springframework.web.servlet.ModelAndView;
49
50 @Controller
51 @RequestMapping({"/"})
52 public class AdminTabController extends RestrictedBaseController {
53
54     private static final Logger LOGGER = FlexLogger.getLogger(AdminTabController.class);
55     private static final String CHARACTER_ENCODING = "UTF-8";
56
57     private static CommonClassDao commonClassDao;
58
59     public AdminTabController() {
60         // default constructor
61     }
62
63     @Autowired
64     private AdminTabController(CommonClassDao commonClassDao) {
65         AdminTabController.commonClassDao = commonClassDao;
66     }
67
68     public static CommonClassDao getCommonClassDao() {
69         return commonClassDao;
70     }
71
72     public static void setCommonClassDao(CommonClassDao commonClassDao) {
73         AdminTabController.commonClassDao = commonClassDao;
74     }
75
76     /**
77      * getAdminTabEntityData.
78      *
79      * @param request HttpServletRequest
80      * @param response HttpServletResponse
81      */
82     @RequestMapping(
83             value = {"/get_LockDownData"},
84             method = {org.springframework.web.bind.annotation.RequestMethod.GET},
85             produces = MediaType.APPLICATION_JSON_VALUE)
86     public void getAdminTabEntityData(HttpServletRequest request, HttpServletResponse response) {
87         try {
88             Map<String, Object> model = new HashMap<>();
89             ObjectMapper mapper = new ObjectMapper();
90             model.put("lockdowndata", mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class)));
91             response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
92         } catch (Exception e) {
93             LOGGER.error("Exception Occured" + e);
94         }
95     }
96
97     /**
98      * saveAdminTabLockdownValue.
99      *
100      * @param request HttpServletRequest
101      * @param response HttpServletResponse
102      * @return ModelAndView object
103      * @throws IOException IOException
104      */
105     @RequestMapping(
106             value = {"/adminTabController/save_LockDownValue.htm"},
107             method = {org.springframework.web.bind.annotation.RequestMethod.POST})
108     public ModelAndView saveAdminTabLockdownValue(HttpServletRequest request, HttpServletResponse response)
109             throws IOException {
110         response.setCharacterEncoding(CHARACTER_ENCODING);
111         request.setCharacterEncoding(CHARACTER_ENCODING);
112         try {
113             ObjectMapper mapper = new ObjectMapper();
114             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
115             String userId = UserUtils.getUserSession(request).getOrgUserId();
116             LOGGER.info(
117                     "********************Logging UserID for Application Lockdown Function**************************");
118             LOGGER.info("UserId:  " + userId);
119             LOGGER.info(
120                     "**********************************************************************************************");
121             JsonNode root = mapper.readTree(request.getReader());
122             GlobalRoleSettings globalRole =
123                     mapper.readValue(root.get("lockdowndata").toString(), GlobalRoleSettings.class);
124             globalRole.setRole("super-admin");
125             commonClassDao.update(globalRole);
126
127             response.setContentType("application / json");
128
129             String responseString = mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class));
130
131             response.getWriter().write(new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString
132                     + "}").toString());
133         } catch (Exception e) {
134             LOGGER.error("Exception Occured" + e);
135             response.getWriter().write(PolicyUtils.CATCH_EXCEPTION);
136         }
137         return null;
138     }
139 }