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.portalsdk.core.logging.logic.EELFLoggerDelegate;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.context.annotation.EnableAspectJAutoProxy;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.web.bind.annotation.ExceptionHandler;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestMethod;
47 import org.springframework.web.bind.annotation.RestController;
50 @RequestMapping("/auxapi")
51 @org.springframework.context.annotation.Configuration
52 @EnableAspectJAutoProxy
54 public class ExternalAppsRestfulController extends EPRestrictedRESTfulBaseController {
56 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppsRestfulController.class);
59 FunctionalMenuService functionalMenuService;
62 EPLoginService epLoginService;
65 AdminRolesService adminRolesService;
67 @RequestMapping(value={"/getFavorites"}, method = RequestMethod.GET,produces = "application/json")
68 public List<FavoritesFunctionalMenuItemJson> getFavoritesForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
70 String userAgent = "";
71 List<FavoritesFunctionalMenuItemJson> favorites = null;
73 loginId = request.getHeader(EPSystemProperties.MDC_LOGIN_ID);
74 userAgent = MDC.get(EPSystemProperties.PARTNER_NAME);
76 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
77 logger.info(EELFLoggerDelegate.errorLogger, "getFavorites request was received from " + userAgent + " for the user " + loginId + ".");
78 if (epUser==null || epUser.getId()==null) {
79 logger.error(EELFLoggerDelegate.errorLogger, "No User record found for the LoginId '" + loginId + "' in the database.");
80 throw new Exception("Received null for Login-Id.");
82 favorites = functionalMenuService.getFavoriteItems(epUser.getId());
83 FieldsValidator fieldsValidator = new FieldsValidator();
84 response.setStatus(fieldsValidator.httpStatusCode.intValue());
86 EcompPortalUtils.logAndSerializeObject("/auxapi/getFavorites", "result = ", favorites);
92 @RequestMapping(value={"/functionalMenuItemsForUser"}, method = RequestMethod.GET,produces = "application/json")
93 public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
95 String userAgent = "";
96 List<FunctionalMenuItem> fnMenuItems = null;
98 loginId = request.getHeader("LoginId");
99 userAgent = MDC.get(EPSystemProperties.PARTNER_NAME);
101 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
102 logger.info(EELFLoggerDelegate.errorLogger, "getFunctionalMenuItemsForUser request was received from " + userAgent + " for the user " + loginId + ".");
103 if (epUser==null || epUser.getId()==null) {
104 logger.error(EELFLoggerDelegate.errorLogger, "No User record found for the LoginId '" + loginId + "' in the database.");
105 throw new Exception("Received null for Login-Id.");
106 } else if (adminRolesService.isSuperAdmin(epUser)) {
107 logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenuHandler: SuperUser, about to call getFunctionalMenuItems()");
108 fnMenuItems = functionalMenuService.getFunctionalMenuItems();
111 logger.debug(EELFLoggerDelegate.debugLogger, "getMenuItemsForAuthUser: about to call getFunctionalMenuItemsForUser()");
112 fnMenuItems = functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId());
115 FieldsValidator fieldsValidator = new FieldsValidator();
116 response.setStatus(fieldsValidator.httpStatusCode.intValue());
118 EcompPortalUtils.logAndSerializeObject("/auxapi/functionalMenuItemsForUser", "result = ", fnMenuItems);
123 @ExceptionHandler(Exception.class)
124 protected void handleBadRequests(Exception e, HttpServletResponse response) throws IOException {
125 logger.warn(EELFLoggerDelegate.errorLogger, "Handling bad request", e);
126 response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());