From 42dc6f1236bf0a4db190e16a5da9387d55f3bd6c Mon Sep 17 00:00:00 2001 From: Ivan Olexyn Date: Fri, 24 Apr 2020 16:57:38 +0200 Subject: [PATCH] + upload only needed --- mirror/README.md | 5 + mirror/build.xml | 40 ++++ mirror/src/com/olexyn/mirror/Mirror.java | 86 +++++++ mirror/web.xml | 23 ++ mirror/web/hello.jsp | 23 ++ mirror/web/images/tomcat.gif | Bin 0 -> 1441 bytes mirror/web/index.html | 25 ++ misp-fwd/README.md | 5 + misp-fwd/install-locally.sh | 10 + misp-fwd/pom.xml | 97 ++++++++ .../java/com/olexyn/misp/fwd/FwdProxy.java | 217 ++++++++++++++++++ misp-fwd/src/main/webapp/WEB-INF/web.xml | 22 ++ misp-fwd/src/main/webapp/images/io42630.png | Bin 0 -> 617 bytes misp-fwd/src/main/webapp/index.jsp | 20 ++ misp-helper/install-locally.sh | 10 + misp-helper/pom.xml | 87 +++++++ .../com/olexyn/misp/helper/JsonHelper.java | 21 ++ .../java/com/olexyn/misp/helper/Ride.java | 139 +++++++++++ .../java/com/olexyn/misp/helper/WebPrint.java | 66 ++++++ misp-helper/src/site/site.xml | 26 +++ misp-rev/install-locally.sh | 10 + misp-rev/pom.xml | 90 ++++++++ .../com/olexyn/misp/rev/ConnectionHelper.java | 16 ++ .../main/java/com/olexyn/misp/rev/Main.java | 12 + .../java/com/olexyn/misp/rev/RevProxy.java | 169 ++++++++++++++ misp-rev/src/main/webapp/WEB-INF/web.xml | 22 ++ misp-rev/src/main/webapp/images/io42630.png | Bin 0 -> 617 bytes misp-rev/src/main/webapp/index.jsp | 20 ++ 28 files changed, 1261 insertions(+) create mode 100644 mirror/README.md create mode 100644 mirror/build.xml create mode 100644 mirror/src/com/olexyn/mirror/Mirror.java create mode 100644 mirror/web.xml create mode 100644 mirror/web/hello.jsp create mode 100644 mirror/web/images/tomcat.gif create mode 100644 mirror/web/index.html create mode 100644 misp-fwd/README.md create mode 100755 misp-fwd/install-locally.sh create mode 100644 misp-fwd/pom.xml create mode 100644 misp-fwd/src/main/java/com/olexyn/misp/fwd/FwdProxy.java create mode 100644 misp-fwd/src/main/webapp/WEB-INF/web.xml create mode 100644 misp-fwd/src/main/webapp/images/io42630.png create mode 100644 misp-fwd/src/main/webapp/index.jsp create mode 100755 misp-helper/install-locally.sh create mode 100644 misp-helper/pom.xml create mode 100644 misp-helper/src/main/java/com/olexyn/misp/helper/JsonHelper.java create mode 100644 misp-helper/src/main/java/com/olexyn/misp/helper/Ride.java create mode 100644 misp-helper/src/main/java/com/olexyn/misp/helper/WebPrint.java create mode 100644 misp-helper/src/site/site.xml create mode 100755 misp-rev/install-locally.sh create mode 100644 misp-rev/pom.xml create mode 100644 misp-rev/src/main/java/com/olexyn/misp/rev/ConnectionHelper.java create mode 100644 misp-rev/src/main/java/com/olexyn/misp/rev/Main.java create mode 100644 misp-rev/src/main/java/com/olexyn/misp/rev/RevProxy.java create mode 100644 misp-rev/src/main/webapp/WEB-INF/web.xml create mode 100644 misp-rev/src/main/webapp/images/io42630.png create mode 100644 misp-rev/src/main/webapp/index.jsp diff --git a/mirror/README.md b/mirror/README.md new file mode 100644 index 0000000..37f6e99 --- /dev/null +++ b/mirror/README.md @@ -0,0 +1,5 @@ +#### About +The `mirror` servlet. Prints the list of request it received. +* `./src` the code. +* `./war/wrapper` supplements needed for `.war`. +* `./war/.war` copy this to `tomcat/webapps`. \ No newline at end of file diff --git a/mirror/build.xml b/mirror/build.xml new file mode 100644 index 0000000..0b59974 --- /dev/null +++ b/mirror/build.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mirror/src/com/olexyn/mirror/Mirror.java b/mirror/src/com/olexyn/mirror/Mirror.java new file mode 100644 index 0000000..df7fa25 --- /dev/null +++ b/mirror/src/com/olexyn/mirror/Mirror.java @@ -0,0 +1,86 @@ +package com.olexyn.mirror; + +import com.olexyn.misp.helper.Ride; +import com.olexyn.misp.helper.WebPrint; +import org.apache.commons.io.IOUtils; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Mirror extends HttpServlet { + + protected static final String MISP_CLIENT_URL = "http://localhost:9090/mispclient/core"; + + + private final List list = new ArrayList<>(); + + + + private void addRequest(HttpServletRequest request){ + synchronized (list) { + StringBuffer sb = new StringBuffer(); + sb.append(request.getRequestURL().toString()); + sb.append(WebPrint.SPLIT); + sb.append(request.getMethod()); + sb.append(WebPrint.SPLIT); + sb.append(request.getQueryString()); + list.add(sb.toString()); + } + } + // ####### + // + // ####### + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + + addRequest(request); + + PrintWriter print = response.getWriter(); + + print.println(""); + + print.println(""); + print.println(""); + print.println(""); + print.println("title"); + print.println(""); + print.println(""); + print.println(""); + print.println(""); + synchronized (list) { + + print.println(WebPrint.requestList(list)); + } + + + + + print.println(" "); + + + } + + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) { + + addRequest(request); + } + + + @Override + public void doPut(HttpServletRequest request, HttpServletResponse response){ + addRequest(request); + } +} + + diff --git a/mirror/web.xml b/mirror/web.xml new file mode 100644 index 0000000..a4e2df5 --- /dev/null +++ b/mirror/web.xml @@ -0,0 +1,23 @@ + + + + Mirror + + This is a simple web application with a source code organization + based on the recommendations of the Application Developer's Guide. + + + + Mirror + com.olexyn.mirror.Mirror + + + + Mirror + /core + + + diff --git a/mirror/web/hello.jsp b/mirror/web/hello.jsp new file mode 100644 index 0000000..9596088 --- /dev/null +++ b/mirror/web/hello.jsp @@ -0,0 +1,23 @@ + + +Sample Application JSP Page + + + + + + + + +
+ + +

