Search in sources :

Example 1 with ICountryEnvParamService

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

the class DisableEnvironment 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"));
    // Init Answer with potencial error from Parsing parameter.
    // AnswerItem answer = new AnswerItem(msg);
    String eMailContent = "";
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IEmailService emailService = appContext.getBean(IEmailService.class);
    ICountryEnvParamService countryEnvParamService = appContext.getBean(ICountryEnvParamService.class);
    ICountryEnvParam_logService countryEnvParam_logService = appContext.getBean(ICountryEnvParam_logService.class);
    ILogEventService logEventService = appContext.getBean(LogEventService.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 {
        // 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 disable it!"));
            answerItem.setResultMessage(msg);
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can update it.
             */
            CountryEnvParam cepData = (CountryEnvParam) answerItem.getItem();
            cepData.setActive(false);
            Answer answer = countryEnvParamService.update(cepData);
            if (!(answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()))) {
                /**
                 * Object could not be updated. We stop here and report the
                 * error.
                 */
                answerItem.setResultMessage(answer.getResultMessage());
            } else {
                /**
                 * Update was successful.
                 */
                // Adding Log entry.
                logEventService.createForPrivateCalls("/DisableEnvironment", "UPDATE", "Updated CountryEnvParam : ['" + system + "','" + country + "','" + env + "']", request);
                // Adding CountryEnvParam Log entry.
                countryEnvParam_logService.createLogEntry(system, country, env, "", "", "Disabled.", request.getUserPrincipal().getName());
                /**
                 * Email notification.
                 */
                String OutputMessage = "";
                MessageEvent me = emailService.generateAndSendDisableEnvEmail(system, country, env);
                if (!"OK".equals(me.getMessage().getCodeString())) {
                    LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched." + me.getMessage().getDescription());
                    logEventService.createForPrivateCalls("/DisableEnvironment", "DISABLE", "Warning on Disable 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%", "Environment").replace("%OPERATION%", OPERATION));
                    answerItem.setResultMessage(msg);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", "Environment").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 : ICountryEnvParam_logService(org.cerberus.crud.service.ICountryEnvParam_logService) 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) 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)

Example 2 with ICountryEnvParamService

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

the class NewBuildRev 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 build = policy.sanitize(request.getParameter("build"));
    String revision = policy.sanitize(request.getParameter("revision"));
    // Init Answer with potencial error from Parsing parameter.
    // AnswerItem answer = new AnswerItem(msg);
    String eMailContent;
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IEmailService emailService = appContext.getBean(IEmailService.class);
    IEmailGenerationService emailGenerationService = appContext.getBean(IEmailGenerationService.class);
    IParameterService parameterService = appContext.getBean(IParameterService.class);
    ICountryEnvParamService countryEnvParamService = appContext.getBean(ICountryEnvParamService.class);
    ICountryEnvParam_logService countryEnvParam_logService = appContext.getBean(ICountryEnvParam_logService.class);
    ILogEventService logEventService = appContext.getBean(LogEventService.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("build") == null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "Build is missing!"));
        answerItem.setResultMessage(msg);
    } else if (request.getParameter("revision") == null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "Revision 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 activate it!"));
            answerItem.setResultMessage(msg);
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can update it.
             */
            // Email Calculation. Email must be calcuated before we update the Build and revision in order to have the old build revision still available in the mail.
            String OutputMessage = "";
            Email email = null;
            try {
                email = emailGenerationService.generateRevisionChangeEmail(system, country, env, build, revision);
            } catch (Exception ex) {
                LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched.", ex);
                logEventService.createForPrivateCalls("/NewBuildRev", "NEWBUILDREV", "Warning on New Build/Revision environment : ['" + system + "','" + country + "','" + env + "'] " + ex.getMessage(), request);
                OutputMessage = ex.getMessage();
            }
            // We update the object.
            CountryEnvParam cepData = (CountryEnvParam) answerItem.getItem();
            cepData.setBuild(build);
            cepData.setRevision(revision);
            cepData.setActive(true);
            Answer answer = countryEnvParamService.update(cepData);
            if (!(answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()))) {
                /**
                 * Object could not be updated. We stop here and report the
                 * error.
                 */
                answerItem.setResultMessage(answer.getResultMessage());
            } else {
                /**
                 * Update was successful.
                 */
                // Adding Log entry.
                logEventService.createForPrivateCalls("/NewBuildRev", "UPDATE", "Updated CountryEnvParam : ['" + system + "','" + country + "','" + env + "']", request);
                // Adding CountryEnvParam Log entry.
                countryEnvParam_logService.createLogEntry(system, country, env, build, revision, "New Build Revision.", request.getUserPrincipal().getName());
                /**
                 * Email notification.
                 */
                try {
                    emailService.sendHtmlMail(email);
                } catch (Exception e) {
                    LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched.", e);
                    logEventService.createForPrivateCalls("/NewBuildRev", "NEWBUILDREV", "Warning on New Build/Revision environment : ['" + system + "','" + country + "','" + env + "'] " + e.getMessage(), request);
                    OutputMessage = e.getMessage();
                }
                if (OutputMessage.equals("")) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", "Environment").replace("%OPERATION%", OPERATION));
                    answerItem.setResultMessage(msg);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", "Environment").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 : Email(org.cerberus.service.email.entity.Email) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) IParameterService(org.cerberus.crud.service.IParameterService) AnswerItem(org.cerberus.util.answer.AnswerItem) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) ICountryEnvParam_logService(org.cerberus.crud.service.ICountryEnvParam_logService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) ILogEventService(org.cerberus.crud.service.ILogEventService) ICountryEnvParamService(org.cerberus.crud.service.ICountryEnvParamService) IEmailService(org.cerberus.service.email.IEmailService) CountryEnvParam(org.cerberus.crud.entity.CountryEnvParam) IEmailGenerationService(org.cerberus.service.email.IEmailGenerationService)

Example 3 with ICountryEnvParamService

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

the class UpdateCountryEnvParam 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();
    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);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    String charset = request.getCharacterEncoding();
    ICountryEnvironmentDatabaseService cebService = appContext.getBean(ICountryEnvironmentDatabaseService.class);
    ICountryEnvironmentParametersService ceaService = appContext.getBean(ICountryEnvironmentParametersService.class);
    ICountryEnvDeployTypeService cedService = appContext.getBean(ICountryEnvDeployTypeService.class);
    ICountryEnvLinkService celService = appContext.getBean(ICountryEnvLinkService.class);
    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
    String system = policy.sanitize(request.getParameter("system"));
    String country = policy.sanitize(request.getParameter("country"));
    String environment = policy.sanitize(request.getParameter("environment"));
    String type = policy.sanitize(request.getParameter("type"));
    String chain = policy.sanitize(request.getParameter("chain"));
    boolean maintenanceAct = ParameterParserUtil.parseBooleanParam(request.getParameter("maintenanceAct"), true);
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
    String maintenanceStr = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("maintenanceStr"), "01:00:00", charset);
    String maintenanceEnd = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("maintenanceEnd"), "01:00:00", charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String distribList = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("distribList"), "", charset);
    String eMailBodyRevision = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("eMailBodyRevision"), "", charset);
    String eMailBodyChain = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("eMailBodyChain"), "", charset);
    String eMailBodyDisableEnvironment = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("eMailBodyDisableEnvironment"), "", charset);
    // Getting list of database from JSON Call
    JSONArray objDatabaseArray = new JSONArray(request.getParameter("database"));
    List<CountryEnvironmentDatabase> cebList;
    cebList = getCountryEnvironmentDatabaseFromParameter(request, appContext, system, country, environment, objDatabaseArray);
    // Getting list of application from JSON Call
    JSONArray objApplicationArray = new JSONArray(request.getParameter("application"));
    List<CountryEnvironmentParameters> ceaList;
    ceaList = getCountryEnvironmentApplicationFromParameter(request, appContext, system, country, environment, objApplicationArray);
    // Getting list of database from JSON Call
    JSONArray objDeployTypeArray = new JSONArray(request.getParameter("deployType"));
    List<CountryEnvDeployType> cedList;
    cedList = getCountryEnvironmentDeployTypeFromParameter(request, appContext, system, country, environment, objDeployTypeArray);
    // Getting list of database from JSON Call
    JSONArray objDepArray = new JSONArray(request.getParameter("dependencies"));
    List<CountryEnvLink> celList;
    celList = getCountryEnvironmentLinkFromParameter(request, appContext, system, country, environment, objDepArray);
    // Prepare the final answer.
    MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
    Answer finalAnswer = new Answer(msg1);
    /**
     * Checking all constrains before calling the services.
     */
    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 is missing"));
        ans.setResultMessage(msg);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
    } else if (StringUtil.isNullOrEmpty(country)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "Country is missing"));
        ans.setResultMessage(msg);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
    } else if (StringUtil.isNullOrEmpty(environment)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "Environment is missing"));
        ans.setResultMessage(msg);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ICountryEnvParamService cepService = appContext.getBean(ICountryEnvParamService.class);
        AnswerItem resp = cepService.readByKey(system, country, environment);
        if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
            /**
             * Object could not be found. We stop here and report the error.
             */
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can update it.
             */
            CountryEnvParam cepData = (CountryEnvParam) resp.getItem();
            cepData.setDescription(description);
            cepData.setDistribList(distribList);
            cepData.seteMailBodyRevision(eMailBodyRevision);
            cepData.setType(type);
            cepData.seteMailBodyChain(eMailBodyChain);
            cepData.seteMailBodyDisableEnvironment(eMailBodyDisableEnvironment);
            if (request.getParameter("maintenanceAct") != null) {
                cepData.setMaintenanceAct(maintenanceAct);
            }
            cepData.setMaintenanceStr(maintenanceStr);
            cepData.setMaintenanceEnd(maintenanceEnd);
            cepData.setChain(chain);
            ans = cepService.update(cepData);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Update was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/UpdateCountryEnvParam", "UPDATE", "Updated CountryEnvParam : ['" + system + "','" + country + "','" + environment + "']", request);
            }
            // Update the Database with the new list.
            ans = cebService.compareListAndUpdateInsertDeleteElements(system, country, environment, cebList);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
            // Update the Database with the new list.
            ans = ceaService.compareListAndUpdateInsertDeleteElements(system, country, environment, ceaList);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
            // Update the Database with the new list.
            ans = cedService.compareListAndUpdateInsertDeleteElements(system, country, environment, cedList);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
            // Update the Database with the new list.
            ans = celService.compareListAndUpdateInsertDeleteElements(system, country, environment, celList);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        }
    }
    /**
     * 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();
}
Also used : CountryEnvDeployType(org.cerberus.crud.entity.CountryEnvDeployType) IFactoryCountryEnvDeployType(org.cerberus.crud.factory.IFactoryCountryEnvDeployType) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) JSONArray(org.json.JSONArray) ICountryEnvDeployTypeService(org.cerberus.crud.service.ICountryEnvDeployTypeService) AnswerItem(org.cerberus.util.answer.AnswerItem) ICountryEnvironmentParametersService(org.cerberus.crud.service.ICountryEnvironmentParametersService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) CountryEnvLink(org.cerberus.crud.entity.CountryEnvLink) IFactoryCountryEnvLink(org.cerberus.crud.factory.IFactoryCountryEnvLink) ICountryEnvLinkService(org.cerberus.crud.service.ICountryEnvLinkService) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters) ILogEventService(org.cerberus.crud.service.ILogEventService) ICountryEnvironmentDatabaseService(org.cerberus.crud.service.ICountryEnvironmentDatabaseService) ICountryEnvParamService(org.cerberus.crud.service.ICountryEnvParamService) CountryEnvParam(org.cerberus.crud.entity.CountryEnvParam) CountryEnvironmentDatabase(org.cerberus.crud.entity.CountryEnvironmentDatabase) IFactoryCountryEnvironmentDatabase(org.cerberus.crud.factory.IFactoryCountryEnvironmentDatabase)

Example 4 with ICountryEnvParamService

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

the class ReadCountryEnvParam method findCountryEnvParamByKey.

private AnswerItem findCountryEnvParamByKey(String system, String country, String environment, ApplicationContext appContext, boolean userHasPermissions) throws JSONException, CerberusException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    ICountryEnvParamService libService = appContext.getBean(ICountryEnvParamService.class);
    // finds the CountryEnvParam
    AnswerItem answer = libService.readByKey(system, country, environment);
    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
        CountryEnvParam lib = (CountryEnvParam) answer.getItem();
        JSONObject response = convertCountryEnvParamtoJSONObject(lib);
        object.put("contentTable", response);
    }
    object.put("hasPermissions", userHasPermissions);
    item.setItem(object);
    item.setResultMessage(answer.getResultMessage());
    return item;
}
Also used : JSONObject(org.json.JSONObject) ICountryEnvParamService(org.cerberus.crud.service.ICountryEnvParamService) AnswerItem(org.cerberus.util.answer.AnswerItem) CountryEnvParam(org.cerberus.crud.entity.CountryEnvParam)

Example 5 with ICountryEnvParamService

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

the class CreateCountryEnvParam 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
    String system = policy.sanitize(request.getParameter("system"));
    String country = policy.sanitize(request.getParameter("country"));
    String environment = policy.sanitize(request.getParameter("environment"));
    String type = policy.sanitize(request.getParameter("type"));
    boolean active = (request.getParameter("active") != null);
    boolean maintenanceAct = "Y".equals(policy.sanitize(request.getParameter("maintenanceAct"))) ? true : false;
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
    String maintenanceStr = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("maintenanceStr"), "01:00:00", charset);
    String maintenanceEnd = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("maintenanceEnd"), "01:00:00", charset);
    maintenanceStr = maintenanceStr.isEmpty() ? "00:00:00" : maintenanceStr;
    maintenanceEnd = maintenanceEnd.isEmpty() ? "00:00:00" : maintenanceEnd;
    String build = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("build"), "", charset);
    if ("ALL".equalsIgnoreCase(build))
        build = "";
    String revision = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("revision"), "", charset);
    if ("ALL".equalsIgnoreCase(revision))
        revision = "";
    String chain = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("chain"), "", charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String distribList = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("distribList"), "", charset);
    String emailBodyRevision = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("eMailBodyRevision"), "", charset);
    String emailBodyChain = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("eMailBodyChain"), "", charset);
    String emailBodyDisableEnvironment = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("eMailBodyDisableEnvironment"), "", charset);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(system)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "System is missing!"));
        ans.setResultMessage(msg);
    } else if (StringUtil.isNullOrEmpty(country)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "Country is missing!"));
        ans.setResultMessage(msg);
    } else if (StringUtil.isNullOrEmpty(environment)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "Environment is missing!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ICountryEnvParamService countryEnvParamService = appContext.getBean(ICountryEnvParamService.class);
        IFactoryCountryEnvParam factoryCountryEnvParam = appContext.getBean(IFactoryCountryEnvParam.class);
        CountryEnvParam countryEnvParamData = factoryCountryEnvParam.create(system, country, environment, description, build, revision, chain, distribList, emailBodyRevision, type, emailBodyChain, emailBodyDisableEnvironment, active, maintenanceAct, maintenanceStr, maintenanceEnd);
        ans = countryEnvParamService.create(countryEnvParamData);
        if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /**
             * Object created. Adding Log entry.
             */
            ILogEventService logEventService = appContext.getBean(LogEventService.class);
            logEventService.createForPrivateCalls("/CreateCountryEnvParam", "CREATE", "Create CountryEnvParam : ['" + system + "'|'" + country + "'|'" + environment + "']", 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) ILogEventService(org.cerberus.crud.service.ILogEventService) ICountryEnvParamService(org.cerberus.crud.service.ICountryEnvParamService) IFactoryCountryEnvParam(org.cerberus.crud.factory.IFactoryCountryEnvParam) IFactoryCountryEnvParam(org.cerberus.crud.factory.IFactoryCountryEnvParam) CountryEnvParam(org.cerberus.crud.entity.CountryEnvParam)

Aggregations

ICountryEnvParamService (org.cerberus.crud.service.ICountryEnvParamService)12 ApplicationContext (org.springframework.context.ApplicationContext)11 CountryEnvParam (org.cerberus.crud.entity.CountryEnvParam)10 MessageEvent (org.cerberus.engine.entity.MessageEvent)10 ILogEventService (org.cerberus.crud.service.ILogEventService)9 JSONObject (org.json.JSONObject)9 Answer (org.cerberus.util.answer.Answer)8 AnswerItem (org.cerberus.util.answer.AnswerItem)7 PolicyFactory (org.owasp.html.PolicyFactory)7 IEmailService (org.cerberus.service.email.IEmailService)6 ICountryEnvParam_logService (org.cerberus.crud.service.ICountryEnvParam_logService)4 IInvariantService (org.cerberus.crud.service.IInvariantService)4 IParameterService (org.cerberus.crud.service.IParameterService)4 PrintWriter (java.io.PrintWriter)3 List (java.util.List)3 AnswerList (org.cerberus.util.answer.AnswerList)3 JSONArray (org.json.JSONArray)3 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 IBuildRevisionBatchService (org.cerberus.crud.service.IBuildRevisionBatchService)2