2 * ================================================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
20 package org.openecomp.portalapp.portal.controller;
\r
22 import java.io.InputStream;
\r
23 import java.nio.charset.StandardCharsets;
\r
25 import javax.servlet.http.HttpServletRequest;
\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
52 import io.swagger.annotations.ApiOperation;
\r
55 @RequestMapping(PortalConstants.REST_AUX_API)
\r
57 @EnableAspectJAutoProxy
\r
59 public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseController{
\r
61 private static final String APP_KEY = "uebkey";
\r
64 AuditService auditService;
\r
67 AppsCacheService appCacheService;
\r
69 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WebAnalyticsExtAppController.class);
\r
71 protected boolean isAuxRESTfulCall() {
\r
76 * Answers requests from partner applications for a file that is expected to
\r
77 * contain javascript to support web analytics.
\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
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
95 if (analyticsFileStream != null)
\r
96 analyticsFileStream.close();
\r
99 String feURLContext = SystemProperties.getProperty("frontend_url");
\r
100 String feURL = feURLContext.substring(0, feURLContext.lastIndexOf('/'));
\r
102 responseText = responseText.replace("PORTAL_ENV_URL", feURL);
\r
103 return responseText;
\r
107 * Accepts data from partner applications with web analytics data.
\r
110 * @param analyticsMap
\r
113 * @throws Exception
\r
115 @RequestMapping(value = { "/storeAnalytics" }, method = RequestMethod.POST, produces = "application/json")
\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
120 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP,EPEELFLoggerAdvice.getCurrentDateTimeUTC());
\r
121 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP,EPEELFLoggerAdvice.getCurrentDateTimeUTC());
\r
122 String appName = "";
\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
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
133 else appName = appRecord.getName();
\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
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
148 PortalAPIResponse response = new PortalAPIResponse(true, "success");
\r
150 } catch (Exception e) {
\r
151 logger.error(EELFLoggerDelegate.errorLogger, "storeAnalytics failed", e);
\r
152 PortalAPIResponse response = new PortalAPIResponse(true, "error");
\r