From d3c155cfea53a1ce0d2bfedd6199dd12c686e9d3 Mon Sep 17 00:00:00 2001 From: HURIEZ Mathis Date: Tue, 26 Mar 2024 16:17:39 +0100 Subject: [PATCH 1/2] Patch Web Page action, some URL were not displayed --- .../builder/model/util/ModelResourceUtil.java | 74 +++++++++++-------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/app/display/model/src/main/java/org/csstudio/display/builder/model/util/ModelResourceUtil.java b/app/display/model/src/main/java/org/csstudio/display/builder/model/util/ModelResourceUtil.java index 3cc941179f..f0249da95c 100644 --- a/app/display/model/src/main/java/org/csstudio/display/builder/model/util/ModelResourceUtil.java +++ b/app/display/model/src/main/java/org/csstudio/display/builder/model/util/ModelResourceUtil.java @@ -7,8 +7,6 @@ *******************************************************************************/ package org.csstudio.display.builder.model.util; -import static org.csstudio.display.builder.model.ModelPlugin.logger; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -24,6 +22,7 @@ import java.nio.file.Paths; import java.time.Duration; import java.util.logging.Level; +import java.util.logging.Logger; import org.csstudio.display.builder.model.DisplayModel; import org.csstudio.display.builder.model.ModelPlugin; @@ -37,6 +36,7 @@ @SuppressWarnings("nls") public class ModelResourceUtil { + public final static Logger logger = Logger.getLogger(ModelResourceUtil.class.getPackageName()); /** Schema used for the built-in display examples */ public static final String EXAMPLES_SCHEMA = "examples"; @@ -309,49 +309,59 @@ private static String doResolveResource(final String parent_display, final Strin */ private static boolean canOpenUrl(final String resource_name) { + boolean canOpenUrl = false; final URL example = getExampleURL(resource_name); if (example != null) { try { example.openStream().close(); - return true; + canOpenUrl = true; } catch (Exception ex) { - return false; + logger.log(Level.SEVERE, "URL cannot open " + resource_name , ex); } } - if (! isURL(resource_name)) - return false; - // This implementation is expensive: - // On success, caller will soon open the URL again. - // In practice, not too bad because second time around - // is usually quite fast as result of web server cache. - // - // Alternative would be to always return the stream as - // a result, updating all callers from - // - // resolved = ModelResourceUtil.resolveResource(parent_display, display_file); - // stream = ModelResourceUtil.openResourceStream(resolved) - // - // to just - // - // stream = ModelResourceUtil.resolveResource(parent_display, display_file); - // - // This can break code which really just needs the resolved name. + if (isURL(resource_name)) { + // This implementation is expensive: + // On success, caller will soon open the URL again. + // In practice, not too bad because second time around + // is usually quite fast as result of web server cache. + // + // Alternative would be to always return the stream as + // a result, updating all callers from + // + // resolved = ModelResourceUtil.resolveResource(parent_display, display_file); + // stream = ModelResourceUtil.openResourceStream(resolved) + // + // to just + // + // stream = ModelResourceUtil.resolveResource(parent_display, display_file); + // + // This can break code which really just needs the resolved name. - try - { - final InputStream stream = openURL(resource_name); - stream.close(); - return true; - } - catch (Exception ex) - { - return false; + try + { + //In case of https an authentication is request + //It sends an Exception Connection refused: + //Use openConnection instead of openStream + //Stream is not OK for a https + URL url = new URL(resource_name); + url.openConnection(); + // int responseCode = huc.getResponseCode(); + // Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode); + canOpenUrl = true; + } + catch (Exception ex) + { + ex.printStackTrace(); + logger.log(Level.SEVERE, "URL cannot open " + resource_name , ex); + } } + + return canOpenUrl; } /** Check for "examples:.." @@ -421,7 +431,7 @@ public static InputStream openResourceStream(final String resource_name) throws // final long milli = Math.round(1000 + Math.random()*4000); // Thread.sleep(milli); // } - if (resource_name.startsWith("http") || resource_name.startsWith("file:/")) + if (resource_name.startsWith("http")) return openURL(resource_name); // Handle legacy RCP URL From 1b832413f098e77be4bac262ddcb27856bab5f12 Mon Sep 17 00:00:00 2001 From: HURIEZ Mathis Date: Tue, 26 Mar 2024 16:44:37 +0100 Subject: [PATCH 2/2] Rollback to keep file check for resource name --- .../display/builder/model/util/ModelResourceUtil.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/display/model/src/main/java/org/csstudio/display/builder/model/util/ModelResourceUtil.java b/app/display/model/src/main/java/org/csstudio/display/builder/model/util/ModelResourceUtil.java index f0249da95c..101447f514 100644 --- a/app/display/model/src/main/java/org/csstudio/display/builder/model/util/ModelResourceUtil.java +++ b/app/display/model/src/main/java/org/csstudio/display/builder/model/util/ModelResourceUtil.java @@ -350,8 +350,6 @@ private static boolean canOpenUrl(final String resource_name) //Stream is not OK for a https URL url = new URL(resource_name); url.openConnection(); - // int responseCode = huc.getResponseCode(); - // Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode); canOpenUrl = true; } catch (Exception ex) @@ -431,7 +429,7 @@ public static InputStream openResourceStream(final String resource_name) throws // final long milli = Math.round(1000 + Math.random()*4000); // Thread.sleep(milli); // } - if (resource_name.startsWith("http")) + if (resource_name.startsWith("http") || resource_name.startsWith("file:/")) return openURL(resource_name); // Handle legacy RCP URL