Skip to content

Commit e7c341b

Browse files
committed
fix: ExternalResolver
1 parent 1e031c9 commit e7c341b

File tree

6 files changed

+111
-17
lines changed

6 files changed

+111
-17
lines changed

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<packaging>jar</packaging>
88

99
<parent>
10-
<groupId>com.predic8</groupId>
10+
<groupId>io.membrane-api</groupId>
1111
<artifactId>soa-model-parent</artifactId>
1212
<relativePath>../pom.xml</relativePath>
1313
<version>2.1.0</version>

core/src/main/groovy/com/predic8/xml/util/ExternalResolver.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
package com.predic8.xml.util
1313

14+
import com.predic8.util.URIUtil
1415
import org.slf4j.Logger
1516
import org.slf4j.LoggerFactory
1617
import org.apache.http.HttpHost;
@@ -63,7 +64,7 @@ class ExternalResolver extends ResourceResolver {
6364
throw new RuntimeException("Do not know how to resolve $input")
6465

6566
if(input.startsWith('file')) {
66-
return fixUtf(new FileInputStream(new URL(input).getPath()))
67+
return fixUtf(new FileInputStream(URIUtil.pathFromFileURI(input)))
6768
}
6869

6970
if(input.startsWith('http:') || input.startsWith('https:')) {
@@ -80,7 +81,7 @@ class ExternalResolver extends ResourceResolver {
8081
resolveAsFile(input, baseDir)
8182
}
8283

83-
public InputStream resolveAsFile(java.lang.String filename, java.lang.String baseDir) {
84+
InputStream resolveAsFile(java.lang.String filename, java.lang.String baseDir) {
8485
if(baseDir) {
8586
return fixUtf(new FileInputStream(new File(baseDir,filename)))
8687
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Copyright 2024 predic8 GmbH, www.predic8.com
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
package com.predic8.util;
15+
16+
import java.util.*;
17+
import java.util.regex.*;
18+
19+
import static java.net.URLDecoder.*;
20+
import static java.nio.charset.StandardCharsets.*;
21+
import static java.util.Optional.*;
22+
23+
/**
24+
* From Membrane API Gateway core project
25+
*/
26+
public class URIUtil {
27+
28+
private static final Pattern driveLetterPattern = Pattern.compile("^(\\w)[/:|].*");
29+
30+
/**
31+
* Removes file protocol from uri
32+
*
33+
* @param uri path that can contain file protocol
34+
* @return path without the file protocol
35+
*/
36+
public static String pathFromFileURI(String uri) {
37+
return decode(processDecodedPart(stripFilePrefix(uri)), UTF_8);
38+
}
39+
40+
private static String processDecodedPart(String path) {
41+
if (path.charAt(0) != '/')
42+
return path;
43+
String p = removeLocalhost(removeLeadingSlashes(path));
44+
return getPossibleDriveLetter(p).map(driveLetter -> driveLetter + ":" + slashToBackslash(removeDriveLetterAndSlash(p))).orElseGet(() -> "/" + p);
45+
}
46+
47+
static String removeDriveLetterAndSlash(String path) {
48+
return path.replaceFirst("\\w[:?|/]/*", "");
49+
}
50+
51+
static String slashToBackslash(String path) {
52+
return path.replace('/', '\\');
53+
}
54+
55+
static String removeLocalhost(String s) {
56+
if (s.startsWith("localhost"))
57+
return s.substring(10);
58+
return s;
59+
}
60+
61+
static String removeLeadingSlashes(String s) {
62+
return s.replaceAll("^/*", "");
63+
}
64+
65+
static Optional<String> getPossibleDriveLetter(String p) {
66+
Matcher m = driveLetterPattern.matcher(p);
67+
if (m.matches()) {
68+
return Optional.of(m.group(1));
69+
}
70+
return empty();
71+
}
72+
73+
private static String stripFilePrefix(String uri) {
74+
if (!uri.startsWith("file:"))
75+
return uri;
76+
return uri.substring(5); // Remove "file:"
77+
}
78+
}

core/src/test/groovy/com/predic8/xml/util/ExternalResolverTest.groovy

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,34 @@ import org.junit.Assume
1818
import org.junit.Before
1919
import org.junit.Test
2020

21+
import java.nio.charset.StandardCharsets
22+
import java.util.stream.Collectors
23+
2124
class ExternalResolverTest {
22-
23-
def resolver
24-
def url
25+
26+
def resolver
27+
def url
2528

2629
@Before
27-
void setUp() {
28-
resolver = new ExternalResolver()
29-
url = 'https://www.predic8.de/city-service?wsdl'
30-
}
31-
32-
@Test
33-
void testResolveAsString() {
34-
Assume.assumeTrue(!System.getenv('OFFLINETESTING'))
35-
assert resolver.resolveAsString(url) != null
36-
}
30+
void setUp() {
31+
resolver = new ExternalResolver()
32+
url = 'https://www.predic8.de/city-service?wsdl'
33+
}
34+
35+
@Test
36+
void testResolveAsString() {
37+
Assume.assumeTrue(!System.getenv('OFFLINETESTING'))
38+
assert resolver.resolveAsString(url) != null
39+
}
40+
41+
@Test
42+
void fileUriWithSpaces() {
43+
def is = resolver.resolve(this.getClass().getResource("/").toString() + "resource with spaces.xml", "")
44+
assert readFromInputStream(is).equals("<foo/>")
45+
}
46+
47+
private String readFromInputStream(is) {
48+
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))
49+
reader.lines().collect(Collectors.joining(System.lineSeparator()))
50+
}
3751
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<foo/>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

5-
<groupId>com.predic8</groupId>
5+
<groupId>io.membrane-api</groupId>
66
<artifactId>soa-model-parent</artifactId>
77
<version>2.1.0</version>
88
<packaging>pom</packaging>

0 commit comments

Comments
 (0)