Skip to content

Commit db11024

Browse files
authored
Write and read blobs was not working using SpringBoot. (#813)
* Write and read blobs was not working using SpringBoot. Issue: 106122 * Write and read blobs was not working using SpringBoot. Issue: 106122 * Write and read blobs was not working using SpringBoot. Issue: 106122 * Write and read blobs was not working using SpringBoot. Issue: 106122 * Write and read blobs was not working using SpringBoot. Issue: 106122 * Write and read blobs was not working using SpringBoot. Issue: 106122
1 parent 3c0ac76 commit db11024

4 files changed

Lines changed: 72 additions & 17 deletions

File tree

common/src/main/java/com/genexus/util/GXFileInfo.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
public class GXFileInfo implements IGXFileInfo {
1616

1717
private File fileSource;
18+
private ClassPathResource resource;
1819
private boolean isDirectory;
1920

2021
public GXFileInfo(File file){
2122
this(file, false);
2223
}
2324
public GXFileInfo(File file, boolean isDirectory){
25+
if (ApplicationContext.getInstance().isSpringBootApp() && ! file.exists() && !file.getPath().equals(""))
26+
resource = new ClassPathResource(file.getPath());
2427
fileSource = file;
2528
this.isDirectory = isDirectory;
2629
}
@@ -36,8 +39,8 @@ public String getPath(){
3639
}
3740
}
3841
public boolean exists(){
39-
if (ApplicationContext.getInstance().isSpringBootApp() && !isDirectory) {
40-
if (!fileSource.exists() && !new ClassPathResource(fileSource.getPath()).exists())
42+
if (resource != null && !isDirectory) {
43+
if (!fileSource.exists() && !resource.exists())
4144
return false;
4245
return true;
4346
}
@@ -48,6 +51,8 @@ public boolean exists(){
4851
return false;
4952
}
5053
public boolean isFile(){
54+
if (resource != null)
55+
return true;
5156
return fileSource.isFile();
5257
}
5358
public boolean isDirectory(){
@@ -88,6 +93,13 @@ public String getAbsolutePath(){
8893
return fileSource.getAbsolutePath();
8994
}
9095
public long length(){
96+
if (resource != null && !fileSource.exists())
97+
try {
98+
return resource.contentLength();
99+
}
100+
catch (IOException _) {
101+
return 0;
102+
}
91103
return fileSource.length();
92104
}
93105
public Date lastModified(){
@@ -139,8 +151,8 @@ public GXDirectoryCollection listDirectories(){
139151

140152
public InputStream getStream(){
141153
try {
142-
if (ApplicationContext.getInstance().isSpringBootApp()) {
143-
return new ClassPathResource(fileSource.getPath()).getInputStream();
154+
if (resource != null && !fileSource.exists()) {
155+
return resource.getInputStream();
144156
}
145157

146158
return new FileInputStream(fileSource);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,48 @@
11
package com.genexus.springboot;
22

3+
import com.genexus.Application;
4+
import com.genexus.common.interfaces.SpecificImplementation;
5+
import com.genexus.diagnostics.core.ILogger;
6+
import com.genexus.diagnostics.core.LogManager;
7+
import org.springframework.beans.factory.annotation.Value;
38
import org.springframework.context.annotation.Configuration;
49
import org.springframework.util.AntPathMatcher;
510
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
611
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
712
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
13+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
814

915
@Configuration
1016
@EnableWebMvc
1117
public class GXConfig implements WebMvcConfigurer {
18+
public static final ILogger logger = LogManager.getLogger(GXConfig.class);
1219

1320
@Override
1421
public void configurePathMatch(PathMatchConfigurer configurer) {
1522
AntPathMatcher matcher = new AntPathMatcher();
1623
matcher.setCaseSensitive(false);
1724
configurer.setPathMatcher(matcher);
1825
}
26+
27+
@Value("${server.servlet.context-parameters.gxcfg}")
28+
private String gxConfig;
29+
30+
@Override
31+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
32+
try {
33+
Application.init(Class.forName(gxConfig));
34+
35+
String webImageDir = Application.getClientContext().getClientPreferences().getWEB_IMAGE_DIR();
36+
String blobPath = SpecificImplementation.Application.getDefaultPreferences().getBLOB_PATH().replace("\\", "");
37+
38+
registry.addResourceHandler(webImageDir + "**")
39+
.addResourceLocations("classpath:" + webImageDir);
40+
41+
registry.addResourceHandler("/" + blobPath + "/**")
42+
.addResourceLocations("file:./" + blobPath + "/");
43+
}
44+
catch (ClassNotFoundException e) {
45+
logger.error("Error setting context folders ", e);
46+
}
47+
}
1948
}

java/src/main/java/com/genexus/db/driver/GXPreparedStatement.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
import java.io.InputStreamReader;
1010
import java.io.Reader;
1111
import java.io.UnsupportedEncodingException;
12-
import java.io.Writer;
13-
import java.lang.reflect.Field;
14-
import java.lang.reflect.InvocationTargetException;
1512
import java.lang.reflect.Method;
1613
import java.math.BigDecimal;
1714
import java.net.MalformedURLException;
@@ -1414,7 +1411,13 @@ public void setBLOBFile(int index, String fileName, boolean isMultiMedia) throws
14141411
if(webContext != null)
14151412
{
14161413
if (webContext instanceof com.genexus.webpanels.HttpContextWeb) {
1417-
fileName = ((com.genexus.webpanels.HttpContextWeb) webContext).getRealPath(fileName);
1414+
if (ApplicationContext.getInstance().isSpringBootApp() && ! new File(fileName).isAbsolute())
1415+
{
1416+
if (fileName.startsWith(webContext.getContextPath()))
1417+
fileName = fileName.replaceFirst(webContext.getContextPath(), "").substring(1);
1418+
}
1419+
else
1420+
fileName = ((com.genexus.webpanels.HttpContextWeb) webContext).getRealPath(fileName);
14181421
}
14191422
else
14201423
{
@@ -1452,16 +1455,17 @@ else if(blobFiles.length < index)
14521455
{
14531456
if (Application.getExternalProvider() == null)
14541457
{
1455-
try
1458+
GXFile file = new GXFile(fileName);
1459+
InputStream is = file.getStream();
1460+
if (is != null)
14561461
{
1457-
File file = new File(fileName);
1458-
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
1459-
setBinaryStream(index, inputStream, (int) file.length());
1462+
BufferedInputStream inputStream = new BufferedInputStream(is);
1463+
setBinaryStream(index, inputStream, (int) file.getLength());
14601464
}
1461-
catch (IOException e)
1465+
else
14621466
{
14631467
throw new SQLException("The filename does not exists in url " + fileName);
1464-
}
1468+
}
14651469
}
14661470
else
14671471
{

java/src/main/java/com/genexus/webpanels/HttpContextWeb.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public String getResourceRelative(String path, boolean includeBasePath) {
143143
}
144144
;
145145
String Resource = path;
146-
String basePath = getDefaultPath();
146+
String basePath = getDefaultPath(true);
147147
if (Resource.startsWith(basePath) && Resource.length() >= basePath.length())
148148
Resource = Resource.substring(basePath.length());
149149
if (ContextPath != null && !ContextPath.equals("") && Resource.startsWith(ContextPath))
@@ -516,6 +516,9 @@ public String getContextPath() {
516516
public void setContextPath(String path) {}
517517

518518
public String getRealPath(String path) {
519+
if (ApplicationContext.getInstance().isSpringBootApp())
520+
return path;
521+
519522
String realPath = path;
520523

521524
File file = new File(path);
@@ -1255,8 +1258,15 @@ private boolean useCustomRedirect() {
12551258
}
12561259

12571260
public String getDefaultPath() {
1258-
if (ApplicationContext.getInstance().isSpringBootApp())
1259-
return "";
1261+
return getDefaultPath(false);
1262+
}
1263+
public String getDefaultPath(boolean useAbsolutePath) {
1264+
if (ApplicationContext.getInstance().isSpringBootApp()) {
1265+
if (useAbsolutePath)
1266+
return new File("").getAbsolutePath();
1267+
else
1268+
return "";
1269+
}
12601270

12611271
if (servletContext == null)
12621272
return "";

0 commit comments

Comments
 (0)