Search in sources :

Example 1 with IFactoryTest

use of org.cerberus.crud.factory.IFactoryTest in project cerberus-source by cerberustesting.

the class CreateTest method processRequest.

/**
 * 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
 * @throws org.json.JSONException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, 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");
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    /**
     * Parsing and securing all required parameters.
     */
    String test = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("test"), "");
    String active = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("Active"), "");
    String automated = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("Automated"), "");
    String description = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("Description"), "");
    /**
     * Checking all constrains before calling the services.
     */
    if (test.isEmpty()) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Test").replace("%OPERATION%", "Create").replace("%REASON%", "Test name is missing!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ITestService testService = appContext.getBean(ITestService.class);
        IFactoryTest factoryTest = appContext.getBean(IFactoryTest.class);
        Test testData = factoryTest.create(test, description, active, automated, "");
        ans = testService.create(testData);
        if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /**
             * Object created. Adding Log entry.
             */
            ILogEventService logEventService = appContext.getBean(LogEventService.class);
            IFactoryLogEvent factoryLogEvent = appContext.getBean(FactoryLogEvent.class);
            logEventService.createForPrivateCalls("/CreateTest", "CREATE", "Create Test : ['" + test + "']", 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();
}
Also used : Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) IFactoryTest(org.cerberus.crud.factory.IFactoryTest) IFactoryLogEvent(org.cerberus.crud.factory.IFactoryLogEvent) JSONObject(org.json.JSONObject) PolicyFactory(org.owasp.html.PolicyFactory) ITestService(org.cerberus.crud.service.ITestService) Test(org.cerberus.crud.entity.Test) IFactoryTest(org.cerberus.crud.factory.IFactoryTest) MessageEvent(org.cerberus.engine.entity.MessageEvent) ILogEventService(org.cerberus.crud.service.ILogEventService)

Aggregations

Test (org.cerberus.crud.entity.Test)1 IFactoryLogEvent (org.cerberus.crud.factory.IFactoryLogEvent)1 IFactoryTest (org.cerberus.crud.factory.IFactoryTest)1 ILogEventService (org.cerberus.crud.service.ILogEventService)1 ITestService (org.cerberus.crud.service.ITestService)1 MessageEvent (org.cerberus.engine.entity.MessageEvent)1 Answer (org.cerberus.util.answer.Answer)1 JSONObject (org.json.JSONObject)1 PolicyFactory (org.owasp.html.PolicyFactory)1 ApplicationContext (org.springframework.context.ApplicationContext)1