Best Cerberus-source code snippet using org.cerberus.servlet.crud.testexecution.ReadTestCaseExecutionMedia.processRequest
Source:ReadTestCaseExecutionMedia.java
...69 * @throws ServletException if a servlet-specific error occurs70 * @throws IOException if an I/O error occurs71 * @throws CerberusException72 */73 protected void processRequest(HttpServletRequest request, HttpServletResponse response)74 throws ServletException, IOException, CerberusException {75 String charset = request.getCharacterEncoding();76 boolean auto = ParameterParserUtil.parseBooleanParam(request.getParameter("auto"), true);77 String type = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("type"), "", charset);78 String test = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("test"), "", charset);79 String testcase = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("testcase"), "", charset);80 String fileName = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("filename"), "", charset);81 String fileType = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("filetype"), "", charset);82 String fileDesc = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("filedesc"), "", charset);83 int step = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("step"), 0, charset);84 int index = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("index"), 1, charset);85 int sequence = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("sequence"), 0, charset);86 int sequenceControl = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("sequenceControl"), 0, charset);87 int iterator = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("iterator"), 0, charset);88 boolean autoContentType = ParameterParserUtil.parseBooleanParam(request.getParameter("autoContentType"), true);89 long id = ParameterParserUtil.parseLongParamAndDecode(request.getParameter("id"), 0, charset);90 // Calling Servlet Transversal Util.91 ServletUtil.servletStart(request);92 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());93 IParameterService parameterService = appContext.getBean(IParameterService.class);94 BufferedImage b = null;95 AnswerList al = new AnswerList<>(new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED));96 TestCaseExecutionFile tceFile = null;97 if (!(fileName.equals(""))) {98 IFactoryTestCaseExecutionFile factoryTestCaseExecutionFile = appContext.getBean(IFactoryTestCaseExecutionFile.class);99 tceFile = factoryTestCaseExecutionFile.create(0, 0, "", fileDesc, fileName, fileType, "", null, "", null);100 } else {101 ITestCaseExecutionFileService testCaseExecutionFileService = appContext.getBean(ITestCaseExecutionFileService.class);102 String levelFile = "";103 if (type.equals("action")) {104 levelFile = test + "-" + testcase + "-" + step + "-" + index + "-" + sequence;105 } else if (type.equals("control")) {106 levelFile = test + "-" + testcase + "-" + step + "-" + index + "-" + sequence + "-" + sequenceControl;107 }108 al = testCaseExecutionFileService.readByVarious(id, levelFile);109 if (al.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && !al.getDataList().isEmpty()) {110 Iterator i = al.getDataList().iterator();111 int indexIterator = -1;112 while (i.hasNext() && indexIterator != iterator) {113 indexIterator++;114 TestCaseExecutionFile tctemp = (TestCaseExecutionFile) i.next();115 if (indexIterator == iterator) {116 tceFile = tctemp;117 }118 }119 } else {120 // If previous read failed we try without index. (that can be removed few moths after step index has been introduced in Jan 2017)121 if (type.equals("action")) {122 levelFile = test + "-" + testcase + "-" + step + "-" + sequence;123 } else if (type.equals("control")) {124 levelFile = test + "-" + testcase + "-" + step + "-" + sequence + "-" + sequenceControl;125 }126 al = testCaseExecutionFileService.readByVarious(id, levelFile);127 if (al.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && !al.getDataList().isEmpty()) {128 Iterator i = al.getDataList().iterator();129 int indexIterator = -1;130 while (i.hasNext() && indexIterator != iterator) {131 indexIterator++;132 TestCaseExecutionFile tctemp = (TestCaseExecutionFile) i.next();133 if (indexIterator == iterator) {134 tceFile = tctemp;135 }136 }137 }138 }139 }140 if (tceFile != null) {141 String pathString = "";142 if (auto) {143 pathString = parameterService.getParameterStringByKey("cerberus_exeautomedia_path", "", "");144 } else {145 pathString = parameterService.getParameterStringByKey("cerberus_exemanualmedia_path", "", "");146 }147 switch (tceFile.getFileType()) {148 case "JPG":149 case "JPEG":150 if (autoContentType) {151 response.setContentType("image/jpeg");152 }153 returnImage(request, response, tceFile, pathString);154 break;155 case "PNG":156 if (autoContentType) {157 response.setContentType("image/png");158 }159 returnImage(request, response, tceFile, pathString);160 break;161 case "GIF":162 if (autoContentType) {163 response.setContentType("image/gif");164 }165 returnImage(request, response, tceFile, pathString);166 break;167 case "HTML":168 if (autoContentType) {169 response.setContentType("text/html");170 }171 returnFile(request, response, tceFile, pathString);172 break;173 case "XML":174 if (autoContentType) {175 response.setContentType("application/xml");176 }177 returnFile(request, response, tceFile, pathString);178 break;179 case "JSON":180 if (autoContentType) {181 response.setContentType("application/json");182 }183 returnFile(request, response, tceFile, pathString);184 break;185 case "TXT":186 returnFile(request, response, tceFile, pathString);187 break;188 case "PDF":189 returnPDF(request, response, tceFile, pathString);190 default:191 returnNotSupported(request, response, tceFile, pathString);192 }193 }194 }195 private void returnImage(HttpServletRequest request, HttpServletResponse response, TestCaseExecutionFile tc, String filePath) throws IOException {196 int width = (!StringUtils.isEmpty(request.getParameter("w"))) ? Integer.valueOf(request.getParameter("w")) : 150;197 int height = (!StringUtils.isEmpty(request.getParameter("h"))) ? Integer.valueOf(request.getParameter("h")) : 100;198 Boolean real = request.getParameter("r") != null;199 BufferedImage image = null;200 BufferedImage b = null;201 filePath = StringUtil.addSuffixIfNotAlready(filePath, File.separator);202 File picture = new File(filePath + tc.getFileName());203 LOG.debug("Accessing File : " + picture.getAbsolutePath());204 try {205 if (real) {206 b = ImageIO.read(picture);207 ImageIO.write(b, "png", response.getOutputStream());208 } else {209 image = ImageIO.read(picture);210 // We test if file is too thin or too long. That prevent 500 error in case files are not compatible with resize. In that case, we crop the file.211 if ((image.getHeight() * width / image.getWidth() < 10) || (image.getWidth() * height / image.getHeight() < 15)) {212 LOG.debug("Image is too big of thin. Target Height : " + image.getHeight() * width / image.getWidth() + " Target Width : " + image.getWidth() * height / image.getHeight());213 b = ImageIO.read(picture);214 int minwidth = width;215 if (width > image.getWidth()) {216 minwidth = image.getWidth();217 }218 int minheight = height;219 if (height > image.getHeight()) {220 minheight = image.getHeight();221 }222 BufferedImage crop = ((BufferedImage) b).getSubimage(0, 0, minwidth, minheight);223 b = crop;224 response.setHeader("Format-Status", "ERROR");225 response.setHeader("Format-Status-Message", "Image Crop from : " + image.getWidth() + "X" + image.getHeight() + " to : " + minwidth + "X" + minheight);226 } else {227 ResampleOp rop = new ResampleOp(DimensionConstrain.createMaxDimension(width, height, true));228 rop.setNumberOfThreads(4);229 b = rop.filter(image, null);230 response.setHeader("Format-Status", "OK");231 }232 }233 } catch (IOException e) {234 }235 response.setHeader("Last-Modified", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString());236 response.setHeader("Expires", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString());237 response.setHeader("Type", "PNG");238 response.setHeader("Description", tc.getFileDesc());239 ImageIO.write(b, "png", response.getOutputStream());240 }241 private void returnPDF(HttpServletRequest request, HttpServletResponse response, TestCaseExecutionFile tc, String filePath) {242 File pdfFile = null;243 filePath = StringUtil.addSuffixIfNotAlready(filePath, File.separator);244 pdfFile = new File(filePath + tc.getFileName());245 response.setContentType("application/pdf");246 response.setContentLength((int) pdfFile.length());247 try {248 Files.copy(pdfFile, response.getOutputStream());249 } catch (IOException e) {250 Log.warn(e);251 }252 }253 private void returnFile(HttpServletRequest request, HttpServletResponse response, TestCaseExecutionFile tc, String filePath) {254 String everything = "";255 filePath = StringUtil.addSuffixIfNotAlready(filePath, File.separator);256 LOG.debug("Accessing File : " + filePath + tc.getFileName());257 try (FileInputStream inputStream = new FileInputStream(filePath + tc.getFileName())) {258 everything = IOUtils.toString(inputStream);259 response.getWriter().print(everything);260 } catch (FileNotFoundException e) {261 } catch (IOException e) {262 }263 response.setHeader("Last-Modified", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString());264 response.setHeader("Expires", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString());265 response.setHeader("Type", tc.getFileType());266 response.setHeader("Description", tc.getFileDesc());267 }268 private void returnText(HttpServletRequest request, HttpServletResponse response, TestCaseExecutionFile tc, String filePath) {269 response.setHeader("Last-Modified", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString());270 response.setHeader("Expires", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString());271 response.setHeader("Type", tc.getFileType());272 response.setHeader("Description", tc.getFileDesc());273 }274 private void returnNotSupported(HttpServletRequest request, HttpServletResponse response, TestCaseExecutionFile tc, String filePath) {275 response.setHeader("Last-Modified", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString());276 response.setHeader("Expires", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString());277 response.setHeader("Type", tc.getFileType());278 }279 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">280 /**281 * Handles the HTTP <code>GET</code> method.282 *283 * @param request servlet request284 * @param response servlet response285 * @throws ServletException if a servlet-specific error occurs286 * @throws IOException if an I/O error occurs287 */288 @Override289 protected void doGet(HttpServletRequest request, HttpServletResponse response)290 throws ServletException, IOException {291 try {292 processRequest(request, response);293 } catch (CerberusException ex) {294 LOG.warn(ex);295 }296 }297 /**298 * Handles the HTTP <code>POST</code> method.299 *300 * @param request servlet request301 * @param response servlet response302 * @throws ServletException if a servlet-specific error occurs303 * @throws IOException if an I/O error occurs304 */305 @Override306 protected void doPost(HttpServletRequest request, HttpServletResponse response)307 throws ServletException, IOException {308 try {309 processRequest(request, response);310 } catch (CerberusException ex) {311 LOG.warn(ex);312 }313 }314 /**315 * Returns a short description of the servlet.316 *317 * @return a String containing servlet description318 */319 @Override320 public String getServletInfo() {321 return "Short description";322 }// </editor-fold>323}...
processRequest
Using AI Code Generation
1import org.cerberus.servlet.crud.testexecution.ReadTestCaseExecutionMedia;2ReadTestCaseExecutionMedia readTestCaseExecutionMedia = new ReadTestCaseExecutionMedia();3String result = readTestCaseExecutionMedia.processRequest(request, response, application);4import org.cerberus.servlet.crud.testexecution.ReadTestCaseExecutionMedia;5ReadTestCaseExecutionMedia readTestCaseExecutionMedia = new ReadTestCaseExecutionMedia();6String result = readTestCaseExecutionMedia.processRequest(request, response, application);7import org.cerberus.servlet.crud.testexecution.ReadTestCaseExecutionMedia;8ReadTestCaseExecutionMedia readTestCaseExecutionMedia = new ReadTestCaseExecutionMedia();9String result = readTestCaseExecutionMedia.processRequest(request, response, application);
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!!