8e17eaae7a74de616a27dd0b545d0d943e9b1128
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
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.controller.core;
21
22 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
23
24 import java.io.IOException;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.json.JSONArray;
30 import org.json.JSONObject;
31 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
32 import org.openecomp.portalsdk.core.domain.App;
33 import org.openecomp.portalsdk.core.domain.User;
34 import org.openecomp.portalsdk.core.logging.aspect.AuditLog;
35 import org.openecomp.portalsdk.core.logging.format.AlarmSeverityEnum;
36 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.openecomp.portalsdk.core.onboarding.rest.FunctionalMenuClient;
38 import org.openecomp.portalsdk.core.onboarding.ueb.UebException;
39 import org.openecomp.portalsdk.core.onboarding.ueb.UebManager;
40 import org.openecomp.portalsdk.core.onboarding.ueb.UebMsg;
41 import org.openecomp.portalsdk.core.onboarding.ueb.UebMsgTypes;
42 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
43 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
44 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
45 import org.openecomp.portalsdk.core.service.AppService;
46 import org.openecomp.portalsdk.core.util.SystemProperties;
47 import org.openecomp.portalsdk.core.web.support.UserUtils;
48 import org.slf4j.MDC;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.context.annotation.EnableAspectJAutoProxy;
51 import org.springframework.stereotype.Controller;
52 import org.springframework.web.bind.annotation.RequestMapping;
53 import org.springframework.web.bind.annotation.RequestMethod;
54
55 @Controller
56 @RequestMapping("/")
57 @org.springframework.context.annotation.Configuration
58 @EnableAspectJAutoProxy
59 public class FuncMenuController extends RestrictedBaseController{
60         
61         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FuncMenuController.class);
62
63         @Autowired
64         AppService appService;
65
66         @AuditLog
67         @RequestMapping(value = {"/get_functional_menu" }, method = RequestMethod.GET)
68         public void functionalMenu(HttpServletRequest request, HttpServletResponse response) {
69                 
70         User user = UserUtils.getUserSession(request);
71         //JSONArray validMenu = new JSONArray("[{\"menuId\":140,\"column\":1,\"text\":\"RT SDK Menu\",\"parentMenuId\":139,\"url\":\"http://www.cnn.com\"},{\"menuId\":139,\"column\":1,\"text\":\"RT Menu\",\"parentMenuId\":11,\"url\":\"\"},{\"menuId\":11,\"column\":1,\"text\":\"Product Design\",\"parentMenuId\":1,\"url\":\"\"},{\"menuId\":1,\"column\":1,\"text\":\"Design\",\"url\":\"\"}]");
72  
73                 try {
74                         if ( user != null ) {
75                                 String useRestForFunctionalMenu = PortalApiProperties.getProperty(PortalApiConstants.USE_REST_FOR_FUNCTIONAL_MENU);
76                                 String funcMenuJsonString = "";
77                                 if (useRestForFunctionalMenu==null || useRestForFunctionalMenu=="" || useRestForFunctionalMenu.equalsIgnoreCase("false")) {
78                                         logger.info(EELFLoggerDelegate.errorLogger, "Making use of UEB communication and Requesting functional menu for user " + user.getOrgUserId());
79                                         funcMenuJsonString = getFunctionalMenu(user.getOrgUserId());
80                                 } else {
81                                         funcMenuJsonString = getFunctionalMenuViaREST(user.getOrgUserId());
82                                 }
83                                 response.setContentType("application/json");
84                                 response.getWriter().write(funcMenuJsonString);
85                         } else {
86                                 logger.info(EELFLoggerDelegate.errorLogger, "Http request did not contain user info, cannot retrieve functional menu");
87                             response.setContentType("application/json");
88                             JSONArray jsonResponse = new JSONArray();
89                             JSONObject error = new JSONObject();
90                             error.put("error","Http request did not contain user info, cannot retrieve functional menu");
91                                 jsonResponse.put(error);
92                                 response.getWriter().write(jsonResponse.toString());
93                         }       
94                 } catch (Exception e) { 
95                         response.setCharacterEncoding("UTF-8");
96                     response.setContentType("application/json");
97                     JSONArray jsonResponse = new JSONArray();
98                     JSONObject error = new JSONObject();
99                         try {
100                                 if ( null == e.getMessage() ) {
101                                         error.put("error","No menu data");
102                                 } else {
103                                         error.put("error",e.getMessage());
104                                 }
105                                 jsonResponse.put(error);
106                                 response.getWriter().write(jsonResponse.toString());
107                                 logger.error(EELFLoggerDelegate.errorLogger, "Error getting functional_menu: " + e.getMessage(),AlarmSeverityEnum.MAJOR);
108                         } catch (IOException e1) {
109                                 e1.printStackTrace();
110                         }
111                 }
112                 
113         }
114         
115         //--------------------------------------------------------------------------
116         // Makes a synchronous call to ECOMP Portal to get the JSON file that
117         // contains the contents of the functional menu.  The JSON file will be
118         // in the payload of the returned UEB message.
119         //--------------------------------------------------------------------------
120         private String getFunctionalMenu(String userId) throws UebException
121         {
122                 String returnString = null;
123                 UebMsg funcMenuUebMsg = null;
124                 UebMsg msg = new UebMsg();
125                 msg.putMsgType(UebMsgTypes.UEB_MSG_TYPE_GET_FUNC_MENU);
126                 msg.putUserId(userId);
127                 funcMenuUebMsg = UebManager.getInstance().requestReply(msg); 
128                 if (funcMenuUebMsg != null) {
129                         if (funcMenuUebMsg.getPayload().startsWith("Error:")) {
130                                 logger.error(EELFLoggerDelegate.errorLogger, "getFunctionalMenu received an error in UEB msg = " + funcMenuUebMsg.getPayload());
131                         } else {
132                                 returnString = funcMenuUebMsg.getPayload();
133                         }
134                 }
135                 
136                 logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenu response: " + returnString);
137                 
138                 return returnString; 
139         }
140         
141         private String getFunctionalMenuViaREST(String userId) {
142                 String appName                  = "";
143                 String requestId                = "";
144                 String appUserName              = "";
145                 String decryptedPwd     = "";
146                 
147                 logger.info(EELFLoggerDelegate.debugLogger, "Making use of REST API communication and Requesting functional menu for user " + userId);
148                 
149                 App app = appService.getDefaultApp();
150                 if (app!=null) {
151                         appName = app.getName();
152                         appUserName = app.getUsername();
153                         try{
154                                 decryptedPwd = CipherUtil.decrypt(app.getAppPassword(), SystemProperties.getProperty(SystemProperties.Decryption_Key));
155                         } catch(Exception e) {
156                                 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in WebServiceCallServiceImpl.get while decrypting the password. Details: " + e.toString());
157                         }
158                 } else {
159                         logger.warn(EELFLoggerDelegate.errorLogger, "Unable to locate the app information from the database.");
160                         appName = SystemProperties.SDK_NAME;
161                 }
162                 requestId = MDC.get(MDC_KEY_REQUEST_ID);
163                 
164                 String fnMenu = null;
165                 try {
166                         fnMenu = FunctionalMenuClient.getFunctionalMenu(userId, appName, requestId, appUserName, decryptedPwd);
167                 }catch(Exception ex) {
168                         fnMenu = "Failed to get functional menu: " + ex.toString();
169                 }
170                 
171                 logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenu response: {}", fnMenu);
172                 
173                 return fnMenu;
174         }
175 }