1ddc07da45794aa7f6e43d7387b4f5d3b63df314
[portal.git] / ecomp-portal-BE-os / src / main / java / org / onap / portalapp / portal / controller / ExternalAppsRestfulController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import java.io.IOException;
41 import java.util.List;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.onap.portalapp.controller.EPRestrictedRESTfulBaseController;
47 import org.onap.portalapp.portal.controller.ExternalAppsRestfulController;
48 import org.onap.portalapp.portal.domain.EPUser;
49 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
50 import org.onap.portalapp.portal.service.AdminRolesService;
51 import org.onap.portalapp.portal.service.EPLoginService;
52 import org.onap.portalapp.portal.service.FunctionalMenuService;
53 import org.onap.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
54 import org.onap.portalapp.portal.transport.FieldsValidator;
55 import org.onap.portalapp.portal.transport.FunctionalMenuItem;
56 import org.onap.portalapp.portal.utils.EPSystemProperties;
57 import org.onap.portalapp.portal.utils.EcompPortalUtils;
58 import org.onap.portalapp.portal.utils.PortalConstants;
59 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
60 import org.slf4j.MDC;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.context.annotation.EnableAspectJAutoProxy;
63 import org.springframework.http.HttpStatus;
64 import org.springframework.web.bind.annotation.ExceptionHandler;
65 import org.springframework.web.bind.annotation.RequestMapping;
66 import org.springframework.web.bind.annotation.GetMapping;
67 import org.springframework.web.bind.annotation.RequestMethod;
68 import org.springframework.web.bind.annotation.RestController;
69
70 @RestController
71 @RequestMapping(PortalConstants.REST_AUX_API)
72 @org.springframework.context.annotation.Configuration
73 @EnableAspectJAutoProxy
74 @EPAuditLog
75 public class ExternalAppsRestfulController extends EPRestrictedRESTfulBaseController {
76         
77         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppsRestfulController.class);
78         
79         @Autowired
80         FunctionalMenuService functionalMenuService;
81         
82         @Autowired
83         EPLoginService epLoginService;
84         
85         @Autowired
86         AdminRolesService adminRolesService;
87         
88         @GetMapping(value={"/getFavorites"}, produces = "application/json")
89         public List<FavoritesFunctionalMenuItemJson> getFavoritesForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
90                 String loginId                  = "";
91                 String userAgent                = "";
92                 List<FavoritesFunctionalMenuItemJson> favorites = null;
93                 
94                 loginId                 = request.getHeader(EPSystemProperties.MDC_LOGIN_ID);
95                 userAgent               = MDC.get(EPSystemProperties.PARTNER_NAME);
96                                 
97                 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
98                 logger.info(EELFLoggerDelegate.errorLogger, "getFavorites request was received from " + userAgent + " for the user " + loginId + ".");
99                 if (epUser==null || epUser.getId()==null) {
100                         logger.error(EELFLoggerDelegate.errorLogger, "No User record found for the LoginId '" + loginId + "' in the database.");
101                         throw new Exception("Received null for Login-Id.");
102                 } else {
103                         favorites = functionalMenuService.getFavoriteItems(epUser.getId());
104                         FieldsValidator fieldsValidator = new FieldsValidator();
105                         response.setStatus(fieldsValidator.httpStatusCode.intValue());
106                         
107                         EcompPortalUtils.logAndSerializeObject("/auxapi/getFavorites", "result = ", favorites);
108                 }
109                 
110                 return favorites;
111         }
112         
113         @GetMapping(value={"/functionalMenuItemsForUser"}, produces = "application/json")
114         public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
115                 String loginId                  = "";
116                 String userAgent                = "";
117                 List<FunctionalMenuItem> fnMenuItems = null;
118                 
119                 loginId                 = request.getHeader("LoginId");
120                 userAgent               = MDC.get(EPSystemProperties.PARTNER_NAME);
121
122                 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
123                 logger.info(EELFLoggerDelegate.errorLogger, "getFunctionalMenuItemsForUser request was received from " + userAgent + " for the user " + loginId + ".");
124                 if (epUser==null || epUser.getId()==null) {
125                         logger.error(EELFLoggerDelegate.errorLogger, "No User record found for the LoginId '" + loginId + "' in the database.");
126                         throw new Exception("Received null for Login-Id.");
127                 } else if (adminRolesService.isSuperAdmin(epUser)) {
128                         logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenuHandler: SuperUser, about to call getFunctionalMenuItems()");
129                         fnMenuItems = functionalMenuService.getFunctionalMenuItems();
130                 }
131                 else {
132                         logger.debug(EELFLoggerDelegate.debugLogger, "getMenuItemsForAuthUser: about to call getFunctionalMenuItemsForUser()");
133                         fnMenuItems = functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId());
134                 }
135                 
136                 FieldsValidator fieldsValidator = new FieldsValidator();
137                 response.setStatus(fieldsValidator.httpStatusCode.intValue());
138                 
139                 EcompPortalUtils.logAndSerializeObject("/auxapi/functionalMenuItemsForUser", "result = ", fnMenuItems);
140                 
141                 return fnMenuItems;
142         }
143         
144         @ExceptionHandler(Exception.class)
145         protected void handleBadRequests(Exception e, HttpServletResponse response) throws IOException {
146                 logger.warn(EELFLoggerDelegate.errorLogger, "Handling bad request", e);
147                 response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
148         }
149 }