Search in sources :

Example 46 with CerberusException

use of org.cerberus.exception.CerberusException in project cerberus-source by cerberustesting.

the class ReadLogEvent 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 {
    String echo = request.getParameter("sEcho");
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    response.setContentType("application/json");
    response.setCharacterEncoding("utf8");
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    // Default message to unexpected error.
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    /**
     * Parsing and securing all required parameters.
     */
    long idlog = 0;
    boolean idlog_error = true;
    try {
        if (request.getParameter("logeventid") != null && !request.getParameter("logeventid").equals("")) {
            idlog = Integer.valueOf(policy.sanitize(request.getParameter("logeventid")));
            idlog_error = false;
        }
    } catch (Exception ex) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "LogEvent"));
        msg.setDescription(msg.getDescription().replace("%OPERATION%", "Read"));
        msg.setDescription(msg.getDescription().replace("%REASON%", "logeventid must be an integer value."));
        idlog_error = true;
    }
    // Get Parameters
    String columnName = ParameterParserUtil.parseStringParam(request.getParameter("columnName"), "");
    // Init Answer with potencial error from Parsing parameter.
    AnswerItem answer = new AnswerItem(msg);
    try {
        JSONObject jsonResponse = new JSONObject();
        if (!Strings.isNullOrEmpty(columnName)) {
            answer = findDistinctValuesOfColumn(appContext, request, columnName);
            jsonResponse = (JSONObject) answer.getItem();
        } else if (request.getParameter("logeventid") == null) {
            answer = findLogEventList(appContext, request);
            jsonResponse = (JSONObject) answer.getItem();
        } else if ((request.getParameter("logeventid") != null) && !(idlog_error)) {
            answer = findLogEventByID(appContext, idlog);
            jsonResponse = (JSONObject) answer.getItem();
        }
        jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());
        jsonResponse.put("message", answer.getResultMessage().getDescription());
        jsonResponse.put("sEcho", echo);
        response.getWriter().print(jsonResponse.toString());
    } catch (JSONException e) {
        LOG.warn(e);
        // returns a default error message with the json format that is able to be parsed by the client-side
        response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) PolicyFactory(org.owasp.html.PolicyFactory) JSONObject(org.json.JSONObject) MessageEvent(org.cerberus.engine.entity.MessageEvent) JSONException(org.json.JSONException) AnswerItem(org.cerberus.util.answer.AnswerItem) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException)

Example 47 with CerberusException

use of org.cerberus.exception.CerberusException in project cerberus-source by cerberustesting.

the class UpdateMyUserReporting method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
    String reporting = request.getUserPrincipal().getName();
    String login = request.getUserPrincipal().getName();
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IUserService userService = appContext.getBean(UserService.class);
    try {
        User user = userService.findUserByKey(login);
        user.setReportingFavorite(reporting);
        userService.updateUser(user);
        ILogEventService logEventService = appContext.getBean(LogEventService.class);
        logEventService.createForPrivateCalls("/UpdateMyUserReporting", "UPDATE", "Update user reporting preference for user: " + login, request);
    } catch (CerberusException e) {
        // To change body of catch statement use File | Settings | File Templates.
        e.printStackTrace();
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) CerberusException(org.cerberus.exception.CerberusException) User(org.cerberus.crud.entity.User) IUserService(org.cerberus.crud.service.IUserService) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 48 with CerberusException

use of org.cerberus.exception.CerberusException in project cerberus-source by cerberustesting.

