Skip to content

URLRewrite implementation in Spring Boot #886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 53 additions & 30 deletions common/src/main/java/com/genexus/URLRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.io.*;
import java.util.regex.Pattern;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;


public class URLRouter
Expand Down Expand Up @@ -120,14 +122,33 @@ private static String convertParmsToQueryString(boolean useNamedParameters, Stri
return queryString;
}

private static void load()
{
String line;
InputStream is = null;
String defaultPath = SpecificImplementation.Application.getModelContext().getHttpContext().getDefaultPath();
private static void load() {
if (com.genexus.ApplicationContext.getInstance().isSpringBootApp())
loadRewriteFilesSB();
else
loadRewriteFiles();
}

private static void loadRewriteFilesSB() {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
Resource[] resources = resolver.getResources("./*.rewrite");
for (Resource resource : resources) {
loadRewriteInputStream(resource.getInputStream());
}
}
catch (IOException e)
{
logger.error(e.toString(), e);
}
}

private static void loadRewriteFiles() {
String appPackage = SpecificImplementation.Application.getClientPreferences().getPACKAGE();
if (!appPackage.equals(""))
if (!appPackage.equals(""))
appPackage = File.separatorChar + appPackage.replace('.', File.separatorChar);
InputStream is;
String defaultPath = SpecificImplementation.Application.getModelContext().getHttpContext().getDefaultPath();
String classesDirectoryPath = defaultPath + File.separator + "WEB-INF" + File.separatorChar + "classes" + appPackage;
GXDirectory classesDirectory = new GXDirectory(classesDirectoryPath);
GXFileCollection rewriteFiles = classesDirectory.getFiles(".rewrite");
Expand All @@ -137,37 +158,39 @@ private static void load()
{
serverRelative = true;
AbstractGXFile rewriteFile = rewriteFiles.item(i);
try
{
is = SpecificImplementation.Messages.getInputStream(rewriteFile.getName());
is = SpecificImplementation.Messages.getInputStream(rewriteFile.getName());

if (is != null)
{
try (BufferedReader bufread = new BufferedReader(new InputStreamReader(is, "UTF8"))) {
line = bufread.readLine();
while (line != null) {
parseLine(line);
line = bufread.readLine();
}
}
}
}
catch (UnsupportedEncodingException e)
if (is != null)
{
logger.error(e.toString(), e);
}
catch (FileNotFoundException e)
{
logger.info("There is no URLRouter file");
}
catch (IOException e)
{
logger.error(e.toString(), e);
loadRewriteInputStream(is);
}
}
}
}

private static void loadRewriteInputStream(InputStream is) {
String line;
try (BufferedReader bufread = new BufferedReader(new InputStreamReader(is, "UTF8"))) {
line = bufread.readLine();
while (line != null) {
parseLine(line);
line = bufread.readLine();
}
}
catch (UnsupportedEncodingException e)
{
logger.error(e.toString(), e);
}
catch (FileNotFoundException e)
{
logger.info("There is no URLRouter file");
}
catch (IOException e)
{
logger.error(e.toString(), e);
}
}

private static void parseLine(String line)
{
int len = line.length();
Expand Down
7 changes: 6 additions & 1 deletion gxspringboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.2</version>
<scope>provided</scope>
</dependency>
</dependency>
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>5.1.3</version>
</dependency>
</dependencies>

<build>
Expand Down
20 changes: 20 additions & 0 deletions gxspringboot/src/main/java/com/genexus/springboot/GXConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
import com.genexus.common.interfaces.SpecificImplementation;
import com.genexus.diagnostics.core.ILogger;
import com.genexus.diagnostics.core.LogManager;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
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;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;

@Configuration
@EnableWebMvc
public class GXConfig implements WebMvcConfigurer {
public static final ILogger logger = LogManager.getLogger(GXConfig.class);
private static final String REWRITE_FILE = "rewrite.config";

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
Expand Down Expand Up @@ -45,4 +50,19 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
logger.error("Error setting context folders ", e);
}
}

@Bean
public FilterRegistrationBean<UrlRewriteFilter> urlRewriteFilter() {
FilterRegistrationBean<UrlRewriteFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new UrlRewriteFilter());
registrationBean.addUrlPatterns("/*");
if (new ClassPathResource(REWRITE_FILE).exists()) {
registrationBean.addInitParameter("modRewriteConf", "true");
registrationBean.addInitParameter("confPath", REWRITE_FILE);
}
else {
registrationBean.setEnabled(false);
}
return registrationBean;
}
}
Loading