|
| 1 | +/* |
| 2 | + * Copyright [2019] [恒宇少年 - 于起宇] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.minbox.framework.api.boot.autoconfigure.logging.admin.ui; |
| 19 | + |
| 20 | +import org.minbox.framework.api.boot.plugin.logging.admin.ui.HomepageForwardingFilter; |
| 21 | +import org.minbox.framework.api.boot.plugin.logging.admin.ui.LoggingAdminUiEndpoint; |
| 22 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 23 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| 24 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 25 | +import org.springframework.context.ApplicationContext; |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| 29 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 30 | +import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; |
| 31 | +import org.thymeleaf.templatemode.TemplateMode; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.nio.charset.StandardCharsets; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +import static java.util.Arrays.asList; |
| 38 | + |
| 39 | +/** |
| 40 | + * ApiBoot Logging Admin Ui Configuration |
| 41 | + * |
| 42 | + * @author:恒宇少年 - 于起宇 |
| 43 | + * <p> |
| 44 | + * DateTime:2019-07-26 15:08 |
| 45 | + * Blog:http://blog.yuqiyu.com |
| 46 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 47 | + * Gitee:https://gitee.com/hengboy |
| 48 | + * GitHub:https://github.com/hengboy |
| 49 | + */ |
| 50 | +@Configuration |
| 51 | +@EnableConfigurationProperties(ApiBootLoggingAdminUiProperties.class) |
| 52 | +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) |
| 53 | +public class ApiBootLoggingAdminUiAutoConfiguration implements WebMvcConfigurer { |
| 54 | + /** |
| 55 | + * ApiBoot Logging Admin Ui Resource Handler Prefix |
| 56 | + */ |
| 57 | + private static final String RESOURCE_PREFIX = "/**"; |
| 58 | + /** |
| 59 | + * ApiBoot Logging Admin Template Html Suffix |
| 60 | + */ |
| 61 | + private static final String TEMPLATE_SUFFIX = ".html"; |
| 62 | + /** |
| 63 | + * Default Ui Router List |
| 64 | + */ |
| 65 | + private static final List<String> DEFAULT_UI_ROUTES = asList( |
| 66 | + "/about/**", |
| 67 | + "/applications/**", |
| 68 | + "/journal/**", |
| 69 | + "/wallboard/**" |
| 70 | + ); |
| 71 | + /** |
| 72 | + * Application Context |
| 73 | + */ |
| 74 | + private ApplicationContext applicationContext; |
| 75 | + /** |
| 76 | + * ApiBoot Logging Admin Ui Properties |
| 77 | + */ |
| 78 | + private ApiBootLoggingAdminUiProperties adminUiProperties; |
| 79 | + |
| 80 | + public ApiBootLoggingAdminUiAutoConfiguration(ApplicationContext applicationContext, ApiBootLoggingAdminUiProperties adminUiProperties) { |
| 81 | + this.applicationContext = applicationContext; |
| 82 | + this.adminUiProperties = adminUiProperties; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Configuration Resource Handler |
| 87 | + * add "api-boot-logging-admin-ui" resource path handler |
| 88 | + * |
| 89 | + * @param registry ResourceHandlerRegistry |
| 90 | + */ |
| 91 | + @Override |
| 92 | + public void addResourceHandlers(ResourceHandlerRegistry registry) { |
| 93 | + registry.addResourceHandler(RESOURCE_PREFIX).addResourceLocations(this.adminUiProperties.getResourceLocations()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Config thymeleaf Template Resolver |
| 98 | + * |
| 99 | + * @return SpringResourceTemplateResolver |
| 100 | + */ |
| 101 | + @Bean |
| 102 | + public SpringResourceTemplateResolver adminTemplateResolver() { |
| 103 | + SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); |
| 104 | + resolver.setApplicationContext(this.applicationContext); |
| 105 | + resolver.setPrefix(this.adminUiProperties.getTemplateLocation()); |
| 106 | + resolver.setSuffix(TEMPLATE_SUFFIX); |
| 107 | + resolver.setTemplateMode(TemplateMode.HTML); |
| 108 | + resolver.setCharacterEncoding(StandardCharsets.UTF_8.name()); |
| 109 | + resolver.setCacheable(this.adminUiProperties.isCacheTemplates()); |
| 110 | + resolver.setOrder(10); |
| 111 | + resolver.setCheckExistence(true); |
| 112 | + return resolver; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Home Page Forwarding Filter |
| 117 | + * |
| 118 | + * @return HomepageForwardingFilter |
| 119 | + * @throws IOException Io Exception |
| 120 | + */ |
| 121 | + @Bean |
| 122 | + @ConditionalOnMissingBean |
| 123 | + public HomepageForwardingFilter homepageForwardFilter() throws IOException { |
| 124 | + return new HomepageForwardingFilter("/", DEFAULT_UI_ROUTES); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Logging Admin Ui Endpoint |
| 129 | + * The ability to provide logging admin data to the public |
| 130 | + * |
| 131 | + * @return LoggingAdminUiEndpoint |
| 132 | + */ |
| 133 | + @Bean |
| 134 | + @ConditionalOnMissingBean |
| 135 | + public LoggingAdminUiEndpoint loggingAdminUiEndpoint() { |
| 136 | + return new LoggingAdminUiEndpoint( |
| 137 | + LoggingAdminUiEndpoint.Settings.builder() |
| 138 | + .brand(adminUiProperties.getBrand()) |
| 139 | + .title(adminUiProperties.getTitle()) |
| 140 | + .routes(DEFAULT_UI_ROUTES) |
| 141 | + .build() |
| 142 | + ); |
| 143 | + } |
| 144 | +} |
0 commit comments