From 29255d8ee05774211c80619818eede6aeac76e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Sexenian?= Date: Tue, 3 Jan 2023 12:35:07 -0300 Subject: [PATCH 1/3] ImageWidth and ImageHeigth wasnt working. Issue 100577 --- .../main/java/com/genexus/GxImageUtil.java | 13 +++++++++- .../java/com/genexus/PrivateUtilities.java | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/java/src/main/java/com/genexus/GxImageUtil.java b/java/src/main/java/com/genexus/GxImageUtil.java index 297171ed8..3d3f088e7 100644 --- a/java/src/main/java/com/genexus/GxImageUtil.java +++ b/java/src/main/java/com/genexus/GxImageUtil.java @@ -6,6 +6,7 @@ import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.*; +import java.net.URL; import com.genexus.db.driver.ResourceAccessControlList; import com.genexus.util.GxFileInfoSourceType; @@ -22,7 +23,13 @@ private static InputStream getInputStream(String filePathOrUrl) throws IOExcepti private static BufferedImage createBufferedImageFromURI(String filePathOrUrl) throws IOException { - try (InputStream is = getGXFile(filePathOrUrl).getStream()) { + InputStream is; + try { + if (PrivateUtilities.isValidURL(filePathOrUrl)) + is = new URL(filePathOrUrl).openStream(); + else{ + is = getGXFile(filePathOrUrl).getStream(); + } return ImageIO.read(is); } catch (IOException e) { @@ -33,6 +40,10 @@ private static BufferedImage createBufferedImageFromURI(String filePathOrUrl) th private static GXFile getGXFile(String filePathOrUrl) { String basePath = (com.genexus.ModelContext.getModelContext() != null) ? com.genexus.ModelContext.getModelContext().getHttpContext().getDefaultPath(): ""; + String environmentName = basePath.substring(basePath.lastIndexOf("\\") + 1); + if (filePathOrUrl.substring(1).startsWith(environmentName)) { + filePathOrUrl = filePathOrUrl.substring(environmentName.length() + 2); + } return new GXFile(basePath, filePathOrUrl, ResourceAccessControlList.Default, GxFileInfoSourceType.Unknown); } diff --git a/java/src/main/java/com/genexus/PrivateUtilities.java b/java/src/main/java/com/genexus/PrivateUtilities.java index fb7ef791f..281786994 100644 --- a/java/src/main/java/com/genexus/PrivateUtilities.java +++ b/java/src/main/java/com/genexus/PrivateUtilities.java @@ -18,8 +18,10 @@ import java.io.PushbackInputStream; import java.lang.reflect.Field; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URL; import java.nio.file.InvalidPathException; +import java.io.FileNotFoundException; import java.nio.file.Paths; import java.util.Properties; import java.util.Random; @@ -625,6 +627,29 @@ public static boolean isAbsoluteFilePath(String path) { } } + + /** + *
+	 * Checks if a string has valid URL syntax and points to a valid resource on the web.
+	 * Null safe.
+	 * 
+ */ + //Me fijo si la sintaxis del string es la sintaxis de una URL y si + //de verdad existe a lo que esta apuntando + public static boolean isValidURL(String urlString) { + try { + URL url = new URL(urlString); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("HEAD"); + int responseCode = connection.getResponseCode(); + return (responseCode == HttpURLConnection.HTTP_OK); + } catch (MalformedURLException | FileNotFoundException e) { + return false; + } catch (IOException | NullPointerException e) { + return false; + } + } + public static String addLastPathSeparator(String dir) { return addLastChar(dir, File.separator); From b12c6ff898bfad6f512df753d3fe7eb7d3665d84 Mon Sep 17 00:00:00 2001 From: tomas-sexenian Date: Thu, 5 Jan 2023 12:40:02 -0300 Subject: [PATCH 2/3] Prettified code and reverted unnecessary changes --- .../main/java/com/genexus/IHttpContext.java | 2 +- .../main/java/com/genexus/GxImageUtil.java | 22 +++++++--------- .../java/com/genexus/PrivateUtilities.java | 25 ------------------- 3 files changed, 10 insertions(+), 39 deletions(-) diff --git a/common/src/main/java/com/genexus/IHttpContext.java b/common/src/main/java/com/genexus/IHttpContext.java index 97d3ddf36..e6fc5904c 100644 --- a/common/src/main/java/com/genexus/IHttpContext.java +++ b/common/src/main/java/com/genexus/IHttpContext.java @@ -63,5 +63,5 @@ public interface IHttpContext { boolean isHttpContextNull(); boolean isHttpContextWeb(); - + String getContextPath(); } diff --git a/java/src/main/java/com/genexus/GxImageUtil.java b/java/src/main/java/com/genexus/GxImageUtil.java index 3d3f088e7..ffb927eca 100644 --- a/java/src/main/java/com/genexus/GxImageUtil.java +++ b/java/src/main/java/com/genexus/GxImageUtil.java @@ -23,27 +23,23 @@ private static InputStream getInputStream(String filePathOrUrl) throws IOExcepti private static BufferedImage createBufferedImageFromURI(String filePathOrUrl) throws IOException { - InputStream is; - try { - if (PrivateUtilities.isValidURL(filePathOrUrl)) - is = new URL(filePathOrUrl).openStream(); - else{ + IHttpContext httpContext = com.genexus.ModelContext.getModelContext().getHttpContext(); + InputStream is = null; + try{ + if (filePathOrUrl.toLowerCase().startsWith("http://") || filePathOrUrl.toLowerCase().startsWith("https://") || + (httpContext.isHttpContextWeb() && filePathOrUrl.startsWith(httpContext.getContextPath()))) + is = new URL(GXDbFile.pathToUrl( filePathOrUrl, httpContext)).openStream(); + else is = getGXFile(filePathOrUrl).getStream(); - } return ImageIO.read(is); - } - catch (IOException e) { + } catch (IOException e) { log.error("Failed to read image stream: " + filePathOrUrl); throw e; - } + } finally { is.close(); } } private static GXFile getGXFile(String filePathOrUrl) { String basePath = (com.genexus.ModelContext.getModelContext() != null) ? com.genexus.ModelContext.getModelContext().getHttpContext().getDefaultPath(): ""; - String environmentName = basePath.substring(basePath.lastIndexOf("\\") + 1); - if (filePathOrUrl.substring(1).startsWith(environmentName)) { - filePathOrUrl = filePathOrUrl.substring(environmentName.length() + 2); - } return new GXFile(basePath, filePathOrUrl, ResourceAccessControlList.Default, GxFileInfoSourceType.Unknown); } diff --git a/java/src/main/java/com/genexus/PrivateUtilities.java b/java/src/main/java/com/genexus/PrivateUtilities.java index 281786994..fb7ef791f 100644 --- a/java/src/main/java/com/genexus/PrivateUtilities.java +++ b/java/src/main/java/com/genexus/PrivateUtilities.java @@ -18,10 +18,8 @@ import java.io.PushbackInputStream; import java.lang.reflect.Field; import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; import java.nio.file.InvalidPathException; -import java.io.FileNotFoundException; import java.nio.file.Paths; import java.util.Properties; import java.util.Random; @@ -627,29 +625,6 @@ public static boolean isAbsoluteFilePath(String path) { } } - - /** - *
-	 * Checks if a string has valid URL syntax and points to a valid resource on the web.
-	 * Null safe.
-	 * 
- */ - //Me fijo si la sintaxis del string es la sintaxis de una URL y si - //de verdad existe a lo que esta apuntando - public static boolean isValidURL(String urlString) { - try { - URL url = new URL(urlString); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("HEAD"); - int responseCode = connection.getResponseCode(); - return (responseCode == HttpURLConnection.HTTP_OK); - } catch (MalformedURLException | FileNotFoundException e) { - return false; - } catch (IOException | NullPointerException e) { - return false; - } - } - public static String addLastPathSeparator(String dir) { return addLastChar(dir, File.separator); From cf9594d8a4335858e9e6bb19f4a215f8ba856315 Mon Sep 17 00:00:00 2001 From: tomas-sexenian Date: Thu, 5 Jan 2023 19:08:08 -0300 Subject: [PATCH 3/3] Dummy commit to force automatic merge --- java/src/main/java/com/genexus/GxImageUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/src/main/java/com/genexus/GxImageUtil.java b/java/src/main/java/com/genexus/GxImageUtil.java index ffb927eca..73a23c39a 100644 --- a/java/src/main/java/com/genexus/GxImageUtil.java +++ b/java/src/main/java/com/genexus/GxImageUtil.java @@ -35,7 +35,7 @@ private static BufferedImage createBufferedImageFromURI(String filePathOrUrl) th } catch (IOException e) { log.error("Failed to read image stream: " + filePathOrUrl); throw e; - } finally { is.close(); } + } finally {is.close();} } private static GXFile getGXFile(String filePathOrUrl) {