f877587e56811fb1e375002f4e5dbea32bc5a94f
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.RequestMethod;
67 import org.springframework.web.bind.annotation.RestController;
68
69 @RestController
70 @RequestMapping(PortalConstants.REST_AUX_API)
71 @org.springframework.context.annotation.Configuration
72 @EnableAspectJAutoProxy
73 @EPAuditLog
74 public class ExternalAppsRestfulController extends EPRestrictedRESTfulBaseController {
75         
76         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppsRestfulController.class);
77         
78         @Autowired
79         FunctionalMenuService functionalMenuService;
80         
81         @Autowired
82         EPLoginService epLoginService;
83         
84         @Autowired
85         AdminRolesService adminRolesService;
86         
87         @RequestMapping(value={"/getFavorites"}, method = RequestMethod.GET,produces = "application/json")
88         public List<FavoritesFunctionalMenuItemJson> getFavoritesForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
89                 String loginId                  = "";
90                 String userAgent                = "";
91                 List<FavoritesFunctionalMenuItemJson> favorites = null;
92                 
93                 loginId                 = request.getHeader(EPSystemProperties.MDC_LOGIN_ID);
94                 userAgent               = MDC.get(EPSystemProperties.PARTNER_NAME);
95                                 
96                 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
97                 logger.info(EELFLoggerDelegate.errorLogger, "getFavorites request was received from " + userAgent + " for the user " + loginId + ".");
98                 if (epUser==null || epUser.getId()==null) {
99                         logger.error(EELFLoggerDelegate.errorLogger, "No User record found for the LoginId '" + loginId + "' in the database.");
100                         throw new Exception("Received null for Login-Id.");
101                 } else {
102                         favorites = functionalMenuService.getFavoriteItems(epUser.getId());
103                         FieldsValidator fieldsValidator = new FieldsValidator();
104                         response.setStatus(fieldsValidator.httpStatusCode.intValue());
105                         
106                         EcompPortalUtils.logAndSerializeObject("/auxapi/getFavorites", "result = ", favorites);
107                 }
108                 
109                 return favorites;
110         }
111         
112         @RequestMapping(value={"/functionalMenuItemsForUser"}, method = RequestMethod.GET,produces = "application/json")
113         public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
114                 String loginId                  = "";
115                 String userAgent                = "";
116                 List<FunctionalMenuItem> fnMenuItems = null;
117                 
118                 loginId                 = request.getHeader("LoginId");
119                 userAgent               = MDC.get(EPSystemProperties.PARTNER_NAME);
120
121                 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
122                 logger.info(EELFLoggerDelegate.errorLogger, "getFunctionalMenuItemsForUser request was received from " + userAgent + " for the user " + loginId + ".");
123                 if (epUser==null || epUser.getId()==null) {
124                         logger.error(EELFLoggerDelegate.errorLogger, "No User record found for the LoginId '" + loginId + "' in the database.");
125                         throw new Exception("Received null for Login-Id.");
126                 } else if (adminRolesService.isSuperAdmin(epUser)) {
127                         logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenuHandler: SuperUser, about to call getFunctionalMenuItems()");
128                         fnMenuItems = functionalMenuService.getFunctionalMenuItems();
129                 }
130                 else {
131                         logger.debug(EELFLoggerDelegate.debugLogger, "getMenuItemsForAuthUser: about to call getFunctionalMenuItemsForUser()");
132                         fnMenuItems = functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId());
133                 }
134                 
135                 FieldsValidator fieldsValidator = new FieldsValidator();
136                 response.setStatus(fieldsValidator.httpStatusCode.intValue());
137                 
138                 EcompPortalUtils.logAndSerializeObject("/auxapi/functionalMenuItemsForUser", "result = ", fnMenuItems);
139                 
140                 return fnMenuItems;
141         }
142         
143         @ExceptionHandler(Exception.class)
144         protected void handleBadRequests(Exception e, HttpServletResponse response) throws IOException {
145                 logger.warn(EELFLoggerDelegate.errorLogger, "Handling bad request", e);
146                 response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
147         }
148 }