Ejemplo n.º 1
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
   */
  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();
  }
Ejemplo n.º 2
0
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    ApplicationContext appContext =
        WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    testBatteryService = appContext.getBean(ITestBatteryService.class);
    factoryTestBatteryContent = appContext.getBean(IFactoryTestBatteryContent.class);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);

    String jsonResponse = "-1";
    String testbattery = policy.sanitize(request.getParameter("TestBattery"));
    String testBatteryName;

    String[] testcasesselected = request.getParameterValues("testcaseselected");

    if (testcasesselected == null) {
      response.setStatus(404);
      jsonResponse = "Please select at least one testcase !";
    } else {
      try {
        testBatteryName =
            testBatteryService.findTestBatteryByKey(Integer.parseInt(testbattery)).getTestbattery();
      } catch (CerberusException ex) {
        MyLogger.log(AddTestBatteryContent.class.getName(), Level.DEBUG, ex.getMessage());
        testBatteryName = null;
      }

      if (testBatteryName != null) {
        String test;
        String testcase;

        // response.setContentType("text/html");
        for (String testcaseselect : testcasesselected) {
          test =
              policy.sanitize(
                  URLDecoder.decode(
                      testcaseselect.split("Test=")[1].split("&TestCase=")[0], "UTF-8"));
          testcase =
              policy.sanitize(URLDecoder.decode(testcaseselect.split("&TestCase=")[1], "UTF-8"));
          try {

            testBatteryService.createTestBatteryContent(
                factoryTestBatteryContent.create(null, test, testcase, testBatteryName));
            List<TestBatteryContent> batteryContent =
                testBatteryService.findTestBatteryContentsByCriteria(
                    null, testBatteryName, test, testcase);

            if (batteryContent != null && batteryContent.size() == 1) {
              String newTestBatteryContentId =
                  String.valueOf(
                      testBatteryService
                          .findTestBatteryContentsByCriteria(null, testBatteryName, test, testcase)
                          .get(0)
                          .getTestbatterycontentID());
              jsonResponse = newTestBatteryContentId;
            }
          } catch (CerberusException ex) {
            MyLogger.log(AddTestBatteryContent.class.getName(), Level.DEBUG, ex.getMessage());
            jsonResponse = "-1";
          }
        }
      }
    }
    response.getWriter().append(jsonResponse).close();
  }