@Override
 public Answer updateSystemsByUser(User user, List<UserSystem> newGroups) {
   Answer a = new Answer(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
   AnswerList an = this.readByUser(user.getLogin());
   if (an.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
     List<UserSystem> oldGroups = an.getDataList();
     // delete if don't exist in new
     for (UserSystem old : oldGroups) {
       if (!newGroups.contains(old)) {
         Answer del = userSystemDAO.remove(old);
         if (!del.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
           a = del;
         }
       }
     }
     // insert if don't exist in old
     for (UserSystem group : newGroups) {
       if (!oldGroups.contains(group)) {
         Answer add = userSystemDAO.create(group);
         if (!add.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
           a = add;
         }
       }
     }
   }
   return a;
 }
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  final void processRequest(final HttpServletRequest request, final HttpServletResponse response)
      throws ServletException, IOException, CerberusException, JSONException {
    JSONObject jsonResponse = new JSONObject();
    ApplicationContext appContext =
        WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    Answer ans = new Answer();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    ans.setResultMessage(msg);

    response.setContentType("text/html;charset=UTF-8");
    String charset = request.getCharacterEncoding();

    // Parameter that are already controled by GUI (no need to decode) --> We SECURE them
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String testbattery =
        ParameterParserUtil.parseStringParamAndDecodeAndSanitize(
            URLDecoder.decode(request.getParameter("testBattery"), "UTF-8"), null, charset);
    String description =
        ParameterParserUtil.parseStringParamAndDecodeAndSanitize(
            URLDecoder.decode(request.getParameter("description"), "UTF-8"), null, charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String batteryContent =
        ParameterParserUtil.parseStringParam(request.getParameter("batteryContent"), null);
    Answer finalAnswer = new Answer();
    if (StringUtil.isNullOrEmpty(testbattery)) {
      msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
      msg.setDescription(
          msg.getDescription()
              .replace("%ITEM%", "Battery")
              .replace("%OPERATION%", "Create")
              .replace("%REASON%", "Battery name is missing!"));
      finalAnswer.setResultMessage(msg);
    } else {
      ITestBatteryService testBatteryService = appContext.getBean(ITestBatteryService.class);
      IFactoryTestBattery factoryTestBattery = appContext.getBean(IFactoryTestBattery.class);

      TestBattery te = factoryTestBattery.create(0, testbattery, description);
      finalAnswer = testBatteryService.create(te);

      if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {

        /** Adding Log entry. */
        ILogEventService logEventService = appContext.getBean(LogEventService.class);
        logEventService.createPrivateCalls(
            "/CreateTestBattery", "CREATE", "Create Test Battery : " + testbattery, request);

        if (batteryContent != null) {

          JSONArray batteriesContent = new JSONArray(batteryContent);
          ITestBatteryContentService testBatteryContentService =
              appContext.getBean(ITestBatteryContentService.class);
          IFactoryTestBatteryContent factoryTestBatteryContent =
              appContext.getBean(IFactoryTestBatteryContent.class);
          ArrayList<TestBatteryContent> arr = new ArrayList<>();
          for (int i = 0; i < batteriesContent.length(); i++) {
            JSONObject bat = batteriesContent.getJSONObject(i);
            TestBatteryContent co =
                factoryTestBatteryContent.create(
                    0, bat.getString("test"), bat.getString("testCase"), testbattery);
            arr.add(co);
          }

          finalAnswer =
              testBatteryContentService.compareListAndUpdateInsertDeleteElements(
                  te.getTestbattery(), arr);
          if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /** Adding Log entry. */
            logEventService.createPrivateCalls(
                "/CreateTestBattery",
                "Create",
                "Create Test battery : " + te.getTestbattery(),
                request);
          }
        }
      }
    }

    /** Formating and returning the json result. */
    jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());

    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
  }
Beispiel #3
0
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, CerberusException, JSONException {
    JSONObject jsonResponse = new JSONObject();
    Answer ans = new Answer();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    ans.setResultMessage(msg);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);

    response.setContentType("application/json");

    /** Parsing and securing all required parameters. */
    String robot = policy.sanitize(request.getParameter("robot"));
    String host = policy.sanitize(request.getParameter("host"));
    String port = policy.sanitize(request.getParameter("port"));
    String platform = policy.sanitize(request.getParameter("platform"));
    String browser = policy.sanitize(request.getParameter("browser"));
    String version = policy.sanitize(request.getParameter("version"));
    String active = policy.sanitize(request.getParameter("active"));
    String description = policy.sanitize(request.getParameter("description"));
    String userAgent = policy.sanitize(request.getParameter("useragent"));
    Integer robotid = 0;
    boolean robotid_error = true;
    try {
      if (request.getParameter("robotid") != null && !request.getParameter("robotid").equals("")) {
        robotid = Integer.valueOf(policy.sanitize(request.getParameter("robotid")));
        robotid_error = false;
      }
    } catch (Exception ex) {
      robotid_error = true;
    }

    /** Checking all constrains before calling the services. */
    if (StringUtil.isNullOrEmpty(robot)) {
      msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
      msg.setDescription(
          msg.getDescription()
              .replace("%ITEM%", "Robot")
              .replace("%OPERATION%", "Update")
              .replace("%REASON%", "Robot name is missing."));
      ans.setResultMessage(msg);
    } else if (robotid_error) {
      msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
      msg.setDescription(
          msg.getDescription()
              .replace("%ITEM%", "Robot")
              .replace("%OPERATION%", "Update")
              .replace(
                  "%REASON%",
                  "Could not manage to convert robotid to an integer value or robotid is missing."));
      ans.setResultMessage(msg);
    } else {
      /** All data seems cleans so we can call the services. */
      ApplicationContext appContext =
          WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
      IRobotService robotService = appContext.getBean(IRobotService.class);

      AnswerItem resp = robotService.readByKeyTech(robotid);
      if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()))) {
        /** Object could not be found. We stop here and report the error. */
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(
            msg.getDescription()
                .replace("%ITEM%", "Robot")
                .replace("%OPERATION%", "Update")
                .replace("%REASON%", "Robot does not exist."));
        ans.setResultMessage(msg);

      } else {
        /**
         * The service was able to perform the query and confirm the object exist, then we can
         * update it.
         */
        Robot robotData = (Robot) resp.getItem();
        robotData.setRobot(robot);
        robotData.setHost(host);
        robotData.setPort(port);
        robotData.setPlatform(platform);
        robotData.setBrowser(browser);
        robotData.setVersion(version);
        robotData.setActive(active);
        robotData.setDescription(description);
        robotData.setUserAgent(userAgent);
        ans = robotService.update(robotData);

        if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
          /** Update was succesfull. Adding Log entry. */
          ILogEventService logEventService = appContext.getBean(LogEventService.class);
          logEventService.createPrivateCalls(
              "/UpdateRobot",
              "UPDATE",
              "Updated Robot : ['" + robotid + "'|'" + robot + "']",
              request);
        }
      }
    }

    /** Formating and returning the json result. */
    jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", ans.getResultMessage().getDescription());

    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
  }