|
@@ -3,9 +3,9 @@ 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.Iterator;
|
|
|
-import java.util.Map;
|
|
|
import java.util.Timer;
|
|
|
import java.util.TimerTask;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
@@ -15,11 +15,11 @@ 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);
|
|
|
- private final static int BS=2014;
|
|
|
+ private Timer t = new Timer("cleanup", true);
|
|
|
|
|
|
public WebServer(Params params) {
|
|
|
this.params = params;
|
|
@@ -27,22 +27,21 @@ public class WebServer {
|
|
|
|
|
|
public void start() {
|
|
|
port(params.port);
|
|
|
- if(!params.webroot.isEmpty())
|
|
|
+ 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)
|
|
|
+ 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 {
|
|
|
+ os.write(ba, 0, length);
|
|
|
+ } else {
|
|
|
while (true)
|
|
|
os.write(ba);
|
|
|
}
|
|
@@ -51,14 +50,14 @@ public class WebServer {
|
|
|
post("/null", (req, res) -> {
|
|
|
var is = req.raw().getInputStream();
|
|
|
var ba = new byte[BS];
|
|
|
- while (is.read(ba)!=-1);
|
|
|
+ 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);
|
|
|
+ while (is.read(ba) != -1) ;
|
|
|
res.status(200);
|
|
|
return "";
|
|
|
});
|
|
@@ -78,41 +77,49 @@ public class WebServer {
|
|
|
return ja.toString();
|
|
|
});
|
|
|
post("/xfer", (req, res) -> {
|
|
|
- try {
|
|
|
- Transfer t = new Transfer(nextID++);
|
|
|
- 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();
|
|
|
- }
|
|
|
+ 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();) {
|
|
|
+ for (var i = uploads.entrySet().iterator(); i.hasNext(); ) {
|
|
|
var e = i.next();
|
|
|
- if(e.getValue().finished&&(e.getValue().finishedAT+30000<System.currentTimeMillis())){
|
|
|
+ if (e.getValue().finished && (e.getValue().finishedAT + 30000 < System.currentTimeMillis())) {
|
|
|
i.remove();
|
|
|
logger.info("Removed finished transfer {}", e.getValue().id);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- },1000,1000);
|
|
|
+ }, 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|