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