Skip to content

Commit 75d2cce

Browse files
author
kasemir
committed
More robust check of web resources
1 parent 9fd0af7 commit 75d2cce

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

app/display/model/src/main/java/org/csstudio/display/builder/model/util/ModelResourceUtil.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2015-2021 Oak Ridge National Laboratory.
2+
* Copyright (c) 2015-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -330,31 +330,20 @@ private static boolean canOpenUrl(final String resource_name)
330330
logger.log(Level.WARNING, "URL {0} is not a URL", new Object[] { resource_name });
331331
return false;
332332
}
333-
// This implementation is expensive:
334-
// On success, caller will soon open the URL again.
335-
// In practice, not too bad because second time around
336-
// is usually quite fast as result of web server cache.
337-
//
338-
// Alternative would be to always return the stream as
339-
// a result, updating all callers from
340-
//
341-
// resolved = ModelResourceUtil.resolveResource(parent_display, display_file);
342-
// stream = ModelResourceUtil.openResourceStream(resolved)
343-
//
344-
// to just
345-
//
346-
// stream = ModelResourceUtil.resolveResource(parent_display, display_file);
347-
//
348-
// This can break code which really just needs the resolved name.
349333

350-
try
334+
final String escaped = resource_name.replace(" ", "%20");
335+
// Opening the URL stream to check availability seems expensive.
336+
// On success, we'll soon do this again to actually read.
337+
// But 'openURL' uses a cache, the next time around
338+
// we already have the content.
339+
// Attempts to "only check" without reading, for example
340+
// new URL(...).openConnection().connect()
341+
// turned out to sometimes succeed even if the document doesn't exist
342+
// on the web server. Details might be related to the behavior
343+
// of site-specific intermediate proxies.
344+
// In any case, this seems the most reliable approach
345+
try (InputStream stream = openURL(escaped))
351346
{
352-
// final InputStream stream = openURL(resource_name);
353-
// stream.close();
354-
//Test only if the page exist and not read the content
355-
final String escaped = resource_name.replace(" ", "%20");
356-
URL resource = new URL(escaped);
357-
resource.openConnection();
358347
return true;
359348
}
360349
catch (Exception ex)

0 commit comments

Comments
 (0)