Sample Application JSP Page

+This is the output of a JSP page that is part of the Hello, World +application. +
+ +<%= new String("Hello!") %> + + + diff --git a/mirror/web/images/tomcat.gif b/mirror/web/images/tomcat.gif new file mode 100644 index 0000000000000000000000000000000000000000..32f7d809fbf745e814598cae44329394515c4879 GIT binary patch literal 1441 zcmV;S1z!3`Nk%w1VPpVD0J8u9?d|O+B_HU@b7@o+r;}NBZdk3YtNkgMYcdmxvLDB z3k3wE&!9rlKuzGPY8;a- zBi9R*frQ3fG0HGx<2Ev@GR53BXC{m%G)VNLfCm<(3@d!93@{LZo_h$0rKE)ez={AR zgN!_u;fgt_k*ub?wkzoZv`}`Ys26raPFHm$%%BQT?K!Jd*G5{=2gTkKm85a{lvhHM zq%hB6VYfiyxgkqU9BLfq5RU* z2xlq)4&Xs{t#U;v$CFDj&NmWlD78X3I~6kLRd#bsr!75?4-CS74p59TK!KLf(JWu{ zs2v*S6ek^IHWKo4`3Crh2n(HYAT;)8-4N!?Qv zNbi9l1PcSqNrRkkR)i&s4h%q4MFf=5oO?=aIRu&^c=rJw0k{aKqKI;+K%I56kbr3( zT^9|EFF~oypO-Kx)_@a?poNQXGV3h=heLGfTx2gvE6D=$^|mAimqz*Qx6i`q0EiqZ zX{>b0T!2TWRdNdMyA7Df-B5uH!I}}iQP5DR3&J@iyZ|@QpsyT3VN?@)6_TL?|JvGb zsR2JbFnZ>Em8}t9itts#&O#Tl#4NP8&A=BMaR>)2aO~^~Du2x3z~t)k)w%Hy0Wb|N zf17BMQk3jS7A-St-iA4YT*F$sb-+w3wCY?#(cjA3oB($E+yrXkHtlSK1V9}%g5$L? z4jxm=8#ISec@1urM2z!T!(E(>UmR+KtH75H9xTQonZA6CVvE^3jNEGf8?Dt{4bgYM zB8}xx0fgr)FOFfvw|E2Nu^`I-E1+OfSsnpQ6FIyCXB@^}X!r0#Q?Ha1Tqt*iKKj-* zbZu`Hd;Pnc4;4Jdc?Pd1&`RjkM63YE4c>dh0Ym&NF1B{PqBjP@eV!ELIakRle2Onf z!}iJE#RJVc2-}0!;N}Fr2?j5C`v(gnatq2yDd;Sm3ncK%`!KwXpTQ?+TmTTKffX(5 zeh_;T4ald1=DBZMCtzJt5WE%0q#&l0rBXp38ywnN(Lc7mOyr?ig~*8{&%esThK=OkAph3=niSAp+oHso + +Mirror + + + + + + + + +
+ + +

