|
| 1 | +package org.georchestra.gateway.app; |
| 2 | + |
| 3 | +import org.springframework.core.env.Environment; |
| 4 | +import org.springframework.core.io.ClassPathResource; |
| 5 | +import org.springframework.core.io.FileSystemResource; |
| 6 | +import org.springframework.core.io.Resource; |
| 7 | +import org.springframework.util.StringUtils; |
| 8 | + |
| 9 | +/** |
| 10 | + * Utility methods for resolving and checking static resources, and for |
| 11 | + * computing the web prefix used to serve static content. |
| 12 | + */ |
| 13 | +public class StaticResourcesUtils { |
| 14 | + /** |
| 15 | + * Resolves a resource path into a Spring {@link Resource} instance. |
| 16 | + * <p> |
| 17 | + * Supported prefixes: |
| 18 | + * <ul> |
| 19 | + * <li><b>classpath:</b> resolved as a {@link ClassPathResource}</li> |
| 20 | + * <li><b>file:</b> resolved as a {@link FileSystemResource}</li> |
| 21 | + * <li>otherwise</li> |
| 22 | + * </ul> |
| 23 | + * If no prefix is provided, the path is interpreted as a filesystem path. |
| 24 | + * |
| 25 | + * @param path fully qualified resource path |
| 26 | + * @return the resolved {@link Resource} |
| 27 | + */ |
| 28 | + public static Resource resolveResource(String path) { |
| 29 | + if (path.startsWith("classpath:")) { |
| 30 | + return new ClassPathResource(path.substring(10)); |
| 31 | + } |
| 32 | + if (path.startsWith("file:")) { |
| 33 | + return new FileSystemResource(path.substring(5)); |
| 34 | + } |
| 35 | + return new FileSystemResource(path); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Checks whether a resource exists and is readable. |
| 40 | + * <p> |
| 41 | + * Resolution is delegated to {@link #resolveResource(String)}. |
| 42 | + * |
| 43 | + * @param path fully qualified resource path |
| 44 | + * @return {@code true} if the resource exists and can be read |
| 45 | + */ |
| 46 | + public static boolean resourceExists(String path) { |
| 47 | + return resolveResource(path).isReadable(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Computes the web prefix used for serving static resources, based on Spring |
| 52 | + * MVC or WebFlux configuration. |
| 53 | + * <p> |
| 54 | + * Reads: |
| 55 | + * <ul> |
| 56 | + * <li>{@code spring.webflux.static-path-pattern}</li> |
| 57 | + * <li>{@code spring.mvc.static-path-pattern}</li> |
| 58 | + * </ul> |
| 59 | + * If no pattern is defined, "/" is returned. The resulting prefix is normalized |
| 60 | + * to start and end with a slash, and the terminal "/**" pattern is removed if |
| 61 | + * present. |
| 62 | + * |
| 63 | + * @param environment the Spring {@link Environment} |
| 64 | + * @return the normalized static web prefix (for example "/static/") |
| 65 | + */ |
| 66 | + public static String computeStaticResourceWebPrefix(Environment environment) { |
| 67 | + String pattern = environment.getProperty("spring.webflux.static-path-pattern"); |
| 68 | + if (!StringUtils.hasText(pattern)) { |
| 69 | + pattern = environment.getProperty("spring.mvc.static-path-pattern"); |
| 70 | + } |
| 71 | + if (!StringUtils.hasText(pattern)) { |
| 72 | + return "/"; |
| 73 | + } |
| 74 | + pattern = pattern.trim(); |
| 75 | + if (!pattern.startsWith("/")) { |
| 76 | + pattern = "/" + pattern; |
| 77 | + } |
| 78 | + if (pattern.endsWith("/**")) { |
| 79 | + pattern = pattern.substring(0, pattern.length() - 2); |
| 80 | + } |
| 81 | + if (pattern.isEmpty() || "/".equals(pattern)) { |
| 82 | + return "/"; |
| 83 | + } |
| 84 | + if (!pattern.endsWith("/")) { |
| 85 | + pattern = pattern + "/"; |
| 86 | + } |
| 87 | + return pattern; |
| 88 | + } |
| 89 | +} |
0 commit comments