Best Cerberus-source code snippet using org.cerberus.servlet.crud.transversaltables.CreateLabel.processRequest
Source: CreateLabel.java
...64 * @throws IOException if an I/O error occurs65 * @throws org.cerberus.exception.CerberusException66 * @throws org.json.JSONException67 */68 protected void processRequest(HttpServletRequest request, HttpServletResponse response)69 throws ServletException, IOException, CerberusException, JSONException {70 JSONObject jsonResponse = new JSONObject();71 Answer ans = new Answer();72 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);73 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));74 ans.setResultMessage(msg);75 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);76 String charset = request.getCharacterEncoding();77 response.setContentType("application/json");78 // Calling Servlet Transversal Util.79 ServletUtil.servletStart(request);80 81 /**82 * Parsing and securing all required parameters.83 */84 // Parameter that are already controled by GUI (no need to decode) --> We SECURE them85 String id = policy.sanitize(request.getParameter("id"));86 String system = policy.sanitize(request.getParameter("system"));87 String type = policy.sanitize(request.getParameter("type"));88 String longDesc = policy.sanitize(request.getParameter("longdesc"));89 String reqtype = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("reqtype"), "", charset);90 String reqstatus = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("reqstatus"), "", charset);91 String reqcriticity = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("reqcriticity"), "", charset);92 // Parameter that needs to be secured --> We SECURE+DECODE them93 String label = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("label"), "", charset);94 String color = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("color"), "", charset);95 String parentLabel = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("parentLabel"), "", charset);96 String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);97 String usr = request.getUserPrincipal().getName();98 99 /**100 * Checking all constrains before calling the services.101 */102 if (StringUtil.isNullOrEmpty(label)) {103 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);104 msg.setDescription(msg.getDescription().replace("%ITEM%", "Label")105 .replace("%OPERATION%", "Create")106 .replace("%REASON%", "Label is missing!"));107 ans.setResultMessage(msg);108 } else {109 /**110 * All data seems cleans so we can call the services.111 */112 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());113 ILabelService labelService = appContext.getBean(ILabelService.class);114 IFactoryLabel factoryLabel = appContext.getBean(IFactoryLabel.class);115 Timestamp creationDate = new Timestamp(new Date().getTime());116 Label labelData = factoryLabel.create(0, system, label, type, color, parentLabel, reqtype, reqstatus, reqcriticity, description, longDesc, usr,creationDate, usr, creationDate);117 ans = labelService.create(labelData);118 if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {119 /**120 * Object created. Adding Log entry.121 */122 ILogEventService logEventService = appContext.getBean(LogEventService.class);123 logEventService.createForPrivateCalls("/CreateLabel", "CREATE", "Create Label : ['" + label + "'] for System : ["+system+"]", request);124 }125 }126 /**127 * Formating and returning the json result.128 */129 jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());130 jsonResponse.put("message", ans.getResultMessage().getDescription());131 response.getWriter().print(jsonResponse);132 response.getWriter().flush();133 }134 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">135 /**136 * Handles the HTTP <code>GET</code> method.137 *138 * @param request servlet request139 * @param response servlet response140 * @throws ServletException if a servlet-specific error occurs141 * @throws IOException if an I/O error occurs142 */143 @Override144 protected void doGet(HttpServletRequest request, HttpServletResponse response)145 throws ServletException, IOException {146 try {147 processRequest(request, response);148 } catch (CerberusException ex) {149 LOG.warn(ex);150 } catch (JSONException ex) {151 LOG.warn(ex);152 }153 }154 /**155 * Handles the HTTP <code>POST</code> method.156 *157 * @param request servlet request158 * @param response servlet response159 * @throws ServletException if a servlet-specific error occurs160 * @throws IOException if an I/O error occurs161 */162 @Override163 protected void doPost(HttpServletRequest request, HttpServletResponse response)164 throws ServletException, IOException {165 try {166 processRequest(request, response);167 } catch (CerberusException ex) {168 LOG.warn(ex);169 } catch (JSONException ex) {170 LOG.warn(ex);171 }172 }173 /**174 * Returns a short description of the servlet.175 *176 * @return a String containing servlet description177 */178 @Override179 public String getServletInfo() {180 return "Short description";...
Check out the latest blogs from LambdaTest on this topic:
The QA testing career includes following an often long, winding road filled with fun, chaos, challenges, and complexity. Financially, the spectrum is broad and influenced by location, company type, company size, and the QA tester’s experience level. QA testing is a profitable, enjoyable, and thriving career choice.
Hey LambdaTesters! We’ve got something special for you this week. ????
API (Application Programming Interface) is a set of definitions and protocols for building and integrating applications. It’s occasionally referred to as a contract between an information provider and an information user establishing the content required from the consumer and the content needed by the producer.
In general, software testers have a challenging job. Software testing is frequently the final significant activity undertaken prior to actually delivering a product. Since the terms “software” and “late” are nearly synonymous, it is the testers that frequently catch the ire of the whole business as they try to test the software at the end. It is the testers who are under pressure to finish faster and deem the product “release candidate” before they have had enough opportunity to be comfortable. To make matters worse, if bugs are discovered in the product after it has been released, everyone looks to the testers and says, “Why didn’t you spot those bugs?” The testers did not cause the bugs, but they must bear some of the guilt for the bugs that were disclosed.
In some sense, testing can be more difficult than coding, as validating the efficiency of the test cases (i.e., the ‘goodness’ of your tests) can be much harder than validating code correctness. In practice, the tests are just executed without any validation beyond the pass/fail verdict. On the contrary, the code is (hopefully) always validated by testing. By designing and executing the test cases the result is that some tests have passed, and some others have failed. Testers do not know much about how many bugs remain in the code, nor about their bug-revealing efficiency.
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!!