lower code smells
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / WebAnalyticsExtAppController.java
index a3f4d30..a8eb1d6 100644 (file)
@@ -4,6 +4,8 @@
  * ===================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ===================================================================
+ * Modifications Copyright (c) 2020 IBM
+ * ===================================================================
  *
  * Unless otherwise specified, all software contained herein is licensed
  * under the Apache License, Version 2.0 (the "License");
@@ -77,6 +79,8 @@ import org.springframework.util.concurrent.ListenableFuture;
 import org.springframework.util.concurrent.SuccessCallback;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.RestController;
@@ -89,13 +93,14 @@ import org.springframework.web.client.AsyncRestTemplate;
 @EPAuditLog
 @NoArgsConstructor
 public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseController {
+       public static final String FEED_ML = "feed.ml";
        private WidgetMService widgetMService;
        private AppsCacheService appCacheService;
 
        private static final String MACHINE_LEARNING_SERVICE_CTX = "/ml_api";
        private static final String REGISTER_ACTION = MACHINE_LEARNING_SERVICE_CTX + "/" + "registerAction";
-       private static final String CONSUL_ML_SERVICE_ID = "machine-learning";
        private static final String APP_KEY = "uebkey";
+       private static final String ERROR_MSG = " Error retrieving Application to capture app name for analytics; Proceeding with empty app name";
        private final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WebAnalyticsExtAppController.class);
        private final AsyncRestTemplate restTemplate = new AsyncRestTemplate();
        private final SuccessCallback<ResponseEntity<String>> successCallback = arg -> logger.info(EELFLoggerDelegate.debugLogger, arg.getBody());
@@ -116,7 +121,7 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl
         * @return String
         */
        @ApiOperation(value = "Gets javascript with functions that support gathering and reporting web analytics.", response = String.class)
-       @RequestMapping(value = { "/analytics" }, method = RequestMethod.GET, produces = "application/javascript")
+       @GetMapping(value = { "/analytics" }, produces = "application/javascript")
        public String getAnalyticsScript(HttpServletRequest request) {
                String responseText = "";
                EPApp app = null;
@@ -124,8 +129,8 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl
                try {
                        app = getApp(request);
                } catch (Exception e) {
-                       logger.error(EELFLoggerDelegate.errorLogger,
-                                       " Error retrieving Application to capture app name for analytics; Proceeding with empty app name");
+            logger.error(EELFLoggerDelegate.errorLogger,
+                    ERROR_MSG, e);
                }
                if (app != null) {
                        String restEndPoint = app.getAppRestEndpoint();
@@ -161,7 +166,7 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl
         *            Analytics
         * @return PortalAPIResponse
         */
-       @RequestMapping(value = { "/storeAnalytics" }, method = RequestMethod.POST, produces = "application/json")
+       @PostMapping(value = { "/storeAnalytics" }, produces = "application/json")
        @ResponseBody
        @ApiOperation(value = "Accepts data from partner applications with web analytics data.", response = PortalAPIResponse.class)
        public PortalAPIResponse storeAnalyticsScript(HttpServletRequest request, @RequestBody Analytics analyticsMap) {
@@ -172,14 +177,18 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl
                                appName = getAppName(request, appName);
                        } catch (Exception e) {
                                logger.error(EELFLoggerDelegate.errorLogger,
-                                               " Error retrieving Application to capture app name for analytics; Proceeding with empty app name");
+                        ERROR_MSG, e);
                        }
 
                        try {
-                               storeAuxAnalytics(analyticsMap, appName);
+                               if(SystemProperties.containsProperty(FEED_ML) && 
+                                               SystemProperties.getProperty(FEED_ML).equals("true")) {
+                                       storeAuxAnalytics(analyticsMap, appName);
+                               }
+                               
                        } catch (Exception e) {
                                logger.error(EELFLoggerDelegate.errorLogger,
-                                               " Error retrieving Application to capture app name for analytics; Proceeding with empty app name");
+                        ERROR_MSG, e);
                        }
 
                        MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
@@ -224,15 +233,16 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl
                return appRecord;
        }
 
-       private void storeAuxAnalytics(Analytics analyticsMap, String appName) {
+       protected void storeAuxAnalytics(Analytics analyticsMap, String appName) {
                logger.info(EELFLoggerDelegate.debugLogger,
                                " Registering an action for recommendation: AppName/Function/UserId " + appName + "/"
                                                + analyticsMap.getFunction() + "/" + analyticsMap.getUserid());
 
+               
                Map<String, String> requestMapping = new HashMap<>();
                requestMapping.put("id", analyticsMap.getUserid());
                requestMapping.put("action", appName + "|" + analyticsMap.getFunction());
-
+               
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
 
@@ -241,12 +251,11 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl
 
                // send it!
                ListenableFuture<ResponseEntity<String>> out = restTemplate.exchange(
-                               EcompPortalUtils.widgetMsProtocol() + "://"
-                                               + widgetMService.getServiceLocation(CONSUL_ML_SERVICE_ID,
-                                                               SystemProperties.getProperty("microservices.m-learn.local.port"))
+                               EcompPortalUtils.widgetMLProtocol() + "://"
+                                               + widgetMService.getMLServiceLocation()
                                                + REGISTER_ACTION,
                                HttpMethod.POST, entity, String.class);
                out.addCallback(successCallback, failureCallback);
        }
        
-}
+}
\ No newline at end of file