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.portal.controller;
22 import java.io.IOException;
23 import java.util.List;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
28 import org.openecomp.portalapp.controller.EPRestrictedRESTfulBaseController;
29 import org.openecomp.portalapp.portal.domain.EPUser;
30 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
31 import org.openecomp.portalapp.portal.service.AdminRolesService;
32 import org.openecomp.portalapp.portal.service.EPLoginService;
33 import org.openecomp.portalapp.portal.service.FunctionalMenuService;
34 import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
35 import org.openecomp.portalapp.portal.transport.FieldsValidator;
36 import org.openecomp.portalapp.portal.transport.FunctionalMenuItem;
37 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
38 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
39 import org.openecomp.portalapp.portal.utils.PortalConstants;
40 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.context.annotation.EnableAspectJAutoProxy;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.web.bind.annotation.ExceptionHandler;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestMethod;
48 import org.springframework.web.bind.annotation.RestController;
51 @RequestMapping(PortalConstants.REST_AUX_API)
52 @org.springframework.context.annotation.Configuration
53 @EnableAspectJAutoProxy
55 public class ExternalAppsRestfulController extends EPRestrictedRESTfulBaseController {
57 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppsRestfulController.class);
60 FunctionalMenuService functionalMenuService;
63 EPLoginService epLoginService;
66 AdminRolesService adminRolesService;
68 @RequestMapping(value={"/getFavorites"}, method = RequestMethod.GET,produces = "application/json")
69 public List<FavoritesFunctionalMenuItemJson> getFavoritesForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
71 String userAgent = "";
72 List<FavoritesFunctionalMenuItemJson> favorites = null;
74 loginId = request.getHeader(EPSystemProperties.MDC_LOGIN_ID);
75 userAgent = MDC.get(EPSystemProperties.PARTNER_NAME);
77 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
78 logger.info(EELFLoggerDelegate.errorLogger, "getFavorites request was received from " + userAgent + " for the user " + loginId + ".");
79 if (epUser==null || epUser.getId()==null) {
80 logger.error(EELFLoggerDelegate.errorLogger, "No User record found for the LoginId '" + loginId + "' in the database.");
81 throw new Exception("Received null for Login-Id.");
83 favorites = functionalMenuService.getFavoriteItems(epUser.getId());
84 FieldsValidator fieldsValidator = new FieldsValidator();
85 response.setStatus(fieldsValidator.httpStatusCode.intValue());
87 EcompPortalUtils.logAndSerializeObject("/auxapi/getFavorites", "result = ", favorites);
93 @RequestMapping(value={"/functionalMenuItemsForUser"}, method = RequestMethod.GET,produces = "application/json")
94 public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
96 String userAgent = "";
97 List<FunctionalMenuItem> fnMenuItems = null;
99 loginId = request.getHeader("LoginId");
100 userAgent = MDC.get(EPSystemProperties.PARTNER_NAME);
102 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
103 logger.info(EELFLoggerDelegate.errorLogger, "getFunctionalMenuItemsForUser request was received from " + userAgent + " for the user " + loginId + ".");
104 if (epUser==null || epUser.getId()==null) {
105 logger.error(EELFLoggerDelegate.errorLogger, "No User record found for the LoginId '" + loginId + "' in the database.");
106 throw new Exception("Received null for Login-Id.");
107 } else if (adminRolesService.isSuperAdmin(epUser)) {
108 logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenuHandler: SuperUser, about to call getFunctionalMenuItems()");
109 fnMenuItems = functionalMenuService.getFunctionalMenuItems();
112 logger.debug(EELFLoggerDelegate.debugLogger, "getMenuItemsForAuthUser: about to call getFunctionalMenuItemsForUser()");
113 fnMenuItems = functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId());
116 FieldsValidator fieldsValidator = new FieldsValidator();
117 response.setStatus(fieldsValidator.httpStatusCode.intValue());
119 EcompPortalUtils.logAndSerializeObject("/auxapi/functionalMenuItemsForUser", "result = ", fnMenuItems);
124 @ExceptionHandler(Exception.class)
125 protected void handleBadRequests(Exception e, HttpServletResponse response) throws IOException {
126 logger.warn(EELFLoggerDelegate.errorLogger, "Handling bad request", e);
127 response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());