Search in sources :

Example 1 with IBatchInvariantService

use of org.cerberus.crud.service.IBatchInvariantService 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)

Example 2 with IBatchInvariantService

use of org.cerberus.crud.service.IBatchInvariantService in project cerberus-source by cerberustesting.

the class ReadBatchInvariant method findBatchInvariantByKey.

private AnswerItem findBatchInvariantByKey(String batch, ApplicationContext appContext, boolean userHasPermissions) throws JSONException, CerberusException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    IBatchInvariantService libService = appContext.getBean(IBatchInvariantService.class);
    // finds the project
    AnswerItem answer = null;
    answer = libService.readByKey(batch);
    if (answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // if the service returns an OK message then we can get the item and convert it to JSONformat
        BatchInvariant bri = (BatchInvariant) answer.getItem();
        JSONObject response = convertToJSONObject(bri);
        object.put("contentTable", response);
    }
    object.put("hasPermissions", userHasPermissions);
    item.setItem(object);
    item.setResultMessage(answer.getResultMessage());
    return item;
}
Also used : JSONObject(org.json.JSONObject) IBatchInvariantService(org.cerberus.crud.service.IBatchInvariantService) AnswerItem(org.cerberus.util.answer.AnswerItem) BatchInvariant(org.cerberus.crud.entity.BatchInvariant)

Example 3 with IBatchInvariantService

use of org.cerberus.crud.service.IBatchInvariantService in project cerberus-source by cerberustesting.

the class UpdateBatchInvariant 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
 */
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%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "Batch is missing!"));
        ans.setResultMessage(msg);
    } else if (StringUtil.isNullOrEmpty(system)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "System 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);
        AnswerItem resp = batchInvariantService.readByKey(batch);
        if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
            /**
             * 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%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "BatchInvariant 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.
             */
            BatchInvariant batchInvariantData = (BatchInvariant) resp.getItem();
            batchInvariantData.setSystem(system);
            batchInvariantData.setDescription(description);
            ans = batchInvariantService.update(batchInvariantData.getBatch(), batchInvariantData);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Update was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/UpdateBatchInvariant", "UPDATE", "Updated 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) MessageEvent(org.cerberus.engine.entity.MessageEvent) IBatchInvariantService(org.cerberus.crud.service.IBatchInvariantService) ILogEventService(org.cerberus.crud.service.ILogEventService) LogEventService(org.cerberus.crud.service.impl.LogEventService) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerItem(org.cerberus.util.answer.AnswerItem) BatchInvariant(org.cerberus.crud.entity.BatchInvariant)

Example 4 with IBatchInvariantService

use of org.cerberus.crud.service.IBatchInvariantService in project cerberus-source by cerberustesting.

the class DeleteBatchInvariant 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
 */
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");
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    /**
     * Parsing and securing all required parameters.
     */
    String batch = policy.sanitize(request.getParameter("batch"));
    /**
     * 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%", OBJECT_NAME).replace("%OPERATION%", "Delete").replace("%REASON%", "Batch 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);
        AnswerItem resp = batchInvariantService.readByKey(batch);
        if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
            /**
             * 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%", OBJECT_NAME).replace("%OPERATION%", "Delete").replace("%REASON%", "Batch does not exist."));
            ans.setResultMessage(msg);
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can delete it.
             */
            BatchInvariant batchInvariantData = (BatchInvariant) resp.getItem();
            ans = batchInvariantService.delete(batchInvariantData);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Delete was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/DeleteBatchInvariant", "DELETE", "Delete 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.toString());
    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) MessageEvent(org.cerberus.engine.entity.MessageEvent) IBatchInvariantService(org.cerberus.crud.service.IBatchInvariantService) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerItem(org.cerberus.util.answer.AnswerItem) BatchInvariant(org.cerberus.crud.entity.BatchInvariant)

Example 5 with IBatchInvariantService

