WebServer.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.acdp.transceivr;
  2. import com.eclipsesource.json.JsonArray;
  3. import info.faljse.SDNotify.SDNotify;
  4. import org.slf4j.LoggerFactory;
  5. import spark.Request;
  6. import spark.Response;
  7. import java.util.Timer;
  8. import java.util.TimerTask;
  9. import java.util.concurrent.ConcurrentHashMap;
  10. import static spark.Spark.*;
  11. public class WebServer {
  12. private final static org.slf4j.Logger logger = LoggerFactory.getLogger(WebServer.class);
  13. private final static int BS = 2014;
  14. private final Params params;
  15. private int nextID = 1;
  16. private ConcurrentHashMap<Integer, Transfer> uploads = new ConcurrentHashMap<>();
  17. private Timer t = new Timer("cleanup", true);
  18. public WebServer(Params params) {
  19. this.params = params;
  20. }
  21. public void start() {
  22. port(params.port);
  23. if (!params.webroot.isEmpty())
  24. staticFileLocation(params.webroot);
  25. else
  26. staticFiles.location("/webroot");
  27. get("/zero", (req, res) -> {
  28. var os = res.raw().getOutputStream();
  29. var ba = new byte[BS];
  30. var lengthParam = req.queryParams("length");
  31. if (lengthParam != null) {
  32. res.header("Content-Length", lengthParam);
  33. int length = Integer.parseInt(lengthParam);
  34. for (; length > BS; length -= BS)
  35. os.write(ba);
  36. os.write(ba, 0, length);
  37. } else {
  38. while (true)
  39. os.write(ba);
  40. }
  41. return "";
  42. });
  43. post("/null", (req, res) -> {
  44. var is = req.raw().getInputStream();
  45. var ba = new byte[BS];
  46. while (is.read(ba) != -1) ;
  47. res.status(200);
  48. return "";
  49. });
  50. put("/null", (req, res) -> {
  51. var is = req.raw().getInputStream();
  52. var ba = new byte[BS];
  53. while (is.read(ba) != -1) ;
  54. res.status(200);
  55. return "";
  56. });
  57. post("/cancel", (req, res) -> {
  58. int id = Integer.parseInt(req.queryParams("id"));
  59. var t = uploads.get(id);
  60. t.canceled = true;
  61. t.call.cancel();
  62. res.status(200);
  63. return "OK";
  64. });
  65. get("/status", (req, res) -> {
  66. JsonArray ja = new JsonArray();
  67. for (Transfer t : uploads.values())
  68. ja.add(t.toJSON());
  69. res.header("Content-Type", "application/json");
  70. return ja.toString();
  71. });
  72. post("/xfer", (req, res) -> {
  73. return sendBody(req, res, null);
  74. });
  75. post("/send", (req, res) -> {
  76. return sendBody(req, res, req.queryParams("body"));
  77. });
  78. awaitInitialization();
  79. t.scheduleAtFixedRate(new TimerTask() {
  80. @Override
  81. public void run() {
  82. for (var i = uploads.entrySet().iterator(); i.hasNext(); ) {
  83. var e = i.next();
  84. if (e.getValue().finished && (e.getValue().finishedAT + 30000 < System.currentTimeMillis())) {
  85. i.remove();
  86. logger.info("Removed finished transfer {}", e.getValue().id);
  87. }
  88. }
  89. }
  90. }, 1000, 1000);
  91. SDNotify.sendNotify(); //notify: ready
  92. logger.info("Running");
  93. }
  94. private String sendBody(Request req, Response res, String body) {
  95. try {
  96. Transfer t = new Transfer(nextID++);
  97. t.body=body;
  98. t.from = req.queryParams("from");
  99. t.to = req.queryParams("to");
  100. t.toMethod = req.queryParamOrDefault("toMethod", "POST");
  101. t.cpeeCallback = req.headers("CPEE-CALLBACK");
  102. t.cpeeCallbackId = req.headers("CPEE-CALLBACK-ID");
  103. t.cpeeInstanceURL = req.headers("CPEE-INSTANCE-URL");
  104. if (Boolean.valueOf(req.queryParams("callback"))) {
  105. res.header("CPEE-CALLBACK", "true");
  106. t.doCpeeCallback = true;
  107. }
  108. LoadTools.startTransfer(t);
  109. uploads.put(t.id, t);
  110. res.status(200);
  111. return "OK: " + t.id;
  112. } catch (Exception e) {
  113. return "FAILED: " + e.toString();
  114. }
  115. }
  116. }