Mirror

+
+ +
    +

    This Servlet currently provides the following features: + +

  • Mirror of all requests. +
+ + + diff --git a/misp-fwd/README.md b/misp-fwd/README.md new file mode 100644 index 0000000..da391f5 --- /dev/null +++ b/misp-fwd/README.md @@ -0,0 +1,5 @@ +#### About +The `mispbridge` servlet. +* `./src` the code. +* `./war/wrapper` supplements needed for `.war`. +* `./war/.war` copy this to `tomcat/webapps`. \ No newline at end of file diff --git a/misp-fwd/install-locally.sh b/misp-fwd/install-locally.sh new file mode 100755 index 0000000..8282387 --- /dev/null +++ b/misp-fwd/install-locally.sh @@ -0,0 +1,10 @@ +#!/bin/bash +version="0.1" +file="target/misp-fwd-${version}.war" +groupId="com.olexyn.misp.fwd" +artifactId="misp-fwd" + + + +mvn package +mvn install:install-file -Dfile=${file} -DgroupId=${groupId} -DartifactId=${artifactId} -Dversion=${version} -Dpackaging=war -DgeneratePom=true diff --git a/misp-fwd/pom.xml b/misp-fwd/pom.xml new file mode 100644 index 0000000..1611c74 --- /dev/null +++ b/misp-fwd/pom.xml @@ -0,0 +1,97 @@ + + + + 4.0.0 + + com.olexyn.misp.fwd + misp-fwd + 0.1 + war + + misp-fwd Maven Webapp + + http://www.example.com + + + UTF-8 + + + + + + junit + junit + 4.11 + test + + + com.olexyn.misp.helper + misp-helper + 0.1 + compile + + + commons-io + commons-io + 2.6 + compile + + + org.eclipse.jetty.orbit + javax.servlet + 3.0.0.v201112011016 + compile + + + org.json + json + 20190722 + compile + + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + 11 + 11 + true + + + + maven-surefire-plugin + 2.22.1 + + + maven-war-plugin + 3.2.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + + diff --git a/misp-fwd/src/main/java/com/olexyn/misp/fwd/FwdProxy.java b/misp-fwd/src/main/java/com/olexyn/misp/fwd/FwdProxy.java new file mode 100644 index 0000000..07d0b01 --- /dev/null +++ b/misp-fwd/src/main/java/com/olexyn/misp/fwd/FwdProxy.java @@ -0,0 +1,217 @@ +package com.olexyn.misp.fwd; + +import com.olexyn.misp.helper.JsonHelper; +import com.olexyn.misp.helper.Ride; +import org.apache.commons.io.IOUtils; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Map; + +public class FwdProxy extends HttpServlet { + + protected static final String MISP_CLIENT_URL = "http://localhost:9090/mispclient/core"; + + public final Map available = new HashMap<>(); + public final Map booked = new HashMap<>(); + public final Map loaded = new HashMap<>(); + + // ####### + // + // ####### + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + + + final String payload = IOUtils.toString(request.getReader()); + final boolean isJson = JsonHelper.isJson(payload); + boolean hasID = false; + boolean hasRequest = false; + boolean hasData = false; + + if (isJson) { + final Ride ridePayload = new Ride(payload); + hasID = ridePayload.getID() != null; + hasRequest = ridePayload.getRequest() != null; + hasData = ridePayload.getData() != null; + } + + + if (isJson && hasID && hasRequest && hasData) { + Thread handleGetRideRequestDataThread = new Thread(() -> { + try { + handleGetRideRequestData(request, response); + } catch (IOException | InterruptedException e) { e.printStackTrace(); } + }); + handleGetRideRequestDataThread.setName("handleGetRideRequestDataThread"); + handleGetRideRequestDataThread.start(); + + } else { + Thread handleGetRequestThread = new Thread(() -> { + try { + handleGetRequest(request, response); + } catch (IOException | InterruptedException e) {e.printStackTrace(); } + }); + handleGetRequestThread.setName("handleGetRequestThread"); + handleGetRequestThread.start(); + } + + } + + + /** + * 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; + + + + //final ServletInputStream in = request.getInputStream(); + final String parsedRequest = null; //new String(in.readAllBytes()); + byte[] foo =null; + try{ + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + ObjectOutputStream objOut = new ObjectOutputStream(byteOut); + objOut.writeObject(request); + int br =0; + foo = byteOut.toByteArray(); + objOut.close(); + byteOut.close(); + br=1; + + }catch (IOException e){ + int br =0; + } + int br =0; + + + 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())) { + loaded.notify(); + 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) { + loaded.put(ride.getID(), ride); + loaded.notify(); + } + } + + // ####### + // + // ####### + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + Thread handlePostRideThread = new Thread(() -> { + try {handlePostRide(request, response); } catch (IOException | InterruptedException e) { e.printStackTrace(); } + }); + 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(); + } +} \ No newline at end of file diff --git a/misp-fwd/src/main/webapp/WEB-INF/web.xml b/misp-fwd/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..26f5142 --- /dev/null +++ b/misp-fwd/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,22 @@ + + + + + misp-fwd + + + misp-fwd + com.olexyn.misp.fwd.FwdProxy + + + + misp-fwd + /core + + + diff --git a/misp-fwd/src/main/webapp/images/io42630.png b/misp-fwd/src/main/webapp/images/io42630.png new file mode 100644 index 0000000000000000000000000000000000000000..01d99bf52efa311d32a32001004ab18602c9cd05 GIT binary patch literal 617 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k2}mkgS)OEIU^4J@aSW-L^X5t**C7W1R!8@u z|JwcL0wS|EM17r~$b7ciPC-FO=#Y{kRf+3&`G3gnuKq4;XJ&jcwcFJxLZxNk3D!A> zlO_s?aV8Q??AUdA_ifq!GiR>f|9AWUzVwYA9NjG&1{2Xx96_y^TMtY%|Nq+_=9;)I SZ)q`*!{F)a=d#Wzp$PyZXv%5; literal 0 HcmV?d00001 diff --git a/misp-fwd/src/main/webapp/index.jsp b/misp-fwd/src/main/webapp/index.jsp new file mode 100644 index 0000000..24fd5b1 --- /dev/null +++ b/misp-fwd/src/main/webapp/index.jsp @@ -0,0 +1,20 @@ + + +misp-fwd + + + + + + + + + + +
+ +
+

