|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2015-2021 Oak Ridge National Laboratory. |
| 2 | + * Copyright (c) 2015-2025 Oak Ridge National Laboratory. |
3 | 3 | * All rights reserved. This program and the accompanying materials |
4 | 4 | * are made available under the terms of the Eclipse Public License v1.0 |
5 | 5 | * which accompanies this distribution, and is available at |
@@ -330,31 +330,20 @@ private static boolean canOpenUrl(final String resource_name) |
330 | 330 | logger.log(Level.WARNING, "URL {0} is not a URL", new Object[] { resource_name }); |
331 | 331 | return false; |
332 | 332 | } |
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. |
349 | 333 |
|
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)) |
351 | 346 | { |
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(); |
358 | 347 | return true; |
359 | 348 | } |
360 | 349 | catch (Exception ex) |
|
0 commit comments