Search in sources :

Example 16 with ILogEventService

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

the class JenkinsDeploy method doGet.

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IParameterService parameterService = appContext.getBean(ParameterService.class);
        String user = parameterService.findParameterByKey("cerberus_jenkinsadmin_user", "").getValue();
        String pass = parameterService.findParameterByKey("cerberus_jenkinsadmin_password", "").getValue();
        String url = parameterService.findParameterByKey("cerberus_jenkinsdeploy_url", "").getValue();
        String final_url;
        final_url = url.replace("%APPLI%", request.getParameter("application"));
        final_url = final_url.replace("%JENKINSBUILDID%", request.getParameter("jenkinsbuildid"));
        final_url = final_url.replace("%DEPLOYTYPE%", request.getParameter("deploytype"));
        final_url = final_url.replace("%JENKINSAGENT%", request.getParameter("jenkinsagent"));
        final_url = final_url.replace("%RELEASE%", request.getParameter("release"));
        final_url = final_url.replace("%REPOSITORYURL%", request.getParameter("repositoryurl"));
        // send request to Jenkins
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet requesthttp = new HttpGet(final_url);
        String auth = user + ":" + pass;
        byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("ISO-8859-1")));
        String authHeader = "Basic " + new String(encodedAuth);
        requesthttp.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
        HttpResponse responsehttp = client.execute(requesthttp);
        int statusCode = responsehttp.getStatusLine().getStatusCode();
        if ((statusCode != 200) && (statusCode != 201)) {
            out.print("ERROR Contacting Jenkins HTTP Response " + statusCode);
            out.print("Sent request : " + final_url);
        } else {
            /**
             * Jenkins was called successfuly. Adding Log entry.
             */
            ILogEventService logEventService = appContext.getBean(ILogEventService.class);
            logEventService.createForPrivateCalls("/JenkinsDeploy", "DEPLOY", "JenkinsDeploy Triggered : ['" + final_url + "']", request);
            out.print("Sent request : " + final_url);
        }
    } catch (Exception ex) {
        LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched.", ex);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ILogEventService(org.cerberus.crud.service.ILogEventService) IParameterService(org.cerberus.crud.service.IParameterService) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 17 with ILogEventService

use of org.cerberus.crud.service.ILogEventService 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 18 with ILogEventService

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

the class UpdateInvariant 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();
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("idName"), "", charset);
    String value = request.getParameter("value");
    String oriId = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("originalIdName"), "", charset);
    String oriValue = request.getParameter("originalValue");
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
    String veryShortDescField = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("veryShortDesc"), "", charset);
    String gp1 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp1"), "", charset);
    String gp2 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp2"), "", charset);
    String gp3 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp3"), "", charset);
    String gp4 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp4"), "", charset);
    String gp5 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp5"), "", charset);
    String gp6 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp6"), "", charset);
    String gp7 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp7"), "", charset);
    String gp8 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp8"), "", charset);
    String gp9 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp9"), "", charset);
    Integer sort = 10;
    boolean sort_error = false;
    try {
        if (request.getParameter("sort") != null && !request.getParameter("sort").equals("")) {
            sort = Integer.valueOf(policy.sanitize(request.getParameter("sort")));
        }
    } catch (Exception ex) {
        sort_error = true;
    }
    boolean userHasPermissions = request.isUserInRole("Administrator");
    // 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(id)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "Invariant name is missing!"));
        finalAnswer.setResultMessage(msg);
    } else if (sort_error) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert sort to an integer value!"));
        finalAnswer.setResultMessage(msg);
    } else if (!userHasPermissions) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "You don't have the right to do that"));
        finalAnswer.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IInvariantService invariantService = appContext.getBean(IInvariantService.class);
        AnswerItem resp = invariantService.readByKey(oriId, oriValue);
        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 {
            Invariant invariantData = (Invariant) resp.getItem();
            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 {
                if (invariantService.hasPermissionsUpdate(invariantData, request)) {
                    invariantData.setIdName(id);
                    invariantData.setValue(value);
                    invariantData.setSort(sort);
                    invariantData.setDescription(description);
                    invariantData.setVeryShortDesc(veryShortDescField);
                    invariantData.setGp1(gp1);
                    invariantData.setGp2(gp2);
                    invariantData.setGp3(gp3);
                    invariantData.setGp4(gp4);
                    invariantData.setGp5(gp5);
                    invariantData.setGp6(gp6);
                    invariantData.setGp7(gp7);
                    invariantData.setGp8(gp8);
                    invariantData.setGp9(gp9);
                    ans = invariantService.update(oriId, oriValue, invariantData);
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                    if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                        /**
                         * Object updated. Adding Log entry.
                         */
                        ILogEventService logEventService = appContext.getBean(LogEventService.class);
                        logEventService.createForPrivateCalls("/UpdateInvariant2", "UPDATE", "Update Invariant : ['" + id + "']", request);
                    }
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "The Invariant is not Public!"));
                    ans.setResultMessage(msg);
                    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 : Invariant(org.cerberus.crud.entity.Invariant) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) IInvariantService(org.cerberus.crud.service.IInvariantService) AnswerItem(org.cerberus.util.answer.AnswerItem) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 19 with ILogEventService

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

