WIP: proxy
authorKai Moritz <kai@juplo.de>
Thu, 16 Jun 2016 11:40:02 +0000 (13:40 +0200)
committerKai Moritz <kai@juplo.de>
Thu, 16 Jun 2016 11:40:02 +0000 (13:40 +0200)
src/main/java/de/juplo/thymeproxy/Application.java
src/main/java/de/juplo/thymeproxy/RegexPathMatcher.java [new file with mode: 0644]
src/main/java/de/juplo/thymeproxy/RegexUrlHandlerMapping.java [new file with mode: 0644]

index c0293cd..8a49695 100644 (file)
@@ -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<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;
   }
 
diff --git a/src/main/java/de/juplo/thymeproxy/RegexPathMatcher.java b/src/main/java/de/juplo/thymeproxy/RegexPathMatcher.java
new file mode 100644 (file)
index 0000000..7ee56d3
--- /dev/null
@@ -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<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;
+  }
+  
+}
diff --git a/src/main/java/de/juplo/thymeproxy/RegexUrlHandlerMapping.java b/src/main/java/de/juplo/thymeproxy/RegexUrlHandlerMapping.java
new file mode 100644 (file)
index 0000000..c7e52b4
--- /dev/null
@@ -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
+{
+  
+}