Search in sources :

Example 1 with FactoryParameter

use of org.cerberus.crud.factory.impl.FactoryParameter 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)

Example 2 with FactoryParameter

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

the class ParameterDAO method loadFromResultSetWithSystem1.

@Override
public Parameter loadFromResultSetWithSystem1(ResultSet rs) throws SQLException {
    String param = ParameterParserUtil.parseStringParam(rs.getString("par.param"), "");
    String value = ParameterParserUtil.parseStringParam(rs.getString("par.value"), "");
    String description = ParameterParserUtil.parseStringParam(rs.getString("par.description"), "");
    String system1 = ParameterParserUtil.parseStringParam(rs.getString("system1"), "");
    String system1Value = ParameterParserUtil.parseStringParam(rs.getString("system1Value"), "");
    // TODO remove when working in test with mockito and autowired
    factoryParameter = new FactoryParameter();
    return factoryParameter.create("", param, value, description, system1, system1Value);
}
Also used : IFactoryParameter(org.cerberus.crud.factory.IFactoryParameter) FactoryParameter(org.cerberus.crud.factory.impl.FactoryParameter)

Example 3 with FactoryParameter

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

the class ParameterDAO method loadFromResultSet.

@Override
public Parameter loadFromResultSet(ResultSet rs) throws SQLException {
    String system = ParameterParserUtil.parseStringParam(rs.getString("system"), "");
    String param = ParameterParserUtil.parseStringParam(rs.getString("param"), "");
    String value = ParameterParserUtil.parseStringParam(rs.getString("value"), "");
    String description = ParameterParserUtil.parseStringParam(rs.getString("description"), "");
    // TODO remove when working in test with mockito and autowired
    factoryParameter = new FactoryParameter();
    return factoryParameter.create(system, param, value, description);
}
Also used : IFactoryParameter(org.cerberus.crud.factory.IFactoryParameter) FactoryParameter(org.cerberus.crud.factory.impl.FactoryParameter)

Aggregations

FactoryParameter (org.cerberus.crud.factory.impl.FactoryParameter)3 IFactoryParameter (org.cerberus.crud.factory.IFactoryParameter)2 Parameter (org.cerberus.crud.entity.Parameter)1 ILogEventService (org.cerberus.crud.service.ILogEventService)1 IParameterService (org.cerberus.crud.service.IParameterService)1 MessageEvent (org.cerberus.engine.entity.MessageEvent)1 Answer (org.cerberus.util.answer.Answer)1 JSONObject (org.json.JSONObject)1 PolicyFactory (org.owasp.html.PolicyFactory)1 ApplicationContext (org.springframework.context.ApplicationContext)1