Search in sources :

Example 1 with IFactoryCountryEnvironmentParameters

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

the class UpdateApplication 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();
    ICountryEnvironmentParametersService ceaService = appContext.getBean(ICountryEnvironmentParametersService.class);
    IFactoryCountryEnvironmentParameters cedFactory = appContext.getBean(IFactoryCountryEnvironmentParameters.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"));
    String deployType = policy.sanitize(request.getParameter("deploytype"));
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String application = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("application"), null, charset);
    String originalApplication = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("originalApplication"), null, charset);
    String subSystem = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("subsystem"), "", charset);
    String mavenGpID = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("mavengroupid"), "", charset);
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String svnURL = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("svnurl"), "", charset);
    String bugTrackerURL = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("bugtrackerurl"), "", charset);
    String newBugURL = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("bugtrackernewurl"), "", 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;
    }
    // Getting list of application from JSON Call
    JSONArray objApplicationArray = new JSONArray(request.getParameter("environmentList"));
    List<CountryEnvironmentParameters> ceaList = new ArrayList();
    ceaList = getCountryEnvironmentApplicationFromParameter(request, appContext, system, application, objApplicationArray);
    // 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(application)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Application").replace("%OPERATION%", "Update").replace("%REASON%", "Application ID (application) is missing."));
        ans.setResultMessage(msg);
    } else if (StringUtil.isNullOrEmpty(system)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Application").replace("%OPERATION%", "Update").replace("%REASON%", "System is missing!"));
        ans.setResultMessage(msg);
    } else if (sort_error) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Application").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert sort to an integer value."));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        IApplicationService applicationService = appContext.getBean(IApplicationService.class);
        AnswerItem resp = applicationService.readByKey(originalApplication);
        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.
             */
            Application applicationData = (Application) resp.getItem();
            applicationData.setApplication(application);
            applicationData.setSystem(system);
            applicationData.setSubsystem(subSystem);
            applicationData.setType(type);
            applicationData.setMavengroupid(mavenGpID);
            applicationData.setDeploytype(deployType);
            applicationData.setSvnurl(svnURL);
            applicationData.setBugTrackerUrl(bugTrackerURL);
            applicationData.setBugTrackerNewUrl(newBugURL);
            applicationData.setDescription(description);
            applicationData.setSort(sort);
            applicationData.setUsrModif(request.getRemoteUser());
            ans = applicationService.update(originalApplication, applicationData);
            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("/UpdateApplication", "UPDATE", "Updated Application : ['" + originalApplication + "']", request);
                // Update the Database with the new list.
                ans = ceaService.compareListAndUpdateInsertDeleteElements(system, application, ceaList);
                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 : PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) AnswerItem(org.cerberus.util.answer.AnswerItem) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) ICountryEnvironmentParametersService(org.cerberus.crud.service.ICountryEnvironmentParametersService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters) ILogEventService(org.cerberus.crud.service.ILogEventService) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) Application(org.cerberus.crud.entity.Application) IApplicationService(org.cerberus.crud.service.IApplicationService)

Example 2 with IFactoryCountryEnvironmentParameters

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

the class UpdateApplication method getCountryEnvironmentApplicationFromParameter.

private List<CountryEnvironmentParameters> getCountryEnvironmentApplicationFromParameter(HttpServletRequest request, ApplicationContext appContext, String system, String application, JSONArray json) throws JSONException {
    List<CountryEnvironmentParameters> cedList = new ArrayList();
    ICountryEnvironmentParametersService ceaService = appContext.getBean(ICountryEnvironmentParametersService.class);
    IFactoryCountryEnvironmentParameters cedFactory = appContext.getBean(IFactoryCountryEnvironmentParameters.class);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    String charset = request.getCharacterEncoding();
    for (int i = 0; i < json.length(); i++) {
        JSONObject tcsaJson = json.getJSONObject(i);
        // Parameter that are already controled by GUI (no need to decode) --> We SECURE them
        boolean delete = tcsaJson.getBoolean("toDelete");
        String country = policy.sanitize(tcsaJson.getString("country"));
        String environment = policy.sanitize(tcsaJson.getString("environment"));
        // Parameter that needs to be secured --> We SECURE+DECODE them
        // Parameter that we cannot secure as we need the html --> We DECODE them
        String ip = tcsaJson.getString("ip");
        String domain = tcsaJson.getString("domain");
        String url = tcsaJson.getString("url");
        String urlLogin = tcsaJson.getString("urlLogin");
        String var1 = tcsaJson.getString("var1");
        String var2 = tcsaJson.getString("var2");
        String var3 = tcsaJson.getString("var3");
        String var4 = tcsaJson.getString("var4");
        String strPoolSize = tcsaJson.getString("poolSize");
        int poolSize;
        if (strPoolSize.isEmpty()) {
            poolSize = CountryEnvironmentParameters.DEFAULT_POOLSIZE;
        } else {
            try {
                poolSize = Integer.parseInt(strPoolSize);
            } catch (NumberFormatException e) {
                LOG.warn("Unable to parse pool size: " + strPoolSize + ". Applying default value");
                poolSize = CountryEnvironmentParameters.DEFAULT_POOLSIZE;
            }
        }
        if (!delete) {
            CountryEnvironmentParameters ced = cedFactory.create(system, country, environment, application, ip, domain, url, urlLogin, var1, var2, var3, var4, poolSize);
            cedList.add(ced);
        }
    }
    return cedList;
}
Also used : PolicyFactory(org.owasp.html.PolicyFactory) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) ICountryEnvironmentParametersService(org.cerberus.crud.service.ICountryEnvironmentParametersService)

