From: Kai Moritz Date: Thu, 16 Jun 2016 11:40:02 +0000 (+0200) Subject: WIP: proxy X-Git-Tag: alt~31 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=858d9fe319628293e92984e8d780b8fcafd50715;p=maven-thymeleaf-skin WIP: proxy --- diff --git a/src/main/java/de/juplo/thymeproxy/Application.java b/src/main/java/de/juplo/thymeproxy/Application.java index c0293cd..8a49695 100644 --- a/src/main/java/de/juplo/thymeproxy/Application.java +++ b/src/main/java/de/juplo/thymeproxy/Application.java @@ -1,15 +1,12 @@ package de.juplo.thymeproxy; import java.util.Collections; -import java.util.HashMap; -import java.util.Map; import org.apache.http.impl.client.CloseableHttpClient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.core.Ordered; import org.springframework.core.env.Environment; -import org.springframework.web.HttpRequestHandler; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; @@ -21,15 +18,9 @@ public class Application ProxyHttpRequestHandler handler ) { - SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(); + RegexUrlHandlerMapping mapping = new RegexUrlHandlerMapping(); mapping.setOrder(Ordered.HIGHEST_PRECEDENCE); - Map mappings = new HashMap<>(); - mappings.put("/*.html", handler); - mappings.put("/*/", handler); - mappings.put("/*/*.html", handler); - mappings.put("/**/", handler); - mappings.put("/**/*.html", handler); - mapping.setUrlMap(mappings); + mapping.setUrlMap(Collections.singletonMap(".*\\.html$", handler)); return mapping; } diff --git a/src/main/java/de/juplo/thymeproxy/RegexPathMatcher.java b/src/main/java/de/juplo/thymeproxy/RegexPathMatcher.java new file mode 100644 index 0000000..7ee56d3 --- /dev/null +++ b/src/main/java/de/juplo/thymeproxy/RegexPathMatcher.java @@ -0,0 +1,124 @@ +package de.juplo.thymeproxy; + + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.PathMatcher; + + + +/** + * + * @author kai + */ +public class RegexPathMatcher implements PathMatcher +{ + private final static Logger LOG = + LoggerFactory.getLogger(RegexPathMatcher.class); + + + private int flags = 0; + private Map patternCache = new HashMap<>(); + + + public RegexPathMatcher() + { + } + + public RegexPathMatcher(int flags) + { + this.flags = flags; + } + + + protected boolean compilePattern(String string) + { + try + { + Pattern pattern = Pattern.compile(string, flags); + patternCache.put(string, pattern); + return true; + } + catch(PatternSyntaxException e) + { + LOG.debug("\"{}\" is no valid pattern: {}", string, e.getMessage()); + return false; + } + } + + + @Override + public boolean isPattern(String path) + { + if (path.startsWith("^") || path.endsWith("$")) + return false; + + if (patternCache.containsKey(path)) + return true; + + return compilePattern(path); + } + + @Override + public boolean match(String pattern, String path) + { + if (!patternCache.containsKey(pattern)) + compilePattern(pattern); + Matcher matcher = patternCache.get(pattern).matcher(path); + return matcher.matches(); + } + + @Override + public boolean matchStart(String pattern, String path) + { + if (!patternCache.containsKey(pattern)) + compilePattern(pattern); + Matcher matcher = patternCache.get(pattern).matcher(path); + if (!matcher.find()) + return false; + else + return matcher.start() == 0; + } + + @Override + public String extractPathWithinPattern(String pattern, String path) + { + return ""; + } + + @Override + public Map extractUriTemplateVariables(String pattern, String path) + { + return new HashMap<>(); + } + + @Override + public Comparator getPatternComparator(String path) + { + return new Comparator() + { + @Override + public int compare(String a, String b) + { + return b.length() - a.length(); + } + }; + } + + @Override + public String combine(String a, String b) + { + boolean literal = (flags & Pattern.LITERAL) == Pattern.LITERAL; + String pattern = a + (literal ? "" : ".*") + b; + if (!isPattern(pattern)) + throw new IllegalArgumentException("Cannot combine pattern " + a + " with pattern " + b); + return pattern; + } + +} diff --git a/src/main/java/de/juplo/thymeproxy/RegexUrlHandlerMapping.java b/src/main/java/de/juplo/thymeproxy/RegexUrlHandlerMapping.java new file mode 100644 index 0000000..c7e52b4 --- /dev/null +++ b/src/main/java/de/juplo/thymeproxy/RegexUrlHandlerMapping.java @@ -0,0 +1,16 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package de.juplo.thymeproxy; + + +/** + * + * @author kai + */ +public class RegexUrlHandlerMapping +{ + +}