From 6a27cade9a27b2bc5f4f5aaedffe4ca817d7a382 Mon Sep 17 00:00:00 2001 From: Erwin Bolwidt Date: Tue, 30 Oct 2018 16:43:10 +1100 Subject: [PATCH] Added inputStream method to ITemplateResources to allow leading of binary resources (images, etc) from templates folder by extensions --- .../SpringResourceTemplateResource.java | 13 +++++++++++-- .../SpringResourceTemplateResource.java | 12 +++++++++++- .../SpringResourceTemplateResource.java | 13 +++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/thymeleaf-spring3/src/main/java/org/thymeleaf/spring3/templateresource/SpringResourceTemplateResource.java b/thymeleaf-spring3/src/main/java/org/thymeleaf/spring3/templateresource/SpringResourceTemplateResource.java index 24b27875..f8550a58 100644 --- a/thymeleaf-spring3/src/main/java/org/thymeleaf/spring3/templateresource/SpringResourceTemplateResource.java +++ b/thymeleaf-spring3/src/main/java/org/thymeleaf/spring3/templateresource/SpringResourceTemplateResource.java @@ -99,8 +99,7 @@ public boolean exists() { public Reader reader() throws IOException { - // Will never return null, but an IOException if not found - final InputStream inputStream = this.resource.getInputStream(); + final InputStream inputStream = inputStream(); if (!StringUtils.isEmptyOrWhitespace(this.characterEncoding)) { return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream), this.characterEncoding)); @@ -110,6 +109,11 @@ public Reader reader() throws IOException { } + public InputStream inputStream() throws IOException { + // Will never return null, but an IOException if not found + return this.resource.getInputStream(); + } + public ITemplateResource relative(final String relativeLocation) { final Resource relativeResource; try { @@ -195,6 +199,11 @@ public Reader reader() throws IOException { throw new IOException("Invalid relative resource", this.ioException); } + @Override + public InputStream inputStream() throws IOException { + throw new IOException("Invalid relative resource", this.ioException); + } + @Override public ITemplateResource relative(final String relativeLocation) { return this; diff --git a/thymeleaf-spring4/src/main/java/org/thymeleaf/spring4/templateresource/SpringResourceTemplateResource.java b/thymeleaf-spring4/src/main/java/org/thymeleaf/spring4/templateresource/SpringResourceTemplateResource.java index 445890fb..6df91dc6 100644 --- a/thymeleaf-spring4/src/main/java/org/thymeleaf/spring4/templateresource/SpringResourceTemplateResource.java +++ b/thymeleaf-spring4/src/main/java/org/thymeleaf/spring4/templateresource/SpringResourceTemplateResource.java @@ -100,7 +100,7 @@ public boolean exists() { public Reader reader() throws IOException { // Will never return null, but an IOException if not found - final InputStream inputStream = this.resource.getInputStream(); + final InputStream inputStream = inputStream(); if (!StringUtils.isEmptyOrWhitespace(this.characterEncoding)) { return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream), this.characterEncoding)); @@ -110,6 +110,11 @@ public Reader reader() throws IOException { } + public InputStream inputStream() throws IOException { + // Will never return null, but an IOException if not found + return this.resource.getInputStream(); + } + public ITemplateResource relative(final String relativeLocation) { final Resource relativeResource; try { @@ -195,6 +200,11 @@ public Reader reader() throws IOException { throw new IOException("Invalid relative resource", this.ioException); } + @Override + public InputStream inputStream() throws IOException { + throw new IOException("Invalid relative resource", this.ioException); + } + @Override public ITemplateResource relative(final String relativeLocation) { return this; diff --git a/thymeleaf-spring5/src/main/java/org/thymeleaf/spring5/templateresource/SpringResourceTemplateResource.java b/thymeleaf-spring5/src/main/java/org/thymeleaf/spring5/templateresource/SpringResourceTemplateResource.java index 8c7fe617..f9f73a3d 100644 --- a/thymeleaf-spring5/src/main/java/org/thymeleaf/spring5/templateresource/SpringResourceTemplateResource.java +++ b/thymeleaf-spring5/src/main/java/org/thymeleaf/spring5/templateresource/SpringResourceTemplateResource.java @@ -99,8 +99,7 @@ public boolean exists() { public Reader reader() throws IOException { - // Will never return null, but an IOException if not found - final InputStream inputStream = this.resource.getInputStream(); + final InputStream inputStream = inputStream(); if (!StringUtils.isEmptyOrWhitespace(this.characterEncoding)) { return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream), this.characterEncoding)); @@ -110,6 +109,11 @@ public Reader reader() throws IOException { } + public InputStream inputStream() throws IOException { + // Will never return null, but an IOException if not found + return this.resource.getInputStream(); + } + public ITemplateResource relative(final String relativeLocation) { final Resource relativeResource; try { @@ -195,6 +199,11 @@ public Reader reader() throws IOException { throw new IOException("Invalid relative resource", this.ioException); } + @Override + public InputStream inputStream() throws IOException { + throw new IOException("Invalid relative resource", this.ioException); + } + @Override public ITemplateResource relative(final String relativeLocation) { return this;