misp-fwd

+
+ + diff --git a/misp-helper/install-locally.sh b/misp-helper/install-locally.sh new file mode 100755 index 0000000..0fe410e --- /dev/null +++ b/misp-helper/install-locally.sh @@ -0,0 +1,10 @@ +#!/bin/bash +version="0.1" +file="target/misp-helper-${version}.jar" +groupId="com.olexyn.misp.helper" +artifactId="misp-helper" + + + +mvn package +mvn install:install-file -Dfile=${file} -DgroupId=${groupId} -DartifactId=${artifactId} -Dversion=${version} -Dpackaging=jar -DgeneratePom=true diff --git a/misp-helper/pom.xml b/misp-helper/pom.xml new file mode 100644 index 0000000..a23a86e --- /dev/null +++ b/misp-helper/pom.xml @@ -0,0 +1,87 @@ + + + + 4.0.0 + + com.olexyn.misp.helper + misp-helper + 0.1 + + misp-helper + A simple misp-helper. + + http://www.example.com + + + UTF-8 + 1.11 + 1.11 + + + + + junit + junit + 3.8.1 + + + org.json + json + 20190722 + + + + + + + + + maven-clean-plugin + 3.1.0 + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + + + + + + maven-project-info-reports-plugin + + + + diff --git a/misp-helper/src/main/java/com/olexyn/misp/helper/JsonHelper.java b/misp-helper/src/main/java/com/olexyn/misp/helper/JsonHelper.java new file mode 100644 index 0000000..d8a9b4c --- /dev/null +++ b/misp-helper/src/main/java/com/olexyn/misp/helper/JsonHelper.java @@ -0,0 +1,21 @@ +package com.olexyn.misp.helper; + +import org.json.JSONException; +import org.json.JSONObject; + +public class JsonHelper { + + + + + public static boolean isJson(String string){ + + try{ + new JSONObject(string); + }catch (JSONException | NullPointerException e){ + return false; + } + + return true; + } +} diff --git a/misp-helper/src/main/java/com/olexyn/misp/helper/Ride.java b/misp-helper/src/main/java/com/olexyn/misp/helper/Ride.java new file mode 100644 index 0000000..4f693b7 --- /dev/null +++ b/misp-helper/src/main/java/com/olexyn/misp/helper/Ride.java @@ -0,0 +1,139 @@ +package com.olexyn.misp.helper; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Objects; + +public class Ride { + + private static long count = 0L; + + final private Long id; + private String request; + private String data; + + // FUTURE it might be possible to use a ride for many requests. + // private List requests = new ArrayList<>(); + // private Map data = new HashMap<>(); + + + public Ride() { + id = count++; + } + + public Ride(String jsonString) { + + JSONObject obj = new JSONObject(); + try { + obj = new JSONObject(jsonString); + }catch (JSONException e){ + int br = 0; + } + + + long _id; + + try { + _id = obj.getLong("id"); + }catch (JSONException e){ + _id = count++; + } + id = _id; + try{ + request = obj.getString("request"); + } catch (JSONException e){ + request = null; + } + try{ + data = obj.getString("data"); + }catch (JSONException e){ + data = null; + } + + + } + + + public Ride(JSONObject obj){ + long _id; + + try { + _id = obj.getLong("id"); + }catch (JSONException e){ + _id = count++; + } + id = _id; + try{ + request = obj.getString("request"); + } catch (JSONException e){ + request = null; + } + try{ + data = obj.getString("data"); + }catch (JSONException e){ + data = null; + } + } + + + + + + public void setRequest(String request) { + this.request = request; + } + + public void setData(String data) { + this.data = data; + } + + + + + public String getRequest() { + return this.request; + } + + public String getData() { + return this.data; + } + + public Long getID() { + return this.id; + } + + + + private String brace(String foo) { + return "\"" + foo + "\""; + } + + private String unbrace(String foo) { return foo.replace("\"", ""); } + + public String json() { + JSONObject obj = new JSONObject(); + obj.put("id", id); + obj.put("request", request); + obj.put("data",data); + return obj.toString(); + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Ride ride = (Ride) o; + return Objects.equals(id, ride.id) && Objects.equals(request, ride.request) && Objects.equals(data, ride.data); + } + + @Override + public int hashCode() { + return Objects.hash(id, request, data); + } + + +} + + diff --git a/misp-helper/src/main/java/com/olexyn/misp/helper/WebPrint.java b/misp-helper/src/main/java/com/olexyn/misp/helper/WebPrint.java new file mode 100644 index 0000000..c6616a2 --- /dev/null +++ b/misp-helper/src/main/java/com/olexyn/misp/helper/WebPrint.java @@ -0,0 +1,66 @@ +package com.olexyn.misp.helper; + +import java.util.List; + +public class WebPrint { + + final static public String SPLIT = "io32413445353"; + + public static String list(List list, String type) { + StringBuilder sb = new StringBuilder(); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + + for (String entry : list) { + sb.append(""); + } + + return sb.toString(); + } + + + public static String requestList(List list) { + + StringBuilder sb = new StringBuilder(); + sb.append("
"); + sb.append("List: "); + sb.append(type); + sb.append("
"); + sb.append(entry); + sb.append("
"); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + + for (String entry : list) { + String[] split = entry.split(SPLIT); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + } + + return sb.toString(); + } + + + + +} diff --git a/misp-helper/src/site/site.xml b/misp-helper/src/site/site.xml new file mode 100644 index 0000000..dcd7d4f --- /dev/null +++ b/misp-helper/src/site/site.xml @@ -0,0 +1,26 @@ + + + + + misp-helper + https://maven.apache.org/images/apache-maven-project.png + https://www.apache.org/ + + + + https://maven.apache.org/images/maven-logo-black-on-white.png + https://maven.apache.org/ + + + + org.apache.maven.skins + maven-fluido-skin + 1.7 + + + + + + + \ No newline at end of file diff --git a/misp-rev/install-locally.sh b/misp-rev/install-locally.sh new file mode 100755 index 0000000..9504c27 --- /dev/null +++ b/misp-rev/install-locally.sh @@ -0,0 +1,10 @@ +#!/bin/bash +version="0.1" +file="target/misp-rev-${version}.war" +groupId="com.olexyn.misp.rev" +artifactId="misp-rev" + + + +mvn package +mvn install:install-file -Dfile=${file} -DgroupId=${groupId} -DartifactId=${artifactId} -Dversion=${version} -Dpackaging=war -DgeneratePom=true diff --git a/misp-rev/pom.xml b/misp-rev/pom.xml new file mode 100644 index 0000000..eff9fc0 --- /dev/null +++ b/misp-rev/pom.xml @@ -0,0 +1,90 @@ + + + + 4.0.0 + + com.olexyn.misp.rev + misp-rev + 0.1 + war + + misp-rev Maven Webapp + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + com.olexyn.misp.helper + misp-helper + 0.1 + compile + + + commons-io + commons-io + 2.6 + compile + + + org.eclipse.jetty.orbit + javax.servlet + 3.0.0.v201112011016 + compile + + + org.json + json + 20190722 + compile + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-war-plugin + 3.2.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + + diff --git a/misp-rev/src/main/java/com/olexyn/misp/rev/ConnectionHelper.java b/misp-rev/src/main/java/com/olexyn/misp/rev/ConnectionHelper.java new file mode 100644 index 0000000..f66d3f8 --- /dev/null +++ b/misp-rev/src/main/java/com/olexyn/misp/rev/ConnectionHelper.java @@ -0,0 +1,16 @@ +package com.olexyn.misp.rev; + +import com.olexyn.misp.helper.Ride; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import org.json.JSONObject; + +public class ConnectionHelper { + + + +} diff --git a/misp-rev/src/main/java/com/olexyn/misp/rev/Main.java b/misp-rev/src/main/java/com/olexyn/misp/rev/Main.java new file mode 100644 index 0000000..22843e6 --- /dev/null +++ b/misp-rev/src/main/java/com/olexyn/misp/rev/Main.java @@ -0,0 +1,12 @@ +package com.olexyn.misp.rev; + +public class Main { + + + + public static void main(String... args){ + new RevProxy(); + } + + +} diff --git a/misp-rev/src/main/java/com/olexyn/misp/rev/RevProxy.java b/misp-rev/src/main/java/com/olexyn/misp/rev/RevProxy.java new file mode 100644 index 0000000..c171c00 --- /dev/null +++ b/misp-rev/src/main/java/com/olexyn/misp/rev/RevProxy.java @@ -0,0 +1,169 @@ +package com.olexyn.misp.rev; + + +import com.olexyn.misp.helper.Ride; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + + +public class RevProxy { + + protected static final String MISP_BRIDGE_URL = "http://localhost:9090/mispbridge/core"; + protected static final String APP_URL = "http://localhost:9090/mirror/core"; + + public static final int AVAILABLE_RIDES_OVERHEAD_TRIGGER = 4; + public static final int AVAILABLE_RIDES_OVERHEAD = 8; + + + public final Map available = new HashMap<>(); + public final Map booked = new HashMap<>(); + public final Map loaded = new HashMap<>(); + + + public RevProxy() { + + Thread postRideThread = new Thread(new PostRideRunnable(this)); + postRideThread.setName("postRideThread"); + postRideThread.start(); + + Thread getRequestThread = new Thread(new GetRequestRunnable(this)); + getRequestThread.setName("getRequestThread"); + getRequestThread.start(); + + Thread getRideRequestDataThread = new Thread(new GetRideRequestDataRunnable(this)); + getRideRequestDataThread.setName("getRideRequestDataThread"); + getRideRequestDataThread.start(); + } + + + void sendPostRide() throws IOException { + + final Ride ride = new Ride(); + + synchronized (available) { available.put(ride.getID(), ride); } + + final String result = send("POST", MISP_BRIDGE_URL, ride.json()); + + synchronized (available) { + available.remove(ride.getID()); + ride.setRequest(new Ride(result).getRequest()); + } + + synchronized (booked) { booked.put(ride.getID(), ride); } + } + + + void sendGetRequest(Ride ride) throws IOException { + + synchronized (booked) {booked.remove(ride.getID()); } + + final String result = send("GET", APP_URL, ride.getRequest()); + ride.setData(result); + + synchronized (loaded) {loaded.put(ride.getID(), ride); } + } + + + void sendGetRideRequestData(Ride ride) throws IOException { + + send("GET", MISP_BRIDGE_URL, ride.json()); + + synchronized (loaded) {loaded.remove(ride.getID()); } + } + + + private static String send(String method, String urlString, String body) throws IOException { + + URL url = new URL(urlString); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod(method); + + connection.setDoOutput(true); + DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); + outputStream.writeBytes(body); + outputStream.flush(); + outputStream.close(); + + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String out; + StringBuilder sb = new StringBuilder(); + while ((out = in.readLine()) != null) { sb.append(out); } + in.close(); + return sb.toString(); + } + + +} + + +class PostRideRunnable implements Runnable { + + final private RevProxy adapter; + + public PostRideRunnable(RevProxy adapter) { + this.adapter = adapter; + } + + @Override + public void run() { + while (true) { + synchronized (adapter.available) { + if (adapter.available.size() < RevProxy.AVAILABLE_RIDES_OVERHEAD_TRIGGER) { + for (int i = 0; i < RevProxy.AVAILABLE_RIDES_OVERHEAD; i++) { + try {adapter.sendPostRide();} catch (IOException ignored) {} + } + } + } + } + } +} + + +class GetRequestRunnable implements Runnable { + + final private RevProxy adapter; + + public GetRequestRunnable(RevProxy adapter) { + this.adapter = adapter; + } + + @Override + public void run() { + while (true) { + synchronized (adapter.booked) { + if (adapter.booked.size() > 0) { + final Ride ride = adapter.booked.entrySet().iterator().next().getValue(); + try { adapter.sendGetRequest(ride); } catch (IOException ignored) {} + } + } + } + } +} + +class GetRideRequestDataRunnable implements Runnable { + + final private RevProxy adapter; + + public GetRideRequestDataRunnable(RevProxy adapter) { + this.adapter = adapter; + } + + @Override + public void run() { + while (true) { + synchronized (adapter.loaded) { + if (adapter.loaded.size() > 0) { + final Ride ride = adapter.loaded.entrySet().iterator().next().getValue(); + try { adapter.sendGetRideRequestData(ride); } catch (IOException ignored) {} + } + } + } + } +} \ No newline at end of file diff --git a/misp-rev/src/main/webapp/WEB-INF/web.xml b/misp-rev/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..717dad2 --- /dev/null +++ b/misp-rev/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,22 @@ + + + + + misp-fwd + + + misp-rev + com.olexyn.misp.rev.RevProxy + + + + misp-rev + /core + + + diff --git a/misp-rev/src/main/webapp/images/io42630.png b/misp-rev/src/main/webapp/images/io42630.png new file mode 100644 index 0000000000000000000000000000000000000000..01d99bf52efa311d32a32001004ab18602c9cd05 GIT binary patch literal 617 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k2}mkgS)OEIU^4J@aSW-L^X5t**C7W1R!8@u z|JwcL0wS|EM17r~$b7ciPC-FO=#Y{kRf+3&`G3gnuKq4;XJ&jcwcFJxLZxNk3D!A> zlO_s?aV8Q??AUdA_ifq!GiR>f|9AWUzVwYA9NjG&1{2Xx96_y^TMtY%|Nq+_=9;)I SZ)q`*!{F)a=d#Wzp$PyZXv%5; literal 0 HcmV?d00001 diff --git a/misp-rev/src/main/webapp/index.jsp b/misp-rev/src/main/webapp/index.jsp new file mode 100644 index 0000000..d53407d --- /dev/null +++ b/misp-rev/src/main/webapp/index.jsp @@ -0,0 +1,20 @@ + + +misp-rev + + + +
"); + sb.append("URL"); + sb.append(""); + sb.append("Method"); + sb.append(""); + sb.append("Query"); + sb.append("
"); + sb.append(split[0]); + sb.append(""); + sb.append(split[1]); + sb.append(""); + sb.append(split[2]); + sb.append("
+ + + + + + +
+ +
+

misp-rev

+
+ +