|
3 | 3 | import static org.springframework.web.reactive.function.server.RequestPredicates.GET; |
4 | 4 | import static org.springframework.web.reactive.function.server.RequestPredicates.accept; |
5 | 5 |
|
6 | | -import com.fasterxml.jackson.core.type.TypeReference; |
7 | | -import com.fasterxml.jackson.databind.JsonNode; |
8 | 6 | import java.net.MalformedURLException; |
9 | | -import java.net.URI; |
10 | | -import java.net.URISyntaxException; |
11 | | -import java.util.List; |
12 | 7 | import lombok.AllArgsConstructor; |
13 | | -import lombok.Data; |
14 | | -import org.springframework.beans.factory.ObjectProvider; |
15 | 8 | import org.springframework.context.annotation.Bean; |
16 | 9 | import org.springframework.http.MediaType; |
17 | 10 | import org.springframework.stereotype.Component; |
18 | 11 | import org.springframework.web.reactive.function.server.RouterFunction; |
19 | 12 | import org.springframework.web.reactive.function.server.RouterFunctions; |
20 | 13 | import org.springframework.web.reactive.function.server.ServerResponse; |
21 | | -import reactor.core.publisher.Mono; |
| 14 | +import reactor.core.Exceptions; |
22 | 15 | import run.halo.app.infra.ExternalUrlSupplier; |
23 | | -import run.halo.app.infra.utils.JsonUtils; |
24 | | -import run.halo.app.plugin.SettingFetcher; |
25 | 16 |
|
26 | 17 | @Component |
27 | 18 | @AllArgsConstructor |
28 | 19 | public class SitemapPluginConfig { |
29 | | - private final ExternalUrlSupplier externalUrlSupplier; |
30 | 20 |
|
31 | | - @Bean |
32 | | - public SitemapGeneratorOptions sitemapGeneratorOptions() |
33 | | - throws MalformedURLException { |
34 | | - URI siteUri = externalUrlSupplier.get(); |
35 | | - return SitemapGeneratorOptions.builder() |
36 | | - .siteUrl(siteUri.toURL()) |
37 | | - .build(); |
38 | | - } |
| 21 | + private final ExternalUrlSupplier externalUrlSupplier; |
39 | 22 |
|
40 | 23 | @Bean |
41 | | - RouterFunction<ServerResponse> sitemapRouterFunction( |
42 | | - CachedSitemapGetter cachedSitemapGetter) { |
| 24 | + RouterFunction<ServerResponse> sitemapRouterFunction(CachedSitemapGetter cachedSitemapGetter) { |
43 | 25 | return RouterFunctions.route(GET("/sitemap.xml") |
44 | | - .and(accept(MediaType.TEXT_XML)), request -> cachedSitemapGetter.get() |
45 | | - .flatMap(sitemap -> ServerResponse.ok() |
46 | | - .contentType(MediaType.TEXT_XML).bodyValue(sitemap)) |
| 26 | + .and(accept(MediaType.TEXT_XML)), request -> { |
| 27 | + var uri = externalUrlSupplier.get(); |
| 28 | + if (!uri.isAbsolute()) { |
| 29 | + uri = request.exchange().getRequest().getURI().resolve(uri); |
| 30 | + } |
| 31 | + SitemapGeneratorOptions options; |
| 32 | + try { |
| 33 | + options = SitemapGeneratorOptions.builder() |
| 34 | + .siteUrl(uri.toURL()) |
| 35 | + .build(); |
| 36 | + } catch (MalformedURLException e) { |
| 37 | + throw Exceptions.propagate(e); |
| 38 | + } |
| 39 | + return cachedSitemapGetter.get(options) |
| 40 | + .flatMap(sitemap -> ServerResponse.ok() |
| 41 | + .contentType(MediaType.TEXT_XML).bodyValue(sitemap)); |
| 42 | + } |
47 | 43 | ); |
48 | 44 | } |
49 | 45 | } |
0 commit comments