Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / portal / controller / ExternalAppsRestfulController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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.openecomp.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.openecomp.portalapp.controller.EPRestrictedRESTfulBaseController;
47 import org.openecomp.portalapp.portal.domain.EPUser;
48 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
49 import org.openecomp.portalapp.portal.service.AdminRolesService;
50 import org.openecomp.portalapp.portal.service.EPLoginService;
51 import org.openecomp.portalapp.portal.service.FunctionalMenuService;
52 import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
53 import org.openecomp.portalapp.portal.transport.FieldsValidator;
54 import org.openecomp.portalapp.portal.transport.FunctionalMenuItem;
55 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
56 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
57 import org.openecomp.portalapp.portal.utils.PortalConstants;
58 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.slf4j.MDC;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.context.annotation.EnableAspectJAutoProxy;
62 import org.springframework.http.HttpStatus;
63 import org.springframework.web.bind.annotation.ExceptionHandler;
64 import org.springframework.web.bind.annotation.RequestMapping;
65 import org.springframework.web.bind.annotation.RequestMethod;
66 import org.springframework.web.bind.annotation.RestController;
67
68 @RestController
69 @RequestMapping(PortalConstants.REST_AUX_API)
70 @org.springframework.context.annotation.Configuration
71 @EnableAspectJAutoProxy
72 @EPAuditLog
73 public class ExternalAppsRestfulController extends EPRestrictedRESTfulBaseController {
74         
75         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppsRestfulController.class);
76         
77         @Autowired
78         FunctionalMenuService functionalMenuService;
79         
80         @Autowired
81         EPLoginService epLoginService;
82         
83         @Autowired
84         AdminRolesService adminRolesService;
85         
86         @RequestMapping(value={"/getFavorites"}, method = RequestMethod.GET,produces = "application/json")
87         public List<FavoritesFunctionalMenuItemJson> getFavoritesForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
88                 String loginId                  = "";
89                 String userAgent                = "";
90                 List<FavoritesFunctionalMenuItemJson> favorites = null;
91                 
92                 loginId                 = request.getHeader(EPSystemProperties.MDC_LOGIN_ID);
93                 userAgent               = MDC.get(EPSystemProperties.PARTNER_NAME);
94                                 
95                 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
96                 logger.info(EELFLoggerDelegate.errorLogger, "getFavorites request was received from " + userAgent + " for the user " + loginId + ".");
97                 if (epUser==null || epUser.getId()==null) {
98                         logger.error(EELFLoggerDelegate.errorLogger, "No User record found for the LoginId '" + loginId + "' in the database.");
99                         throw new Exception("Received null for Login-Id.");
100                 } else {
101                         favorites = functionalMenuService.getFavoriteItems(epUser.getId());
102                         FieldsValidator fieldsValidator = new FieldsValidator();
103                         response.setStatus(fieldsValidator.httpStatusCode.intValue());
104                         
105                         EcompPortalUtils.logAndSerializeObject("/auxapi/getFavorites", "result = ", favorites);
106                 }
107                 
108                 return favorites;
109         }
110         
111         @RequestMapping(value={"/functionalMenuItemsForUser"}, method = RequestMethod.GET,produces = "application/json")
112         public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
113                 String loginId                  = "";
114                 String userAgent                = "";
115                 List<FunctionalMenuItem> fnMenuItems = null;
116                 
117                 loginId                 = request.getHeader("LoginId");
118                 userAgent               = MDC.get(EPSystemProperties.PARTNER_NAME);
119
120                 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
121                 logger.info(EELFLoggerDelegate.errorLogger, "getFunctionalMenuItemsForUser request was received from " + userAgent + " for the user " + loginId + ".");
122                 if (epUser==null || epUser.getId()==null) {
123                         logger.error(EELFLoggerDelegate.errorLogger, "No User record found for the LoginId '" + loginId + "' in the database.");
124                         throw new Exception("Received null for Login-Id.");
125                 } else if (adminRolesService.isSuperAdmin(epUser)) {
126                         logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenuHandler: SuperUser, about to call getFunctionalMenuItems()");
127                         fnMenuItems = functionalMenuService.getFunctionalMenuItems();
128                 }
129                 else {
130                         logger.debug(EELFLoggerDelegate.debugLogger, "getMenuItemsForAuthUser: about to call getFunctionalMenuItemsForUser()");
131                         fnMenuItems = functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId());
132                 }
133                 
134                 FieldsValidator fieldsValidator = new FieldsValidator();
135                 response.setStatus(fieldsValidator.httpStatusCode.intValue());
136                 
137                 EcompPortalUtils.logAndSerializeObject("/auxapi/functionalMenuItemsForUser", "result = ", fnMenuItems);
138                 
139                 return fnMenuItems;
140         }
141         
142         @ExceptionHandler(Exception.class)
143         protected void handleBadRequests(Exception e, HttpServletResponse response) throws IOException {
144                 logger.warn(EELFLoggerDelegate.errorLogger, "Handling bad request", e);
145                 response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
146         }
147 }