|
|
|
@ -11,12 +11,20 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public final class ClientServlet extends HttpServlet {
|
|
|
|
|
|
|
|
|
|
private static final String MISP_BRIDGE_URL = "http://localhost:9090/mispbridge/core";
|
|
|
|
|
|
|
|
|
|
private List<Ride> availableRides = new ArrayList<>();
|
|
|
|
|
private List<Ride> reservedRides = new ArrayList<>();
|
|
|
|
|
private List<Ride> newRequests = new ArrayList<>();
|
|
|
|
|
private List<Ride> forwardedRequests = new ArrayList<>();
|
|
|
|
|
private List<Ride> newData = new ArrayList<>();
|
|
|
|
|
private List<Ride> forwardedData = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ClientServlet() {
|
|
|
|
@ -87,9 +95,6 @@ public final class ClientServlet extends HttpServlet {
|
|
|
|
|
writer.println("</tr>");
|
|
|
|
|
writer.println(make2ColumnRow("foo", "bar"));
|
|
|
|
|
|
|
|
|
|
writer.println(make2ColumnRow("sendGet", sendGet()));
|
|
|
|
|
writer.println(make2ColumnRow("sendPost", sendPost()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writer.println("</table>");
|
|
|
|
|
writer.println("</body>");
|
|
|
|
@ -97,7 +102,7 @@ public final class ClientServlet extends HttpServlet {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String make2ColumnRow(String one, String two){
|
|
|
|
|
private String make2ColumnRow(String one, String two) {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
sb.append("<tr>");
|
|
|
|
|
sb.append("<td>");
|
|
|
|
@ -112,11 +117,9 @@ public final class ClientServlet extends HttpServlet {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String sendGet() throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
URL url = new URL(MISP_BRIDGE_URL);
|
|
|
|
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
|
|
|
@ -148,48 +151,141 @@ public final class ClientServlet extends HttpServlet {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void sendPostRide() throws IOException {
|
|
|
|
|
|
|
|
|
|
URL url = new URL(MISP_BRIDGE_URL);
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
|
|
|
|
// prepare
|
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
|
connection.setRequestProperty("User-Agent", "USER_AGENT");
|
|
|
|
|
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
|
|
|
|
connection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
|
|
|
|
|
|
Ride ride = new Ride();
|
|
|
|
|
availableRides.add(ride);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// send POST
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
|
|
|
|
|
outputStream.writeBytes(ride.json());
|
|
|
|
|
outputStream.flush();
|
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// read OK
|
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
|
String output;
|
|
|
|
|
StringBuilder response = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
while ((output = in.readLine()) != null) {
|
|
|
|
|
response.append(output);
|
|
|
|
|
}
|
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// process
|
|
|
|
|
availableRides.remove(ride);
|
|
|
|
|
reservedRides.add(ride);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String sendPost() throws IOException{
|
|
|
|
|
private void sendGetRide(Ride ride) throws IOException {
|
|
|
|
|
|
|
|
|
|
URL url = new URL(MISP_BRIDGE_URL);
|
|
|
|
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
|
|
|
|
// Setting basic post request
|
|
|
|
|
con.setRequestMethod("POST");
|
|
|
|
|
con.setRequestProperty("User-Agent", "USER_AGENT");
|
|
|
|
|
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
|
|
|
|
con.setRequestProperty("Content-Type","application/json");
|
|
|
|
|
// prepare
|
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
|
connection.setRequestProperty("User-Agent", "USER_AGENT");
|
|
|
|
|
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
|
|
|
|
connection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
|
|
|
|
|
|
String postJsonData = "{\"id\":5,\"countryName\":\"USA\",\"population\":8000}";
|
|
|
|
|
|
|
|
|
|
// Send post request
|
|
|
|
|
con.setDoOutput(true);
|
|
|
|
|
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
|
|
|
|
wr.writeBytes(postJsonData);
|
|
|
|
|
wr.flush();
|
|
|
|
|
wr.close();
|
|
|
|
|
|
|
|
|
|
int responseCode = con.getResponseCode();
|
|
|
|
|
System.out.println("nSending 'POST' request to URL : " + url);
|
|
|
|
|
System.out.println("Post Data : " + postJsonData);
|
|
|
|
|
System.out.println("Response Code : " + responseCode);
|
|
|
|
|
|
|
|
|
|
BufferedReader in = new BufferedReader(
|
|
|
|
|
new InputStreamReader(con.getInputStream()));
|
|
|
|
|
// send POST
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
|
|
|
|
|
outputStream.writeBytes(ride.json());
|
|
|
|
|
outputStream.flush();
|
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// read OK
|
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
|
String output;
|
|
|
|
|
StringBuffer response = new StringBuffer();
|
|
|
|
|
StringBuilder response = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
while ((output = in.readLine()) != null) {
|
|
|
|
|
response.append(output);
|
|
|
|
|
}
|
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
|
|
//printing result from response
|
|
|
|
|
System.out.println();
|
|
|
|
|
|
|
|
|
|
Ride rideRequest = new Ride(response.toString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// process
|
|
|
|
|
// TODO add checks
|
|
|
|
|
reservedRides.remove(ride);
|
|
|
|
|
newRequests.add(rideRequest);
|
|
|
|
|
sendGetRequest(rideRequest.getRequest());
|
|
|
|
|
newRequests.remove(rideRequest);
|
|
|
|
|
forwardedRequests.add(rideRequest);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendGetRequest(String request) throws IOException {
|
|
|
|
|
|
|
|
|
|
URL url = new URL(MISP_BRIDGE_URL);
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
|
|
|
|
// prepare
|
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
|
connection.setRequestProperty("User-Agent", "USER_AGENT");
|
|
|
|
|
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
|
|
|
|
connection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO replace
|
|
|
|
|
Ride ride = new Ride();
|
|
|
|
|
|
|
|
|
|
// send POST
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
|
|
|
|
|
outputStream.writeBytes(ride.json());
|
|
|
|
|
outputStream.flush();
|
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// read OK
|
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
|
String output;
|
|
|
|
|
StringBuilder response = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
while ((output = in.readLine()) != null) {
|
|
|
|
|
response.append(output);
|
|
|
|
|
}
|
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ride rideRequest = new Ride(response.toString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// process
|
|
|
|
|
// TODO add checks
|
|
|
|
|
reservedRides.remove(ride);
|
|
|
|
|
newRequests.add(ride);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|