From 25a404935e041dbdb7928e414a4918bb9a4487b8 Mon Sep 17 00:00:00 2001 From: Alexander Bainczyk Date: Wed, 24 Jul 2019 13:03:30 +0200 Subject: [PATCH] also open make requests to absolute URLs --- .../alex/data/entities/actions/rest/CallAction.java | 3 ++- .../alex/data/entities/actions/web/GotoAction.java | 5 ++++- .../alex/learning/services/BaseUrlManager.java | 9 +++++++-- .../services/connectors/WebServiceConnector.java | 11 ++++++++++- .../services/connectors/WebSiteConnector.java | 11 ++++++++++- .../request-action-form.component.html | 2 +- .../open-url-action-form.component.html | 2 +- 7 files changed, 35 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/de/learnlib/alex/data/entities/actions/rest/CallAction.java b/backend/src/main/java/de/learnlib/alex/data/entities/actions/rest/CallAction.java index 9c0b8c76c..1f0537df4 100644 --- a/backend/src/main/java/de/learnlib/alex/data/entities/actions/rest/CallAction.java +++ b/backend/src/main/java/de/learnlib/alex/data/entities/actions/rest/CallAction.java @@ -84,7 +84,8 @@ public enum Method { private Method method; /** - * The url to call. This is just the suffix which will be appended to the base url. + * The url to call. + * The URL can either be a path relative to the project's base URL or a absolute URL that starts with https?:// */ @NotBlank private String url; diff --git a/backend/src/main/java/de/learnlib/alex/data/entities/actions/web/GotoAction.java b/backend/src/main/java/de/learnlib/alex/data/entities/actions/web/GotoAction.java index c4b0eeae5..aaf16213b 100644 --- a/backend/src/main/java/de/learnlib/alex/data/entities/actions/web/GotoAction.java +++ b/backend/src/main/java/de/learnlib/alex/data/entities/actions/web/GotoAction.java @@ -45,7 +45,10 @@ public class GotoAction extends WebSymbolAction { private static final Logger LOGGER = LogManager.getLogger(); - /** The URL of the site. */ + /** + * The URL of the site. + * The URL can either be a path relative to the project's base URL or a absolute URL that starts with https?:// + */ @NotBlank private String url; diff --git a/backend/src/main/java/de/learnlib/alex/learning/services/BaseUrlManager.java b/backend/src/main/java/de/learnlib/alex/learning/services/BaseUrlManager.java index 4ea63c0a0..ebff501e2 100644 --- a/backend/src/main/java/de/learnlib/alex/learning/services/BaseUrlManager.java +++ b/backend/src/main/java/de/learnlib/alex/learning/services/BaseUrlManager.java @@ -78,12 +78,17 @@ public String getAbsoluteUrl(String path) { */ public String getAbsoluteUrl(String path, Credentials credentials) { String url = combineUrls(baseUrl, path); + return BaseUrlManager.getUrlWithCredentials(url, credentials); + } + + public static String getUrlWithCredentials(String url, Credentials credentials) { if (credentials != null && credentials.areValid()) { - url = url.replaceFirst("^(http[s]?://)", "$1" + return url.replaceFirst("^(http[s]?://)", "$1" + credentials.getName() + ":" + credentials.getPassword() + "@"); + } else { + return url; } - return url; } /** diff --git a/backend/src/main/java/de/learnlib/alex/learning/services/connectors/WebServiceConnector.java b/backend/src/main/java/de/learnlib/alex/learning/services/connectors/WebServiceConnector.java index b6dbfadf2..decb88960 100644 --- a/backend/src/main/java/de/learnlib/alex/learning/services/connectors/WebServiceConnector.java +++ b/backend/src/main/java/de/learnlib/alex/learning/services/connectors/WebServiceConnector.java @@ -27,6 +27,8 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Map; import java.util.Set; @@ -281,7 +283,14 @@ private void followRedirects(Response response) { private Invocation.Builder getRequestObject(String path, Map requestHeaders, Set requestCookies, int timeout) { final String[] splitPath = path.split("\\?"); - WebTarget tmpTarget = target.path(splitPath[0]); + + WebTarget tmpTarget; + try { + new URL(path); + tmpTarget = client.target(splitPath[0]); + } catch (MalformedURLException e) { + tmpTarget = target.path(splitPath[0]); + } if (splitPath.length > 1) { for (final String queryParam : splitPath[1].split("&")) { diff --git a/backend/src/main/java/de/learnlib/alex/learning/services/connectors/WebSiteConnector.java b/backend/src/main/java/de/learnlib/alex/learning/services/connectors/WebSiteConnector.java index bd7e5cdd5..e9af12c2e 100644 --- a/backend/src/main/java/de/learnlib/alex/learning/services/connectors/WebSiteConnector.java +++ b/backend/src/main/java/de/learnlib/alex/learning/services/connectors/WebSiteConnector.java @@ -30,6 +30,8 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import java.net.MalformedURLException; +import java.net.URL; import java.util.List; import java.util.concurrent.TimeUnit; @@ -126,7 +128,14 @@ public void get(String path, Credentials credentials) throws Exception { this.driver = driverConfig.createDriver(); } - final String url = baseUrl.getAbsoluteUrl(path, credentials); + String url; + try { + new URL(path); + url = BaseUrlManager.getUrlWithCredentials(path, credentials); + } catch (MalformedURLException e) { + url = baseUrl.getAbsoluteUrl(path, credentials); + } + int numRetries = 0; while (numRetries < MAX_RETRIES) { try { diff --git a/frontend/src/main/javascript/src/js/components/forms/actions/rest/request-action-form/request-action-form.component.html b/frontend/src/main/javascript/src/js/components/forms/actions/rest/request-action-form/request-action-form.component.html index fd6232ece..af7cb08d4 100644 --- a/frontend/src/main/javascript/src/js/components/forms/actions/rest/request-action-form/request-action-form.component.html +++ b/frontend/src/main/javascript/src/js/components/forms/actions/rest/request-action-form/request-action-form.component.html @@ -1,6 +1,6 @@
Make request
-

Make a HTTP request to a URL (relative to your projects default URL).

+

Make a HTTP request to a URL that is either relative to your project's base URL or an absolute one.


diff --git a/frontend/src/main/javascript/src/js/components/forms/actions/web/open-url-action-form/open-url-action-form.component.html b/frontend/src/main/javascript/src/js/components/forms/actions/web/open-url-action-form/open-url-action-form.component.html index 5a935f863..2cf7d53cd 100644 --- a/frontend/src/main/javascript/src/js/components/forms/actions/web/open-url-action-form/open-url-action-form.component.html +++ b/frontend/src/main/javascript/src/js/components/forms/actions/web/open-url-action-form/open-url-action-form.component.html @@ -1,7 +1,7 @@
Open URL

- Open a URL that is relative to your projects' base URL + Open a URL that is either relative to your projects' base URL or an absolute one.