the class UpdateLabel 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);
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    String charset = request.getCharacterEncoding();
    ILabelService labelService = appContext.getBean(ILabelService.class);
    IFactoryLabel labelFactory = appContext.getBean(IFactoryLabel.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 type = policy.sanitize(request.getParameter("type"));
    Integer id = Integer.valueOf(policy.sanitize(request.getParameter("id")));
    String reqtype = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("reqtype"), "", charset);
    String reqstatus = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("reqstatus"), "", charset);
    String reqcriticity = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("reqcriticity"), "", charset);
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String label = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("label"), "", charset);
    String color = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("color"), "", charset);
    String parentLabel = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("parentLabel"), "", charset);
    String description = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("description"), "", charset);
    String longDesc = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("longdesc"), "", charset);
    String usr = request.getUserPrincipal().getName();
    /**
     * Checking all constrains before calling the services.
     */
    if (id == 0) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Label").replace("%OPERATION%", "Update").replace("%REASON%", "Label ID is missing."));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        AnswerItem resp = labelService.readByKey(id);
        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%", "Label").replace("%OPERATION%", "Update").replace("%REASON%", "Label 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.
             */
            Timestamp updateDate = new Timestamp(new Date().getTime());
            Label l = labelFactory.create(id, system, label, type, color, parentLabel, reqtype, reqstatus, reqcriticity, description, longDesc, null, null, usr, updateDate);
            ans = labelService.update(l);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Delete was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/UpdateLabel", "UPDATE", "Update Label : ['" + id + "']", 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 : PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) IFactoryLabel(org.cerberus.crud.factory.IFactoryLabel) Label(org.cerberus.crud.entity.Label) AnswerItem(org.cerberus.util.answer.AnswerItem) Timestamp(java.sql.Timestamp) Date(java.util.Date) ILabelService(org.cerberus.crud.service.ILabelService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) IFactoryLabel(org.cerberus.crud.factory.IFactoryLabel) JSONObject(org.json.JSONObject) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 20 with ILogEventService

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

the class UpdateParameter 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();
    ILogEventService logEventService;
    String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("id"), "", charset);
    String value = ParameterParserUtil.parseStringParam(request.getParameter("value"), "");
    String system = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system"), "", charset);
    String system1value = ParameterParserUtil.parseStringParam(request.getParameter("system1Value"), null);
    String system1 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system1"), null, charset);
    boolean userHasPermissions = request.isUserInRole("Administrator");
    // 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(id) || StringUtil.isNullOrEmpty(system1)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Parameter").replace("%OPERATION%", "Update").replace("%REASON%", "Parameter id or system1 is missing!"));
        finalAnswer.setResultMessage(msg);
    } else if (!userHasPermissions) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Parameter").replace("%OPERATION%", "Update").replace("%REASON%", "You don't have the right to do that"));
        finalAnswer.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IParameterService parameterService = appContext.getBean(IParameterService.class);
        FactoryParameter factoryparameter = appContext.getBean(FactoryParameter.class);
        Parameter para = factoryparameter.create(system, id, value, "");
        ans = parameterService.save(para);
        if (!ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && !ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED.getCode())) {
            /**
             * Object could not be found. We stop here and report the error.
             */
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        } else {
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/UpdateParameter", "UPDATE", "Update Parameter : ['" + id + "','" + system + "']", request);
            }
            if (system1 != null && system1value != null) {
                Parameter para1 = factoryparameter.create(system1, id, system1value, "");
                ans = parameterService.save(para1);
                if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                    /**
                     * Object updated. Adding Log entry.
                     */
                    logEventService = appContext.getBean(LogEventService.class);
                    logEventService.createForPrivateCalls("/UpdateParameter", "UPDATE", "Update Parameter : ['" + id + "','" + system1 + "']", request);
                }
            }
        }
    }
    /**
     * 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 : FactoryParameter(org.cerberus.crud.factory.impl.FactoryParameter) 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) Parameter(org.cerberus.crud.entity.Parameter) FactoryParameter(org.cerberus.crud.factory.impl.FactoryParameter) IParameterService(org.cerberus.crud.service.IParameterService)

Aggregations

ILogEventService (org.cerberus.crud.service.ILogEventService)93 ApplicationContext (org.springframework.context.ApplicationContext)92 JSONObject (org.json.JSONObject)80 MessageEvent (org.cerberus.engine.entity.MessageEvent)77 Answer (org.cerberus.util.answer.Answer)72 PolicyFactory (org.owasp.html.PolicyFactory)62 AnswerItem (org.cerberus.util.answer.AnswerItem)55 CerberusException (org.cerberus.exception.CerberusException)34 JSONException (org.json.JSONException)33 IOException (java.io.IOException)23 ServletException (javax.servlet.ServletException)23 ArrayList (java.util.ArrayList)18 PrintWriter (java.io.PrintWriter)16 TestCase (org.cerberus.crud.entity.TestCase)15 ITestCaseService (org.cerberus.crud.service.ITestCaseService)15 JSONArray (org.json.JSONArray)15 IParameterService (org.cerberus.crud.service.IParameterService)13 List (java.util.List)12 ITestCaseCountryService (org.cerberus.crud.service.ITestCaseCountryService)12 LogEventService (org.cerberus.crud.service.impl.LogEventService)12