nexus site path corrected
[portal.git] / ecomp-portal-BE / 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.portalsdk.core.logging.logic.EELFLoggerDelegate;
40 import org.slf4j.MDC;
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;
48
49 @RestController
50 @RequestMapping("/auxapi")
51 @org.springframework.context.annotation.Configuration
52 @EnableAspectJAutoProxy
53 @EPAuditLog
54 public class ExternalAppsRestfulController extends EPRestrictedRESTfulBaseController {
55         
56         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppsRestfulController.class);
57         
58         @Autowired
59         FunctionalMenuService functionalMenuService;
60         
61         @Autowired
62         EPLoginService epLoginService;
63         
64         @Autowired
65         AdminRolesService adminRolesService;
66         
67         @RequestMapping(value={"/getFavorites"}, method = RequestMethod.GET,produces = "application/json")
68         public List<FavoritesFunctionalMenuItemJson> getFavoritesForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
69                 String loginId                  = "";
70                 String userAgent                = "";
71                 List<FavoritesFunctionalMenuItemJson> favorites = null;
72                 
73                 loginId                 = request.getHeader(EPSystemProperties.MDC_LOGIN_ID);
74                 userAgent               = MDC.get(EPSystemProperties.PARTNER_NAME);
75                                 
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.");
81                 } else {
82                         favorites = functionalMenuService.getFavoriteItems(epUser.getId());
83                         FieldsValidator fieldsValidator = new FieldsValidator();
84                         response.setStatus(fieldsValidator.httpStatusCode.intValue());
85                         
86                         EcompPortalUtils.logAndSerializeObject("/auxapi/getFavorites", "result = ", favorites);
87                 }
88                 
89                 return favorites;
90         }
91         
92         @RequestMapping(value={"/functionalMenuItemsForUser"}, method = RequestMethod.GET,produces = "application/json")
93         public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
94                 String loginId                  = "";
95                 String userAgent                = "";
96                 List<FunctionalMenuItem> fnMenuItems = null;
97                 
98                 loginId                 = request.getHeader("LoginId");
99                 userAgent               = MDC.get(EPSystemProperties.PARTNER_NAME);
100
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();
109                 }
110                 else {
111                         logger.debug(EELFLoggerDelegate.debugLogger, "getMenuItemsForAuthUser: about to call getFunctionalMenuItemsForUser()");
112                         fnMenuItems = functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId());
113                 }
114                 
115                 FieldsValidator fieldsValidator = new FieldsValidator();
116                 response.setStatus(fieldsValidator.httpStatusCode.intValue());
117                 
118                 EcompPortalUtils.logAndSerializeObject("/auxapi/functionalMenuItemsForUser", "result = ", fnMenuItems);
119                 
120                 return fnMenuItems;
121         }
122         
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());
127         }
128 }