da26fe69ce9758f59efe2b92c4789f15b33ea683
[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 items
79          * 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 = null;
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.error(EELFLoggerDelegate.errorLogger,
96                                                 "Http request did not contain user info, cannot retrieve favorites.");
97                                 response.setContentType("application/json");
98                                 JSONArray jsonResponse = new JSONArray();
99                                 JSONObject error = new JSONObject();
100                                 error.put("error", "Http request did not contain user info, cannot retrieve favorites.");
101                                 jsonResponse.put(error);
102                                 response.getWriter().write(jsonResponse.toString());
103                         } else {
104                                 logger.debug(EELFLoggerDelegate.debugLogger,
105                                                 "Retrieving Favorites for the user '" + MDC.get(SystemProperties.MDC_LOGIN_ID) + "'.");
106
107                                 App app = appService.getDefaultApp();
108                                 if (app != null) {
109                                         appName = app.getName();
110                                         appUserName = app.getUsername();
111                                         try {
112                                                 decryptedPwd = CipherUtil.decrypt(app.getAppPassword(),
113                                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
114                                         } catch (Exception e) {
115                                                 logger.error(EELFLoggerDelegate.errorLogger,
116                                                                 "FavoritesController.getFavorites failed while decrypting password", e);
117                                         }
118                                 } else {
119                                         logger.error(EELFLoggerDelegate.errorLogger,
120                                                         "Unable to locate the app information from the database.");
121                                         appName = SystemProperties.SDK_NAME;
122                                 }
123                                 requestId = MDC.get(MDC_KEY_REQUEST_ID);
124
125                                 String jsonResponse = FavoritesClient.getFavorites(MDC.get(SystemProperties.MDC_LOGIN_ID), appName,
126                                                 requestId, appUserName, decryptedPwd);
127
128                                 logger.debug(EELFLoggerDelegate.debugLogger, "FavoritesMenu response: {}", jsonResponse);
129                                 response.setContentType("application/json");
130                                 response.getWriter().write(jsonResponse);
131                         }
132                 } catch (Exception e) {
133                         logger.error(EELFLoggerDelegate.errorLogger,
134                                         "FavoritesController.getFavorites failed", e);
135                 }
136         }
137 }