use of org.cerberus.crud.service.IBatchInvariantService in project cerberus-source by cerberustesting.

the class NewEnvironmentEventV000 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
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    String charset = request.getCharacterEncoding();
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    /**
     * Adding Log entry.
     */
    ILogEventService logEventService = appContext.getBean(ILogEventService.class);
    logEventService.createForPublicCalls("/NewEnvironmentEventV000", "CALL", "NewEnvironmentEventV000 called : " + request.getRequestURL(), request);
    ICountryEnvParamService countryEnvParamService = appContext.getBean(ICountryEnvParamService.class);
    IInvariantService invariantService = appContext.getBean(IInvariantService.class);
    IBatchInvariantService batchInvariantService = appContext.getBean(IBatchInvariantService.class);
    IBuildRevisionBatchService buildRevisionBatchService = appContext.getBean(IBuildRevisionBatchService.class);
    IEmailService emailService = appContext.getBean(IEmailService.class);
    // Parsing all parameters.
    String system = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system"), "", charset);
    String country = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("country"), "", charset);
    String environment = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("environment"), "", charset);
    String event = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("event"), "", charset);
    String helpMessage = "\nThis servlet is used to inform Cerberus about an event that occured on a given environment. For example when a treatment has been executed.\n\nParameter list :\n" + "- system [mandatory] : the system where the Build Revision has been deployed. [" + system + "]\n" + "- country [mandatory] : the country where the Build Revision has been deployed. You can use ALL if you want to perform the action for all countries that exist for the given system and environement. [" + country + "]\n" + "- environment [mandatory] : the environment where the Build Revision has been deployed. [" + environment + "]\n" + "- event [mandatory] : the event that should be recorded.. [" + event + "]\n";
    boolean error = false;
    // Checking the parameter validity. If application has been entered, does it exist ?
    if (system.equalsIgnoreCase("")) {
        out.println("Error - Parameter system is mandatory.");
        error = true;
    }
    if (!system.equalsIgnoreCase("") && !invariantService.isInvariantExist("SYSTEM", system)) {
        out.println("Error - System does not exist  : " + system);
        error = true;
    }
    if (environment.equalsIgnoreCase("")) {
        out.println("Error - Parameter environment is mandatory.");
        error = true;
    }
    if (!environment.equalsIgnoreCase("") && !invariantService.isInvariantExist("ENVIRONMENT", environment)) {
        out.println("Error - Environment does not exist  : " + environment);
        error = true;
    }
    if (country.equalsIgnoreCase("")) {
        out.println("Error - Parameter country is mandatory.");
        error = true;
    } else if (!country.equalsIgnoreCase(PARAMETERALL)) {
        if (!invariantService.isInvariantExist("COUNTRY", country)) {
            out.println("Error - Country does not exist  : " + country);
            error = true;
        }
        if (!error) {
            if (!countryEnvParamService.exist(system, country, environment)) {
                out.println("Error - System/Country/Environment does not exist : " + system + "/" + country + "/" + environment);
                error = true;
            }
        }
    }
    if (event.equalsIgnoreCase("")) {
        out.println("Error - Parameter event is mandatory.");
        error = true;
    }
    if (!event.equalsIgnoreCase("") && !batchInvariantService.exist(event)) {
        out.println("Error - Event does not exist  : " + event);
        error = true;
    }
    // Starting the database update only when no blocking error has been detected.
    if (error == false) {
        /**
         * Getting the list of objects to treat.
         */
        MessageEvent msg = new MessageEvent(MessageEventEnum.GENERIC_OK);
        Answer finalAnswer = new Answer(msg);
        AnswerList answerList = new AnswerList();
        if (country.equalsIgnoreCase(PARAMETERALL)) {
            country = null;
        }
        answerList = countryEnvParamService.readByVarious(system, country, environment, null, null, "Y");
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) answerList);
        for (CountryEnvParam cepData : (List<CountryEnvParam>) answerList.getDataList()) {
            /**
             * For each object, we can update it.
             */
            // Adding CountryEnvParam Log entry.
            buildRevisionBatchService.create(cepData.getSystem(), cepData.getCountry(), cepData.getEnvironment(), cepData.getBuild(), cepData.getRevision(), event);
            /**
             * Email notification.
             */
            String OutputMessage = "";
            MessageEvent me = emailService.generateAndSendNewChainEmail(cepData.getSystem(), cepData.getCountry(), cepData.getEnvironment(), event);
            if (!"OK".equals(me.getMessage().getCodeString())) {
                LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched." + me.getMessage().getDescription());
                logEventService.createForPrivateCalls("/NewEnvironmentEventV000", "NEW", "Warning on New environment event : ['" + cepData.getSystem() + "','" + cepData.getCountry() + "','" + cepData.getEnvironment() + "'] " + me.getMessage().getDescription(), request);
                OutputMessage = me.getMessage().getDescription();
            }
            if (OutputMessage.equals("")) {
                msg = new MessageEvent(MessageEventEnum.GENERIC_OK);
                Answer answerSMTP = new AnswerList(msg);
                finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, answerSMTP);
            } else {
                msg = new MessageEvent(MessageEventEnum.GENERIC_WARNING);
                msg.setDescription(msg.getDescription().replace("%REASON%", OutputMessage + " when sending email for " + cepData.getSystem() + "/" + cepData.getCountry() + "/" + cepData.getEnvironment()));
                Answer answerSMTP = new AnswerList(msg);
                finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, answerSMTP);
            }
        }
        /**
         * Formating and returning the result.
         */
        out.println(finalAnswer.getResultMessage().getMessage().getCodeString() + " - " + finalAnswer.getResultMessage().getDescription());
    } else {
        // In case of errors, we display the help message.
        out.println(helpMessage);
    }
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) MessageEvent(org.cerberus.engine.entity.MessageEvent) IInvariantService(org.cerberus.crud.service.IInvariantService) IBatchInvariantService(org.cerberus.crud.service.IBatchInvariantService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerList(org.cerberus.util.answer.AnswerList) List(java.util.List) ICountryEnvParamService(org.cerberus.crud.service.ICountryEnvParamService) IEmailService(org.cerberus.service.email.IEmailService) CountryEnvParam(org.cerberus.crud.entity.CountryEnvParam) PrintWriter(java.io.PrintWriter) IBuildRevisionBatchService(org.cerberus.crud.service.IBuildRevisionBatchService)

Aggregations

IBatchInvariantService (org.cerberus.crud.service.IBatchInvariantService)5 BatchInvariant (org.cerberus.crud.entity.BatchInvariant)4 ILogEventService (org.cerberus.crud.service.ILogEventService)4 MessageEvent (org.cerberus.engine.entity.MessageEvent)4 Answer (org.cerberus.util.answer.Answer)4 JSONObject (org.json.JSONObject)4 ApplicationContext (org.springframework.context.ApplicationContext)4 AnswerItem (org.cerberus.util.answer.AnswerItem)3 PolicyFactory (org.owasp.html.PolicyFactory)3 PrintWriter (java.io.PrintWriter)1 List (java.util.List)1 CountryEnvParam (org.cerberus.crud.entity.CountryEnvParam)1 IFactoryBatchInvariant (org.cerberus.crud.factory.IFactoryBatchInvariant)1 IBuildRevisionBatchService (org.cerberus.crud.service.IBuildRevisionBatchService)1 ICountryEnvParamService (org.cerberus.crud.service.ICountryEnvParamService)1 IInvariantService (org.cerberus.crud.service.IInvariantService)1 LogEventService (org.cerberus.crud.service.impl.LogEventService)1 IEmailService (org.cerberus.service.email.IEmailService)1 AnswerList (org.cerberus.util.answer.AnswerList)1