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

sdedic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 2f2ee94e30 #6377: workaround vscode jar/nbjrt URI mangle
     new 61f724274c Merge pull request #6378 from 
sdedic/lsp/uri-mangle-workaround
2f2ee94e30 is described below

commit 2f2ee94e3015375c7cb3195ceca2eca82ee7dc01
Author: Svata Dedic <svatopluk.de...@oracle.com>
AuthorDate: Fri Aug 25 16:17:33 2023 +0200

    #6377: workaround vscode jar/nbjrt URI mangle
---
 .../modules/java/lsp/server/URITranslator.java     | 36 +++++++++++++++------
 .../modules/java/lsp/server/UtilsTest.java         | 37 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 10 deletions(-)

diff --git 
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/URITranslator.java
 
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/URITranslator.java
index 00f4a91ea5..58eafebfa7 100644
--- 
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/URITranslator.java
+++ 
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/URITranslator.java
@@ -21,12 +21,10 @@ package org.netbeans.modules.java.lsp.server;
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.net.URLDecoder;
 import java.nio.charset.StandardCharsets;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -47,7 +45,6 @@ public final class URITranslator {
     private static final URITranslator INSTANCE = new URITranslator();
 
     private final Map<String, String> uriFromCacheMap = new LRUCacheMap();
-    private final Map<String, String> uriToCacheMap = new LRUCacheMap();
 
     public static URITranslator getDefault() {
         return INSTANCE;
@@ -96,13 +93,32 @@ public final class URITranslator {
         });
     }
 
-    public synchronized String uriFromLSP(String nbUri) {
-        return uriToCacheMap.computeIfAbsent(nbUri, uri -> {
-            try {
-                return URLDecoder.decode(nbUri, StandardCharsets.UTF_8.name());
-            } catch (UnsupportedEncodingException ex) {}
-            return uri;
-        });
+    public String uriFromLSP(String nbUri) {
+        // handle special cases. We use file:jar:.....!/ and 
nbjrt:file:..../!/ but vscode url-encodes that, but wrongly, as it leaves other 
URI-encoded
+        // special characters as they are. However doing a full decode using 
Decoder will damage other URL-encoded special characters that would be
+        // parsed by URI.create() later.
+        // So handle the special cases manually and only remove the 
vscode-added scheme/delimiter encoded chars
+        int slash = nbUri.indexOf('/'); // NOI18N
+        if (slash != -1) {
+            int indexOfInnerScheme = nbUri.substring(0, slash).indexOf("%3A"); 
// NOI18N
+            if (indexOfInnerScheme > 0 && indexOfInnerScheme < slash) {
+                int n = indexOfInnerScheme + 3;
+                StringBuilder sb = new StringBuilder();
+                sb.append(nbUri, 0, indexOfInnerScheme);
+                sb.append(':'); // NOI18N
+                
+                // now search for the mangled !
+                int mangledExclamation = nbUri.indexOf("%21/"); // NOI18N
+                if (mangledExclamation > 0) {
+                    sb.append(nbUri, n, mangledExclamation);
+                    sb.append('!'); // NOI18N
+                    n = mangledExclamation + 3;
+                }
+                sb.append(nbUri, n, nbUri.length());
+                return sb.toString();
+            }
+        }
+        return nbUri;
     }
 
     /**
diff --git 
a/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/UtilsTest.java
 
b/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/UtilsTest.java
index f0ebdae200..81cb2673b7 100644
--- 
a/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/UtilsTest.java
+++ 
b/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/UtilsTest.java
@@ -20,7 +20,10 @@ package org.netbeans.modules.java.lsp.server;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
+import org.junit.Assume;
 import org.junit.Test;
+import org.netbeans.api.java.platform.JavaPlatform;
+import org.openide.filesystems.FileObject;
 
 /**
  *
@@ -104,4 +107,38 @@ public class UtilsTest {
         String result = Utils.html2plain(s, true);
         assertEquals(expResult, result);
     }
+    
+    /**
+     * Checks that mangled URI pointing into a JAR file is properly 
interpreted from LSP.
+     * @throws Exception 
+     */
+    @Test
+    public void testDecodeMangledJarURI() throws Exception {
+        FileObject fo = 
JavaPlatform.getDefault().getSourceFolders().findResource("java/util/Collections.java");
+        String uri = Utils.toUri(fo);
+        // mangle:
+        String replaced = uri.replace("file:", "file%3A").replace("!/", 
"%21/");
+        
+        assertEquals(uri, URITranslator.getDefault().uriFromLSP(replaced));
+        assertSame(fo, Utils.fromUri(replaced));
+    }
+
+    /**
+     * Checks that mangled URI pointing into a JDK11 module is properly 
interpreted from LSP.
+     * @throws Exception 
+     */
+    @Test
+    public void testDecodeMangledNbjrtURI() throws Exception {
+        String jdkVersion = System.getProperty("java.specification.version");
+        // ignore on JDK8
+        Assume.assumeTrue(!jdkVersion.startsWith("1.") && 
Integer.parseInt(jdkVersion) >= 9);
+        
+        FileObject fo = 
JavaPlatform.getDefault().getBootstrapLibraries().findResource("java/util/Collections.class");
+        String uri = Utils.toUri(fo);
+        // mangle:
+        String replaced = uri.replace("file:", "file%3A").replace("!/", 
"%21/");
+        
+        assertEquals(uri, URITranslator.getDefault().uriFromLSP(replaced));
+        assertSame(fo, Utils.fromUri(replaced));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to