WebServer.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. logger.info("Source: {}, Content-Type: {}", req.host(), req.headers("Content-Type"));
  49. return "";
  50. });
  51. put("/null", (req, res) -> {
  52. var is = req.raw().getInputStream();
  53. var ba = new byte[BS];
  54. while (is.read(ba) != -1) ;
  55. res.status(200);
  56. return "";
  57. });
  58. post("/cancel", (req, res) -> {
  59. int id = Integer.parseInt(req.queryParams("id"));
  60. var t = uploads.get(id);
  61. t.canceled = true;
  62. t.call.cancel();
  63. res.status(200);
  64. return "OK";
  65. });
  66. get("/status", (req, res) -> {
  67. JsonArray ja = new JsonArray();
  68. for (Transfer t : uploads.values())
  69. ja.add(t.toJSON());
  70. res.header("Content-Type", "application/json");
  71. return ja.toString();
  72. });
  73. post("/xfer", (req, res) -> {
  74. return sendBody(req, res, null);
  75. });
  76. post("/send", (req, res) -> {
  77. return sendBody(req, res, req.queryParams("body"));
  78. });
  79. awaitInitialization();
  80. t.scheduleAtFixedRate(new TimerTask() {
  81. @Override
  82. public void run() {
  83. for (var i = uploads.entrySet().iterator(); i.hasNext(); ) {
  84. var e = i.next();
  85. if (e.getValue().finished && (e.getValue().finishedAT + 30000 < System.currentTimeMillis())) {
  86. i.remove();
  87. logger.info("Removed finished transfer {}", e.getValue().id);
  88. }
  89. }
  90. }
  91. }, 1000, 1000);
  92. SDNotify.sendNotify(); //notify: ready
  93. logger.info("Running");
  94. }
  95. private String sendBody(Request req, Response res, String body) {
  96. try {
  97. Transfer t = new Transfer(nextID++);
  98. t.body=body;
  99. if(t.body!=null) {
  100. t.bytesRead=t.body.length();
  101. t.contentLength=t.body.length();
  102. }
  103. t.from = req.queryParams("from");
  104. t.to = req.queryParams("to");
  105. t.toMethod = req.queryParamOrDefault("toMethod", "POST");
  106. t.cpeeCallback = req.headers("CPEE-CALLBACK");
  107. t.cpeeCallbackId = req.headers("CPEE-CALLBACK-ID");
  108. t.cpeeInstanceURL = req.headers("CPEE-INSTANCE-URL");
  109. t.toMime = req.queryParamOrDefault("toMime", "application/octet-stream");
  110. if (Boolean.valueOf(req.queryParams("callback"))) {
  111. res.header("CPEE-CALLBACK", "true");
  112. t.doCpeeCallback = true;
  113. }
  114. LoadTools.startTransfer(t);
  115. uploads.put(t.id, t);
  116. res.status(200);
  117. return "OK: " + t.id;
  118. } catch (Exception e) {
  119. return "FAILED: " + e.toString();
  120. }
  121. }
  122. }