1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jeuclid.testsuite;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.xml.transform.Result;
30 import javax.xml.transform.Source;
31 import javax.xml.transform.stream.StreamResult;
32 import javax.xml.transform.stream.StreamSource;
33
34
35
36
37
38
39 public class Servlet extends HttpServlet {
40 private static final int BLOCK_SIZE = 4096;
41
42 private static final TestSuiteProcessor TSP = TestSuiteProcessor
43 .getInstance();
44
45
46
47
48 private static final long serialVersionUID = 1L;
49
50
51
52
53 public Servlet() {
54 }
55
56
57 @Override
58 protected void doGet(final HttpServletRequest req,
59 final HttpServletResponse resp) throws ServletException,
60 IOException {
61 final String file = req.getPathInfo();
62 final InputStream stream = Thread.currentThread()
63 .getContextClassLoader().getResourceAsStream(
64 "mml2-testsuite/" + file);
65 if (stream == null) {
66 resp.sendError(HttpServletResponse.SC_NOT_FOUND, file);
67 } else {
68 final OutputStream out = resp.getOutputStream();
69
70 boolean processed = false;
71 if (file.endsWith(".xml")) {
72 final Source inputSource = new StreamSource(stream);
73 final Result result = new StreamResult(out);
74 processed = Servlet.TSP.process(inputSource, result, false);
75 }
76 if (!processed) {
77 final byte[] buf = new byte[Servlet.BLOCK_SIZE];
78 int count = stream.read(buf);
79 while (count > -1) {
80 out.write(buf, 0, count);
81 count = stream.read(buf);
82 }
83 }
84 }
85 }
86 }