[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / WebAnalyticsExtAppController.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 package org.openecomp.portalapp.portal.controller;\r
21 \r
22 import java.io.InputStream;\r
23 import java.nio.charset.StandardCharsets;\r
24 \r
25 import javax.servlet.http.HttpServletRequest;\r
26 \r
27 import org.apache.commons.io.IOUtils;\r
28 import org.openecomp.portalapp.controller.EPRestrictedRESTfulBaseController;\r
29 import org.openecomp.portalapp.portal.domain.EPApp;\r
30 import org.openecomp.portalapp.portal.domain.EcompAuditLog;\r
31 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;\r
32 import org.openecomp.portalapp.portal.logging.aop.EPEELFLoggerAdvice;\r
33 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;\r
34 import org.openecomp.portalapp.portal.service.AppsCacheService;\r
35 import org.openecomp.portalapp.portal.transport.Analytics;\r
36 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;\r
37 import org.openecomp.portalapp.portal.utils.PortalConstants;\r
38 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
39 import org.openecomp.portalsdk.core.onboarding.crossapi.PortalAPIResponse;\r
40 import org.openecomp.portalsdk.core.service.AuditService;\r
41 import org.openecomp.portalsdk.core.util.SystemProperties;\r
42 import org.slf4j.MDC;\r
43 import org.springframework.beans.factory.annotation.Autowired;\r
44 import org.springframework.context.annotation.Configuration;\r
45 import org.springframework.context.annotation.EnableAspectJAutoProxy;\r
46 import org.springframework.web.bind.annotation.RequestBody;\r
47 import org.springframework.web.bind.annotation.RequestMapping;\r
48 import org.springframework.web.bind.annotation.RequestMethod;\r
49 import org.springframework.web.bind.annotation.ResponseBody;\r
50 import org.springframework.web.bind.annotation.RestController;\r
51 \r
52 import io.swagger.annotations.ApiOperation;\r
53 \r
54 @RestController\r
55 @RequestMapping(PortalConstants.REST_AUX_API)\r
56 @Configuration\r
57 @EnableAspectJAutoProxy\r
58 @EPAuditLog\r
59 public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseController{\r
60         \r
61         private static final String APP_KEY = "uebkey";\r
62         \r
63         @Autowired\r
64         AuditService auditService;\r
65         \r
66         @Autowired\r
67         AppsCacheService appCacheService;\r
68         \r
69         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WebAnalyticsExtAppController.class);\r
70 \r
71         protected boolean isAuxRESTfulCall() {\r
72                 return true;\r
73         }\r
74 \r
75         /*\r
76          * Answers requests from partner applications for a file that is expected to\r
77          * contain javascript to support web analytics.\r
78          * \r
79          * @param request\r
80          * @return\r
81          * @throws Exception\r
82          */\r
83         @ApiOperation(value = "Gets javascript with functions that support gathering and reporting web analytics.", response = String.class)\r
84         @RequestMapping(value = { "/analytics" }, method = RequestMethod.GET, produces = "application/javascript")\r
85         public String getAnalyticsScript(HttpServletRequest request) throws Exception {\r
86                 String responseText = "";\r
87                 final String fileName = "analytics.txt";\r
88                 InputStream analyticsFileStream = null;\r
89                 try {\r
90                         analyticsFileStream = this.getClass().getClassLoader().getResourceAsStream(fileName);\r
91                         responseText = IOUtils.toString(analyticsFileStream, StandardCharsets.UTF_8.name());\r
92                 } catch (Exception e) {\r
93                         logger.error(EELFLoggerDelegate.errorLogger, "Error reading contents of the file " + fileName, e);\r
94                 } finally {\r
95                         if (analyticsFileStream != null)\r
96                                 analyticsFileStream.close();\r
97                 }\r
98 \r
99                 String feURLContext = SystemProperties.getProperty("frontend_url");\r
100                 String feURL = feURLContext.substring(0, feURLContext.lastIndexOf('/'));\r
101 \r
102                 responseText = responseText.replace("PORTAL_ENV_URL", feURL);\r
103                 return responseText;\r
104         }\r
105 \r
106         /**\r
107          * Accepts data from partner applications with web analytics data.\r
108          * \r
109          * @param request\r
110          * @param analyticsMap\r
111          * @param response\r
112          * @return\r
113          * @throws Exception\r
114          */\r
115         @RequestMapping(value = { "/storeAnalytics" }, method = RequestMethod.POST, produces = "application/json")\r
116         @ResponseBody\r
117         @ApiOperation(value = "Accepts data from partner applications with web analytics data.", response = PortalAPIResponse.class)\r
118         public PortalAPIResponse storeAnalyticsScript(HttpServletRequest request, @RequestBody Analytics analyticsMap) throws Exception {       \r
119                 try{\r
120                 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP,EPEELFLoggerAdvice.getCurrentDateTimeUTC());          \r
121                 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP,EPEELFLoggerAdvice.getCurrentDateTimeUTC());\r
122                 String appName = "";\r
123                 try {\r
124                         \r
125                         String appKeyValue = request.getHeader(APP_KEY);\r
126                         if(appKeyValue == null || appKeyValue.equals("")) {\r
127                                 logger.error(EELFLoggerDelegate.errorLogger, " App Key unavailable; Proceeding with null app name");\r
128                         } else {        \r
129                                 EPApp appRecord = appCacheService.getAppForAnalytics(appKeyValue); \r
130                                 if(appRecord == null){\r
131                                         logger.error(EELFLoggerDelegate.errorLogger, " App could not be found for the key "+ appKeyValue);\r
132                                 }\r
133                                 else appName = appRecord.getName();\r
134                                 \r
135                         }\r
136                 \r
137                 } catch(Exception e) {\r
138                         logger.error(EELFLoggerDelegate.errorLogger, " Error retrieving Application to capture app name for analytics; Proceeding with empty app name");\r
139                 }\r
140                 \r
141                 \r
142                 \r
143                 logger.info(EELFLoggerDelegate.auditLogger, EPLogUtil.formatStoreAnalyticsAuditLogMessage(analyticsMap.getUserId(),appName, "WebAnalyticsExtAppController.postWebAnalyticsData", \r
144                                 EcompAuditLog.CD_ACTIVITY_STORE_ANALYTICS, analyticsMap.getAction(),analyticsMap.getPage(),analyticsMap.getFunction(),analyticsMap.getType()));                 \r
145                 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);\r
146                 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);\r
147                 \r
148                 PortalAPIResponse response = new PortalAPIResponse(true, "success");\r
149                 return response;\r
150                 } catch (Exception e) {\r
151                         logger.error(EELFLoggerDelegate.errorLogger, "storeAnalytics failed", e);\r
152                         PortalAPIResponse response = new PortalAPIResponse(true, "error");\r
153                         return response;\r
154                 }\r
155         }\r
156 \r
157 }\r