Skip to content

Instantly share code, notes, and snippets.

@Aletho95
Created December 2, 2019 15:10
distributed system help
package com.mycompany.eventer;
import io.javalin.Javalin;
import static io.javalin.apibuilder.ApiBuilder.*;
/**
*
* @author jens
*/
public class API {
private final int PORT;
public API(int port) {
this.PORT = port;
}
public void start() {
Javalin app = Javalin.create()
.enableRouteOverview("/routes")
.enableCorsForAllOrigins()
.enableAutogeneratedEtags()
.port(PORT);
APIHandler handler = new APIHandler();
setRoutes(app, handler);
app.start();
}
private void setRoutes(Javalin app, APIHandler handler) {
app.routes(() -> {
path("/api", () -> {
//post calls
post("/create-meetup/:creator/:name/:location/:date/:description/:maxparticipants", handler::createMeetup);
post("/create-corporateevent/:task/:name/:location/:date/:description/:maxparticipants", handler::createCorporateEvent);
});
get("/get-meetup/:id", handler::getMeetup);
});
}
}
package com.mycompany.presentation;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mycompany.domain.event.Meetup;
import com.mycompany.domain.event.Task;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
/**
*
* @author jens
*/
public class APIcaller {
private int port;
private String ipAddress;
public APIcaller(int port, String ipAddress) {
this.port = port;
this.ipAddress = ipAddress;
}
public int getPort() {
return port;
}
public String getIpAddress() {
return ipAddress;
}
public void createMeetup(String creator, String name, String location, Date date, String description, int maxParticipants) {
String routeUrl = "create-meetup/" + creator + "/" + name + "/" + location + "/" + date + "/" + description + "/" + maxParticipants;
writeToServer(routeUrl);
}
public String getMeetup(int id) throws UnirestException {
String routeUrl = "get-meetup/" + id;
String t = getFromServer(routeUrl);
return t;
}
public void createCorperateEvent(List<Task> list, String name, String location, String date, String description, int maxParticipants) {
StringBuilder builder = new StringBuilder();
for (Task task : list) {
builder.append(task.getTaskName());
builder.append("-");
builder.append(task.getDescription());
builder.append("-");
builder.append(task.isDefault());
builder.append("§");
}
String routeUrl = "create-corporateevent/" + builder.toString() + "/" + name + "/" + location + "/" + date + "/" + description + "/" + maxParticipants;
writeToServer(routeUrl);
}
private boolean writeToServer(String routeUrl) {
String ipAddress = this.getIpAddress();
int port = this.getPort();
String url = "http://" + ipAddress + ":" + port + "/api/" + routeUrl;
try {
InetAddress inet = InetAddress.getByName(ipAddress);
if (inet.isReachable(port)) {
HttpResponse res = Unirest.post(url).asString();
if (res.getStatus() == 200) {
return true;
}
}
System.out.println("GNode did not respond on url: " + url);
return false;
} catch (UnirestException ex) {
ex.getMessage();
return false;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
private String getFromServer(String routeUrl) throws UnirestException {
String ipAddress = this.getIpAddress();
int port = this.getPort();
String url = "http://" + ipAddress + ":" + port + "/api/" + routeUrl;
try {
InetAddress inet = InetAddress.getByName(ipAddress);
if (inet.isReachable(port)) {
HttpResponse res = Unirest.get(url).asString();
String body = Unirest.get(url).asString().getBody();
if (res.getStatus() == 200) {
return body;
}
}
System.out.println("GNode did not respond on url: " + url);
return "";
} catch (UnirestException ex) {
ex.getMessage();
return "";
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
package com.mycompany.eventer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mycompany.domain.event.EventManager;
import com.mycompany.domain.event.Meetup;
import com.mycompany.domain.event.Task;
import io.javalin.Context;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author jens
*/
public class APIHandler {
EventManager eventManager;
public APIHandler() {
eventManager = new EventManager();
}
public void createCorporateEvent(Context context) {
try {
Scanner scanner = new Scanner(context.pathParam("task"));
Scanner scan;
String[] stringArray = scanner.delimiter().split("§");
List<Task> listOfTasks = new ArrayList<>();
Task task;
for (String string : stringArray) {
scan = new Scanner(string);
String[] taskFromArray = scan.delimiter().split("-");
if (taskFromArray[2].equals("true")) {
task = new Task(taskFromArray[0], taskFromArray[1], true);
} else {
task = new Task(taskFromArray[0], taskFromArray[1], false);
}
listOfTasks.add(task);
}
eventManager.createCorporateEvent(listOfTasks,
context.pathParam("name"),
context.pathParam("location"),
context.pathParam("date"),
context.pathParam("description"),
Integer.parseInt(context.pathParam("maxparticipants")));
} catch (NumberFormatException e) {
context.status(400);
}
}
public void createMeetup(Context context) {
System.out.println("Route called: addData");
try {
eventManager.createMeetupEvent(context.pathParam("creator"),
context.pathParam("name"),
context.pathParam("location"),
context.pathParam("date"),
context.pathParam("description"),
Integer.parseInt(context.pathParam("maxparticipants")));
} catch (NumberFormatException e) {
context.status(400);
}
}
public Meetup getMeetup(Context context) {
System.out.println("Route called: getData");
Meetup meetup = eventManager.readMeetupEvent(Integer.parseInt(context.pathParam("id")));
ObjectMapper mapper = new ObjectMapper();
if (meetup != null) {
try {
context.json(mapper.writeValueAsString(meetup));
} catch (JsonProcessingException e) {
context.status(500);
e.printStackTrace();
}
} else {
context.status(500);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment