package com.wacai.sdk.jtr;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.HandlerWrapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UrlRemapping extends HandlerWrapper {

    private final File             file;
    private final List<Substitute> substitutes;

    public UrlRemapping(File file) {
        this.file = file;
        this.substitutes = new ArrayList<>();
    }

    @Override
    protected void doStart() throws Exception {
        load();
        super.doStart();
    }

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        for (Substitute sub : substitutes) {
            try {
                String replaced = sub.apply(target);
                baseRequest.setPathInfo(replaced);
                super.handle(replaced, baseRequest, request, response);
                return;
            } catch (NotMatchingException ignore) { }
        }
        super.handle(target, baseRequest, request, response);
    }

    void load() throws IOException {
        final Path path = file.toPath();
        final List<String> lines = Files.readAllLines(path, Charset.forName("UTF-8"));
        for (String line : lines) {
            final String[] split = line.split("\\s+", 2);
            substitutes.add(new Substitute(Pattern.compile(split[0]), split[1]));
        }
    }

    static class Substitute {

        private final Pattern regex;
        private final String  replacement;

        public Substitute(Pattern regex, String replacement) {
            this.regex = regex;
            this.replacement = replacement;
        }

        String apply(String input) {
            final Matcher matcher = regex.matcher(input);
            if (matcher.matches()) return matcher.replaceAll(replacement);
            throw new NotMatchingException();
        }

    }

    private static class NotMatchingException extends RuntimeException {
        @Override
        public synchronized Throwable fillInStackTrace() {
            return this;
        }
    }
}

@@ -17,24 +17,59 @@
 i.sdk.jtr;%0A%0A
+import com.google.common.io.Files;%0A
 import org.e
@@ -372,63 +372,8 @@
 et;%0A
-import java.nio.file.Files;%0Aimport java.nio.file.Path;%0A
 impo
@@ -1471,49 +1471,8 @@
 n %7B%0A
-        final Path path = file.toPath();%0A
     
@@ -1516,21 +1516,18 @@
 read
-All
 Lines(
-path
+file
 , Ch

Use Files in guava.