|
|
@ -1,9 +1,8 @@
|
|
|
|
package com.olexyn.mirror;
|
|
|
|
package com.olexyn.mirror;
|
|
|
|
|
|
|
|
|
|
|
|
import com.olexyn.misp.helper.Ride;
|
|
|
|
import com.olexyn.misp.helper.Ride;
|
|
|
|
|
|
|
|
import com.olexyn.misp.helper.WebPrint;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
@ -11,16 +10,20 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
public class Mirror extends HttpServlet {
|
|
|
|
public class Mirror extends HttpServlet {
|
|
|
|
|
|
|
|
|
|
|
|
protected static final String MISP_CLIENT_URL = "http://localhost:9090/mispclient/core";
|
|
|
|
protected static final String MISP_CLIENT_URL = "http://localhost:9090/mispclient/core";
|
|
|
|
|
|
|
|
|
|
|
|
public final Map<Long, Ride> available = new HashMap<>();
|
|
|
|
|
|
|
|
public final Map<Long, Ride> booked = new HashMap<>();
|
|
|
|
private final List<String> getList = new ArrayList<>();
|
|
|
|
public final Map<Long, Ride> loaded = new HashMap<>();
|
|
|
|
private final List<String> postList = new ArrayList<>();
|
|
|
|
|
|
|
|
private final List<String> putList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #######
|
|
|
|
// #######
|
|
|
|
//
|
|
|
|
//
|
|
|
@ -29,172 +32,46 @@ public class Mirror extends HttpServlet {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synchronized (getList) {
|
|
|
|
|
|
|
|
getList.add(IOUtils.toString(request.getReader()));
|
|
|
|
|
|
|
|
|
|
|
|
String payload = IOUtils.toString(request.getReader());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject obj = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
obj = new JSONObject(payload);
|
|
|
|
|
|
|
|
}catch (JSONException jsonE){
|
|
|
|
|
|
|
|
int br = 0;
|
|
|
|
|
|
|
|
Thread handleGetUserRequestThread = new Thread(() -> {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
handleGetRequest(request, response);
|
|
|
|
|
|
|
|
} catch (IOException | InterruptedException e) {e.printStackTrace(); }
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
handleGetUserRequestThread.setName("handleGetUserRequestThread");
|
|
|
|
|
|
|
|
handleGetUserRequestThread.start();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ride ridePayload = new Ride(payload);
|
|
|
|
PrintWriter print = response.getWriter();
|
|
|
|
boolean hasID = ridePayload.getID() != null;
|
|
|
|
|
|
|
|
boolean hasRequest = ridePayload.getRequest() != null;
|
|
|
|
|
|
|
|
boolean hasData = ridePayload.getData() != null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (hasID && hasRequest && hasData) {
|
|
|
|
|
|
|
|
Thread handleGetRideRequestDataThread = new Thread(() -> {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
handleGetRideRequestData(request, response);
|
|
|
|
|
|
|
|
} catch (IOException | InterruptedException e) { e.printStackTrace(); }
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
handleGetRideRequestDataThread.setName("handleGetRideRequestDataThread");
|
|
|
|
|
|
|
|
handleGetRideRequestDataThread.start();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print.println("<!DOCTYPE html>");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print.println("<html lang=\"en\">");
|
|
|
|
|
|
|
|
print.println("<head>");
|
|
|
|
|
|
|
|
print.println("<meta charset=\"utf-8\">");
|
|
|
|
|
|
|
|
print.println("<title>title</title>");
|
|
|
|
|
|
|
|
print.println("<link rel=\"stylesheet\" href=\"style.css\">");
|
|
|
|
|
|
|
|
print.println("<script src=\"script.js\"></script>");
|
|
|
|
|
|
|
|
print.println("</head>");
|
|
|
|
|
|
|
|
print.println("<body>");
|
|
|
|
|
|
|
|
synchronized (getList) {
|
|
|
|
|
|
|
|
print.println(WebPrint.list(getList, "GET"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print.println(" </body></html>");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* handle GET (Link)
|
|
|
|
|
|
|
|
* remove Ride from AvailableRides
|
|
|
|
|
|
|
|
* add Ride to ReservedRides
|
|
|
|
|
|
|
|
* send OK (Ride) to mispclient
|
|
|
|
|
|
|
|
* send OK (Ride) to public
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
protected void handleGetRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
|
|
|
|
|
|
|
|
final Ride ride;
|
|
|
|
|
|
|
|
String parsedRequest = IOUtils.toString(request.getReader());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synchronized (available) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (available.size() < 1) {
|
|
|
|
|
|
|
|
available.notify();
|
|
|
|
|
|
|
|
available.wait();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ride exists only locally, thus safe
|
|
|
|
|
|
|
|
ride = available.entrySet().iterator().next().getValue();
|
|
|
|
|
|
|
|
// ride exists only in "available", access through which is sync, thus safe
|
|
|
|
|
|
|
|
available.remove(ride.getID());
|
|
|
|
|
|
|
|
// needed because POST (Ride) wait()s
|
|
|
|
|
|
|
|
available.notify();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synchronized (booked) {
|
|
|
|
|
|
|
|
// ride exists only locally, thus safe
|
|
|
|
|
|
|
|
booked.put(ride.getID(), ride);
|
|
|
|
|
|
|
|
// ride exists only in "booked", access through which is sync, thus safe
|
|
|
|
|
|
|
|
ride.setRequest(parsedRequest);
|
|
|
|
|
|
|
|
// POST (Ride) wait()s
|
|
|
|
|
|
|
|
booked.notify();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
synchronized (loaded) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (!loaded.containsKey(ride.getID())) {
|
|
|
|
@Override
|
|
|
|
loaded.notify();
|
|
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
|
loaded.wait();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CARE this is tricky
|
|
|
|
|
|
|
|
// what if ride exists in another map, e.g. "available'
|
|
|
|
|
|
|
|
// in that case illegal access is possible
|
|
|
|
|
|
|
|
// be carefull to removing ride from all other references, when adding it to "loaded".
|
|
|
|
|
|
|
|
ride.setData(loaded.remove(ride.getID()).getData());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response.setStatus(200);
|
|
|
|
|
|
|
|
final PrintWriter writer = response.getWriter();
|
|
|
|
|
|
|
|
writer.write(ride.getData());
|
|
|
|
|
|
|
|
writer.flush();
|
|
|
|
|
|
|
|
writer.close(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* handle GET (Ride)(Data)
|
|
|
|
|
|
|
|
* if Ride in ForwardedRequest
|
|
|
|
|
|
|
|
* remove Ride from ForwardedRequest
|
|
|
|
|
|
|
|
* add Ride to NewData
|
|
|
|
|
|
|
|
* send OK (Ride)(Data)
|
|
|
|
|
|
|
|
* remove Ride from NewData
|
|
|
|
|
|
|
|
* add Ride to ForwardedData
|
|
|
|
|
|
|
|
* send OK (EOL)
|
|
|
|
|
|
|
|
* remove Ride from ForwardedData
|
|
|
|
|
|
|
|
* add Ride to EOL
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
protected void handleGetRideRequestData(HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
|
|
|
|
|
|
|
|
final String jsonPayload = IOUtils.toString(request.getReader());
|
|
|
|
|
|
|
|
final Ride ride = new Ride(jsonPayload);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synchronized (booked) {
|
|
|
|
|
|
|
|
booked.remove(ride.getID());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synchronized (loaded) {
|
|
|
|
synchronized (postList) {
|
|
|
|
loaded.put(ride.getID(), ride);
|
|
|
|
postList.add(IOUtils.toString(request.getReader()));
|
|
|
|
loaded.notify();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #######
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// #######
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
|
|
|
public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException{
|
|
|
|
Thread handlePostRideThread = new Thread(() -> {
|
|
|
|
synchronized (putList){
|
|
|
|
try {handlePostRide(request, response); } catch (IOException | InterruptedException e) { e.printStackTrace(); }
|
|
|
|
putList.add(IOUtils.toString(request.getReader()));
|
|
|
|
});
|
|
|
|
|
|
|
|
handlePostRideThread.setName("handlePostRideThread");
|
|
|
|
|
|
|
|
handlePostRideThread.start();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* handle POST (Ride)
|
|
|
|
|
|
|
|
* add Ride to AvailableRides
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
protected void handlePostRide(HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
|
|
|
|
|
|
|
|
String jsonPayload = IOUtils.toString(request.getReader());
|
|
|
|
|
|
|
|
final Ride ride = new Ride(jsonPayload);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synchronized (available) {
|
|
|
|
|
|
|
|
available.put(ride.getID(), ride);
|
|
|
|
|
|
|
|
available.notify();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ID is final/threadsafe
|
|
|
|
|
|
|
|
while(!(booked.containsKey(ride.getID()))){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
synchronized (booked) {
|
|
|
|
|
|
|
|
//booked.notify();
|
|
|
|
|
|
|
|
//booked.wait();
|
|
|
|
|
|
|
|
ride.setRequest(booked.get(ride.getID()).getRequest());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response.setStatus(200);
|
|
|
|
|
|
|
|
PrintWriter writer = response.getWriter();
|
|
|
|
|
|
|
|
writer.write(ride.json());
|
|
|
|
|
|
|
|
writer.flush();
|
|
|
|
|
|
|
|
writer.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|