Example 3 with IFactoryCountryEnvironmentParameters

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

the class UpdateCountryEnvParam method getCountryEnvironmentApplicationFromParameter.

private List<CountryEnvironmentParameters> getCountryEnvironmentApplicationFromParameter(HttpServletRequest request, ApplicationContext appContext, String system, String country, String environment, JSONArray json) throws JSONException {
    List<CountryEnvironmentParameters> ceaList = new ArrayList();
    ICountryEnvironmentParametersService ceaService = appContext.getBean(ICountryEnvironmentParametersService.class);
    IFactoryCountryEnvironmentParameters ceaFactory = appContext.getBean(IFactoryCountryEnvironmentParameters.class);
    for (int i = 0; i < json.length(); i++) {
        JSONObject tcsaJson = json.getJSONObject(i);
        boolean delete = tcsaJson.getBoolean("toDelete");
        String application = tcsaJson.getString("application");
        String ip = tcsaJson.getString("ip");
        String domain = tcsaJson.getString("domain");
        String url = tcsaJson.getString("url");
        String urlLogin = tcsaJson.getString("urlLogin");
        String var1 = tcsaJson.getString("var1");
        String var2 = tcsaJson.getString("var2");
        String var3 = tcsaJson.getString("var3");
        String var4 = tcsaJson.getString("var4");
        String strPoolSize = tcsaJson.getString("poolSize");
        int poolSize;
        if (strPoolSize.isEmpty()) {
            poolSize = CountryEnvironmentParameters.DEFAULT_POOLSIZE;
        } else {
            try {
                poolSize = Integer.parseInt(strPoolSize);
            } catch (NumberFormatException e) {
                LOG.warn("Unable to parse pool size: " + strPoolSize + ". Applying default value");
                poolSize = CountryEnvironmentParameters.DEFAULT_POOLSIZE;
            }
        }
        if (!delete) {
            CountryEnvironmentParameters cea = ceaFactory.create(system, country, environment, application, ip, domain, url, urlLogin, var1, var2, var3, var4, poolSize);
            ceaList.add(cea);
        }
    }
    return ceaList;
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) ICountryEnvironmentParametersService(org.cerberus.crud.service.ICountryEnvironmentParametersService)

Aggregations

ArrayList (java.util.ArrayList)3 CountryEnvironmentParameters (org.cerberus.crud.entity.CountryEnvironmentParameters)3 IFactoryCountryEnvironmentParameters (org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters)3 ICountryEnvironmentParametersService (org.cerberus.crud.service.ICountryEnvironmentParametersService)3 JSONObject (org.json.JSONObject)3 PolicyFactory (org.owasp.html.PolicyFactory)2 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 Application (org.cerberus.crud.entity.Application)1 IApplicationService (org.cerberus.crud.service.IApplicationService)1 ILogEventService (org.cerberus.crud.service.ILogEventService)1 MessageEvent (org.cerberus.engine.entity.MessageEvent)1 CerberusException (org.cerberus.exception.CerberusException)1 Answer (org.cerberus.util.answer.Answer)1 AnswerItem (org.cerberus.util.answer.AnswerItem)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 ApplicationContext (org.springframework.context.ApplicationContext)1