From 05d06bdb3dc3ca558c64d3da14575dc95a81af93 Mon Sep 17 00:00:00 2001 From: KingStar Date: Sun, 6 Mar 2022 13:08:31 +0000 Subject: [PATCH] update static web update static web --- .../com/gitee/starblues/utils/FilesUtils.java | 56 --------- .../com/gitee/starblues/utils/UrlUtils.java | 113 ++++++++++++++++++ .../PluginInterceptorRegistration.java | 8 +- .../web/PluginControllerProcessor.java | 2 +- .../starblues/core/DefaultPluginManager.java | 9 +- .../web/PluginStaticResourceConfig.java | 45 ++++++- .../web/PluginStaticResourceResolver.java | 86 ++++++++----- .../PluginStaticResourceWebMvcConfigurer.java | 2 +- .../starblues/utils/PluginConfigUtils.java | 2 +- 9 files changed, 228 insertions(+), 95 deletions(-) create mode 100644 spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/UrlUtils.java diff --git a/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java b/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java index 477935f..8f7bfa0 100644 --- a/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java +++ b/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java @@ -45,62 +45,6 @@ public class FilesUtils { } - /** - * rest接口拼接路径 - * - * @param path1 路径1 - * @param path2 路径2 - * @return 拼接的路径 - */ - public static String restJoiningPath(String path1, String path2){ - if(path1 != null && path2 != null){ - if(path1.endsWith("/") && path2.startsWith("/")){ - return path1 + path2.substring(1); - } else if(!path1.endsWith("/") && !path2.startsWith("/")){ - return path1 + "/" + path2; - } else { - return path1 + path2; - } - } else if(path1 != null){ - return path1; - } else if(path2 != null){ - return path2; - } else { - return ""; - } - } - - - /** - * 拼接url路径 - * - * @param paths 拼接的路径 - * @return 拼接的路径 - */ - public static String joiningUrlPath(String ...paths){ - if(paths == null || paths.length == 0){ - return ""; - } - StringBuilder stringBuilder = new StringBuilder(); - int length = paths.length; - for (int i = 0; i < length; i++) { - String path = paths[i]; - if(ObjectUtils.isEmpty(path)) { - continue; - } - if((i < length - 1) && path.endsWith("/")){ - path = path.substring(path.lastIndexOf("/")); - } - if(path.startsWith("/")){ - stringBuilder.append(path); - } else { - stringBuilder.append("/").append(path); - } - } - - return stringBuilder.toString(); - } - /** * 拼接file路径 * diff --git a/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/UrlUtils.java b/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/UrlUtils.java new file mode 100644 index 0000000..90b80db --- /dev/null +++ b/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/UrlUtils.java @@ -0,0 +1,113 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.utils; + +/** + * http url util + * + * @author starBlues + * @version 3.0.0 + */ +public class UrlUtils { + + private UrlUtils(){} + + public static final String SPLIT = "/"; + + /** + * rest接口拼接路径 + * + * @param path1 路径1 + * @param path2 路径2 + * @return 拼接的路径 + */ + public static String restJoiningPath(String path1, String path2){ + if(path1 != null && path2 != null){ + if(path1.endsWith(SPLIT) && path2.startsWith(SPLIT)){ + return path1 + path2.substring(1); + } else if(!path1.endsWith(SPLIT) && !path2.startsWith(SPLIT)){ + return path1 + SPLIT + path2; + } else { + return path1 + path2; + } + } else if(path1 != null){ + return path1; + } else if(path2 != null){ + return path2; + } else { + return ""; + } + } + + + /** + * 拼接url路径 + * + * @param paths 拼接的路径 + * @return 拼接的路径 + */ + public static String joiningUrlPath(String ...paths){ + if(paths == null || paths.length == 0){ + return ""; + } + StringBuilder stringBuilder = new StringBuilder(); + int length = paths.length; + for (int i = 0; i < length; i++) { + String path = paths[i]; + if(ObjectUtils.isEmpty(path)) { + continue; + } + if((i < length - 1) && path.endsWith(SPLIT)){ + path = path.substring(path.lastIndexOf(SPLIT)); + } + if(path.startsWith(SPLIT)){ + stringBuilder.append(path); + } else { + stringBuilder.append(SPLIT).append(path); + } + } + + return stringBuilder.toString(); + } + + /** + * 格式化 url + * @param url 原始url + * @return 格式化后的url + */ + public static String format(String url){ + if(ObjectUtils.isEmpty(url)){ + return url; + } + String[] split = url.split(SPLIT); + StringBuilder stringBuilder = new StringBuilder(); + int length = split.length; + for (int i = 0; i < length; i++) { + String str = split[i]; + if(ObjectUtils.isEmpty(str)){ + continue; + } + if(i < length - 1){ + stringBuilder.append(str).append(SPLIT); + } else { + stringBuilder.append(str); + } + } + return stringBuilder.toString(); + } + +} diff --git a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/interceptor/PluginInterceptorRegistration.java b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/interceptor/PluginInterceptorRegistration.java index 9c098d1..668ab01 100644 --- a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/interceptor/PluginInterceptorRegistration.java +++ b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/interceptor/PluginInterceptorRegistration.java @@ -16,8 +16,8 @@ package com.gitee.starblues.bootstrap.processor.interceptor; -import com.gitee.starblues.utils.FilesUtils; import com.gitee.starblues.utils.ObjectUtils; +import com.gitee.starblues.utils.UrlUtils; import org.springframework.lang.Nullable; import org.springframework.util.PathMatcher; import org.springframework.web.servlet.HandlerInterceptor; @@ -87,7 +87,7 @@ public class PluginInterceptorRegistration { if(ObjectUtils.isEmpty(pattern)){ continue; } - this.includePatterns.add(FilesUtils.joiningUrlPath(pluginRestApiPrefix, pattern)); + this.includePatterns.add(UrlUtils.joiningUrlPath(pluginRestApiPrefix, pattern)); } return this; } @@ -105,7 +105,7 @@ public class PluginInterceptorRegistration { if(ObjectUtils.isEmpty(pattern)){ continue; } - this.excludePatterns.add(FilesUtils.joiningUrlPath(pluginRestApiPrefix, pattern)); + this.excludePatterns.add(UrlUtils.joiningUrlPath(pluginRestApiPrefix, pattern)); } return this; } @@ -153,7 +153,7 @@ public class PluginInterceptorRegistration { protected Object getInterceptor() { if(type == PluginInterceptorRegistry.Type.PLUGIN){ if(this.includePatterns.isEmpty()){ - this.includePatterns.add(FilesUtils.joiningUrlPath(pluginRestApiPrefix, "/**")); + this.includePatterns.add(UrlUtils.joiningUrlPath(pluginRestApiPrefix, "/**")); } } if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) { diff --git a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginControllerProcessor.java b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginControllerProcessor.java index 4d16cdc..a097e64 100644 --- a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginControllerProcessor.java +++ b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginControllerProcessor.java @@ -241,7 +241,7 @@ public class PluginControllerProcessor implements SpringPluginProcessor { if(definePath.contains(pathPrefix)){ newPath[i++] = definePath; } else { - newPath[i++] = FilesUtils.restJoiningPath(pathPrefix, definePath); + newPath[i++] = UrlUtils.restJoiningPath(pathPrefix, definePath); } } if(newPath.length == 0){ diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java index 4d405bd..580378e 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java @@ -126,12 +126,17 @@ public class DefaultPluginManager implements PluginManager{ } List scanPluginPaths = provider.getPluginScanner().scan(pluginRootDirs); if(ObjectUtils.isEmpty(scanPluginPaths)){ - StringBuilder warn = new StringBuilder("\n\n路径 {} 中未发现插件.\n"); + StringBuilder warn = new StringBuilder("以下路径未发现插件: \n"); + for (int i = 0; i < pluginRootDirs.size(); i++) { + warn.append(i + 1).append(". ").append(pluginRootDirs.get(i)).append("\n"); + } warn.append("请检查路径是否合适.\n"); if(provider.getRuntimeMode() == RuntimeMode.DEV){ warn.append("请检查插件包是否编译.\n"); + } else { + warn.append("请检查插件是否合法.\n"); } - log.warn(warn.toString(), pluginRootDirs); + log.warn(warn.toString()); return Collections.emptyList(); } pluginListenerFactory = createPluginListenerFactory(); diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceConfig.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceConfig.java index 6cb835d..984e6a0 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceConfig.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceConfig.java @@ -16,6 +16,8 @@ package com.gitee.starblues.spring.web; +import com.gitee.starblues.utils.Assert; +import com.gitee.starblues.utils.UrlUtils; import lombok.Data; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,16 +28,57 @@ import org.springframework.http.CacheControl; * @author starBlues * @version 3.0.0 */ -@Data public class PluginStaticResourceConfig { public static final String DEFAULT_PLUGIN_STATIC_RESOURCE_PATH_PREFIX = "static-plugin"; + public static final String DEFAULT_INDEX_PAGE_NAME = "index.html"; private static final Logger log = LoggerFactory.getLogger(PluginStaticResourceConfig.class); + /** + * 插件静态资源访问前缀 + */ private String pathPrefix = DEFAULT_PLUGIN_STATIC_RESOURCE_PATH_PREFIX; + + /** + * 默认首页页面名称 + */ + private String indexPageName =DEFAULT_INDEX_PAGE_NAME; + + /** + * 页面是否缓存 + */ private CacheControl cacheControl = CacheControl.noCache(); + public void logPathPrefix(){ log.info("插件静态资源访问前缀配置为: /{}/{pluginId}", pathPrefix); } + + + public String getPathPrefix() { + return pathPrefix; + } + + public void setPathPrefix(String pathPrefix) { + Assert.isNotEmpty(pathPrefix, "配置 pathPrefix 不能为空"); + this.pathPrefix = UrlUtils.format(pathPrefix); + } + + public String getIndexPageName() { + return indexPageName; + } + + public void setIndexPageName(String indexPageName) { + Assert.isNotEmpty(pathPrefix, "配置 indexPageName 不能为空"); + this.indexPageName = indexPageName; + } + + public CacheControl getCacheControl() { + return cacheControl; + } + + public void setCacheControl(CacheControl cacheControl) { + Assert.isNotNull(pathPrefix, "配置 cacheControl 不能为空"); + this.cacheControl = cacheControl; + } } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceResolver.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceResolver.java index b4aed6a..58e0e85 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceResolver.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceResolver.java @@ -20,6 +20,7 @@ import com.gitee.starblues.core.descriptor.PluginDescriptor; import com.gitee.starblues.spring.WebConfig; import com.gitee.starblues.utils.MsgUtils; import com.gitee.starblues.utils.ObjectUtils; +import com.gitee.starblues.utils.UrlUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.FileUrlResource; @@ -51,7 +52,10 @@ public class PluginStaticResourceResolver extends AbstractResourceResolver { private final static Map PLUGIN_RESOURCE_MAP = new ConcurrentHashMap<>(); - public PluginStaticResourceResolver() { + private final PluginStaticResourceConfig config; + + public PluginStaticResourceResolver(PluginStaticResourceConfig config) { + this.config = config; } @@ -59,42 +63,66 @@ public class PluginStaticResourceResolver extends AbstractResourceResolver { protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath, List locations, ResourceResolverChain chain) { + if(request != null){ + String requestUri = request.getRequestURI(); + String formatUri = UrlUtils.format(requestUri); + requestPath = UrlUtils.format(formatUri.replace(config.getPathPrefix(), "")); + } + int startOffset = requestPath.indexOf("/"); + String pluginId = null; + String partialPath = null; + if (startOffset == -1) { + pluginId = requestPath; + partialPath = config.getIndexPageName(); + } else { + pluginId = requestPath.substring(0, startOffset); + partialPath = requestPath.substring(startOffset + 1); + } - int startOffset = (requestPath.startsWith("/") ? 1 : 0); - int endOffset = requestPath.indexOf('/', 1); - if (endOffset != -1) { - String pluginId = requestPath.substring(startOffset, endOffset); - String partialPath = requestPath.substring(endOffset + 1); - PluginStaticResource pluginResource = PLUGIN_RESOURCE_MAP.get(pluginId); + PluginStaticResource pluginResource = PLUGIN_RESOURCE_MAP.get(pluginId); - if(pluginResource == null){ - return null; - } + if(pluginResource == null){ + return chain.resolveResource(request, requestPath, locations); + } - String key = computeKey(request, requestPath); - // 先判断缓存中是否存在。 - Resource resource = pluginResource.getCacheResource(key); - if(resource != null){ - return resource; + String key = computeKey(request, requestPath); + // 先判断缓存中是否存在。 + Resource resource = pluginResource.getCacheResource(key); + if(resource != null){ + return resource; + } + resource = findResource(pluginResource, partialPath); + if(resource != null){ + pluginResource.putCacheResource(key, resource); + return resource; + } else { + // 尝试获取首页页面 + String indexPageName = config.getIndexPageName(); + if(ObjectUtils.isEmpty(indexPageName)){ + indexPageName = PluginStaticResourceConfig.DEFAULT_INDEX_PAGE_NAME; } - - // 从classpath 获取资源 - resource = resolveClassPath(pluginResource, partialPath); - if(resource != null){ - pluginResource.putCacheResource(key, resource); - return resource; + if(partialPath.lastIndexOf(".") > -1){ + // 存在后缀 + return null; } + partialPath = UrlUtils.joiningUrlPath(partialPath, indexPageName); + return findResource(pluginResource, partialPath); + } + } - // 从外置文件路径获取资源 - resource = resolveFilePath(pluginResource, partialPath); - if(resource != null){ - pluginResource.putCacheResource(key, resource); - return resource; - } - return null; + private Resource findResource(PluginStaticResource pluginResource, String partialPath){ + // 从classpath 获取资源 + Resource resource = resolveClassPath(pluginResource, partialPath); + if(resource != null){ + return resource; + } + // 从外置文件路径获取资源 + resource = resolveFilePath(pluginResource, partialPath); + if(resource != null){ + return resource; } - return chain.resolveResource(request, requestPath, locations); + return resource; } /** diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceWebMvcConfigurer.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceWebMvcConfigurer.java index dc725ac..984a198 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceWebMvcConfigurer.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceWebMvcConfigurer.java @@ -46,7 +46,7 @@ public class PluginStaticResourceWebMvcConfigurer implements WebMvcConfigurer { } resourceHandlerRegistration .resourceChain(false) - .addResolver(new PluginStaticResourceResolver()); + .addResolver(new PluginStaticResourceResolver(resourceConfig)); } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginConfigUtils.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginConfigUtils.java index 22bc4a5..961ce09 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginConfigUtils.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginConfigUtils.java @@ -98,7 +98,7 @@ public class PluginConfigUtils { String pathPrefix = configuration.pluginRestPathPrefix(); if(configuration.enablePluginIdRestPathPrefix()){ if(pathPrefix != null && !"".equals(pathPrefix)){ - pathPrefix = FilesUtils.restJoiningPath(pathPrefix, pluginId); + pathPrefix = UrlUtils.restJoiningPath(pathPrefix, pluginId); } else { pathPrefix = pluginId; } -- Gitee