/*
* sets the response type
*/
response.setContentType("application/json");
JSONObject res = new JSONObject();
// Processes the Get Methods
try {
String path = request.getPathInfo().toLowerCase();
if (path.contains("/api/airportcodes")) {
// Outputs the airport codes
AirportCodes ac = AirportCodes.getInstance();
res = ac.getCodes();
} else if (path.contains("/api/airportcodebycity/")) {
// Gets Airport by City
int x = path.lastIndexOf("bycity/") + 7;
String p = path.substring(x);
log.info("Get Airport Code by City : " + p);
AirportCodes ac = AirportCodes.getInstance();
String code = ac.getCodeByCity(p);
res.put("code", code);
} else if (path.contains("/api/airportcodebycityandsate/")) {
// Gets Airport Code by City and State
int x = path.lastIndexOf("/api/airportcodebycityandsate/")
+ "/api/airportcodebycityandsate/".length();
String p = path.substring(x);
String[] par = p.split("/");
log.info("Get Airport Code by City and State : " + par[0] + " "
+ par[1]);
AirportCodes ac = AirportCodes.getInstance();
String code = ac.getCode(par[0], par[1]);
res.put("code", code);
} else if (path.contains("/api/flights/all")) {
// Gets the details of all flights
Flights fs = Flights.getInstance();
res = fs.getFlights();
} else if (path.contains("/api/flight/")) {
// Gets the details of a flight
Flights fs = Flights.getInstance();
int idx = path.indexOf("/api/flight/")
+ "/api/flight/".length();
String flightId = path.substring(idx);
res = fs.getFlightsByID(flightId);
} else if (path.contains("/api/logout")) {
// Logs out the user
HttpSession cur = request.getSession(false);
if (cur != null) {
cur.invalidate();
// request.logout();
}
res.put("result", "loggedout");
} else if (path.contains("/api/messageofday/")) {
// Returns the message of the day for Acme Airlines
MOTD motd = MOTD.getInstance();
res = motd.getRandomMessageOfTheDay();
} else if (path.contains("/api/fc/all")) {
FlightController fc = FlightController.getInstance();
res = fc.getAllFlightStatus();
} else if (path.contains("/api/fc/")) {
int idx = path.indexOf("/api/fc/") + "/api/fc/".length();
String flightId = path.substring(idx);
FlightController fc = FlightController.getInstance();
String status = fc.getFlightStatus(flightId);
res.put("status", status);
} else if (path.contains("/api/myflights/")) { //-
// gets flights for users
Principal prin = request.getUserPrincipal();
if (prin != null) {
String userId = prin.getName();
MyFlights mf = MyFlights.getInstance();
res = mf.getMyFlights(userId);
res.put("result", "success");
} else {
res.put("result", "notloggedin");
}
} else if (path.contains("/api/allflightrequests")) {
// gets all flight requests
Principal prin = request.getUserPrincipal();
if (prin != null) {
String flightId = request.getParameter("flightId");
if (flightId != null) {
MyFlights mf = MyFlights.getInstance();
res = mf.getCustomersByFlight(flightId);
} else {
if (request.isUserInRole("admin")) {
MyFlights mf = MyFlights.getInstance();
res = mf.getAll();
} else {
res.put("result", "not in admin role");
}
}
} else {
res.put("result", "notloggedin");
}
} else if (path.contains("/api/myflightreason")) {
// gets the reason for travel for a selected user and flight
Principal prin = request.getUserPrincipal();
if (prin != null) {
String flightId = request.getParameter("flightId");
String userId = request.getParameter("userId");
if (flightId != null && userId != null) {
MyFlights mf = MyFlights.getInstance();
res = mf.getReasonForTravel(userId, flightId);
} else {
res.put("result", "no parameters");
}
} else {
res.put("result", "notloggedin");
}
}
res.write(response.getWriter());
} catch (Exception e) {
e.printStackTrace();
}
}