Skip to content

Commit 0056f58

Browse files
authored
Adapt to external URI that might not a URL (#6)
1 parent 35ad4d4 commit 0056f58

File tree

9 files changed

+66
-72
lines changed

9 files changed

+66
-72
lines changed

build.gradle

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
plugins {
2-
id "io.github.guqing.plugin-development" version "0.0.5-SNAPSHOT"
2+
id "io.freefair.lombok" version "8.0.0-rc2"
3+
id "io.github.guqing.plugin-development" version "0.0.7-SNAPSHOT"
34
id 'java'
45
}
56

67
group 'run.halo.sitemap'
78
sourceCompatibility = JavaVersion.VERSION_17
89

910
repositories {
11+
mavenCentral()
1012
maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots' }
1113
maven { url 'https://repo.spring.io/milestone' }
12-
mavenCentral()
1314
}
1415

1516
jar {
@@ -18,27 +19,14 @@ jar {
1819
manifest.attributes(
1920
'Plugin-Version': "${project.version}",
2021
)
21-
from {
22-
configurations.runtimeClasspath.collect {
23-
it.isDirectory() ? it : zipTree(it)
24-
}
25-
}
2622
}
2723

2824
dependencies {
29-
compileOnly platform('run.halo.dependencies:halo-dependencies:1.0.0')
30-
31-
compileOnly files("lib/halo-2.0.0-plain.jar")
32-
33-
compileOnly 'org.projectlombok:lombok'
34-
annotationProcessor 'org.projectlombok:lombok:1.18.22'
25+
implementation platform('run.halo.tools.platform:plugin:2.5.0-SNAPSHOT')
26+
compileOnly 'run.halo.app:api'
3527

36-
testImplementation "com.google.guava:guava"
37-
testImplementation 'org.apache.commons:commons-lang3'
38-
testImplementation 'org.springframework.boot:spring-boot-starter-webflux'
28+
testImplementation 'run.halo.app:api'
3929
testImplementation 'org.springframework.boot:spring-boot-starter-test'
40-
testImplementation 'io.projectreactor:reactor-test'
41-
testImplementation files("lib/halo-2.0.0-plain.jar")
4230
}
4331

4432
test {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

lib/halo-2.0.0-plain.jar

-2.96 MB
Binary file not shown.

src/main/java/run/halo/sitemap/CachedSitemapGetter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
@AllArgsConstructor
1414
public class CachedSitemapGetter {
1515

16-
private final Cache<String, String> cache = CacheBuilder.newBuilder()
16+
private final Cache<SitemapGeneratorOptions, String> cache = CacheBuilder.newBuilder()
1717
.concurrencyLevel(Runtime.getRuntime().availableProcessors())
1818
.initialCapacity(8)
1919
.maximumSize(8)
@@ -22,14 +22,13 @@ public class CachedSitemapGetter {
2222

2323
private final DefaultSitemapEntryLister lister;
2424

25-
public Mono<String> get() {
26-
String cacheKey = "sitemap";
27-
return Mono.fromCallable(() -> cache.get(cacheKey, () -> lister.list()
25+
public Mono<String> get(SitemapGeneratorOptions options) {
26+
return Mono.fromCallable(() -> cache.get(options, () -> lister.list(options)
2827
.collectList()
2928
.map(entries -> {
3029
String xml = new SitemapBuilder()
3130
.buildSitemapXml(entries);
32-
cache.put(cacheKey, xml);
31+
cache.put(options, xml);
3332
return xml;
3433
})
3534
.defaultIfEmpty(StringUtils.EMPTY)

src/main/java/run/halo/sitemap/DefaultSitemapEntryLister.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
@Component
2323
@AllArgsConstructor
2424
public class DefaultSitemapEntryLister implements SitemapEntryLister {
25+
2526
private final ReactiveExtensionClient client;
26-
private final SitemapGeneratorOptions options;
2727

2828
@Override
29-
public Flux<SitemapEntry> list() {
30-
return Flux.mergeSequential(listPostUrls(), listCategoryUrls(), listTagUrls(),
31-
listSinglePageUrls())
32-
.concatWith(urlsForListPages())
29+
public Flux<SitemapEntry> list(SitemapGeneratorOptions options) {
30+
return Flux.mergeSequential(listPostUrls(),
31+
listCategoryUrls(),
32+
listTagUrls(),
33+
listSinglePageUrls(),
34+
urlsForListPages())
3335
.distinct()
3436
.map(options::transform);
3537
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package run.halo.sitemap;
22

3-
import java.util.List;
43
import reactor.core.publisher.Flux;
54

65
public interface SitemapEntryLister {
7-
Flux<SitemapEntry> list();
6+
Flux<SitemapEntry> list(SitemapGeneratorOptions options);
87
}

src/main/java/run/halo/sitemap/SitemapPlugin.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import org.pf4j.PluginWrapper;
44
import org.springframework.stereotype.Component;
5-
import run.halo.app.infra.ExternalUrlSupplier;
65
import run.halo.app.plugin.BasePlugin;
7-
import run.halo.app.plugin.HaloPluginManager;
8-
import run.halo.app.plugin.PluginApplicationContext;
96

107
/**
118
* @author ryanwang
@@ -14,7 +11,7 @@
1411
@Component
1512
public class SitemapPlugin extends BasePlugin {
1613

17-
public SitemapPlugin(PluginWrapper wrapper, PluginApplicationContext context) {
14+
public SitemapPlugin(PluginWrapper wrapper) {
1815
super(wrapper);
1916
}
2017

src/main/java/run/halo/sitemap/SitemapPluginConfig.java

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,43 @@
33
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
44
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
55

6-
import com.fasterxml.jackson.core.type.TypeReference;
7-
import com.fasterxml.jackson.databind.JsonNode;
86
import java.net.MalformedURLException;
9-
import java.net.URI;
10-
import java.net.URISyntaxException;
11-
import java.util.List;
127
import lombok.AllArgsConstructor;
13-
import lombok.Data;
14-
import org.springframework.beans.factory.ObjectProvider;
158
import org.springframework.context.annotation.Bean;
169
import org.springframework.http.MediaType;
1710
import org.springframework.stereotype.Component;
1811
import org.springframework.web.reactive.function.server.RouterFunction;
1912
import org.springframework.web.reactive.function.server.RouterFunctions;
2013
import org.springframework.web.reactive.function.server.ServerResponse;
21-
import reactor.core.publisher.Mono;
14+
import reactor.core.Exceptions;
2215
import run.halo.app.infra.ExternalUrlSupplier;
23-
import run.halo.app.infra.utils.JsonUtils;
24-
import run.halo.app.plugin.SettingFetcher;
2516

2617
@Component
2718
@AllArgsConstructor
2819
public class SitemapPluginConfig {
29-
private final ExternalUrlSupplier externalUrlSupplier;
3020

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;
3922

4023
@Bean
41-
RouterFunction<ServerResponse> sitemapRouterFunction(
42-
CachedSitemapGetter cachedSitemapGetter) {
24+
RouterFunction<ServerResponse> sitemapRouterFunction(CachedSitemapGetter cachedSitemapGetter) {
4325
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+
}
4743
);
4844
}
4945
}
Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package run.halo.sitemap;
22

3-
import static org.mockito.Mockito.times;
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.Mockito.mock;
45
import static org.mockito.Mockito.verify;
56
import static org.mockito.Mockito.when;
67

7-
import java.util.concurrent.CountDownLatch;
8+
import java.util.List;
9+
import java.util.concurrent.ExecutionException;
10+
import java.util.concurrent.Executors;
11+
import java.util.concurrent.Future;
12+
import java.util.stream.IntStream;
813
import org.junit.jupiter.api.BeforeEach;
914
import org.junit.jupiter.api.Test;
1015
import org.junit.jupiter.api.extension.ExtendWith;
@@ -20,22 +25,30 @@ public class CachedSitemapGetterTest {
2025

2126
@BeforeEach
2227
void setUp() {
23-
when(lister.list()).thenReturn(
28+
when(lister.list(any())).thenReturn(
2429
Flux.just(SitemapEntry.builder().loc("http://localhost:8090/about").build()));
2530
getter = new CachedSitemapGetter(lister);
2631
}
2732

2833
@Test
29-
void get() throws InterruptedException {
30-
CountDownLatch countDownLatch = new CountDownLatch(10);
31-
32-
for (int i = 0; i < countDownLatch.getCount(); i++) {
33-
new Thread(() -> {
34-
getter.get().block();
35-
countDownLatch.countDown();
36-
}).start();
34+
void get() throws InterruptedException, ExecutionException {
35+
var options = mock(SitemapGeneratorOptions.class);
36+
37+
getter.get(options).block();
38+
verify(lister).list(options);
39+
40+
var executorService = Executors.newCachedThreadPool();
41+
42+
List<? extends Future<?>> futures = IntStream.range(0, 10)
43+
.mapToObj(i -> executorService.submit(() -> {
44+
getter.get(options).block();
45+
}))
46+
.toList();
47+
48+
for (Future<?> future : futures) {
49+
future.get();
3750
}
38-
countDownLatch.await();
39-
verify(lister, times(1)).list();
51+
52+
verify(lister).list(options);
4053
}
4154
}

0 commit comments

Comments
 (0)