[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / portal / controller / ExternalAppsRestfulController.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.controller;
21
22 import java.io.IOException;
23 import java.util.List;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
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;
41 import org.slf4j.MDC;
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;
49
50 @RestController
51 @RequestMapping(PortalConstants.REST_AUX_API)
52 @org.springframework.context.annotation.Configuration
53 @EnableAspectJAutoProxy
54 @EPAuditLog
55 public class ExternalAppsRestfulController extends EPRestrictedRESTfulBaseController {
56         
57         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppsRestfulController.class);
58         
59         @Autowired
60         FunctionalMenuService functionalMenuService;
61         
62         @Autowired
63         EPLoginService epLoginService;
64         
65         @Autowired
66         AdminRolesService adminRolesService;
67         
68         @RequestMapping(value={"/getFavorites"}, method = RequestMethod.GET,produces = "application/json")
69         public List<FavoritesFunctionalMenuItemJson> getFavoritesForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
70                 String loginId                  = "";
71                 String userAgent                = "";
72                 List<FavoritesFunctionalMenuItemJson> favorites = null;
73                 
74                 loginId                 = request.getHeader(EPSystemProperties.MDC_LOGIN_ID);
75                 userAgent               = MDC.get(EPSystemProperties.PARTNER_NAME);
76                                 
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.");
82                 } else {
83                         favorites = functionalMenuService.getFavoriteItems(epUser.getId());
84                         FieldsValidator fieldsValidator = new FieldsValidator();
85                         response.setStatus(fieldsValidator.httpStatusCode.intValue());
86                         
87                         EcompPortalUtils.logAndSerializeObject("/auxapi/getFavorites", "result = ", favorites);
88                 }
89                 
90                 return favorites;
91         }
92         
93         @RequestMapping(value={"/functionalMenuItemsForUser"}, method = RequestMethod.GET,produces = "application/json")
94         public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
95                 String loginId                  = "";
96                 String userAgent                = "";
97                 List<FunctionalMenuItem> fnMenuItems = null;
98                 
99                 loginId                 = request.getHeader("LoginId");
100                 userAgent               = MDC.get(EPSystemProperties.PARTNER_NAME);
101
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();
110                 }
111                 else {
112                         logger.debug(EELFLoggerDelegate.debugLogger, "getMenuItemsForAuthUser: about to call getFunctionalMenuItemsForUser()");
113                         fnMenuItems = functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId());
114                 }
115                 
116                 FieldsValidator fieldsValidator = new FieldsValidator();
117                 response.setStatus(fieldsValidator.httpStatusCode.intValue());
118                 
119                 EcompPortalUtils.logAndSerializeObject("/auxapi/functionalMenuItemsForUser", "result = ", fnMenuItems);
120                 
121                 return fnMenuItems;
122         }
123         
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());
128         }
129 }