the class UpdateMyUserReporting1 method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    JSONObject jsonResponse = new JSONObject();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    String login = request.getUserPrincipal().getName();
    String charset = request.getCharacterEncoding();
    /**
     * Parse parameters - list of values
     */
    List<String> tcstatusList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("tcstatus"), null, charset);
    List<String> groupList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("group"), null, charset);
    List<String> tcactiveList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("tcactive"), null, charset);
    List<String> priorityList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("priority"), null, charset);
    List<String> countryList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("country"), null, charset);
    List<String> browserList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("browser"), null, charset);
    List<String> tcestatusList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("tcestatus"), null, charset);
    // environment
    List<String> environmentList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("environment"), null, charset);
    List<String> projectList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("project"), null, charset);
    /**
     * Parse parameters - free text
     */
    String ip = StringEscapeUtils.escapeHtml4(request.getParameter("ip"));
    String port = StringEscapeUtils.escapeHtml4(request.getParameter("port"));
    String tag = StringEscapeUtils.escapeHtml4(request.getParameter("tag"));
    String browserversion = StringEscapeUtils.escapeHtml4(request.getParameter("browserversion"));
    String comment = StringEscapeUtils.escapeHtml4(request.getParameter("comment"));
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IUserService userService = appContext.getBean(UserService.class);
    try {
        User user = userService.findUserByKey(login);
        if (user != null) {
            JSONObject preferences = new JSONObject();
            if (tcstatusList != null) {
                preferences.put("s", tcstatusList);
            }
            if (groupList != null) {
                preferences.put("g", groupList);
            }
            if (tcactiveList != null) {
                preferences.put("a", tcactiveList);
            }
            if (priorityList != null) {
                preferences.put("pr", priorityList);
            }
            if (countryList != null) {
                preferences.put("co", countryList);
            }
            if (browserList != null) {
                preferences.put("b", browserList);
            }
            if (tcestatusList != null) {
                preferences.put("es", tcestatusList);
            }
            if (environmentList != null) {
                preferences.put("e", environmentList);
            }
            if (projectList != null) {
                preferences.put("prj", projectList);
            }
            if (!StringUtil.isNullOrEmpty(ip)) {
                preferences.put("ip", ip);
            }
            if (!StringUtil.isNullOrEmpty(port)) {
                preferences.put("p", port);
            }
            if (!StringUtil.isNullOrEmpty(tag)) {
                preferences.put("t", tag);
            }
            if (!StringUtil.isNullOrEmpty(browserversion)) {
                preferences.put("br", browserversion);
            }
            if (!StringUtil.isNullOrEmpty(comment)) {
                preferences.put("cm", comment);
            }
            user.setReportingFavorite(preferences.toString());
            // TODO: when converting to the new standard this should return an answer
            userService.updateUser(user);
            // re-send the updated preferences
            jsonResponse.put("preferences", preferences);
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "Execution reporting filters ").replace("%OPERATION%", "Update"));
            ILogEventService logEventService = appContext.getBean(LogEventService.class);
            logEventService.createForPrivateCalls("/UpdateMyUserReporting1", "UPDATE", "Update user reporting preference for user: " + login, request);
        } else {
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to update User was not found!"));
        }
        jsonResponse.put("messageType", msg.getMessage().getCodeString());
        jsonResponse.put("message", msg.getDescription());
    } catch (JSONException ex) {
        LOG.warn(ex);
        // returns a default error message with the json format that is able to be parsed by the client-side
        response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
    } catch (CerberusException ex) {
        LOG.warn(ex);
        // returns a default error message with the json format that is able to be parsed by the client-side
        response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
    }
    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) CerberusException(org.cerberus.exception.CerberusException) User(org.cerberus.crud.entity.User) JSONObject(org.json.JSONObject) MessageEvent(org.cerberus.engine.entity.MessageEvent) IUserService(org.cerberus.crud.service.IUserService) ILogEventService(org.cerberus.crud.service.ILogEventService) JSONException(org.json.JSONException)

Example 49 with CerberusException

use of org.cerberus.exception.CerberusException in project cerberus-source by cerberustesting.

