Search in sources :

Example 1 with IFactoryTestCaseExecutionFile

use of org.cerberus.crud.factory.IFactoryTestCaseExecutionFile in project cerberus-source by cerberustesting.

the class ReadTestCaseExecutionMedia 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
 * @throws CerberusException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException {
    String charset = request.getCharacterEncoding();
    boolean auto = ParameterParserUtil.parseBooleanParam(request.getParameter("auto"), true);
    String type = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("type"), "", charset);
    String test = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("test"), "", charset);
    String testcase = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("testcase"), "", charset);
    String fileName = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("filename"), "", charset);
    String fileType = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("filetype"), "", charset);
    String fileDesc = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("filedesc"), "", charset);
    int step = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("step"), 0, charset);
    int index = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("index"), 1, charset);
    int sequence = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("sequence"), 0, charset);
    int sequenceControl = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("sequenceControl"), 0, charset);
    int iterator = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("iterator"), 0, charset);
    boolean autoContentType = ParameterParserUtil.parseBooleanParam(request.getParameter("autoContentType"), true);
    long id = ParameterParserUtil.parseLongParamAndDecode(request.getParameter("id"), 0, charset);
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IParameterService parameterService = appContext.getBean(IParameterService.class);
    BufferedImage b = null;
    AnswerList al = new AnswerList<>(new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED));
    TestCaseExecutionFile tceFile = null;
    if (!(fileName.equals(""))) {
        IFactoryTestCaseExecutionFile factoryTestCaseExecutionFile = appContext.getBean(IFactoryTestCaseExecutionFile.class);
        tceFile = factoryTestCaseExecutionFile.create(0, 0, "", fileDesc, fileName, fileType, "", null, "", null);
    } else {
        ITestCaseExecutionFileService testCaseExecutionFileService = appContext.getBean(ITestCaseExecutionFileService.class);
        String levelFile = "";
        if (type.equals("action")) {
            levelFile = test + "-" + testcase + "-" + step + "-" + index + "-" + sequence;
        } else if (type.equals("control")) {
            levelFile = test + "-" + testcase + "-" + step + "-" + index + "-" + sequence + "-" + sequenceControl;
        }
        al = testCaseExecutionFileService.readByVarious(id, levelFile);
        if (al.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && !al.getDataList().isEmpty()) {
            Iterator i = al.getDataList().iterator();
            int indexIterator = -1;
            while (i.hasNext() && indexIterator != iterator) {
                indexIterator++;
                TestCaseExecutionFile tctemp = (TestCaseExecutionFile) i.next();
                if (indexIterator == iterator) {
                    tceFile = tctemp;
                }
            }
        } else {
            // If previous read failed we try without index. (that can be removed few moths after step index has been introduced in Jan 2017)
            if (type.equals("action")) {
                levelFile = test + "-" + testcase + "-" + step + "-" + sequence;
            } else if (type.equals("control")) {
                levelFile = test + "-" + testcase + "-" + step + "-" + sequence + "-" + sequenceControl;
            }
            al = testCaseExecutionFileService.readByVarious(id, levelFile);
            if (al.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && !al.getDataList().isEmpty()) {
                Iterator i = al.getDataList().iterator();
                int indexIterator = -1;
                while (i.hasNext() && indexIterator != iterator) {
                    indexIterator++;
                    TestCaseExecutionFile tctemp = (TestCaseExecutionFile) i.next();
                    if (indexIterator == iterator) {
                        tceFile = tctemp;
                    }
                }
            }
        }
    }
    if (tceFile != null) {
        String pathString = "";
        if (auto) {
            pathString = parameterService.getParameterStringByKey("cerberus_exeautomedia_path", "", "");
        } else {
            pathString = parameterService.getParameterStringByKey("cerberus_exemanualmedia_path", "", "");
        }
        switch(tceFile.getFileType()) {
            case "JPG":
            case "JPEG":
                if (autoContentType) {
                    response.setContentType("image/jpeg");
                }
                returnImage(request, response, tceFile, pathString);
                break;
            case "PNG":
                if (autoContentType) {
                    response.setContentType("image/png");
                }
                returnImage(request, response, tceFile, pathString);
                break;
            case "GIF":
                if (autoContentType) {
                    response.setContentType("image/gif");
                }
                returnImage(request, response, tceFile, pathString);
                break;
            case "HTML":
                if (autoContentType) {
                    response.setContentType("text/html");
                }
                returnFile(request, response, tceFile, pathString);
                break;
            case "XML":
                if (autoContentType) {
                    response.setContentType("application/xml");
                }
                returnFile(request, response, tceFile, pathString);
                break;
            case "JSON":
                if (autoContentType) {
                    response.setContentType("application/json");
                }
                returnFile(request, response, tceFile, pathString);
                break;
            case "TXT":
                returnFile(request, response, tceFile, pathString);
                break;
            case "PDF":
                returnPDF(request, response, tceFile, pathString);
            default:
                returnNotSupported(request, response, tceFile, pathString);
        }
    }
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) MessageEvent(org.cerberus.engine.entity.MessageEvent) IFactoryTestCaseExecutionFile(org.cerberus.crud.factory.IFactoryTestCaseExecutionFile) ITestCaseExecutionFileService(org.cerberus.crud.service.ITestCaseExecutionFileService) IParameterService(org.cerberus.crud.service.IParameterService) BufferedImage(java.awt.image.BufferedImage) ApplicationContext(org.springframework.context.ApplicationContext) IFactoryTestCaseExecutionFile(org.cerberus.crud.factory.IFactoryTestCaseExecutionFile) TestCaseExecutionFile(org.cerberus.crud.entity.TestCaseExecutionFile)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1 TestCaseExecutionFile (org.cerberus.crud.entity.TestCaseExecutionFile)1 IFactoryTestCaseExecutionFile (org.cerberus.crud.factory.IFactoryTestCaseExecutionFile)1 IParameterService (org.cerberus.crud.service.IParameterService)1 ITestCaseExecutionFileService (org.cerberus.crud.service.ITestCaseExecutionFileService)1 MessageEvent (org.cerberus.engine.entity.MessageEvent)1 AnswerList (org.cerberus.util.answer.AnswerList)1 ApplicationContext (org.springframework.context.ApplicationContext)1