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;
ProxyHttpRequestHandler handler
)
{
- SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
+ RegexUrlHandlerMapping mapping = new RegexUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
- Map<String, HttpRequestHandler> 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;
}
--- /dev/null
+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<String, Pattern> 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<String, String> extractUriTemplateVariables(String pattern, String path)
+ {
+ return new HashMap<>();
+ }
+
+ @Override
+ public Comparator<String> getPatternComparator(String path)
+ {
+ return new Comparator<String>()
+ {
+ @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;
+ }
+
+}