mirror of https://github.com/halo-dev/halo
Merge pull request #7430 from JohnNiang/bug/cannot-access-post-page-with-specific-pattern
Remove wrong cache of pattern matcherspull/7441/head
commit
88ef2c92e1
|
@ -4,7 +4,6 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
|
||||||
import org.springframework.context.SmartLifecycle;
|
import org.springframework.context.SmartLifecycle;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
|
@ -99,11 +98,6 @@ public class ThemeCompositeRouterFunction
|
||||||
this.cachedRouters = routerFunctions();
|
this.cachedRouters = routerFunctions();
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventListener
|
|
||||||
public void onApplicationStarted(ApplicationReadyEvent event) {
|
|
||||||
this.cachedRouters = routerFunctions();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
if (running) {
|
if (running) {
|
||||||
|
|
|
@ -6,14 +6,10 @@ import static org.springframework.web.reactive.function.server.RequestPredicates
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
|
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
|
||||||
import static run.halo.app.content.permalinks.PostPermalinkPolicy.DEFAULT_CATEGORY;
|
import static run.halo.app.content.permalinks.PostPermalinkPolicy.DEFAULT_CATEGORY;
|
||||||
|
|
||||||
import com.google.common.cache.Cache;
|
|
||||||
import com.google.common.cache.CacheBuilder;
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -241,9 +237,6 @@ public class PostRouteFactory implements RouteFactory {
|
||||||
@Getter
|
@Getter
|
||||||
static class PatternParser {
|
static class PatternParser {
|
||||||
private static final Pattern PATTERN_COMPILE = Pattern.compile("([^&?]*)=\\{(.*?)\\}(&|$)");
|
private static final Pattern PATTERN_COMPILE = Pattern.compile("([^&?]*)=\\{(.*?)\\}(&|$)");
|
||||||
private static final Cache<String, Matcher> MATCHER_CACHE = CacheBuilder.newBuilder()
|
|
||||||
.maximumSize(5)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
private final String pattern;
|
private final String pattern;
|
||||||
private String paramName;
|
private String paramName;
|
||||||
|
@ -252,7 +245,7 @@ public class PostRouteFactory implements RouteFactory {
|
||||||
|
|
||||||
PatternParser(String pattern) {
|
PatternParser(String pattern) {
|
||||||
this.pattern = pattern;
|
this.pattern = pattern;
|
||||||
Matcher matcher = patternToMatcher(pattern);
|
var matcher = PATTERN_COMPILE.matcher(pattern);
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
this.paramName = matcher.group(1);
|
this.paramName = matcher.group(1);
|
||||||
this.placeholderName = matcher.group(2);
|
this.placeholderName = matcher.group(2);
|
||||||
|
@ -262,14 +255,6 @@ public class PostRouteFactory implements RouteFactory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Matcher patternToMatcher(String pattern) {
|
|
||||||
try {
|
|
||||||
return MATCHER_CACHE.get(pattern, () -> PATTERN_COMPILE.matcher(pattern));
|
|
||||||
} catch (ExecutionException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
RequestPredicate toRequestPredicate() {
|
RequestPredicate toRequestPredicate() {
|
||||||
if (!this.isQueryParamPattern) {
|
if (!this.isQueryParamPattern) {
|
||||||
throw new IllegalStateException("Not a query param pattern: " + pattern);
|
throw new IllegalStateException("Not a query param pattern: " + pattern);
|
||||||
|
|
|
@ -7,6 +7,7 @@ import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
|
@ -66,6 +67,14 @@ class PostRouteFactoryTest extends RouteFactoryTestSuite {
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private PostRouteFactory postRouteFactory;
|
private PostRouteFactory postRouteFactory;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldBeSameResultWhenParsePattenMultiply() {
|
||||||
|
var parser = new PostRouteFactory.PatternParser("/?p={slug}");
|
||||||
|
Assertions.assertTrue(parser.isQueryParamPattern());
|
||||||
|
parser = new PostRouteFactory.PatternParser("/?p={slug}");
|
||||||
|
Assertions.assertTrue(parser.isQueryParamPattern());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void create() {
|
void create() {
|
||||||
Post post = TestPost.postV1();
|
Post post = TestPost.postV1();
|
||||||
|
|
Loading…
Reference in New Issue