139b2b87f164180cb9f1655de2a7d7501d215e99
[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
56     private static CommonClassDao commonClassDao;
57
58     public AdminTabController() {
59         // default constructor
60     }
61
62     @Autowired
63     private AdminTabController(CommonClassDao commonClassDao) {
64         AdminTabController.commonClassDao = commonClassDao;
65     }
66
67     public static CommonClassDao getCommonClassDao() {
68         return commonClassDao;
69     }
70
71     public static void setCommonClassDao(CommonClassDao commonClassDao) {
72         AdminTabController.commonClassDao = commonClassDao;
73     }
74
75     /**
76      * getAdminTabEntityData.
77      *
78      * @param request HttpServletRequest
79      * @param response HttpServletResponse
80      */
81     @RequestMapping(
82             value = {"/get_LockDownData"},
83             method = {org.springframework.web.bind.annotation.RequestMethod.GET},
84             produces = MediaType.APPLICATION_JSON_VALUE)
85     public void getAdminTabEntityData(HttpServletRequest request, HttpServletResponse response) {
86         try {
87             Map<String, Object> model = new HashMap<>();
88             ObjectMapper mapper = new ObjectMapper();
89             model.put("lockdowndata", mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class)));
90             response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
91         } catch (Exception e) {
92             LOGGER.error("Exception Occured" + e);
93         }
94     }
95
96     /**
97      * saveAdminTabLockdownValue.
98      *
99      * @param request HttpServletRequest
100      * @param response HttpServletResponse
101      * @return ModelAndView object
102      * @throws IOException IOException
103      */
104     @RequestMapping(
105             value = {"/adminTabController/save_LockDownValue.htm"},
106             method = {org.springframework.web.bind.annotation.RequestMethod.POST})
107     public ModelAndView saveAdminTabLockdownValue(HttpServletRequest request, HttpServletResponse response)
108             throws IOException {
109         response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
110         request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
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.setContentType(PolicyUtils.APPLICATION_JSON);
127
128             String responseString = mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class));
129
130             response.getWriter().write(new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString
131                     + "}").toString());
132         } catch (Exception e) {
133             LOGGER.error("Exception Occured" + e);
134             response.getWriter().write(PolicyUtils.CATCH_EXCEPTION);
135         }
136         return null;
137     }
138 }