From 7be64abc0ab61da69161f0a4d71a34763a8541f2 Mon Sep 17 00:00:00 2001 From: Brett Stewart Date: Tue, 13 Apr 2021 18:23:37 +0900 Subject: [PATCH] Check for widget versioning snippets (#170) --- .circleci/config.yml | 2 +- .gitignore | 1 + pom.xml | 2 +- .../github/wovnio/wovnjava/HtmlConverter.java | 27 ++++++++++++++----- .../com/github/wovnio/wovnjava/Settings.java | 4 ++- .../wovnio/wovnjava/HtmlConverterTest.java | 2 +- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 559d5235..c9bcfed0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,7 +23,7 @@ jobs: sudo apt-get update sudo apt install -y maven cd ~/ - curl -O https://bootstrap.pypa.io/get-pip.py + curl -O https://bootstrap.pypa.io/pip/2.7/get-pip.py python get-pip.py --user export PATH=~/.local/bin/:$PATH pip install awscli --upgrade --user diff --git a/.gitignore b/.gitignore index 4ec88230..058de3b4 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ .project .settings/ .vscode/ +bin/ docker/java7/hello/target docker/java8/hello/target diff --git a/pom.xml b/pom.xml index 6784f418..15715027 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.github.wovnio wovnjava wovnjava - 1.6.0 + 1.6.1 https://github.com/WOVNio/wovnjava diff --git a/src/main/java/com/github/wovnio/wovnjava/HtmlConverter.java b/src/main/java/com/github/wovnio/wovnjava/HtmlConverter.java index 6273edd0..b8dbe687 100644 --- a/src/main/java/com/github/wovnio/wovnjava/HtmlConverter.java +++ b/src/main/java/com/github/wovnio/wovnjava/HtmlConverter.java @@ -18,6 +18,12 @@ class HtmlConverter { private final HashMap hreflangMap; private final HtmlReplaceMarker htmlReplaceMarker; + private static final String[] WOVN_WIDGET_URLS = new String[] { + "j.wovn.io", + "j.dev-wovn.io:3000", + Settings.DefaultVersionedWidgetUrlProduction + }; + HtmlConverter(Settings settings, Headers headers, String original) { this.settings = settings; this.htmlReplaceMarker = new HtmlReplaceMarker(); @@ -60,15 +66,25 @@ private void removeHrefLangIfConflicts() { } } - private boolean isSnippet(String src) { - return src != null && (src.startsWith("//j.wovn.io/") || src.startsWith("//j.dev-wovn.io:3000/")); + private boolean isSnippet(Element element) { + String src = element.attr("src"); + String dataAttr = element.attr("data-wovnio"); + + if (src != null) { + for (String wovnUrl : WOVN_WIDGET_URLS) { + if (src.contains(wovnUrl)) { + return true; + } + } + } + + return dataAttr != null && dataAttr.length() > 0; } private void removeSnippet() { Elements elements = doc.getElementsByTag("script"); for (Element element : elements) { - String src = element.attr("src"); - if (isSnippet(src)) { + if (isSnippet(element)) { element.remove(); } } @@ -77,8 +93,7 @@ private void removeSnippet() { private void removeSnippetAndScripts() { Elements elements = doc.getElementsByTag("script"); for (Element element : elements) { - String src = element.attr("src"); - if (isSnippet(src)) { + if (isSnippet(element)) { element.remove(); } else { replaceNodeToMarkerComment(element); diff --git a/src/main/java/com/github/wovnio/wovnjava/Settings.java b/src/main/java/com/github/wovnio/wovnjava/Settings.java index 3b5023b4..badebfa0 100644 --- a/src/main/java/com/github/wovnio/wovnjava/Settings.java +++ b/src/main/java/com/github/wovnio/wovnjava/Settings.java @@ -13,7 +13,9 @@ class Settings { // Default configuration values public static final int DefaultTimeout = 1000; - public static final String DefaultApiUrlProduction = "https://wovn.global.ssl.fastly.net/v0/"; + public static final String DefaultApiUrlBase = "https://wovn.global.ssl.fastly.net"; + public static final String DefaultApiUrlProduction = DefaultApiUrlBase + "/v0/"; + public static final String DefaultVersionedWidgetUrlProduction = DefaultApiUrlBase + "/widget"; public static final String DefaultApiUrlDevelopment = "http://localhost:3001/v0/"; public static final String DefaultSnippetUrlProduction = "//j.wovn.io/1"; public static final String DefaultSnippetUrlDevelopment = "//j.dev-wovn.io:3000/1"; diff --git a/src/test/java/com/github/wovnio/wovnjava/HtmlConverterTest.java b/src/test/java/com/github/wovnio/wovnjava/HtmlConverterTest.java index 662604e6..1c401bd4 100644 --- a/src/test/java/com/github/wovnio/wovnjava/HtmlConverterTest.java +++ b/src/test/java/com/github/wovnio/wovnjava/HtmlConverterTest.java @@ -26,7 +26,7 @@ public void testDisablePrettyPrint() throws ConfigurationError { } public void testRemoveWovnSnippet() throws ConfigurationError { - String original = ""; + String original = ""; String removedHtml = ""; Settings settings = TestUtil.makeSettings(new HashMap() {{ put("supportedLangs", "en,fr,ja"); }}); HtmlConverter converter = this.createHtmlConverter(settings, location, original);