Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / uebhandler / FunctionalMenuHandler.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.uebhandler;
39
40 import java.util.List;
41
42 import org.openecomp.portalapp.portal.domain.EPUser;
43 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
44 import org.openecomp.portalapp.portal.service.AdminRolesService;
45 import org.openecomp.portalapp.portal.service.FunctionalMenuService;
46 import org.openecomp.portalapp.portal.service.SearchService;
47 import org.openecomp.portalapp.portal.transport.FunctionalMenuItem;
48 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
49 import org.openecomp.portalsdk.core.onboarding.ueb.UebException;
50 import org.openecomp.portalsdk.core.onboarding.ueb.UebManager;
51 import org.openecomp.portalsdk.core.onboarding.ueb.UebMsg;
52 import org.openecomp.portalsdk.core.service.DataAccessService;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.context.annotation.EnableAspectJAutoProxy;
55 import org.springframework.scheduling.annotation.Async;
56 import org.springframework.stereotype.Component;
57
58 import com.google.gson.Gson;
59
60 @Component
61 @org.springframework.context.annotation.Configuration
62 @EnableAspectJAutoProxy
63 @EPAuditLog
64 public class FunctionalMenuHandler {
65         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FunctionalMenuHandler.class);
66
67         @Autowired
68         private AdminRolesService adminRolesService;
69
70         @Autowired
71         private FunctionalMenuService functionalMenuService;
72
73         @Autowired
74         private SearchService searchSvc;
75
76         @Async
77         public Boolean getFunctionalMenu(UebMsg requestMsg) {
78                 UebMsg returnMsg = new UebMsg();
79
80                 if (requestMsg == null) {
81                         logger.error(EELFLoggerDelegate.errorLogger, "handleMenuRequest received null message");
82                         return false;
83                 } else if (requestMsg.getSourceTopicName() == null) {
84                         logger.error(EELFLoggerDelegate.errorLogger,
85                                         "A source topic name is required and not found in this msg:" + requestMsg.toString());
86                         return false;
87                 } else if (requestMsg.getUserId() == null) {
88                         logger.debug(EELFLoggerDelegate.debugLogger,
89                                         "Error getting functional menu.  A userId is required and not found in this msg: "
90                                                         + requestMsg.toString());
91                         returnMsg.putMsgId(requestMsg.getMsgId()); // echo tells requester this is a response
92                         returnMsg.putPayload("Error: A userId is required.  Call msg.putUserId() with an userId");
93                 } else {
94                         logger.debug(EELFLoggerDelegate.debugLogger,
95                                         "Getting functional menu for user = " + requestMsg.getUserId());
96                         EPUser user = searchSvc.searchUserByUserId(requestMsg.getUserId());
97
98                         List<FunctionalMenuItem> menuItems = null;
99                         if (user == null) {
100                                 logger.debug(EELFLoggerDelegate.debugLogger,
101                                                 "Error getting functional menu.  userId not found in directory or is guest: "
102                                                                 + requestMsg.toString());
103                         } else if (adminRolesService.isSuperAdmin(user)) {
104                                 logger.debug(EELFLoggerDelegate.debugLogger,
105                                                 "FunctionalMenuHandler: SuperUser, about to call getFunctionalMenuItems()");
106                                 menuItems = functionalMenuService.getFunctionalMenuItems();
107                         } else {
108                                 logger.debug(EELFLoggerDelegate.debugLogger,
109                                                 "getMenuItemsForAuthUser: about to call getFunctionalMenuItemsForUser()");
110                                 menuItems = functionalMenuService.getFunctionalMenuItemsForUser(requestMsg.getUserId());
111                         }
112
113                         if (menuItems != null) {
114                                 String functionalMenuJsonString = new Gson().toJson(menuItems);
115                                 logger.debug(EELFLoggerDelegate.debugLogger, "returning functional menu : " + functionalMenuJsonString);
116                                 returnMsg.putMsgId(requestMsg.getMsgId()); // echo tells requester this is a response
117                                 returnMsg.putPayload(functionalMenuJsonString);
118                         } else {
119                                 returnMsg.putMsgId(requestMsg.getMsgId()); // echo tells requester this is a response
120                                 returnMsg.putPayload("Error: Not found for userId = " + requestMsg.getUserId());
121                         }
122                 }
123
124                 try {
125                         UebManager.getInstance().publishReplyEP(returnMsg, requestMsg.getSourceTopicName());
126                 } catch (UebException e) {
127                         logger.error(EELFLoggerDelegate.errorLogger,
128                                         "getFunctionalMenu failed to publish reply", e);
129                 } catch (Exception e) {
130                         logger.error(EELFLoggerDelegate.errorLogger,
131                                         "getFunctionalMenu failed", e);
132                 }
133
134                 return true;
135         }
136 }