023f8d7b1b6be30b695ed1530f1e31eeef45df82
[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 javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import javax.servlet.http.HttpSession;
27
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
31 import org.openecomp.portalsdk.core.domain.App;
32 import org.openecomp.portalsdk.core.domain.User;
33 import org.openecomp.portalsdk.core.logging.aspect.AuditLog;
34 import org.openecomp.portalsdk.core.logging.format.AlarmSeverityEnum;
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
36 import org.openecomp.portalsdk.core.onboarding.rest.FavoritesClient;
37 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
38 import org.openecomp.portalsdk.core.service.AppService;
39 import org.openecomp.portalsdk.core.util.SystemProperties;
40 import org.slf4j.MDC;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.context.annotation.EnableAspectJAutoProxy;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestMethod;
46
47 @Controller
48 @RequestMapping("/")
49 @org.springframework.context.annotation.Configuration
50 @EnableAspectJAutoProxy
51 @AuditLog
52 public class FavoritesController extends RestrictedBaseController {
53
54         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FavoritesController.class);
55
56         @Autowired
57         private AppService appService;
58
59         /**
60          * Makes the REST API call to Portal Back-end and retrieves Favorite menu
61          * items for the currently logged in user.
62          * 
63          * @param request
64          * @param response
65          */
66         @RequestMapping(value = { "/get_favorites" }, method = RequestMethod.GET)
67         public void getFavorites(HttpServletRequest request, HttpServletResponse response) {
68                 String appName                  = "";
69                 String requestId                = "";
70                 String appUserName              = "";
71                 String decryptedPwd     = "";
72                 
73                 try {
74                         HttpSession session = request.getSession();
75                         User user = (User) session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME));
76                         if (user == null || user.getId() == null) {
77                                 logger.info(EELFLoggerDelegate.errorLogger,
78                                                 ("Http request did not contain user info, cannot retrieve favorites."));
79
80                                 response.setContentType("application/json");
81                                 JSONArray jsonResponse = new JSONArray();
82                                 JSONObject error = new JSONObject();
83                                 error.put("error", "Http request did not contain user info, cannot retrieve favorites.");
84                                 jsonResponse.put(error);
85                                 response.getWriter().write(jsonResponse.toString());
86                         } else {
87                                 logger.info(EELFLoggerDelegate.errorLogger,
88                                                 "Retrieving Favorites for the user '" + MDC.get(SystemProperties.MDC_LOGIN_ID) + "'.");
89                                 
90                                 App app = appService.getDefaultApp();
91                                 if (app!=null) {
92                                         appName = app.getName();
93                                         appUserName = app.getUsername();
94                                         try{
95                                                 decryptedPwd = CipherUtil.decrypt(app.getAppPassword(), SystemProperties.getProperty(SystemProperties.Decryption_Key));
96                                         } catch(Exception e) {
97                                                 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in WebServiceCallServiceImpl.get while decrypting the password. Details: " + e.getMessage());
98                                         }
99                                 } else {
100                                         logger.warn(EELFLoggerDelegate.errorLogger, "Unable to locate the app information from the database.");
101                                         appName = SystemProperties.SDK_NAME;
102                                 }
103                                 requestId = MDC.get(MDC_KEY_REQUEST_ID);
104                                 
105                                 String jsonResponse = FavoritesClient.getFavorites(MDC.get(SystemProperties.MDC_LOGIN_ID), appName, requestId, appUserName, decryptedPwd);
106                                 
107                                 logger.debug(EELFLoggerDelegate.debugLogger, "FavoritesMenu response: " + jsonResponse);
108                                 
109                                 response.setContentType("application/json");
110                                 response.getWriter().write(jsonResponse);
111                         }
112                 } catch (Exception e) {
113                         logger.error(EELFLoggerDelegate.errorLogger,
114                                         "Exception occurred in FavoritesController.getFavorites while performing get_favorites. Details: "
115                                                         + e.getMessage(), AlarmSeverityEnum.MINOR);
116                 }
117         }
118 }