123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package com.acdp.transceivr;
- import com.eclipsesource.json.JsonArray;
- import info.faljse.SDNotify.SDNotify;
- import org.slf4j.LoggerFactory;
- import spark.Request;
- import spark.Response;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.concurrent.ConcurrentHashMap;
- import static spark.Spark.*;
- public class WebServer {
- private final static org.slf4j.Logger logger = LoggerFactory.getLogger(WebServer.class);
- private final static int BS = 2014;
- private final Params params;
- private int nextID = 1;
- private ConcurrentHashMap<Integer, Transfer> uploads = new ConcurrentHashMap<>();
- private Timer t = new Timer("cleanup", true);
- public WebServer(Params params) {
- this.params = params;
- }
- public void start() {
- port(params.port);
- if (!params.webroot.isEmpty())
- staticFileLocation(params.webroot);
- else
- staticFiles.location("/webroot");
- get("/zero", (req, res) -> {
- var os = res.raw().getOutputStream();
- var ba = new byte[BS];
- var lengthParam = req.queryParams("length");
- if (lengthParam != null) {
- res.header("Content-Length", lengthParam);
- int length = Integer.parseInt(lengthParam);
- for (; length > BS; length -= BS)
- os.write(ba);
- os.write(ba, 0, length);
- } else {
- while (true)
- os.write(ba);
- }
- return "";
- });
- post("/null", (req, res) -> {
- var is = req.raw().getInputStream();
- var ba = new byte[BS];
- while (is.read(ba) != -1) ;
- res.status(200);
- return "";
- });
- put("/null", (req, res) -> {
- var is = req.raw().getInputStream();
- var ba = new byte[BS];
- while (is.read(ba) != -1) ;
- res.status(200);
- return "";
- });
- post("/cancel", (req, res) -> {
- int id = Integer.parseInt(req.queryParams("id"));
- var t = uploads.get(id);
- t.canceled = true;
- t.call.cancel();
- res.status(200);
- return "OK";
- });
- get("/status", (req, res) -> {
- JsonArray ja = new JsonArray();
- for (Transfer t : uploads.values())
- ja.add(t.toJSON());
- res.header("Content-Type", "application/json");
- return ja.toString();
- });
- post("/xfer", (req, res) -> {
- return sendBody(req, res, null);
- });
- post("/send", (req, res) -> {
- return sendBody(req, res, req.queryParams("body"));
- });
- awaitInitialization();
- t.scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- for (var i = uploads.entrySet().iterator(); i.hasNext(); ) {
- var e = i.next();
- if (e.getValue().finished && (e.getValue().finishedAT + 30000 < System.currentTimeMillis())) {
- i.remove();
- logger.info("Removed finished transfer {}", e.getValue().id);
- }
- }
- }
- }, 1000, 1000);
- SDNotify.sendNotify(); //notify: ready
- logger.info("Running");
- }
- private String sendBody(Request req, Response res, String body) {
- try {
- Transfer t = new Transfer(nextID++);
- t.body=body;
- t.from = req.queryParams("from");
- t.to = req.queryParams("to");
- t.toMethod = req.queryParamOrDefault("toMethod", "POST");
- t.cpeeCallback = req.headers("CPEE-CALLBACK");
- t.cpeeCallbackId = req.headers("CPEE-CALLBACK-ID");
- t.cpeeInstanceURL = req.headers("CPEE-INSTANCE-URL");
- if (Boolean.valueOf(req.queryParams("callback"))) {
- res.header("CPEE-CALLBACK", "true");
- t.doCpeeCallback = true;
- }
- LoadTools.startTransfer(t);
- uploads.put(t.id, t);
- res.status(200);
- return "OK: " + t.id;
- } catch (Exception e) {
- return "FAILED: " + e.toString();
- }
- }
- }
|