Search in sources :

Example 1 with IBuildRevisionBatchService

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

the class NewChain 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, JSONException {
    JSONObject jsonResponse = new JSONObject();
    AnswerItem answerItem = new AnswerItem();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    answerItem.setResultMessage(msg);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    response.setContentType("application/json");
    /**
     * Parsing and securing all required parameters.
     */
    String system = policy.sanitize(request.getParameter("system"));
    String country = policy.sanitize(request.getParameter("country"));
    String env = policy.sanitize(request.getParameter("environment"));
    String chain = policy.sanitize(request.getParameter("chain"));
    // Init Answer with potencial error from Parsing parameter.
    // AnswerItem answer = new AnswerItem(msg);
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IEmailService emailService = appContext.getBean(IEmailService.class);
    ICountryEnvParamService countryEnvParamService = appContext.getBean(ICountryEnvParamService.class);
    IBuildRevisionBatchService buildRevisionBatchService = appContext.getBean(IBuildRevisionBatchService.class);
    ILogEventService logEventService = appContext.getBean(ILogEventService.class);
    if (request.getParameter("system") == null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "System name is missing!"));
        answerItem.setResultMessage(msg);
    } else if (request.getParameter("country") == null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "Country is missing!"));
        answerItem.setResultMessage(msg);
    } else if (request.getParameter("environment") == null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "Environment is missing!"));
        answerItem.setResultMessage(msg);
    } else if (request.getParameter("chain") == null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "Chain is missing!"));
        answerItem.setResultMessage(msg);
    } else {
        // All parameters are OK we can start performing the operation.
        // Getting the contryEnvParam based on the parameters.
        answerItem = countryEnvParamService.readByKey(system, country, env);
        if (!(answerItem.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && answerItem.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%", OPERATION).replace("%REASON%", OBJECT_NAME + " ['" + system + "','" + country + "','" + env + "'] does not exist. Cannot register a new event!"));
            answerItem.setResultMessage(msg);
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can update it.
             */
            // Adding BuildRevisionBatch entry.
            // Adding CountryEnvParam Log entry.
            CountryEnvParam cepData = (CountryEnvParam) answerItem.getItem();
            buildRevisionBatchService.create(system, country, env, cepData.getBuild(), cepData.getRevision(), chain);
            /**
             * Email notification.
             */
            String OutputMessage = "";
            MessageEvent me = emailService.generateAndSendNewChainEmail(system, country, env, chain);
            if (!"OK".equals(me.getMessage().getCodeString())) {
                LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched." + me.getMessage().getDescription());
                logEventService.createForPrivateCalls("/NewChain", "NEWCHAIN", "Warning on registering new event on environment : ['" + system + "','" + country + "','" + env + "'] " + me.getMessage().getDescription(), request);
                OutputMessage = me.getMessage().getDescription();
            }
            if (OutputMessage.equals("")) {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION));
                answerItem.setResultMessage(msg);
            } else {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).concat(" Just one warning : ").concat(OutputMessage));
                answerItem.setResultMessage(msg);
            }
        }
    }
    /**
     * Formating and returning the json result.
     */
    jsonResponse.put("messageType", answerItem.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", answerItem.getResultMessage().getDescription());
    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ILogEventService(org.cerberus.crud.service.ILogEventService) ICountryEnvParamService(org.cerberus.crud.service.ICountryEnvParamService) AnswerItem(org.cerberus.util.answer.AnswerItem) IEmailService(org.cerberus.service.email.IEmailService) CountryEnvParam(org.cerberus.crud.entity.CountryEnvParam) IBuildRevisionBatchService(org.cerberus.crud.service.IBuildRevisionBatchService)

Example 2 with IBuildRevisionBatchService

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

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