Best Cerberus-source code snippet using org.cerberus.servlet.zzpublic.ResultCIV003.processRequest
Source:ResultCIV003.java
...48 */49@WebServlet(name = "ResultCIV003", urlPatterns = {"/ResultCIV003"})50public class ResultCIV003 extends HttpServlet {51 private static Logger LOG = LogManager.getLogger(ResultCIV003.class);52 protected void processRequest(HttpServletRequest request,53 HttpServletResponse response) throws ServletException, IOException {54 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());55 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);56 // Calling Servlet Transversal Util.57 ServletUtil.servletStart(request);58 /**59 * Adding Log entry.60 */61 ILogEventService logEventService = appContext.getBean(ILogEventService.class);62 logEventService.createForPublicCalls("/ResultCIV003", "CALL", "ResultCIV003 called : " + request.getRequestURL(), request);63 try {64 JSONObject jsonResponse = new JSONObject();65 String tag = policy.sanitize(request.getParameter("tag"));66 String campaign = policy.sanitize(request.getParameter("campaign"));67 String outputFormat = policy.sanitize(request.getParameter("outputformat"));68 String helpMessage = "This servlet is used to provide various execution counters as well as a global OK or KO status based on the number and status of the execution done on a specific tag. \n"69 + "The number of executions are ponderated by parameters by priority from cerberus_ci_okcoefprio1 to cerberus_ci_okcoefprio4. \n"70 + "Formula used is the following : \n"71 + "Nb Exe Prio 1 testcases * cerberus_ci_okcoefprio1 + Nb Exe Prio 2 testcases * cerberus_ci_okcoefprio2 + "72 + "Nb Exe Prio 3 testcases * cerberus_ci_okcoefprio3 + Nb Exe Prio 4 testcases * cerberus_ci_okcoefprio4.\n"73 + "If no executions are found, the result is KO.\n"74 + "With at least 1 execution, if result is < 1 then global servlet result is OK. If not, it is KO.\n"75 + "All execution needs to have a status equal to KO, FA, NA, PE or NE.\n"76 + "If at least 1 PE or 1 NE if found, global status will be PE\n"77 + "Output format is json by default, or SVG if outputFormat=svg is defined\n"78 + "Parameter list :\n"79 + "- tag : Execution Tag to filter the test cases execution. [" + tag + "]\n"80 + "- campaign : If you feed the campaign, the lastest Tag from that campaign will be selected. [" + campaign + "]\n"81 + "- outputformat : ['text','json', 'svg']. Output format of the result. [" + outputFormat + "]\n";82 jsonResponse.put("helpMessage", helpMessage);83 boolean error = false;84 String error_message = "";85 // Checking the parameter validity. Tag is a mandatory parameter86 if (StringUtil.isNullOrEmpty(tag)) {87 if (!StringUtil.isNullOrEmpty(campaign)) {88 ITagService tagService = appContext.getBean(ITagService.class);89 List<Tag> myList;90 AnswerList myAnswerList = tagService.readByVariousByCriteria(campaign, 0, 1, "id", "desc", null, null);91 if (myAnswerList.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {//the service was able to perform the query, then we should get all values92 for (Tag tagCur : (List<Tag>) myAnswerList.getDataList()) {93 tag = tagCur.getTag();94 }95 }96 }97 }98 if (StringUtil.isNullOrEmpty(tag)) {99 error_message += "Error - Either specify a tag or specify a campaign to get the latest tag from that campaign.";100 error = true;101 }102 if (!error) {103 ICIService ciService = appContext.getBean(ICIService.class);104 jsonResponse = ciService.getCIResult(tag);105 // Log the result with calculation detail.106 logEventService.createForPublicCalls("/ResultCIV003", "CALLRESULT", "ResultCIV003 calculated for tag " + tag + " result [" + jsonResponse.getString("result") + "]", request);107 } else {108 jsonResponse.put("messageType", "KO");109 jsonResponse.put("message", error_message);110 }111 generateResponse(response, outputFormat, jsonResponse, error);112 } catch (JSONException e) {113 LOG.warn(e);114 //returns a default error message with the json format that is able to be parsed by the client-side115 response.getWriter().print(AnswerUtil.createGenericErrorAnswer());116 }117 }118 private void generateResponse(HttpServletResponse response, String outputFormat, JSONObject jsonResponse, boolean error) throws IOException {119 StringBuilder returnMessage = new StringBuilder();120 switch (outputFormat) {121 case "svg":122 response.setContentType("image/svg+xml");123 response.setHeader("Cache-Control", "no-cache");124 response.setDateHeader("Expires", 0);125 try {126 String tagToDisplay = "";127 String resultToDisplay = "";128 if (error) {129 tagToDisplay = StringUtils.substring(jsonResponse.getString("message"), 0, 32);130 resultToDisplay = "ERR";131 } else {132 tagToDisplay = StringUtils.substring(jsonResponse.getString("tag"), 0, 32);133 resultToDisplay = jsonResponse.getString("result");134 }135 String responseSvg = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"350\" height=\"20\">"136 + "<linearGradient id=\"b\" x2=\"0\" y2=\"100%\">"137 + "<stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"></stop>"138 + "<stop offset=\"1\" stop-opacity=\".1\"></stop>"139 + "</linearGradient>"140 //RECTANGLE141 + "<rect rx=\"3\" fill=\"#555\" width=\"250\" height=\"20\"></rect>"142 + "<rect rx=\"3\" x=\"210\" fill=\"" + getColor(resultToDisplay) + "\" width=\"40\" height=\"20\"></rect>"143 //TEXT 144 + "<g fill=\"#fff\" text-anchor=\"start\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"9\">"145 + "<text x=\"10\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">" + tagToDisplay + "</text>"146 + "<text x=\"10\" y=\"14\">" + tagToDisplay + "</text>"147 + "<text x=\"225\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">" + resultToDisplay + "</text>"148 + "<text x=\"225\" y=\"14\">" + resultToDisplay + "</text>"149 + "</g>"150 + "</svg>";151 response.getWriter().print(responseSvg);152 } catch (JSONException ex) {153 LOG.warn(ex);154 }155 break;156 case "text":157 response.setContentType("text");158 response.setCharacterEncoding("utf8");159 try {160 if (error) {161 returnMessage.append(jsonResponse.get("message"));162 returnMessage.append("\n\n");163 returnMessage.append(jsonResponse.get("helpMessage"));164 } else {165 returnMessage.append(jsonResponse.get("result"));166 }167 response.getWriter().print(returnMessage.toString());168 } catch (JSONException ex) {169 LOG.warn(ex);170 }171 break;172 default:173 response.setContentType("application/json");174 response.setCharacterEncoding("utf8");175 response.getWriter().print(jsonResponse.toString());176 }177 }178 private String getColor(String controlStatus) {179 String color = null;180 if ("OK".equals(controlStatus)) {181 color = "#5CB85C";182 } else if ("KO".equals(controlStatus)) {183 color = "#D9534F";184 } else {185 color = "#3498DB";186 }187 return color;188 }189 // <editor-fold defaultstate="collapsed"190 // desc="HttpServlet methods. Click on the + sign on the left to edit the code.">191 /**192 * Handles the HTTP <code>GET</code> method.193 *194 * @param request servlet request195 * @param response servlet response196 * @throws ServletException if a servlet-specific error occurs197 * @throws IOException if an I/O error occurs198 */199 @Override200 protected void doGet(HttpServletRequest request,201 HttpServletResponse response) throws ServletException, IOException {202 processRequest(request, response);203 }204 /**205 * Handles the HTTP <code>POST</code> method.206 *207 * @param request servlet request208 * @param response servlet response209 * @throws ServletException if a servlet-specific error occurs210 * @throws IOException if an I/O error occurs211 */212 @Override213 protected void doPost(HttpServletRequest request,214 HttpServletResponse response) throws ServletException, IOException {215 processRequest(request, response);216 }217 /**218 * Returns a short description of the servlet.219 *220 * @return a String containing servlet description221 */222 @Override223 public String getServletInfo() {224 return "Short description";225 }// </editor-fold>226}...
processRequest
Using AI Code Generation
1var resultCIV003 = new org.cerberus.servlet.zzpublic.ResultCIV003();2var result = resultCIV003.processRequest();3var stringUtil = new org.cerberus.util.StringUtil();4var jsonArray = new org.cerberus.util.json.JSONArray(result);5var count = jsonArray.length();6var table = "|Country|Code|";7for (var i = 0; i < count; i++) {8 var country = jsonArray.getJSONObject(i);9 var name = country.getString("Name");10 var code = country.getString("Code");11| " + stringUtil.escapeMarkdown(name) + " | " + stringUtil.escapeMarkdown(code) + " |";12}13return table;14[code=javascript;title=Sample Code: Retrieve the list of countries and display it in a table using the processRequest method of org.cerberus.servlet.zzpublic.ResultCIV003 class]15[code=javascript;title=Sample Code: Retrieve the list of countries and display it in a table using the processRequest method of org.cerberus.servlet.zzpublic.ResultCIV004 class]
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!!