Best Cerberus-source code snippet using org.cerberus.servlet.crud.usermanagement.ReadUserPublic.processRequest
Source:ReadUserPublic.java
...60 * @param response servlet response61 * @throws ServletException if a servlet-specific error occurs62 * @throws IOException if an I/O error occurs63 */64 protected void processRequest(HttpServletRequest request, HttpServletResponse response)65 throws ServletException, IOException {66 String echo = request.getParameter("sEcho");67 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());68 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);69 response.setContentType("application/json");70 response.setCharacterEncoding("utf8");71 // Default message to unexpected error.72 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);73 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));74 /**75 * Parsing and securing all required parameters.76 */77 Integer brpid = 0;78 boolean brpid_error = true;79 try {80 if (request.getParameter("id") != null && !request.getParameter("id").equals("")) {81 brpid = Integer.valueOf(policy.sanitize(request.getParameter("id")));82 brpid_error = false;83 }84 } catch (Exception ex) {85 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);86 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME));87 msg.setDescription(msg.getDescription().replace("%OPERATION%", "Read"));88 msg.setDescription(msg.getDescription().replace("%REASON%", "id must be an integer value."));89 brpid_error = true;90 }91 // Init Answer with potencial error from Parsing parameter.92 AnswerItem answer = new AnswerItem(msg);93 try {94 JSONObject jsonResponse = new JSONObject();95 if ((request.getParameter("id") != null) && !(brpid_error)) { // ID parameter is specified so we return the unique record of object.96// answer = readByKey(appContext, brpid); // TODO97 jsonResponse = (JSONObject) answer.getItem();98 } else { // Default behaviour, we return the simple list of objects.99 answer = findUserList(appContext, request, response);100 jsonResponse = (JSONObject) answer.getItem();101 }102 jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());103 jsonResponse.put("message", answer.getResultMessage().getDescription());104 jsonResponse.put("sEcho", echo);105 response.getWriter().print(jsonResponse.toString());106 } catch (JSONException e) {107 LOG.warn(e);108 //returns a default error message with the json format that is able to be parsed by the client-side109 response.getWriter().print(AnswerUtil.createGenericErrorAnswer());110 }111 }112 private AnswerItem findUserList(ApplicationContext appContext, HttpServletRequest request, HttpServletResponse response) throws JSONException {113 AnswerItem item = new AnswerItem();114 JSONObject jsonResponse = new JSONObject();115 userService = appContext.getBean(UserService.class);116 int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));117 int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));118 /*int sEcho = Integer.valueOf(request.getParameter("sEcho"));*/119 String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");120 int columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_0"), "1"));121 String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "userID,login,name");122 String columnToSort[] = sColumns.split(",");123 String columnName = columnToSort[columnToSortParameter];124 String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "asc");125 AnswerList resp = userService.readByCriteria(startPosition, length, columnName, sort, searchParameter, "");126 JSONArray jsonArray = new JSONArray();127 if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {//the service was able to perform the query, then we should get all values128 for (User user : (List<User>) resp.getDataList()) {129 jsonArray.put(convertUserToJSONObject(user));130 }131 }132 jsonResponse.put("contentTable", jsonArray);133 jsonResponse.put("iTotalRecords", resp.getTotalRows());134 jsonResponse.put("iTotalDisplayRecords", resp.getTotalRows());135 item.setItem(jsonResponse);136 item.setResultMessage(resp.getResultMessage());137 return item;138 }139// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">140 /**141 * Handles the HTTP <code>GET</code> method.142 *143 * @param request servlet request144 * @param response servlet response145 * @throws ServletException if a servlet-specific error occurs146 * @throws IOException if an I/O error occurs147 */148 @Override149 protected void doGet(HttpServletRequest request, HttpServletResponse response)150 throws ServletException, IOException {151 processRequest(request, response);152 }153 /**154 * Handles the HTTP <code>POST</code> method.155 *156 * @param request servlet request157 * @param response servlet response158 * @throws ServletException if a servlet-specific error occurs159 * @throws IOException if an I/O error occurs160 */161 @Override162 protected void doPost(HttpServletRequest request, HttpServletResponse response)163 throws ServletException, IOException {164 processRequest(request, response);165 }166 /**167 * Returns a short description of the servlet.168 *169 * @return a String containing servlet description170 */171 @Override172 public String getServletInfo() {173 return "Short description";174 }// </editor-fold>175 private JSONObject convertUserToJSONObject(User user) throws JSONException {176 JSONObject result = new JSONObject();177 178 result.put("login", user.getLogin());...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!