Skip to content

Commit 919fe85

Browse files
committed
Refactor URI normalization: rename method for clarity, enhance exception handling, and update references in tests and implementations
1 parent 4468e52 commit 919fe85

4 files changed

Lines changed: 30 additions & 22 deletions

File tree

core/src/main/java/com/predic8/membrane/core/interceptor/server/WSDLPublisherInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ private Outcome handleRequestInternal(final Exchange exc) throws Exception {
234234
return RETURN;
235235
}
236236
} catch (NumberFormatException e) {
237-
exc.setResponse(HttpUtil.setHTMLErrorResponse(Response.internalServerError(), "Bad parameter format.", ""));
237+
exc.setResponse(HttpUtil.setHTMLErrorResponse(Response.badRequest(), "Bad parameter format.", ""));
238238
return ABORT;
239239
} catch (ResourceRetrievalException e) {
240240
exc.setResponse(notFound().build());

core/src/main/java/com/predic8/membrane/core/util/URIUtil.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,17 @@ public static String normalizeSingleDot(String uri) {
140140
}
141141

142142
/**
143-
* Normalizes the given path or URI. This method handles various formats of paths,
143+
* Normalizes the given path or URI and resolves it to an absolute path.
144+
* This method handles various formats of paths,
144145
* including filesystem paths, URIs, and paths with potential Windows drive letters.
145146
* The normalization involves resolving relative components, such as "." or "..",
146147
* and ensuring the path conforms to a standardized format.
147148
*
148-
* @param location the path or URI string to normalize. It must not be null or empty.
149-
* @return the normalized path or URI as a string.
150-
* @throws IllegalArgumentException if the input location is null or empty.
149+
* @param location the path or URI string to normalize.
150+
* @return the normalized absolute path or URI as a string.
151+
* @throws IllegalArgumentException if the input location is null, empty, or contains malformed URI syntax.
151152
*/
152-
public static String normalize(String location) {
153+
public static String getNormalizedAbsolutePathOrUri(String location) {
153154
if (location == null || location.isEmpty())
154155
throw new IllegalArgumentException("location must not be null or empty");
155156

core/src/main/java/com/predic8/membrane/core/util/wsdl/parser/schema/AbstractIncludeImport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected Schema getSchema(WSDLParserContext ctx) {
5252
}
5353

5454
private String resolve(WSDLParserContext ctx) {
55-
return URIUtil.normalize(ResolverMap.combine(ctx.basePath(), schemaLocation));
55+
return URIUtil.getNormalizedAbsolutePathOrUri(ResolverMap.combine(ctx.basePath(), schemaLocation));
5656
}
5757

5858
public String getSchemaLocation() {

core/src/test/java/com/predic8/membrane/core/util/URIUtilTest.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,94 +215,101 @@ void convertPath2FileURITest() throws URISyntaxException {
215215
class normalize {
216216
@Test
217217
void throwsOnNull() {
218-
assertThrows(IllegalArgumentException.class, () -> normalize(null));
218+
assertThrows(IllegalArgumentException.class, () -> getNormalizedAbsolutePathOrUri(null));
219219
}
220220

221+
221222
@Test
222223
void throwsOnEmpty() {
223-
assertThrows(IllegalArgumentException.class, () -> normalize(""));
224+
assertThrows(IllegalArgumentException.class, () -> getNormalizedAbsolutePathOrUri(""));
224225
}
225226

226227
@Test
227228
void normalizesRelativeFilesystemPath() {
228229
var input = "foo/../bar/test.txt";
229230
var expected = Path.of(input).toAbsolutePath().normalize().toString();
230-
assertEquals(expected, normalize(input));
231+
assertEquals(expected, getNormalizedAbsolutePathOrUri(input));
231232
}
232233

233234
@Test
234235
void normalizesAbsoluteFilesystemPath() {
235236
var input = Path.of("foo", "..", "bar").toAbsolutePath().toString();
236237
var expected = Path.of(input).toAbsolutePath().normalize().toString();
237-
assertEquals(expected, normalize(input));
238+
assertEquals(expected, getNormalizedAbsolutePathOrUri(input));
238239
}
239240

240241
@Test
241242
void normalizesFileUri() {
242-
assertEquals("file:/test.xml", normalize("file:///tmp/../test.xml"));
243+
assertEquals("file:/test.xml", getNormalizedAbsolutePathOrUri("file:///tmp/../test.xml"));
243244
}
244245

245246
@Test
246247
void normalizesHttpUri() {
247-
assertEquals("http://example.com/b/wsdl.xsd", normalize("http://example.com/a/../b/wsdl.xsd"));
248+
assertEquals("http://example.com/b/wsdl.xsd", getNormalizedAbsolutePathOrUri("http://example.com/a/../b/wsdl.xsd"));
248249
}
249250

251+
@Test
252+
void normalizesHttpUriWithQuery() {
253+
assertEquals("http://example.com/b/wsdl.xsd?a=a/../b", getNormalizedAbsolutePathOrUri("http://example.com/a/../b/wsdl.xsd?a=a/../b"));
254+
}
255+
256+
250257
@Test
251258
void classpathUri() {
252259
// The '..' within the path portion normalizes correctly.
253-
assertEquals("classpath://authority/xsd/test.xsd", normalize("classpath://authority/schema/../xsd/test.xsd"));
260+
assertEquals("classpath://authority/xsd/test.xsd", getNormalizedAbsolutePathOrUri("classpath://authority/schema/../xsd/test.xsd"));
254261
}
255262

256263
@Test
257264
void keepsClasspathUri() {
258265
// The '..' at the start of the path (after authority) cannot traverse above the authority, so it's preserved.
259-
assertEquals("classpath://authority/../xsd/test.xsd", normalize("classpath://authority/../xsd/test.xsd"));
266+
assertEquals("classpath://authority/../xsd/test.xsd", getNormalizedAbsolutePathOrUri("classpath://authority/../xsd/test.xsd"));
260267
}
261268

262269
@Test
263270
void normalizesUnixLikePath() {
264271
var input = "/tmp/../var/data.xsd";
265272
var expected = Path.of(input).toAbsolutePath().normalize().toString();
266-
assertEquals(expected, normalize(input));
273+
assertEquals(expected, getNormalizedAbsolutePathOrUri(input));
267274
}
268275

269276
@Test
270277
@EnabledOnOs(WINDOWS)
271278
void handlesWindowsDriveLetterPath() {
272279
var input = "C:\\temp\\..\\data\\test.xsd";
273280
var expected = Path.of(input).toAbsolutePath().normalize().toString();
274-
assertEquals(expected, normalize(input));
281+
assertEquals(expected, getNormalizedAbsolutePathOrUri(input));
275282
}
276283

277284
@Test
278285
@EnabledOnOs(WINDOWS)
279286
void handlesWindowsDriveRelativePath() {
280287
var input = "C:temp\\..\\data\\test.xsd";
281288
var expected = Path.of(input).toAbsolutePath().normalize().toString();
282-
assertEquals(expected, normalize(input));
289+
assertEquals(expected, getNormalizedAbsolutePathOrUri(input));
283290
}
284291

285292
@Test
286293
void keepsRelativeUriWithQuery() {
287-
assertEquals("schema.xsd?version=1", normalize("schema.xsd?version=1")
294+
assertEquals("schema.xsd?version=1", getNormalizedAbsolutePathOrUri("schema.xsd?version=1")
288295
);
289296
}
290297

291298
@Test
292299
void keepsRelativeUriWithFragment() {
293-
assertEquals("../types.xsd#frag", normalize("../types.xsd#frag")
300+
assertEquals("../types.xsd#frag", getNormalizedAbsolutePathOrUri("../types.xsd#frag")
294301
);
295302
}
296303

297304
@Test
298305
void keepsRelativeUriWithQueryAndFragment() {
299-
assertEquals("../types.xsd?version=1#frag", normalize("../types.xsd?version=1#frag")
306+
assertEquals("../types.xsd?version=1#frag", getNormalizedAbsolutePathOrUri("../types.xsd?version=1#frag")
300307
);
301308
}
302309

303310
@Test
304311
void keepsSchemeRelativeReference() {
305-
assertEquals("//example.com/schema.xsd?version=1", normalize("//example.com/schema.xsd?version=1"));
312+
assertEquals("//example.com/schema.xsd?version=1", getNormalizedAbsolutePathOrUri("//example.com/schema.xsd?version=1"));
306313
}
307314
}
308315
}

0 commit comments

Comments
 (0)