From e33691d1b25a87f89cb1800e3c2ed70deda9a9b3 Mon Sep 17 00:00:00 2001 From: Phil Medcraft Date: Fri, 8 Mar 2024 14:38:08 +0000 Subject: [PATCH] Improving this code to overcome a serialization problem for customers running the DXA as a SpringBoot application. --- .../content/GenericStaticContentResolver.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/dxa-framework/dxa-tridion-common/src/main/java/com/sdl/dxa/tridion/content/GenericStaticContentResolver.java b/dxa-framework/dxa-tridion-common/src/main/java/com/sdl/dxa/tridion/content/GenericStaticContentResolver.java index 2fd716b73..65e278c49 100644 --- a/dxa-framework/dxa-tridion-common/src/main/java/com/sdl/dxa/tridion/content/GenericStaticContentResolver.java +++ b/dxa-framework/dxa-tridion-common/src/main/java/com/sdl/dxa/tridion/content/GenericStaticContentResolver.java @@ -13,6 +13,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.regex.Pattern; @@ -72,12 +73,27 @@ String getRealPath() { @NotNull private StaticContentItem getStaticContentFileByPath(String path, StaticContentRequestDto requestDto) throws ContentProviderException { - String parentPath = Paths.get(getPublicationPath(requestDto.getLocalizationId())).normalize().toString(); - String normalized_path = Paths.get(path).normalize().toString(); + final Path parentPath = Paths.get(getPublicationPath(requestDto.getLocalizationId())).normalize(); + final Path parentRelativePath = parentPath.subpath(0, parentPath.getNameCount() - 1); + final Path normalized_path = Paths.get(path).normalize(); + + final File file = getFile(parentPath, normalized_path, parentRelativePath); + log.trace("getStaticContentFileByPath: {}", file.getAbsolutePath()); + + final ImageUtils.StaticContentPathInfo pathInfo = new ImageUtils.StaticContentPathInfo(path); + int publicationId = Integer.parseInt(requestDto.getLocalizationId()); + String urlPath = prependFullUrlIfNeeded(pathInfo.getFileName(), requestDto.getBaseUrl()); + return createStaticContentItem(requestDto, file, publicationId, pathInfo, urlPath); + } + + @NotNull + private static File getFile(Path parentPath, Path normalized_path, Path parentRelativePath) throws ContentProviderException { + final File file = new File(parentPath.toString(), normalized_path.toString()); + final Path filePath = file.toPath(); + final Path fileRelativePath = filePath.subpath(0, filePath.getNameCount()-1); - final File file = new File(parentPath, normalized_path); try { - if (!file.getCanonicalPath().startsWith(parentPath)) { + if (!fileRelativePath.startsWith(parentRelativePath)) { throw new ContentProviderException("The path to the static file not starting with the expected " + "parent path. [" + file.getCanonicalPath() + "]"); } @@ -85,12 +101,7 @@ private StaticContentItem getStaticContentFileByPath(String path, StaticContentR catch(IOException ioException) { throw new ContentProviderException(ioException); } - log.trace("getStaticContentFileByPath: {}", file.getAbsolutePath()); - - final ImageUtils.StaticContentPathInfo pathInfo = new ImageUtils.StaticContentPathInfo(path); - int publicationId = Integer.parseInt(requestDto.getLocalizationId()); - String urlPath = prependFullUrlIfNeeded(pathInfo.getFileName(), requestDto.getBaseUrl()); - return createStaticContentItem(requestDto, file, publicationId, pathInfo, urlPath); + return file; } protected String prependFullUrlIfNeeded(String path, String baseUrl) {