Package org.apache.wink.json4j

Examples of org.apache.wink.json4j.JSONObject


        if(!"lists".equalsIgnoreCase(paths[0]) && paths.length > 0 && paths[1].equalsIgnoreCase("lists")){
            //Must be flights for a specific user
            String userId = paths[0];
            String postBody = getBody(req);
            try {
                JSONObject flight = new JSONObject(postBody);
                if(!flight.has(FLIGHT_ID) || !flight.has(APPROVER_ID)) {
                    resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
                            "The POST body must have the following properties: FlightId, UserId, ApproverId");
                    return;
                }
                String flightId = flight.getString(FLIGHT_ID);
                if(flightAlreadyBooked(flightId, userId)) {
                    resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
                    resp.flushBuffer();
                    return;
                }
                MyFlights.getInstance().addMyFlight(flight.getString(FLIGHT_ID), userId,
                        flight.getString(APPROVER_ID), flight.optString(REASON));
                resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
                resp.flushBuffer();
                return;
            } catch (JSONException e) {
                resp.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
View Full Code Here


                        " with the flight number " + flightId + ".");
                return;
            }
            try {
                MyFlights myFlights = MyFlights.getInstance();
                JSONObject updatedFlight = new JSONObject(putBody);
                JSONObject currentFlight = myFlights.getFlight(userId, flightId);
                String approverId = updatedFlight.has(APPROVER_ID) ? updatedFlight.getString(APPROVER_ID) :
                    currentFlight.getString(APPROVER_ID);
                String reason = updatedFlight.has(REASON) ? updatedFlight.getString(REASON) :
                    currentFlight.optString(REASON);
                String state = updatedFlight.has(STATE) ? updatedFlight.getString(STATE) : currentFlight.getString(STATE);
                JSONObject responseFlight = myFlights.updateMyFlight(userId, flightId, approverId, reason, state);
                writeJsonObjectResponse(responseFlight, resp);
                resp.flushBuffer();
            } catch (JSONException e) {
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
                return;
View Full Code Here

            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid request");
        }
    }

    private boolean flightAlreadyBooked(String flightId, String userId) {
        JSONObject myFlightsObj = MyFlights.getInstance().getMyFlights(userId);
        if(myFlightsObj.has("myflights")) {
            try {
                JSONArray myFlights = myFlightsObj.getJSONArray("myflights");
                Iterator<JSONObject> iter = myFlights.iterator();
                while(iter.hasNext()) {
                    if(iter.next().getString(FLIGHT_ID).equals(flightId)) {
                        return true;
                    }
View Full Code Here

    /*
     * 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();
    }
  }
View Full Code Here

    /*
     * sets the response type
     */
    response.setContentType("application/json");
    JSONObject res = new JSONObject();

    try {

      if (path.contains("/api/flight")) {
        // adds a new flight to the flights controller
        JSONObject flightObj = new JSONObject(request.getInputStream());

        FlightController fc = FlightController.getInstance();
        String flightId = (String) flightObj.get("flightId");
        String state = (String) flightObj.get("state");
        fc.addFlight(flightId, state);
        res.put("result", "added");

      } else if (path.contains("/api/flights")) {
        // adds a flight status
        Flights fs = Flights.getInstance();
        JSONObject o = new JSONObject(request.getInputStream());
        String flight = (String) o.get("flight");
        String depart = (String) o.get("depart");
        String arrive = (String) o.get("arrive");
        String time = (String) o.get("time");
        String flightTime = (String) o.get("flighttime");
        fs.add(flight, depart, arrive, time, flightTime);
        res.put("result", "added");

      } else if (path.contains("/api/addmyflight")) {

      } else if (path.contains("/api/login")) {

        // Manages the Login without the ugly basic popup window
        JSONObject loginObj = new JSONObject(request.getInputStream());
        String user = (String) loginObj.get("username");
        String pass = (String) loginObj.get("password");

        // Logs in the user
//        request.login(user, pass);

        if (request.getUserPrincipal().getName().compareTo(user) == 0) {
          res.put("status", "loggedin");
        } else {
          res.put("status", "error in login");
        }
      } else if (path.contains("/api/fc/")) {
        // Creates a new flight status
        JSONObject o = new JSONObject(request.getInputStream());
        String flightId = o.getString("flightId");
        String state = o.getString("state");
        FlightController fc = FlightController.getInstance();
        fc.addFlight(flightId, state);
        res.put("result", "added");
      } else if (path.contains("/api/myflights/")) {
        // Adds a flight
        Principal prin = request.getUserPrincipal();
        if (prin != null) {
          String userId = prin.getName();
          JSONObject o = new JSONObject(request.getInputStream());
          String flightId = o.getString("FlightId");
          String approverId = o.getString("ApproverId");
          String reason = o.getString("Reason");
          MyFlights mf = MyFlights.getInstance();
          mf.addMyFlight(flightId, userId, approverId, reason);
          res.put("result", "success");
        } else {
          res.put("result", "notloggedin");
        }

      } else if (path.contains("/api/approveflight/")) {
        // approves a flight
        Principal prin = request.getUserPrincipal();
        if (prin != null) {
          String approverId = prin.getName();
          JSONObject o = new JSONObject(request.getInputStream());
          String userId = o.getString("userId");
          String flightId = o.getString("flightId");
          MyFlights mf = MyFlights.getInstance();
          mf.approveMyFlight(userId, flightId, approverId);
          res.put("result", "success");
        } else {
          res.put("result", "notloggedin");
View Full Code Here

    /*
     * sets the response type
     */
    response.setContentType("application/json");
    JSONObject res = new JSONObject();

    try {
      if (path.contains("/api/flights")) {
        // deletes a flight status
        Flights fs = Flights.getInstance();
        JSONObject o = new JSONObject(request.getInputStream());
        String flight = (String) o.get("flight");
        fs.removeFlight(flight);

      } else if (path.contains("/api/fc/")) {
        // deletes a new flight status
        JSONObject o = new JSONObject(request.getInputStream());
        String flightId = o.getString("flightId");
        FlightController fc = FlightController.getInstance();
        fc.removeFlightStatus(flightId);
        res.put("result", "deleted");
      } else if (path.contains("/api/myflights/")) {
        // gets all flight requests
        Principal prin = request.getUserPrincipal();

        if (prin != null) {
          MyFlights mf = MyFlights.getInstance();
          JSONObject o = new JSONObject(request.getInputStream());
          String userId = o.getString("userId");
          String flightId = o.getString("flightId");
          mf.removeMyFlights(userId, flightId);
          res.put("result", "flightremoved");

        } else {
          res.put("result", "notloggedin");
View Full Code Here

    /*
     * sets the response type
     */
    response.setContentType("application/json");
    JSONObject res = new JSONObject();

    try {
      if (path.contains("/api/flightstatus")) {
        // updates a flight status
        Flights fs = Flights.getInstance();
        JSONObject o = new JSONObject(request.getInputStream());
        String flight = (String) o.get("flight");
        String depart = (String) o.get("depart");
        String arrive = (String) o.get("arrive");
        String time = (String) o.get("time");
        String flightTime = (String) o.get("flighttime");
        fs.update(flight, depart, arrive, time, flightTime);

      }

      else if (path.contains("/api/fc/")) {
        // Creates a new flight status
        JSONObject o = new JSONObject(request.getInputStream());
        String flightId = o.getString("flightId");
        String state = o.getString("state");
        FlightController fc = FlightController.getInstance();
        fc.updateFlightStatus(flightId, state);
        res.put("result", "updated");
      }

      else if (path.contains("/api/myflights/")) {
        // updates a flight
        Principal prin = request.getUserPrincipal();
        if (prin != null) {
          String userId = prin.getName();
          JSONObject o = new JSONObject(request.getInputStream());
          String flightId = o.getString("FlightId");
          String approverId = o.getString("ApproverId");
          String reason = o.getString("Reason");
          String state = o.getString("state");
          MyFlights mf = MyFlights.getInstance();
          mf.updateMyFlight(userId, flightId, approverId, reason,
              state);
          res.put("result", "success");
        } else {
View Full Code Here

   *
   * @param codestream
   */
  public void load(InputStream codestream) {
    try {
      flights = new JSONObject(codestream);
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
View Full Code Here

   */
  public void add(String flight, String depart, String arrive, String time,
      String flightTime) {
    try {
      // Creates a new flight
      JSONObject newFlight = new JSONObject();
      newFlight.put("Flight", flight);
      newFlight.put("Depart", depart);
      newFlight.put("Arrive", arrive);
      newFlight.put("Time", time);
      newFlight.put("FlightTime", flightTime);

      JSONArray flts = (JSONArray) flights.get("flights");
      flts.add(newFlight);

      flights.remove("flights");
View Full Code Here

   *
   * @param flight
   * @return
   */
  public JSONObject getFlightsByID(String flight) {
    JSONObject res = null;
    try {
      JSONArray flts = (JSONArray) flights.get("flights");
     
      @SuppressWarnings("rawtypes")
      Iterator iter = flts.iterator();
      boolean found = false;
      while(iter.hasNext() && !found){
        JSONObject i = (JSONObject) iter.next();
        String flightId = i.getString("Flight");
        if(flightId.compareTo(flight)==0){
          res = i;
          found = true;
        }
      }
View Full Code Here

TOP

Related Classes of org.apache.wink.json4j.JSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.