You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by gh...@apache.org on 2020/09/23 18:42:54 UTC

[sling-org-apache-sling-api] branch master updated: SLING-9745 Handle absolute URIs without scheme correctly

This is an automated email from the ASF dual-hosted git repository.

ghenzler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 8e6f6c3  SLING-9745 Handle absolute URIs without scheme correctly
8e6f6c3 is described below

commit 8e6f6c3e19b86d9cf0b0647a9ab8af1c0f094bb0
Author: georg.henzler <ge...@netcentric.biz>
AuthorDate: Wed Sep 23 20:39:56 2020 +0200

    SLING-9745 Handle absolute URIs without scheme correctly
---
 .../org/apache/sling/api/uri/SlingUriBuilder.java  | 30 ++++++++--------
 .../org/apache/sling/api/uri/SlingUriTest.java     | 40 ++++++++++++++++++++++
 2 files changed, 54 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java b/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
index 29ef397..7088672 100644
--- a/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
+++ b/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
@@ -657,23 +657,21 @@ public class SlingUriBuilder {
 
     private String toStringInternal(boolean includeScheme, boolean includeFragment) {
         StringBuilder requestUri = new StringBuilder();
-
-        if (isAbsolute()) {
-            if (includeScheme) {
-                requestUri.append(scheme + CHAR_COLON);
+        
+        if (includeScheme && isAbsolute()) {
+            requestUri.append(scheme + CHAR_COLON);
+        }
+        if (isNotBlank(host)) {
+            requestUri.append(CHAR_SLASH + CHAR_SLASH);
+            if (isNotBlank(userInfo)) {
+                requestUri.append(userInfo + CHAR_AT);
             }
-            if (schemeSpecificPart == null) {
-                requestUri.append(CHAR_SLASH + CHAR_SLASH);
-                if (isNotBlank(userInfo)) {
-                    requestUri.append(userInfo + CHAR_AT);
-                }
-                requestUri.append(host);
-                if (port > 0
-                        && !(scheme.equals(HTTP_SCHEME) && port == HTTP_DEFAULT_PORT)
-                        && !(scheme.equals(HTTPS_SCHEME) && port == HTTPS_DEFAULT_PORT)) {
-                    requestUri.append(CHAR_COLON);
-                    requestUri.append(port);
-                }
+            requestUri.append(host);
+            if (port > 0 
+                    && !(HTTP_SCHEME.equals(scheme) && port == HTTP_DEFAULT_PORT)
+                    && !(HTTPS_SCHEME.equals(scheme) && port == HTTPS_DEFAULT_PORT)) {
+                requestUri.append(CHAR_COLON);
+                requestUri.append(port);
             }
         }
         if (schemeSpecificPart != null) {
diff --git a/src/test/java/org/apache/sling/api/uri/SlingUriTest.java b/src/test/java/org/apache/sling/api/uri/SlingUriTest.java
index 8faaa71..3e11d9a 100644
--- a/src/test/java/org/apache/sling/api/uri/SlingUriTest.java
+++ b/src/test/java/org/apache/sling/api/uri/SlingUriTest.java
@@ -81,6 +81,46 @@ public class SlingUriTest {
     }
 
     @Test
+    public void testFullUriWithoutSchemeSimple() {
+
+        String testUriStr = "//cdn.example.com/test/js_file.js";
+
+        testUri(testUriStr, false, false, false, false, false, slingUri -> {
+            assertEquals(null, slingUri.getScheme());
+            assertEquals("//cdn.example.com/test/js_file.js", slingUri.getSchemeSpecificPart());
+            assertEquals(null, slingUri.getUserInfo());
+            assertEquals("cdn.example.com", slingUri.getHost());
+            assertEquals(-1, slingUri.getPort());
+            assertEquals("/test/js_file", slingUri.getResourcePath());
+            assertEquals(null, slingUri.getSelectorString());
+            assertEquals("js", slingUri.getExtension());
+            assertEquals(null, slingUri.getSuffix());
+            assertEquals(null, slingUri.getQuery());
+        }, asList(resolver, null));
+
+    }
+
+    @Test
+    public void testFullUriWithoutSchemeComplex() {
+
+        String testUriStr = "//user:pw@cdn.example.com:3000/test/js_file.txt.js?par1=val1&par2=val2";
+
+        testUri(testUriStr, false, false, false, false, false, slingUri -> {
+            assertEquals(null, slingUri.getScheme());
+            assertEquals("//user:pw@cdn.example.com:3000/test/js_file.txt.js?par1=val1&par2=val2", slingUri.getSchemeSpecificPart());
+            assertEquals("user:pw", slingUri.getUserInfo());
+            assertEquals("cdn.example.com", slingUri.getHost());
+            assertEquals(3000, slingUri.getPort());
+            assertEquals("/test/js_file", slingUri.getResourcePath());
+            assertEquals("txt", slingUri.getSelectorString());
+            assertEquals("js", slingUri.getExtension());
+            assertEquals(null, slingUri.getSuffix());
+            assertEquals("par1=val1&par2=val2", slingUri.getQuery());
+        }, asList(resolver, null));
+
+    }
+    
+    @Test
     public void testAbsolutePathSlingUri() {
         String testUriStr = "/test/to/path.sel1.json/suffix/path?p1=2&p2=3#frag3939";