diff --git a/src/main/java/stirling/software/SPDF/config/FileFallbackTemplateResolver.java b/src/main/java/stirling/software/SPDF/config/FileFallbackTemplateResolver.java index be6e28a7..5a29efee 100644 --- a/src/main/java/stirling/software/SPDF/config/FileFallbackTemplateResolver.java +++ b/src/main/java/stirling/software/SPDF/config/FileFallbackTemplateResolver.java @@ -1,6 +1,7 @@ package stirling.software.SPDF.config; import java.io.IOException; +import java.io.InputStream; import java.util.Map; import org.springframework.core.io.Resource; @@ -11,6 +12,8 @@ import org.thymeleaf.templateresource.ClassLoaderTemplateResource; import org.thymeleaf.templateresource.FileTemplateResource; import org.thymeleaf.templateresource.ITemplateResource; +import stirling.software.SPDF.model.InputStreamTemplateResource; + public class FileFallbackTemplateResolver extends AbstractConfigurableTemplateResolver { private final ResourceLoader resourceLoader; @@ -40,9 +43,10 @@ public class FileFallbackTemplateResolver extends AbstractConfigurableTemplateRe } - return new ClassLoaderTemplateResource( - Thread.currentThread().getContextClassLoader(), - "classpath:/templates/" + resourceName, - characterEncoding); + InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("templates/" + resourceName); + if (inputStream != null) { + return new InputStreamTemplateResource(inputStream, "UTF-8"); + } + return null; } } diff --git a/src/main/java/stirling/software/SPDF/model/InputStreamTemplateResource.java b/src/main/java/stirling/software/SPDF/model/InputStreamTemplateResource.java new file mode 100644 index 00000000..b48eb138 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/model/InputStreamTemplateResource.java @@ -0,0 +1,45 @@ +package stirling.software.SPDF.model; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; + +import org.thymeleaf.templateresource.ITemplateResource; + +public class InputStreamTemplateResource implements ITemplateResource { + private InputStream inputStream; + private String characterEncoding; + + public InputStreamTemplateResource(InputStream inputStream, String characterEncoding) { + this.inputStream = inputStream; + this.characterEncoding = characterEncoding; + } + + @Override + public Reader reader() throws IOException { + return new InputStreamReader(inputStream, characterEncoding); + } + + @Override + public ITemplateResource relative(String relativeLocation) { + // Implement logic for relative resources, if needed + throw new UnsupportedOperationException("Relative resources not supported"); + } + + @Override + public String getDescription() { + return "InputStream resource [Stream]"; + } + + @Override + public String getBaseName() { + return "streamResource"; + } + + @Override + public boolean exists() { + // TODO Auto-generated method stub + return false; + } +}