コード例 #1
0
    // Processes the requests
    public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {

      if (req.getMethod().equals("update")) {

        //				update valuse on textviews in the main_activity
        tempUpdate =
            JSONHandler.testJSONRequest(serverURL, "read_temperature") + (char) 0x00B0 + "C";
        MainActivity.temp_tv.setText("Demo");
        MainActivity.temp_tv.setText(tempUpdate);
        ambUpdate = JSONHandler.testJSONRequest(serverURL, "read_ambient_light_intensity");
        MainActivity.ambient_tv.setText(ambUpdate);
        blindUpdate = JSONHandler.testJSONRequest(serverURL, "blindState");
        MainActivity.blindState_tv.setText(blindUpdate);

        String currentDateandTime = sdf.format(new Date());

        MainActivity.sensorHistory.add(
            1,
            "   "
                + currentDateandTime
                + "   "
                + tempUpdate
                + "   "
                + ambUpdate
                + "   "
                + blindUpdate);

        return new JSONRPC2Response("ok", req.getID());
      } else {
        // Method name not supported
        return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
      }
    }
コード例 #2
0
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    // declare rpc objects
    JSONRPC2Request req = null;
    JSONRPC2Response resp = null;

    try {
      /*
       * Had to change this, since after switching to the JS json-rpc lib
       * the request has no params ("json=" in our case) and the whole payload is the request data itself
       */
      StringBuffer jb = new StringBuffer();
      String line = null;
      // Getting the request reader in order to load the whole request date into a single string
      BufferedReader reader = request.getReader();
      while ((line = reader.readLine()) != null) jb.append(line);

      // populate rpc objects
      req = JSONRPC2Request.parse(jb.toString());
      resp = new JSONRPC2Response(req.getID());

      // define jsonResult
      JSONObject jsonResult = new JSONObject();

      // retrieve request information
      String method = req.getMethod();
      System.out.println(req.getMethod());

      // find the requested method
      if (method.equals("login")) {
        // login user
        jsonResult = loginUser(request, response, req);
        System.out.println("json result: " + jsonResult.toString());
        // resp.setResult(jsonResult.toJSONString());
        resp.setResult(jsonResult);
      } else if (method.equals("register")) {
        // register user
        // TODO duplicates!
        jsonResult = registerUser(req, resp);
        System.out.println("json result to string: " + jsonResult.toString());
        resp.setResult(jsonResult);
      } else if (isLoggedIn(request)) {
        if (method.equals("logout")) {
          // logout user
          jsonResult = logoutUser(request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("loadAllQuestions")) {
          // return all questions as QuestionsList
          QuestionsList questions = QuestionController.loadAllQuestions();
          if (questions != null) {
            JSONObject jsonQuestions = new JSONObject();
            JSONArray allQuestions = questions.toJSONArray();
            jsonQuestions.put("allQuestions", allQuestions);
            System.out.println("jsonQuestions: " + jsonQuestions.toString());
            resp.setResult(jsonQuestions);
          } else {
            resp.setError(JSONRPC2Error.INTERNAL_ERROR);
          }
        } else if (method.equals("loadQuestion")) {
          jsonResult = loadQuestion(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("addQuestion")) {
          // add question
          jsonResult = addQuestion(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("deleteQuestion")) {
          // delete question
          jsonResult = deleteQuestion(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("udpateQuestion")) {
          // update question
          jsonResult = updateQuestion(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("loadTestAndAllQuestions")) {
          // return the test and all questions from the database
          jsonResult = loadTestAndAllQuestions(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("loadAllTests")) {
          // return all questions as QuestionsList
          TestsList tests = TestController.loadAllTests();
          if (tests != null) {
            JSONObject jsonTests = new JSONObject();
            JSONArray allTests = tests.toJSONArray();
            jsonTests.put("allTests", allTests);
            System.out.println("jsonTests: " + jsonTests.toString());
            resp.setResult(jsonTests);

          } else {
            resp.setError(JSONRPC2Error.INTERNAL_ERROR);
          }
        } else if (method.equals("loadQuestionsByTest")) {
          JSONArray testQuestions = new JSONArray();
          testQuestions = loadQuestionsByTest(req, request);
          jsonResult.put("testQuestions", testQuestions);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("addTest")) {
          // add question
          jsonResult = addTest(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("deleteTest")) {
          // delete question
          jsonResult = deleteTest(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("updateTest")) {
          // update question
          jsonResult = updateTest(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("addTestQuestion")) {
          // bind question to test
          jsonResult = addTestQuestion(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("removeTestQuestion")) {
          // delete question from test
          jsonResult = removeTestQuestion(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        } else if (method.equals("checkTest")) {
          // check if test is asnwered correctly
          jsonResult = checkTest(req, request);
          System.out.println("json result: " + jsonResult.toString());
          resp.setResult(jsonResult);
        }
        /*
        JSONObject json = new JSONObject();
        json.put("hi", "opa");
        resp.setResult(json.toJSONString());
        */
      }
    } catch (Throwable t) {
      JSONRPC2Error error = new JSONRPC2Error(1, t.getMessage());

      resp.setError(error);
    } finally {
      // dispatch result
      response.getWriter().print(resp.toJSONObject().toJSONString());
      System.out.println("final result: " + resp.toJSONObject().toJSONString());
    }
  }