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