2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.controller.core;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.util.HashMap;
25 import java.util.List;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.json.JSONObject;
32 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
33 import org.openecomp.portalsdk.core.domain.Role;
34 import org.openecomp.portalsdk.core.domain.RoleFunction;
35 import org.openecomp.portalsdk.core.logging.aspect.EELFLoggerAdvice;
36 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.openecomp.portalsdk.core.service.RoleService;
38 import org.openecomp.portalsdk.core.util.SystemProperties;
39 import org.openecomp.portalsdk.core.web.support.JsonMessage;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.ServletRequestUtils;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestMethod;
46 import org.springframework.web.servlet.ModelAndView;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51 import com.fasterxml.jackson.databind.type.TypeFactory;
55 public class RoleController extends RestrictedBaseController {
56 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleController.class);
59 RoleService roleService;
61 private String viewName;
63 @RequestMapping(value = { "/role" }, method = RequestMethod.GET)
64 public ModelAndView role(HttpServletRequest request) {
65 Map<String, Object> model = new HashMap<String, Object>();
66 ObjectMapper mapper = new ObjectMapper();
68 Role role = roleService.getRole(new Long(ServletRequestUtils.getIntParameter(request, "role_id", 0)));
69 logger.info("role_id" + role.getId());
71 model.put("availableRoleFunctions", mapper.writeValueAsString(roleService.getRoleFunctions()));
72 model.put("availableRoles", mapper.writeValueAsString(roleService.getAvailableChildRoles(role.getId())));
73 model.put("role", mapper.writeValueAsString(role));
74 } catch (Exception e) {
75 logger.error("role: failed", e);
76 logger.error(EELFLoggerDelegate.errorLogger, "role failed", e);
78 return new ModelAndView(getViewName(), model);
81 @RequestMapping(value = { "/get_role" }, method = RequestMethod.GET)
82 public void getRole(HttpServletRequest request, HttpServletResponse response) {
83 Map<String, Object> model = new HashMap<String, Object>();
84 ObjectMapper mapper = new ObjectMapper();
86 Role role = roleService.getRole(new Long(ServletRequestUtils.getIntParameter(request, "role_id", 0)));
87 logger.info(EELFLoggerDelegate.applicationLogger, "role_id" + role.getId());
89 model.put("availableRoleFunctions", mapper.writeValueAsString(roleService.getRoleFunctions()));
90 model.put("availableRoles", mapper.writeValueAsString(roleService.getAvailableChildRoles(role.getId())));
91 model.put("role", mapper.writeValueAsString(role));
93 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
94 JSONObject j = new JSONObject(msg);
95 response.getWriter().write(j.toString());
96 } catch (Exception e) {
97 logger.error(EELFLoggerDelegate.errorLogger, "getRole failed", e);
103 * Creates a new role or updates an existing role.
107 * @return Always returns null.
108 * @throws IOException
109 * If the write to the result project fails
111 @RequestMapping(value = { "/role/saveRole" }, method = RequestMethod.POST)
112 public ModelAndView saveRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
114 logger.debug(EELFLoggerDelegate.debugLogger, "RoleController.save");
116 ObjectMapper mapper = new ObjectMapper();
117 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
118 JsonNode root = mapper.readTree(request.getReader());
119 Role role = mapper.readValue(root.get("role").toString(), Role.class);
121 List<Role> childRoles = mapper.readValue(root.get("childRoles").toString(),
122 TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
124 List<RoleFunction> roleFunctions = mapper.readValue(root.get("roleFunctions").toString(),
125 TypeFactory.defaultInstance().constructCollectionType(List.class, RoleFunction.class));
127 Role domainRole = null;
128 if (role.getId() != null) {
129 doAuditLog("saveRole: updating existing role {}", role.getId());
130 domainRole = roleService.getRole(role.getId());
132 domainRole.setName(role.getName());
133 domainRole.setPriority(role.getPriority());
135 doAuditLog("saveRole: creating new role", role.getName());
136 // check for existing role of same name
137 List<Role> roles = roleService.getAvailableRoles();
138 for (Role existRole : roles)
139 if (existRole.getName().equalsIgnoreCase(role.getName()))
140 throw new Exception("role already exists: " + existRole.getName());
142 domainRole = new Role();
143 domainRole.setName(role.getName());
144 domainRole.setPriority(role.getPriority());
145 if (role.getChildRoles().size() > 0) {
146 for (Object childRole : childRoles) {
147 domainRole.addChildRole((Role) childRole);
150 if (role.getRoleFunctions().size() > 0) {
151 for (Object roleFunction : roleFunctions) {
152 domainRole.addRoleFunction((RoleFunction) roleFunction);
157 roleService.saveRole(domainRole);
159 String responseString = mapper.writeValueAsString(domainRole);
160 j = new JSONObject("{role: " + responseString + "}");
161 } catch (Exception e) {
162 // Produce JSON error message
163 logger.error(EELFLoggerDelegate.errorLogger, "saveRole failed", e);
164 j = new JSONObject("{error: '" + e.getMessage() + "'}");
167 response.setCharacterEncoding("UTF-8");
168 response.setContentType("application/json");
169 PrintWriter out = response.getWriter();
170 out.write(j.toString());
174 @RequestMapping(value = { "/role/removeRoleFunction" }, method = RequestMethod.POST)
175 public ModelAndView removeRoleFunction(HttpServletRequest request, HttpServletResponse response) throws Exception {
177 logger.info(EELFLoggerDelegate.applicationLogger, "RoleController.removeRoleFunction");
180 ObjectMapper mapper = new ObjectMapper();
181 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
182 JsonNode root = mapper.readTree(request.getReader());
183 RoleFunction roleFunction = mapper.readValue(root.get("roleFunction").toString(), RoleFunction.class);
185 Role domainRole = roleService.getRole(new Long(ServletRequestUtils.getIntParameter(request, "role_id", 0)));
186 doAuditLog("Remove role function {} from role {}", roleFunction.getCode(),
187 ServletRequestUtils.getIntParameter(request, "role_id", 0));
189 domainRole.removeRoleFunction(roleFunction.getCode());
191 roleService.saveRole(domainRole);
193 response.setCharacterEncoding("UTF-8");
194 response.setContentType("application/json");
195 String responseString = mapper.writeValueAsString(domainRole);
196 JSONObject j = new JSONObject("{role: " + responseString + "}");
197 PrintWriter out = response.getWriter();
198 out.write(j.toString());
200 } catch (Exception e) {
201 logger.error(EELFLoggerDelegate.errorLogger, "removeRole failed", e);
202 response.setCharacterEncoding("UTF-8");
203 PrintWriter out = response.getWriter();
204 out.write(e.getMessage());
210 @RequestMapping(value = { "/role/addRoleFunction" }, method = RequestMethod.POST)
211 public ModelAndView addRoleFunction(HttpServletRequest request, HttpServletResponse response) throws Exception {
213 logger.info(EELFLoggerDelegate.applicationLogger, "RoleController.removeRoleFunction");
216 ObjectMapper mapper = new ObjectMapper();
217 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
218 JsonNode root = mapper.readTree(request.getReader());
219 RoleFunction roleFunction = mapper.readValue(root.get("roleFunction").toString(), RoleFunction.class);
221 Role domainRole = roleService.getRole(new Long(ServletRequestUtils.getIntParameter(request, "role_id", 0)));
223 domainRole.addRoleFunction(roleFunction);
225 roleService.saveRole(domainRole);
226 doAuditLog("Add role function {} to role {}", roleFunction.getCode(),
227 ServletRequestUtils.getIntParameter(request, "role_id", 0));
229 response.setCharacterEncoding("UTF-8");
230 response.setContentType("application/json");
231 String responseString = mapper.writeValueAsString(domainRole);
232 JSONObject j = new JSONObject("{role: " + responseString + "}");
233 PrintWriter out = response.getWriter();
234 out.write(j.toString());
236 } catch (Exception e) {
237 logger.error(EELFLoggerDelegate.errorLogger, "removeRoleFunction failed", e);
238 response.setCharacterEncoding("UTF-8");
239 PrintWriter out = response.getWriter();
240 out.write(e.getMessage());
246 @RequestMapping(value = { "/role/removeChildRole" }, method = RequestMethod.POST)
247 public ModelAndView removeChildRole(HttpServletRequest request, HttpServletResponse response) throws Exception {
249 logger.info(EELFLoggerDelegate.applicationLogger, "RoleController.removeChileRole");
251 ObjectMapper mapper = new ObjectMapper();
252 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
253 JsonNode root = mapper.readTree(request.getReader());
254 Role childRole = mapper.readValue(root.get("childRole").toString(), Role.class);
256 Role domainRole = roleService.getRole(new Long(ServletRequestUtils.getIntParameter(request, "role_id", 0)));
258 domainRole.removeChildRole(childRole.getId());
259 doAuditLog("remove child role {} from role {}", childRole.getId(),
260 ServletRequestUtils.getIntParameter(request, "role_id", 0));
262 roleService.saveRole(domainRole);
264 response.setCharacterEncoding("UTF-8");
265 response.setContentType("application/json");
266 String responseString = mapper.writeValueAsString(domainRole);
267 JSONObject j = new JSONObject("{role: " + responseString + "}");
268 PrintWriter out = response.getWriter();
269 out.write(j.toString());
271 } catch (Exception e) {
272 logger.error(EELFLoggerDelegate.errorLogger, "removeChildRole failed", e);
273 response.setCharacterEncoding("UTF-8");
274 PrintWriter out = response.getWriter();
275 out.write(e.getMessage());
281 @RequestMapping(value = { "/role/addChildRole" }, method = RequestMethod.POST)
282 public ModelAndView addChildRole(HttpServletRequest request, HttpServletResponse response) throws Exception {
284 logger.info(EELFLoggerDelegate.applicationLogger, "RoleController.addChileRole");
287 ObjectMapper mapper = new ObjectMapper();
288 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
289 JsonNode root = mapper.readTree(request.getReader());
290 Role childRole = mapper.readValue(root.get("childRole").toString(), Role.class);
292 Role domainRole = roleService.getRole(new Long(ServletRequestUtils.getIntParameter(request, "role_id", 0)));
294 domainRole.addChildRole(childRole);
296 roleService.saveRole(domainRole);
297 doAuditLog("Add child role {} to role {}", childRole.getId(),
298 ServletRequestUtils.getIntParameter(request, "role_id", 0));
300 response.setCharacterEncoding("UTF-8");
301 response.setContentType("application/json");
302 String responseString = mapper.writeValueAsString(domainRole);
303 JSONObject j = new JSONObject("{role: " + responseString + "}");
304 PrintWriter out = response.getWriter();
305 out.write(j.toString());
307 } catch (Exception e) {
308 logger.error(EELFLoggerDelegate.errorLogger, "addChildRole failed", e);
309 response.setCharacterEncoding("UTF-8");
310 PrintWriter out = response.getWriter();
311 out.write(e.getMessage());
318 * Sets context with begin and end timestamps at current date & time, writes
319 * the specified message and parameters to the audit log, then removes the
320 * timestamps from context.
325 private void doAuditLog(String message, Object... parameters) {
326 final String currentDateTime = EELFLoggerAdvice.getCurrentDateTimeUTC();
327 // Set the MDC with audit properties
328 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, currentDateTime);
329 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, currentDateTime);
330 logger.info(EELFLoggerDelegate.auditLogger, message, parameters);
331 MDC.remove(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
332 MDC.remove(SystemProperties.AUDITLOG_END_TIMESTAMP);
335 public String getViewName() {
339 public void setViewName(String viewName) {
340 this.viewName = viewName;