+ upload only needed

pull/1/head
Ivan Olexyn 5 years ago
parent 645f18a1f3
commit 42dc6f1236

@ -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/<name>.war` copy this to `tomcat/webapps`.

@ -0,0 +1,40 @@
<?xml version="1.0" ?>
<project name="mirror" basedir="." default="war">
<!-- DEFINE SOME VARS -->
<property name="appname" value="mirror" />
<path id="compile.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
<!-- INIT -->
<target name="init">
<mkdir dir="build/classes"/>
</target>
<!-- COMPILE -->
<target name="compile" depends="init" >
<javac destdir="build/classes" debug="true" srcdir="src">
<classpath refid="compile.classpath"/>
</javac>
</target>
<!-- WAR -->
<target name="war" depends="compile">
<war destfile="${appname}A.war" webxml="web.xml">
<fileset dir="web"/>
<lib dir="lib"/>
<classes dir="build/classes"/>
</war>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>

@ -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<String> 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("<!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 (list) {
print.println(WebPrint.requestList(list));
}
print.println(" </body></html>");
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
addRequest(request);
}
@Override
public void doPut(HttpServletRequest request, HttpServletResponse response){
addRequest(request);
}
}

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Mirror</display-name>
<description>
This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide.
</description>
<servlet>
<servlet-name>Mirror</servlet-name>
<servlet-class>com.olexyn.mirror.Mirror</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Mirror</servlet-name>
<url-pattern>/core</url-pattern>
</servlet-mapping>
</web-app>

@ -0,0 +1,23 @@
<html>
<head>
<title>Sample Application JSP Page</title>
</head>
<body bgcolor=white>
<table border="0">
<tr>
<td align=center>
<img src="images/tomcat.gif">
</td>
<td>
<h1>Sample Application JSP Page</h1>
This is the output of a JSP page that is part of the Hello, World
application.
</td>
</tr>
</table>
<%= new String("Hello!") %>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@ -0,0 +1,25 @@
<html>
<head>
<title>Mirror</title>
</head>
<body bgcolor=white>
<table border="0">
<tr>
<td>
<img src="images/tomcat.gif">
</td>
<td>
<h1>Mirror</h1>
</td>
</tr>
</table>
<ul>
<p>This Servlet currently provides the following features:
<!--<li>To a <a href="hello.jsp">JSP page</a>.-->
<li><a href="core">Mirror</a> of all requests.
</ul>
</body>
</html>

@ -0,0 +1,5 @@
#### About
The `mispbridge` servlet.
* `./src` the code.
* `./war/wrapper` supplements needed for `.war`.
* `./war/<name>.war` copy this to `tomcat/webapps`.

@ -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

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.olexyn.misp.fwd</groupId>
<artifactId>misp-fwd</artifactId>
<version>0.1</version>
<packaging>war</packaging>
<name>misp-fwd Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.olexyn.misp.helper</groupId>
<artifactId>misp-helper</artifactId>
<version>0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.orbit</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0.0.v201112011016</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<!-- or whatever version you use -->
<source>11</source>
<target>11</target>
<verbose>true</verbose>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

@ -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<Long, Ride> available = new HashMap<>();
public final Map<Long, Ride> booked = new HashMap<>();
public final Map<Long, Ride> 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();
}
}

@ -0,0 +1,22 @@
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>misp-fwd</display-name>
<servlet>
<servlet-name>misp-fwd</servlet-name>
<servlet-class>com.olexyn.misp.fwd.FwdProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>misp-fwd</servlet-name>
<url-pattern>/core</url-pattern>
</servlet-mapping>
</web-app>

Binary file not shown.

After

Width:  |  Height:  |  Size: 617 B

@ -0,0 +1,20 @@
<html>
<head>
<title>misp-fwd</title>
</head>
<body bgcolor=white>
<table border="0">
<tr>
<td align=center>
<img src="images/io42630.png">
</td>
</tr>
<tr>
<td>
<h1>misp-fwd</h1>
</td>
</tr>
</table>
</body>
</html>

@ -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

@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.olexyn.misp.helper</groupId>
<artifactId>misp-helper</artifactId>
<version>0.1</version>
<name>misp-helper</name>
<description>A simple misp-helper.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.11</maven.compiler.source>
<maven.compiler.target>1.11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>

@ -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;
}
}

@ -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<String> requests = new ArrayList<>();
// private Map<String,String> 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);
}
}

@ -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<String> list, String type) {
StringBuilder sb = new StringBuilder();
sb.append("<table style=\"width:100%\">");
sb.append("<tr>");
sb.append("<th>");
sb.append("List: ");
sb.append(type);
sb.append("</th>");
sb.append("</tr>");
for (String entry : list) {
sb.append("<tr><td>");
sb.append(entry);
sb.append("</td></tr>");
}
return sb.toString();
}
public static String requestList(List<String> list) {
StringBuilder sb = new StringBuilder();
sb.append("<table style=\"width:100%\">");
sb.append("<tr>");
sb.append("<th>");
sb.append("URL");
sb.append("</th>");
sb.append("<th>");
sb.append("Method");
sb.append("</th>");
sb.append("<th>");
sb.append("Query");
sb.append("</th>");
sb.append("</tr>");
for (String entry : list) {
String[] split = entry.split(SPLIT);
sb.append("<tr>");
sb.append("<td>");
sb.append(split[0]);
sb.append("</td>");
sb.append("<td>");
sb.append(split[1]);
sb.append("</td>");
sb.append("<td>");
sb.append(split[2]);
sb.append("</td>");
sb.append("</tr>");
}
return sb.toString();
}
}

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="misp-helper" xmlns="http://maven.apache.org/DECORATION/1.8.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.8.0 http://maven.apache.org/xsd/decoration-1.8.0.xsd">
<bannerLeft>
<name>misp-helper</name>
<src>https://maven.apache.org/images/apache-maven-project.png</src>
<href>https://www.apache.org/</href>
</bannerLeft>
<bannerRight>
<src>https://maven.apache.org/images/maven-logo-black-on-white.png</src>
<href>https://maven.apache.org/</href>
</bannerRight>
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
<version>1.7</version>
</skin>
<body>
<menu ref="parent" />
<menu ref="reports" />
</body>
</project>

@ -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

@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.olexyn.misp.rev</groupId>
<artifactId>misp-rev</artifactId>
<version>0.1</version>
<packaging>war</packaging>
<name>misp-rev Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.olexyn.misp.helper</groupId>
<artifactId>misp-helper</artifactId>
<version>0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.orbit</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0.0.v201112011016</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

@ -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 {
}

@ -0,0 +1,12 @@
package com.olexyn.misp.rev;
public class Main {
public static void main(String... args){
new RevProxy();
}
}

@ -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<Long, Ride> available = new HashMap<>();
public final Map<Long, Ride> booked = new HashMap<>();
public final Map<Long, Ride> 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) {}
}
}
}
}
}

@ -0,0 +1,22 @@
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>misp-fwd</display-name>
<servlet>
<servlet-name>misp-rev</servlet-name>
<servlet-class>com.olexyn.misp.rev.RevProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>misp-rev</servlet-name>
<url-pattern>/core</url-pattern>
</servlet-mapping>
</web-app>

Binary file not shown.

After

Width:  |  Height:  |  Size: 617 B

@ -0,0 +1,20 @@
<html>
<head>
<title>misp-rev</title>
</head>
<body bgcolor=white>
<table border="0">
<tr>
<td align=center>
<img src="images/io42630.png">
</td>
</tr>
<tr>
<td>
<h1>misp-rev</h1>
</td>
</tr>
</table>
</body>
</html>
Loading…
Cancel
Save