From 16b8ec00e6c03631cf70f3948c51c2e0d71388e4 Mon Sep 17 00:00:00 2001 From: iroqueta Date: Fri, 15 Dec 2023 12:19:35 -0300 Subject: [PATCH 1/6] Write and read blobs was not working using SpringBoot. Issue: 106122 --- .../java/com/genexus/util/GXFileInfo.java | 20 ++++++++++--- .../java/com/genexus/springboot/GXConfig.java | 29 +++++++++++++++++++ .../db/driver/GXPreparedStatement.java | 21 +++++++------- .../com/genexus/webpanels/HttpContextWeb.java | 5 +++- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/common/src/main/java/com/genexus/util/GXFileInfo.java b/common/src/main/java/com/genexus/util/GXFileInfo.java index 439ec4908..088c27bb5 100644 --- a/common/src/main/java/com/genexus/util/GXFileInfo.java +++ b/common/src/main/java/com/genexus/util/GXFileInfo.java @@ -15,12 +15,15 @@ public class GXFileInfo implements IGXFileInfo { private File fileSource; + private ClassPathResource resource; private boolean isDirectory; public GXFileInfo(File file){ this(file, false); } public GXFileInfo(File file, boolean isDirectory){ + if (ApplicationContext.getInstance().isSpringBootApp()) + resource = new ClassPathResource(file.getPath()); fileSource = file; this.isDirectory = isDirectory; } @@ -36,8 +39,8 @@ public String getPath(){ } } public boolean exists(){ - if (ApplicationContext.getInstance().isSpringBootApp() && !isDirectory) { - if (!fileSource.exists() && !new ClassPathResource(fileSource.getPath()).exists()) + if (resource != null && !isDirectory) { + if (!fileSource.exists() && !resource.exists()) return false; return true; } @@ -48,6 +51,8 @@ public boolean exists(){ return false; } public boolean isFile(){ + if (resource != null) + return true; return fileSource.isFile(); } public boolean isDirectory(){ @@ -88,6 +93,13 @@ public String getAbsolutePath(){ return fileSource.getAbsolutePath(); } public long length(){ + if (resource != null) + try { + return resource.contentLength(); + } + catch (IOException _) { + return 0; + } return fileSource.length(); } public Date lastModified(){ @@ -139,8 +151,8 @@ public GXDirectoryCollection listDirectories(){ public InputStream getStream(){ try { - if (ApplicationContext.getInstance().isSpringBootApp()) { - return new ClassPathResource(fileSource.getPath()).getInputStream(); + if (resource != null) { + return resource.getInputStream(); } return new FileInputStream(fileSource); diff --git a/gxspringboot/src/main/java/com/genexus/springboot/GXConfig.java b/gxspringboot/src/main/java/com/genexus/springboot/GXConfig.java index 02fc89f74..40b4566e3 100644 --- a/gxspringboot/src/main/java/com/genexus/springboot/GXConfig.java +++ b/gxspringboot/src/main/java/com/genexus/springboot/GXConfig.java @@ -1,14 +1,21 @@ package com.genexus.springboot; +import com.genexus.Application; +import com.genexus.common.interfaces.SpecificImplementation; +import com.genexus.diagnostics.core.ILogger; +import com.genexus.diagnostics.core.LogManager; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.util.AntPathMatcher; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; @Configuration @EnableWebMvc public class GXConfig implements WebMvcConfigurer { + public static final ILogger logger = LogManager.getLogger(GXConfig.class); @Override public void configurePathMatch(PathMatchConfigurer configurer) { @@ -16,4 +23,26 @@ public void configurePathMatch(PathMatchConfigurer configurer) { matcher.setCaseSensitive(false); configurer.setPathMatcher(matcher); } + + @Value("${server.servlet.context-parameters.gxcfg}") + private String gxConfig; + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + try { + Application.init(Class.forName(gxConfig)); + + String webImageDir = Application.getClientContext().getClientPreferences().getWEB_IMAGE_DIR(); + String blobPath = SpecificImplementation.Application.getDefaultPreferences().getBLOB_PATH().replace("\\", ""); + + registry.addResourceHandler(webImageDir + "**") + .addResourceLocations("classpath:" + webImageDir); + + registry.addResourceHandler("/" + blobPath + "/**") + .addResourceLocations("file:./" + blobPath + "/"); + } + catch (ClassNotFoundException e) { + logger.error("Error setting context folders ", e); + } + } } diff --git a/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java b/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java index 50f46d9bd..99192c00a 100644 --- a/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java +++ b/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java @@ -9,9 +9,6 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; -import java.io.Writer; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.math.BigDecimal; import java.net.MalformedURLException; @@ -1414,7 +1411,10 @@ public void setBLOBFile(int index, String fileName, boolean isMultiMedia) throws if(webContext != null) { if (webContext instanceof com.genexus.webpanels.HttpContextWeb) { - fileName = ((com.genexus.webpanels.HttpContextWeb) webContext).getRealPath(fileName); + if (ApplicationContext.getInstance().isSpringBootApp()) + fileName = fileName.replaceFirst(webContext.getContextPath(), "").substring(1); + else + fileName = ((com.genexus.webpanels.HttpContextWeb) webContext).getRealPath(fileName); } else { @@ -1452,16 +1452,17 @@ else if(blobFiles.length < index) { if (Application.getExternalProvider() == null) { - try + GXFile file = new GXFile(fileName); + InputStream is = file.getStream(); + if (is != null) { - File file = new File(fileName); - BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); - setBinaryStream(index, inputStream, (int) file.length()); + BufferedInputStream inputStream = new BufferedInputStream(is); + setBinaryStream(index, inputStream, (int) file.getLength()); } - catch (IOException e) + else { throw new SQLException("The filename does not exists in url " + fileName); - } + } } else { diff --git a/java/src/main/java/com/genexus/webpanels/HttpContextWeb.java b/java/src/main/java/com/genexus/webpanels/HttpContextWeb.java index 981f09c83..96c7ac5eb 100644 --- a/java/src/main/java/com/genexus/webpanels/HttpContextWeb.java +++ b/java/src/main/java/com/genexus/webpanels/HttpContextWeb.java @@ -516,6 +516,9 @@ public String getContextPath() { public void setContextPath(String path) {} public String getRealPath(String path) { + if (ApplicationContext.getInstance().isSpringBootApp()) + return path; + String realPath = path; File file = new File(path); @@ -1256,7 +1259,7 @@ private boolean useCustomRedirect() { public String getDefaultPath() { if (ApplicationContext.getInstance().isSpringBootApp()) - return ""; + return new File("").getAbsolutePath(); if (servletContext == null) return ""; From 11f98b1bcd2b190f109f22a3f697670d87ac31b0 Mon Sep 17 00:00:00 2001 From: iroqueta Date: Mon, 18 Dec 2023 15:45:20 -0300 Subject: [PATCH 2/6] Write and read blobs was not working using SpringBoot. Issue: 106122 --- .../java/com/genexus/webpanels/HttpContextWeb.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/java/src/main/java/com/genexus/webpanels/HttpContextWeb.java b/java/src/main/java/com/genexus/webpanels/HttpContextWeb.java index 96c7ac5eb..d54081c8c 100644 --- a/java/src/main/java/com/genexus/webpanels/HttpContextWeb.java +++ b/java/src/main/java/com/genexus/webpanels/HttpContextWeb.java @@ -143,7 +143,7 @@ public String getResourceRelative(String path, boolean includeBasePath) { } ; String Resource = path; - String basePath = getDefaultPath(); + String basePath = getDefaultPath(true); if (Resource.startsWith(basePath) && Resource.length() >= basePath.length()) Resource = Resource.substring(basePath.length()); if (ContextPath != null && !ContextPath.equals("") && Resource.startsWith(ContextPath)) @@ -1258,8 +1258,15 @@ private boolean useCustomRedirect() { } public String getDefaultPath() { - if (ApplicationContext.getInstance().isSpringBootApp()) - return new File("").getAbsolutePath(); + return getDefaultPath(false); + } + public String getDefaultPath(boolean useAbsolutePath) { + if (ApplicationContext.getInstance().isSpringBootApp()) { + if (useAbsolutePath) + return new File("").getAbsolutePath(); + else + return ""; + } if (servletContext == null) return ""; From f2f5303c0b371fb30adaa3f8609adb8605475328 Mon Sep 17 00:00:00 2001 From: iroqueta Date: Tue, 19 Dec 2023 12:03:05 -0300 Subject: [PATCH 3/6] Write and read blobs was not working using SpringBoot. Issue: 106122 --- common/src/main/java/com/genexus/util/GXFileInfo.java | 2 +- .../main/java/com/genexus/db/driver/GXPreparedStatement.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/com/genexus/util/GXFileInfo.java b/common/src/main/java/com/genexus/util/GXFileInfo.java index 088c27bb5..2a3c031fa 100644 --- a/common/src/main/java/com/genexus/util/GXFileInfo.java +++ b/common/src/main/java/com/genexus/util/GXFileInfo.java @@ -22,7 +22,7 @@ public GXFileInfo(File file){ this(file, false); } public GXFileInfo(File file, boolean isDirectory){ - if (ApplicationContext.getInstance().isSpringBootApp()) + if (ApplicationContext.getInstance().isSpringBootApp() && ! file.exists()) resource = new ClassPathResource(file.getPath()); fileSource = file; this.isDirectory = isDirectory; diff --git a/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java b/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java index 99192c00a..ffa7ae5cb 100644 --- a/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java +++ b/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java @@ -1411,7 +1411,7 @@ public void setBLOBFile(int index, String fileName, boolean isMultiMedia) throws if(webContext != null) { if (webContext instanceof com.genexus.webpanels.HttpContextWeb) { - if (ApplicationContext.getInstance().isSpringBootApp()) + if (ApplicationContext.getInstance().isSpringBootApp() && ! new File(fileName).isAbsolute()) fileName = fileName.replaceFirst(webContext.getContextPath(), "").substring(1); else fileName = ((com.genexus.webpanels.HttpContextWeb) webContext).getRealPath(fileName); From bf086b6a6a9bf8e30c01de2dd5208722db065bab Mon Sep 17 00:00:00 2001 From: iroqueta Date: Tue, 19 Dec 2023 15:37:19 -0300 Subject: [PATCH 4/6] Write and read blobs was not working using SpringBoot. Issue: 106122 --- common/src/main/java/com/genexus/util/GXFileInfo.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/com/genexus/util/GXFileInfo.java b/common/src/main/java/com/genexus/util/GXFileInfo.java index 2a3c031fa..963467171 100644 --- a/common/src/main/java/com/genexus/util/GXFileInfo.java +++ b/common/src/main/java/com/genexus/util/GXFileInfo.java @@ -93,7 +93,7 @@ public String getAbsolutePath(){ return fileSource.getAbsolutePath(); } public long length(){ - if (resource != null) + if (resource != null && !fileSource.exists()) try { return resource.contentLength(); } @@ -151,7 +151,7 @@ public GXDirectoryCollection listDirectories(){ public InputStream getStream(){ try { - if (resource != null) { + if (resource != null && !fileSource.exists()) { return resource.getInputStream(); } From 29afc765203a891baaf0da095d6ddbf9de323109 Mon Sep 17 00:00:00 2001 From: iroqueta Date: Wed, 20 Dec 2023 15:20:56 -0300 Subject: [PATCH 5/6] Write and read blobs was not working using SpringBoot. Issue: 106122 --- common/src/main/java/com/genexus/util/GXFileInfo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/com/genexus/util/GXFileInfo.java b/common/src/main/java/com/genexus/util/GXFileInfo.java index 963467171..7b5e2e2a1 100644 --- a/common/src/main/java/com/genexus/util/GXFileInfo.java +++ b/common/src/main/java/com/genexus/util/GXFileInfo.java @@ -22,7 +22,7 @@ public GXFileInfo(File file){ this(file, false); } public GXFileInfo(File file, boolean isDirectory){ - if (ApplicationContext.getInstance().isSpringBootApp() && ! file.exists()) + if (ApplicationContext.getInstance().isSpringBootApp() && ! file.exists() && !file.getPath().equals("")) resource = new ClassPathResource(file.getPath()); fileSource = file; this.isDirectory = isDirectory; From c3eabe85de9c788c604c69d6e82c6d2b7ca184b6 Mon Sep 17 00:00:00 2001 From: iroqueta Date: Fri, 22 Dec 2023 19:31:14 -0300 Subject: [PATCH 6/6] Write and read blobs was not working using SpringBoot. Issue: 106122 --- .../main/java/com/genexus/db/driver/GXPreparedStatement.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java b/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java index ffa7ae5cb..4bd757dba 100644 --- a/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java +++ b/java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java @@ -1412,7 +1412,10 @@ public void setBLOBFile(int index, String fileName, boolean isMultiMedia) throws { if (webContext instanceof com.genexus.webpanels.HttpContextWeb) { if (ApplicationContext.getInstance().isSpringBootApp() && ! new File(fileName).isAbsolute()) - fileName = fileName.replaceFirst(webContext.getContextPath(), "").substring(1); + { + if (fileName.startsWith(webContext.getContextPath())) + fileName = fileName.replaceFirst(webContext.getContextPath(), "").substring(1); + } else fileName = ((com.genexus.webpanels.HttpContextWeb) webContext).getRealPath(fileName); }