Search in sources :

Example 71 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class DeleteLabel 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);
    response.setContentType("application/json");
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    /**
     * Parsing and securing all required parameters.
     */
    Integer key = Integer.valueOf(policy.sanitize(request.getParameter("id")));
    /**
     * Checking all constrains before calling the services.
     */
    if (key == 0) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Label").replace("%OPERATION%", "Delete").replace("%REASON%", "Label ID is missing!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ILabelService labelService = appContext.getBean(ILabelService.class);
        AnswerItem resp = labelService.readByKey(key);
        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%", "Delete").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.
             */
            Label labelData = (Label) resp.getItem();
            ans = labelService.delete(labelData);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Delete was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/DeleteLabel", "DELETE", "Delete Label : ['" + key + "']", request);
            }
        }
    }
    /**
     * Formating and returning the json result.
     */
    jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", ans.getResultMessage().getDescription());
    response.getWriter().print(jsonResponse.toString());
    response.getWriter().flush();
}
Also used : ILabelService(org.cerberus.crud.service.ILabelService) 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) Label(org.cerberus.crud.entity.Label) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 72 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class AppServiceDAO method update.

@Override
public Answer update(String service, AppService object) {
    MessageEvent msg = null;
    String query = "UPDATE appservice srv SET `Service` = ?, `Group` = ?, `ServicePath` = ?, `Operation` = ?, ServiceRequest = ?, AttachementURL = ?, " + "Description = ?, `Type` = ?, Method = ?, `UsrModif`= ?, `DateModif` = NOW()";
    if ((object.getApplication() != null) && (!object.getApplication().equals(""))) {
        query += " ,Application = ?";
    } else {
        query += " ,Application = null";
    }
    query += " WHERE `Service` = ?";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.application : " + object.getApplication());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            int i = 1;
            preStat.setString(i++, object.getService());
            preStat.setString(i++, object.getGroup());
            preStat.setString(i++, object.getServicePath());
            preStat.setString(i++, object.getOperation());
            preStat.setString(i++, object.getServiceRequest());
            preStat.setString(i++, object.getAttachementURL());
            preStat.setString(i++, object.getDescription());
            preStat.setString(i++, object.getType());
            preStat.setString(i++, object.getMethod());
            preStat.setString(i++, object.getUsrModif());
            if ((object.getApplication() != null) && (!object.getApplication().equals(""))) {
                preStat.setString(i++, object.getApplication());
            }
            preStat.setString(i++, service);
            preStat.executeUpdate();
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "UPDATE"));
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    return new Answer(msg);
}
Also used : Answer(org.cerberus.util.answer.Answer) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 73 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class AppServiceDAO method delete.

@Override
public Answer delete(AppService object) {
    MessageEvent msg = null;
    final String query = "DELETE FROM appservice WHERE `Service` = ? ";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, object.getService());
            preStat.executeUpdate();
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "DELETE"));
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    return new Answer(msg);
}
Also used : Answer(org.cerberus.util.answer.Answer) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 74 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class AppServiceHeaderDAO method delete.

@Override
public Answer delete(AppServiceHeader object) {
    MessageEvent msg = null;
    final String query = "DELETE FROM appserviceheader WHERE `service` = ? and `key` = ? ";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.service : " + object.getService());
        LOG.debug("SQL.param.key : " + object.getKey());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            int i = 1;
            preStat.setString(i++, object.getService());
            preStat.setString(i++, object.getKey());
            preStat.executeUpdate();
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "DELETE"));
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    return new Answer(msg);
}
Also used : Answer(org.cerberus.util.answer.Answer) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 75 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class AppServiceHeaderDAO method update.

@Override
public Answer update(String service, String key, AppServiceHeader object) {
    MessageEvent msg = null;
    final String query = "UPDATE appserviceheader SET `Service` = ?, `Key` = ?, description = ?, sort = ?, `active` = ?, `value` = ?, " + "dateModif = NOW(), usrModif= ?  WHERE `Service` = ? and `Key` = ?";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.service : " + object.getService());
        LOG.debug("SQL.param.key : " + object.getKey());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            int i = 1;
            preStat.setString(i++, object.getService());
            preStat.setString(i++, object.getKey());
            preStat.setString(i++, object.getDescription());
            preStat.setInt(i++, object.getSort());
            preStat.setString(i++, object.getActive());
            preStat.setString(i++, object.getValue());
            preStat.setString(i++, object.getUsrModif());
            preStat.setString(i++, service);
            preStat.setString(i++, key);
            preStat.executeUpdate();
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "UPDATE"));
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    return new Answer(msg);
}
Also used : Answer(org.cerberus.util.answer.Answer) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Answer (org.cerberus.util.answer.Answer)241 MessageEvent (org.cerberus.engine.entity.MessageEvent)227 Connection (java.sql.Connection)127 PreparedStatement (java.sql.PreparedStatement)127 SQLException (java.sql.SQLException)127 ApplicationContext (org.springframework.context.ApplicationContext)77 JSONObject (org.json.JSONObject)75 ILogEventService (org.cerberus.crud.service.ILogEventService)74 PolicyFactory (org.owasp.html.PolicyFactory)60 AnswerItem (org.cerberus.util.answer.AnswerItem)53 CerberusException (org.cerberus.exception.CerberusException)45 ArrayList (java.util.ArrayList)35 JSONException (org.json.JSONException)26 IOException (java.io.IOException)23 ServletException (javax.servlet.ServletException)19 JSONArray (org.json.JSONArray)15 LogEventService (org.cerberus.crud.service.impl.LogEventService)13 TestCase (org.cerberus.crud.entity.TestCase)10 ITestCaseService (org.cerberus.crud.service.ITestCaseService)10 CountryEnvParam (org.cerberus.crud.entity.CountryEnvParam)8