the class UpdateMyUserRobotPreference 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 {
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IUserService userService = appContext.getBean(UserService.class);
    try {
        String ss_ip = ParameterParserUtil.parseStringParam(request.getParameter("ss_ip"), "");
        String ss_p = ParameterParserUtil.parseStringParam(request.getParameter("ss_p"), "");
        String platform = ParameterParserUtil.parseStringParam(request.getParameter("platform"), "");
        String browser = ParameterParserUtil.parseStringParam(request.getParameter("browser"), "");
        String version = ParameterParserUtil.parseStringParam(request.getParameter("version"), "");
        User usr = userService.findUserByKey(request.getUserPrincipal().getName());
        usr.setRobotHost(ss_ip);
        usr.setRobotPort(ss_p);
        usr.setRobotPlatform(platform);
        usr.setRobotBrowser(browser);
        usr.setRobotVersion(version);
        userService.updateUser(usr);
        ILogEventService logEventService = appContext.getBean(LogEventService.class);
        logEventService.createForPrivateCalls("/UpdateMyUserRobotPreference", "UPDATE", "Update user robot preference for user: " + usr.getLogin(), request);
        response.getWriter().print(usr.getLogin());
    } catch (CerberusException myexception) {
        response.getWriter().print(myexception.getMessageError().getDescription());
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) CerberusException(org.cerberus.exception.CerberusException) User(org.cerberus.crud.entity.User) IUserService(org.cerberus.crud.service.IUserService) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 50 with CerberusException

use of org.cerberus.exception.CerberusException in project cerberus-source by cerberustesting.

the class ReadInvariant 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 {
    String echo = request.getParameter("sEcho");
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    invariantService = appContext.getBean(InvariantService.class);
    String charset = request.getCharacterEncoding();
    response.setContentType("application/json");
    response.setCharacterEncoding("utf8");
    // type=public or private? //TODO?
    try {
        AnswerItem answer;
        JSONObject jsonResponse = new JSONObject();
        String access = request.getParameter("access");
        if (request.getParameter("idName") == null && access != null) {
            if (!Strings.isNullOrEmpty(request.getParameter("columnName"))) {
                answer = findDistinctValuesOfColumn(appContext, request, request.getParameter("columnName"), access);
                jsonResponse = (JSONObject) answer.getItem();
            } else {
                answer = findInvariantList(appContext, access, request, response);
                jsonResponse = (JSONObject) answer.getItem();
            }
        } else if (request.getParameter("value") == null) {
            // loads the list of invariants
            String idName = policy.sanitize(request.getParameter("idName"));
            answer = findInvariantListByIdName(appContext, access, idName);
            jsonResponse = (JSONObject) answer.getItem();
        } else {
            String idName = request.getParameter("idName");
            String value = request.getParameter("value");
            try {
                answer = findInvariantListBykey(appContext, idName, value);
                JSONObject inv = new JSONObject();
                inv = convertInvariantToJSONObject((Invariant) answer.getItem());
                inv.put("hasPermissionsUpdate", invariantService.hasPermissionsUpdate((Invariant) answer.getItem(), request));
                jsonResponse.put("contentTable", inv);
            } catch (CerberusException e) {
                answer = new AnswerItem();
                MessageEvent msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
                answer.setResultMessage(msg);
            }
        }
        jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());
        jsonResponse.put("message", answer.getResultMessage().getDescription());
        // TODO:FN check if this makes sense
        jsonResponse.put("sEcho", echo);
        response.getWriter().print(jsonResponse.toString());
    } catch (JSONException e) {
        LOG.warn(e);
        // returns a default error message with the json format that is able to be parsed by the client-side
        response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) CerberusException(org.cerberus.exception.CerberusException) PolicyFactory(org.owasp.html.PolicyFactory) JSONObject(org.json.JSONObject) MessageEvent(org.cerberus.engine.entity.MessageEvent) JSONException(org.json.JSONException) InvariantService(org.cerberus.crud.service.impl.InvariantService) IInvariantService(org.cerberus.crud.service.IInvariantService) AnswerItem(org.cerberus.util.answer.AnswerItem)

Aggregations

CerberusException (org.cerberus.exception.CerberusException)159 MessageEvent (org.cerberus.engine.entity.MessageEvent)64 MessageGeneral (org.cerberus.engine.entity.MessageGeneral)58 ApplicationContext (org.springframework.context.ApplicationContext)58 JSONObject (org.json.JSONObject)54 JSONException (org.json.JSONException)53 Connection (java.sql.Connection)48 SQLException (java.sql.SQLException)48 PreparedStatement (java.sql.PreparedStatement)47 AnswerItem (org.cerberus.util.answer.AnswerItem)41 ArrayList (java.util.ArrayList)37 IOException (java.io.IOException)35 PolicyFactory (org.owasp.html.PolicyFactory)35 ILogEventService (org.cerberus.crud.service.ILogEventService)34 Answer (org.cerberus.util.answer.Answer)34 ServletException (javax.servlet.ServletException)26 ResultSet (java.sql.ResultSet)18 TestCase (org.cerberus.crud.entity.TestCase)16 JSONArray (org.json.JSONArray)16 HashMap (java.util.HashMap)12