Skip to content

Commit

Permalink
#30454 normalizing the host values to hostname (#30539)
Browse files Browse the repository at this point in the history
Normalizing the host values to hostname all the times
  • Loading branch information
jdotcms authored Nov 12, 2024
1 parent b919bba commit 786c18d
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
final Host site = (Host) collectorContextMap.get("currentHost");
final String language = (String)collectorContextMap.get("lang");
collectorPayloadBean.put("url", uri);
collectorPayloadBean.put("host", host);
collectorPayloadBean.put("host", Objects.nonNull(site)?site.getHostname():host);
collectorPayloadBean.put("language", language);
collectorPayloadBean.put("site", null != site?site.getIdentifier():"unknown");
final String eventType = (String)collectorContextMap.get("eventType") == null?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa

collectorPayloadBean.put("object", fileObject);
collectorPayloadBean.put("url", uri);
collectorPayloadBean.put("host", host);
collectorPayloadBean.put("host", Objects.nonNull(site)?site.getHostname():host);
collectorPayloadBean.put("language", language);
collectorPayloadBean.put("site", null != site?site.getIdentifier():"unknown");
collectorPayloadBean.put("event_type", EventType.FILE_REQUEST.getType());

return collectorPayloadBean;
}

private Optional<Contentlet> getFileAsset(String uri, Host host, Long languageId) {
protected Optional<Contentlet> getFileAsset(String uri, Host host, Long languageId) {
try {
if (uri.endsWith(".dotsass")) {
final String actualUri = uri.substring(0, uri.lastIndexOf('.')) + ".scss";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
collectorPayloadBean.put("language", language);

if (Objects.nonNull(site)) {
collectorPayloadBean.put("host", site.getIdentifier());
collectorPayloadBean.put("host", site.getHostname());
}
return collectorPayloadBean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
final String language = (String)collectorContextMap.get("lang");
final PageMode pageMode = (PageMode)collectorContextMap.get("pageMode");
final HashMap<String, String> pageObject = new HashMap<>();

collectorPayloadBean.put("event_type", EventType.PAGE_REQUEST.getType());
if (Objects.nonNull(uri) && Objects.nonNull(site) && Objects.nonNull(languageId)) {

final boolean isUrlMap = isUrlMap(collectorContextMap);
Expand Down Expand Up @@ -82,7 +82,6 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
this.pageAPI.getPageByPath(uri, site, languageId, true)).get();
pageObject.put("id", page.getIdentifier());
pageObject.put("title", page.getTitle());
collectorPayloadBean.put("event_type", EventType.PAGE_REQUEST.getType());
}
pageObject.put("url", uri);
}
Expand All @@ -92,7 +91,7 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
collectorPayloadBean.put("language", language);

if (Objects.nonNull(site)) {
collectorPayloadBean.put("host", site.getIdentifier());
collectorPayloadBean.put("host", site.getHostname());
}

return collectorPayloadBean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.dotcms.analytics.track.collectors;

import com.dotcms.analytics.track.matchers.RequestMatcher;
import com.dotmarketing.beans.Host;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

/**
* Test for the CustomerEventCollector class
* @author jsanca
*
*/
public class CustomerEventCollectorTest {

/**
* Method to test: CustomerEventCollector#collect
* Given Scenario: Fill the preconditions and check the collect bean is properly filled
* ExpectedResult: the CollectorPayloadBean should be filled properly
*/
@Test
public void test_collect_easy_path() throws IOException {

final CustomerEventCollector customerEventCollector = new CustomerEventCollector();
final Host host = new Host();
host.setIdentifier("1");
host.setHostname("www.dotcms.com");
final CollectorContextMap collectorContextMap = new CollectorContextMap() {
@Override
public Object get(final String key) {
switch (key) {
case "uri":
return "/test-path";
case "host":
return "www2.dotcms.com";
case "currentHost":
return host;
case "lang":
return "en";
case "eventType":
return null;
}
return null;
}

@Override
public RequestMatcher getRequestMatcher() {
return null;
}
};

final CollectorPayloadBean collectorPayloadBean = new ConcurrentCollectorPayloadBean();
customerEventCollector.collect(collectorContextMap, collectorPayloadBean);

Assert.assertEquals("/test-path", collectorPayloadBean.get("url"));
Assert.assertEquals("www.dotcms.com", collectorPayloadBean.get("host"));
Assert.assertEquals("en", collectorPayloadBean.get("language"));
Assert.assertEquals("1", collectorPayloadBean.get("site"));
Assert.assertEquals(EventType.CUSTOM_USER_EVENT.getType(), collectorPayloadBean.get("event_type"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.dotcms.analytics.track.collectors;

import com.dotcms.analytics.track.matchers.RequestMatcher;
import com.dotmarketing.beans.Host;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;

/**
* Test for the FilesCollector class
* @author jsanca
*
*/
public class FilesCollectorTest {

/**
* Method to test: FilesCollector#collect
* Given Scenario: Fill the preconditions and check the collect bean is properly filled
* ExpectedResult: the CollectorPayloadBean should be filled properly
*/
@Test
public void test_collect_easy_path() throws IOException {

final FilesCollector filesCollector = new FilesCollector() {
@Override
protected Optional<Contentlet> getFileAsset(String uri, Host host, Long languageId) {
final Contentlet contentlet = Mockito.mock(Contentlet.class);
Mockito.when(contentlet.getIdentifier()).thenReturn("1");
Mockito.when(contentlet.getTitle()).thenReturn("Test");
return Optional.ofNullable(contentlet);
}
};
final Host host = new Host();
host.setIdentifier("1");
host.setHostname("www.dotcms.com");
final CollectorContextMap collectorContextMap = new CollectorContextMap() {
@Override
public Object get(final String key) {
switch (key) {
case "uri":
return "/test-path";
case "host":
return "www2.dotcms.com";
case "currentHost":
return host;
case "lang":
return "en";
case "langId":
return 1L;
case "eventType":
return null;
}
return null;
}

@Override
public RequestMatcher getRequestMatcher() {
return null;
}
};

final CollectorPayloadBean collectorPayloadBean = new ConcurrentCollectorPayloadBean();
filesCollector.collect(collectorContextMap, collectorPayloadBean);

final Map<String, String> fileObject = (Map<String, String>) collectorPayloadBean.get("object");
Assert.assertNotNull(fileObject);
Assert.assertEquals("1", fileObject.get("id"));
Assert.assertEquals("Test", fileObject.get("title"));
Assert.assertEquals("/test-path", fileObject.get("url"));
Assert.assertEquals("/test-path", collectorPayloadBean.get("url"));
Assert.assertEquals("www.dotcms.com", collectorPayloadBean.get("host"));
Assert.assertEquals("en", collectorPayloadBean.get("language"));
Assert.assertEquals("1", collectorPayloadBean.get("site"));
Assert.assertEquals(EventType.FILE_REQUEST.getType(), collectorPayloadBean.get("event_type"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.dotcms.analytics.track.collectors;

import com.dotcms.analytics.track.matchers.RequestMatcher;
import com.dotmarketing.beans.Host;
import com.dotmarketing.cms.urlmap.URLMapAPIImpl;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.htmlpageasset.business.HTMLPageAssetAPI;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;

/**
* Test for the {@link PagesCollector} class
* @author jsanca
*
*/
public class PagesCollectorTest {

/**
* Method to test: PagesCollector#collect
* Given Scenario: Fill the preconditions and check the collect bean is properly filled
* ExpectedResult: the CollectorPayloadBean should be filled properly
*/
@Test
public void test_collect_easy_path() throws IOException {

final PagesCollector pagesCollector = new PagesCollector(Mockito.mock(HTMLPageAssetAPI.class), Mockito.mock(URLMapAPIImpl.class));
final Host host = new Host();
host.setIdentifier("1");
host.setHostname("www.dotcms.com");
final CollectorContextMap collectorContextMap = new CollectorContextMap() {
@Override
public Object get(final String key) {
switch (key) {
case "uri":
return "/test-path";
case "host":
return "www2.dotcms.com";
case "currentHost":
return host;
case "lang":
return "en";
case "langId":
return null;
case "eventType":
return null;
}
return null;
}

@Override
public RequestMatcher getRequestMatcher() {
return null;
}
};


final CollectorPayloadBean collectorPayloadBean = new ConcurrentCollectorPayloadBean();
pagesCollector.collect(collectorContextMap, collectorPayloadBean);

Assert.assertEquals("/test-path", collectorPayloadBean.get("url"));
Assert.assertEquals("www.dotcms.com", collectorPayloadBean.get("host"));
Assert.assertEquals("en", collectorPayloadBean.get("language"));
Assert.assertEquals(EventType.PAGE_REQUEST.getType(), collectorPayloadBean.get("event_type"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@

import com.dotcms.IntegrationTestBase;
import com.dotcms.LicenseTestUtil;
import com.dotcms.datagen.ContentletDataGen;
import com.dotcms.datagen.HTMLPageDataGen;
import com.dotcms.datagen.SiteDataGen;
import com.dotcms.util.FiltersUtil;
import com.dotcms.util.IntegrationTestInitService;
import com.dotcms.vanityurl.model.CachedVanityUrl;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.filters.Constants;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.htmlpageasset.model.HTMLPageAsset;
import com.dotmarketing.portlets.languagesmanager.model.Language;
import com.dotmarketing.portlets.templates.model.Template;
import com.dotmarketing.util.UUIDUtil;
import com.dotmarketing.util.UtilMethods;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -90,7 +85,7 @@ public void collectAsyncVanityData() throws DotDataException, IOException,

final Map<String, Object> expectedDataMap = Map.of(
"event_type", EventType.PAGE_REQUEST.getType(),
"host", testSite.getIdentifier(),
"host", testSite.getHostname(),
"comeFromVanityURL", true,
"language", defaultLanguage.getIsoCode(),
"url", TEST_PAGE_URL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@
import com.dotcms.JUnit4WeldRunner;
import com.dotcms.LicenseTestUtil;
import com.dotcms.analytics.track.matchers.FilesRequestMatcher;
import com.dotcms.datagen.ContentletDataGen;
import com.dotcms.datagen.FileAssetDataGen;
import com.dotcms.datagen.FolderDataGen;
import com.dotcms.datagen.SiteDataGen;
import com.dotcms.util.IntegrationTestInitService;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.fileassets.business.FileAsset;
import com.dotmarketing.portlets.folders.model.Folder;
import com.dotmarketing.util.PageMode;
import com.dotmarketing.util.UUIDUtil;
import com.dotmarketing.util.UtilMethods;
import javax.enterprise.context.ApplicationScoped;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.enterprise.context.ApplicationScoped;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -76,7 +70,7 @@ public void collectFileData() throws DotDataException, IOException, DotSecurityE
APILocator.getUserAPI().getAnonymousUser());

final Map<String, Object> expectedDataMap = Map.of(
"host", "localhost:8080",
"host", testSite.getHostname(),
"site", testSite.getIdentifier(),
"language", APILocator.getLanguageAPI().getDefaultLanguage().getIsoCode(),
"event_type", EventType.FILE_REQUEST.getType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,28 @@
import com.dotcms.analytics.track.matchers.PagesAndUrlMapsRequestMatcher;
import com.dotcms.contenttype.model.type.ContentType;
import com.dotcms.datagen.ContentletDataGen;
import com.dotcms.datagen.FolderDataGen;
import com.dotcms.datagen.HTMLPageDataGen;
import com.dotcms.datagen.SiteDataGen;
import com.dotcms.util.IntegrationTestInitService;
import com.dotcms.visitor.filter.characteristics.CharacterWebAPI;
import com.dotcms.visitor.filter.characteristics.GDPRCharacter;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.contentlet.model.IndexPolicy;
import com.dotmarketing.portlets.folders.model.Folder;
import com.dotmarketing.portlets.htmlpageasset.model.HTMLPageAsset;
import com.dotmarketing.portlets.languagesmanager.model.Language;
import com.dotmarketing.portlets.templates.model.Template;
import com.dotmarketing.util.PageMode;
import com.dotmarketing.util.UUIDUtil;
import com.dotmarketing.util.UtilMethods;
import javax.enterprise.context.ApplicationScoped;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.enterprise.context.ApplicationScoped;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

Expand Down Expand Up @@ -109,7 +101,7 @@ public void testPageDetailCollector() throws DotDataException, UnknownHostExcept

final Map<String, Object> expectedDataMap = Map.of(
"event_type", EventType.PAGE_REQUEST.getType(),
"host", testSite.getIdentifier(),
"host", testSite.getHostname(),
"language", language.getIsoCode(),
"url", TEST_URL_MAP_DETAIL_PAGE_URL,
"object", Map.of(
Expand Down
Loading

0 comments on commit 786c18d

Please sign in to comment.