|
19 | 19 |
|
20 | 20 | import com.google.common.base.Strings;
|
21 | 21 | import com.google.common.net.MediaType;
|
| 22 | +import org.openqa.selenium.remote.http.Contents; |
| 23 | +import org.openqa.selenium.remote.http.HttpHandler; |
| 24 | +import org.openqa.selenium.remote.http.HttpRequest; |
| 25 | +import org.openqa.selenium.remote.http.HttpResponse; |
22 | 26 |
|
| 27 | +import javax.servlet.http.HttpServlet; |
| 28 | +import javax.servlet.http.HttpServletRequest; |
| 29 | +import javax.servlet.http.HttpServletResponse; |
23 | 30 | import java.io.IOException;
|
24 | 31 | import java.io.OutputStream;
|
| 32 | +import java.io.UncheckedIOException; |
25 | 33 | import java.nio.charset.StandardCharsets;
|
26 | 34 |
|
27 |
| -import javax.servlet.http.HttpServlet; |
28 |
| -import javax.servlet.http.HttpServletRequest; |
29 |
| -import javax.servlet.http.HttpServletResponse; |
| 35 | +public class GeneratedJsTestHandler implements HttpHandler { |
30 | 36 |
|
31 |
| -public class GeneratedJsTestServlet extends HttpServlet { |
| 37 | + private final String stripPrefix; |
| 38 | + |
| 39 | + public GeneratedJsTestHandler(String stripPrefix) { |
| 40 | + this.stripPrefix = stripPrefix; |
| 41 | + } |
32 | 42 |
|
33 | 43 | @Override
|
34 |
| - protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
35 |
| - throws IOException { |
36 |
| - String symbol = Strings.nullToEmpty(req.getPathInfo()).replace("../", "").replace("/", "$"); |
37 |
| - byte[] data = |
| 44 | + public HttpResponse execute(HttpRequest req) throws UncheckedIOException { |
| 45 | + String path = req.getUri(); |
| 46 | + if (path.startsWith(stripPrefix)) { |
| 47 | + path = path.substring(stripPrefix.length()); |
| 48 | + } |
| 49 | + String symbol = path.replace("../", "").replace("/", "$"); |
| 50 | + String data = |
38 | 51 | ("<!DOCTYPE html>\n"
|
39 | 52 | + "<html>\n"
|
40 | 53 | + "<head>\n"
|
41 | 54 | + "<meta http-equiv=\"X-UA-Compatible\" content=\"IE-Edge\">\n"
|
42 | 55 | + "<!-- File generated by " + getClass().getName() + " -->\n"
|
43 |
| - + "<title>" + req.getPathInfo() + "</title>\n" |
| 56 | + + "<title>" + path + "</title>\n" |
44 | 57 | + "<script src=\"/third_party/closure/goog/base.js\"></script>\n"
|
45 | 58 | + "<script src=\"/javascript/deps.js\"></script>\n"
|
46 | 59 | + "<script>\n"
|
47 | 60 | + " (function() {\n"
|
48 |
| - + " var path = '../../.." + req.getPathInfo() + "';\n" |
| 61 | + + " var path = '../../.." + path + "';\n" |
49 | 62 | + " var loadFlags = goog.dependencies_.loadFlags[path];\n"
|
50 | 63 | + " goog.addDependency(path, ['" + symbol + "'],\n"
|
51 |
| - + " goog.dependencies_.requires['../../.." + req.getPathInfo() + "'] || [],\n" |
| 64 | + + " goog.dependencies_.requires['../../.." + path + "'] || [],\n" |
52 | 65 | + " (loadFlags && loadFlags['module'] == 'goog'));\n"
|
53 | 66 | + " goog.require('" + symbol + "');\n"
|
54 | 67 | + " })()\n"
|
55 |
| - + "</script></head><body></body></html>").getBytes(StandardCharsets.UTF_8); |
56 |
| - |
57 |
| - resp.setStatus(HttpServletResponse.SC_OK); |
58 |
| - resp.setContentType(MediaType.HTML_UTF_8.toString()); |
59 |
| - resp.setContentLength(data.length); |
60 |
| - |
61 |
| - OutputStream stream = resp.getOutputStream(); |
62 |
| - stream.write(data); |
63 |
| - stream.flush(); |
64 |
| - stream.close(); |
| 68 | + + "</script></head><body></body></html>"); |
| 69 | +// |
| 70 | +// resp.setStatus(HttpServletResponse.SC_OK); |
| 71 | +// resp.setContentType(MediaType.HTML_UTF_8.toString()); |
| 72 | +// resp.setContentLength(data.length); |
| 73 | +// |
| 74 | +// OutputStream stream = resp.getOutputStream(); |
| 75 | +// stream.write(data); |
| 76 | +// stream.flush(); |
| 77 | +// stream.close(); |
| 78 | + return new HttpResponse() |
| 79 | + .setHeader("Content-Type", MediaType.HTML_UTF_8.toString()) |
| 80 | + .setContent(Contents.utf8String(data)); |
65 | 81 | }
|
66 | 82 | }
|
0 commit comments