2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.controller.core;
22 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
24 import java.io.IOException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
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;
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;
57 @org.springframework.context.annotation.Configuration
58 @EnableAspectJAutoProxy
59 public class FuncMenuController extends RestrictedBaseController{
61 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FuncMenuController.class);
64 AppService appService;
67 @RequestMapping(value = {"/get_functional_menu" }, method = RequestMethod.GET)
68 public void functionalMenu(HttpServletRequest request, HttpServletResponse response) {
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\":\"\"}]");
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());
81 funcMenuJsonString = getFunctionalMenuViaREST(user.getOrgUserId());
83 response.setContentType("application/json");
84 response.getWriter().write(funcMenuJsonString);
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());
94 } catch (Exception e) {
95 response.setCharacterEncoding("UTF-8");
96 response.setContentType("application/json");
97 JSONArray jsonResponse = new JSONArray();
98 JSONObject error = new JSONObject();
100 if ( null == e.getMessage() ) {
101 error.put("error","No menu data");
103 error.put("error",e.getMessage());
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();
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
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());
132 returnString = funcMenuUebMsg.getPayload();
136 logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenu response: " + returnString);
141 private String getFunctionalMenuViaREST(String userId) {
143 String requestId = "";
144 String appUserName = "";
145 String decryptedPwd = "";
147 logger.info(EELFLoggerDelegate.debugLogger, "Making use of REST API communication and Requesting functional menu for user " + userId);
149 App app = appService.getDefaultApp();
151 appName = app.getName();
152 appUserName = app.getUsername();
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());
159 logger.warn(EELFLoggerDelegate.errorLogger, "Unable to locate the app information from the database.");
160 appName = SystemProperties.SDK_NAME;
162 requestId = MDC.get(MDC_KEY_REQUEST_ID);
164 String fnMenu = null;
166 fnMenu = FunctionalMenuClient.getFunctionalMenu(userId, appName, requestId, appUserName, decryptedPwd);
167 }catch(Exception ex) {
168 fnMenu = "Failed to get functional menu: " + ex.toString();
171 logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenu response: {}", fnMenu);