Search in sources :

Example 1 with IFactoryBatchInvariant

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

the class CreateBatchInvariant 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.cerberus.exception.CerberusException
 * @throws org.json.JSONException
 */
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);
    String charset = request.getCharacterEncoding();
    response.setContentType("application/json");
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    /**
     * Parsing and securing all required parameters.
     */
    // 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 system = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system"), "", charset);
    String batch = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("batch"), null, charset);
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(batch)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Batch").replace("%OPERATION%", "Create").replace("%REASON%", "Batch name is missing!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IBatchInvariantService batchInvariantService = appContext.getBean(IBatchInvariantService.class);
        IFactoryBatchInvariant factoryBatchInvariant = appContext.getBean(IFactoryBatchInvariant.class);
        BatchInvariant batchData = factoryBatchInvariant.create(system, batch, description);
        ans = batchInvariantService.create(batchData);
        if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /**
             * Object created. Adding Log entry.
             */
            ILogEventService logEventService = appContext.getBean(LogEventService.class);
            logEventService.createForPrivateCalls("/CreateBatchInvariant", "CREATE", "Create BatchInvariant : ['" + batch + "']", 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) JSONObject(org.json.JSONObject) PolicyFactory(org.owasp.html.PolicyFactory) IFactoryBatchInvariant(org.cerberus.crud.factory.IFactoryBatchInvariant) MessageEvent(org.cerberus.engine.entity.MessageEvent) IBatchInvariantService(org.cerberus.crud.service.IBatchInvariantService) ILogEventService(org.cerberus.crud.service.ILogEventService) IFactoryBatchInvariant(org.cerberus.crud.factory.IFactoryBatchInvariant) BatchInvariant(org.cerberus.crud.entity.BatchInvariant)

Aggregations

BatchInvariant (org.cerberus.crud.entity.BatchInvariant)1 IFactoryBatchInvariant (org.cerberus.crud.factory.IFactoryBatchInvariant)1 IBatchInvariantService (org.cerberus.crud.service.IBatchInvariantService)1 ILogEventService (org.cerberus.crud.service.ILogEventService)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