Search in sources :

Example 26 with FileItemFactory

use of org.apache.commons.fileupload.FileItemFactory in project cerberus-source by cerberustesting.

the class CreateUpdateTestCaseExecutionFile 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 org.cerberus.exception.CerberusException
 * @throws org.json.JSONException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException, JSONException {
    JSONObject jsonResponse = new JSONObject();
    Answer ans = new Answer();
    Gson gson = new Gson();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    ans.setResultMessage(msg);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    response.setContentType("application/json");
    String charset = request.getCharacterEncoding();
    Map<String, String> fileData = new HashMap<String, String>();
    FileItem file = null;
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    try {
        List<FileItem> fields = upload.parseRequest(request);
        Iterator<FileItem> it = fields.iterator();
        if (!it.hasNext()) {
            return;
        }
        while (it.hasNext()) {
            FileItem fileItem = it.next();
            boolean isFormField = fileItem.isFormField();
            if (isFormField) {
                fileData.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));
            } else {
                file = fileItem;
            }
        }
    } catch (FileUploadException e) {
        e.printStackTrace();
    }
    /**
     * Parsing and securing all required parameters
     */
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(fileData.get("desc"), null, charset);
    String extension = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(fileData.get("type"), "", charset);
    String fileName = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(fileData.get("fileName"), null, charset);
    Integer fileID = ParameterParserUtil.parseIntegerParam(fileData.get("fileID"), 0);
    Integer idex = ParameterParserUtil.parseIntegerParam(fileData.get("idex"), 0);
    boolean action = fileData.get("action") != null ? true : false;
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IRecorderService recorderService = appContext.getBean(IRecorderService.class);
    TestCaseStepActionExecution testCaseStepActionExecution = null;
    TestCaseStepActionControlExecution testCaseStepActionControlExecution = null;
    JSONArray obj = null;
    if (action) {
        obj = new JSONArray(fileData.get("action"));
        testCaseStepActionExecution = updateTestCaseStepActionExecutionFromJsonArray(obj, appContext);
    } else {
        obj = new JSONArray(fileData.get("control"));
        testCaseStepActionControlExecution = updateTestCaseStepActionControlExecutionFromJsonArray(obj, appContext);
    }
    if (description.isEmpty()) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "manual testcase execution file").replace("%OPERATION%", "Create/Update").replace("%REASON%", "desc is missing!"));
        ans.setResultMessage(msg);
    } else {
        ans = recorderService.recordManuallyFile(testCaseStepActionExecution, testCaseStepActionControlExecution, extension, description, file, idex, fileName, fileID);
    }
    if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        /**
         * Object created. Adding Log entry.
         */
        ILogEventService logEventService = appContext.getBean(LogEventService.class);
        logEventService.createForPrivateCalls("/CreateUpdateTestCaseExecutionFile", "CREATE", "Create execution file", request);
    }
    /**
     * Formating and returning the json result.
     */
    jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", ans.getResultMessage().getDescription());
    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
}
Also used : PolicyFactory(org.owasp.html.PolicyFactory) HashMap(java.util.HashMap) MessageEvent(org.cerberus.engine.entity.MessageEvent) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) TestCaseStepActionExecution(org.cerberus.crud.entity.TestCaseStepActionExecution) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) Answer(org.cerberus.util.answer.Answer) FileItem(org.apache.commons.fileupload.FileItem) ApplicationContext(org.springframework.context.ApplicationContext) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) IRecorderService(org.cerberus.engine.execution.IRecorderService) JSONObject(org.json.JSONObject) TestCaseStepActionControlExecution(org.cerberus.crud.entity.TestCaseStepActionControlExecution) ILogEventService(org.cerberus.crud.service.ILogEventService) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 27 with FileItemFactory

use of org.apache.commons.fileupload.FileItemFactory in project data-access by pentaho.

the class UploadFileDebugServlet method getFileItem.

private FileItem getFileItem(HttpServletRequest request) {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    try {
        List items = upload.parseRequest(request);
        Iterator it = items.iterator();
        while (it.hasNext()) {
            FileItem item = (FileItem) it.next();
            if (!item.isFormField() && "uploadFormElement".equals(item.getFieldName())) {
                // $NON-NLS-1$
                return item;
            }
        }
    } catch (FileUploadException e) {
        return null;
    }
    return null;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) Iterator(java.util.Iterator) List(java.util.List) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

FileItemFactory (org.apache.commons.fileupload.FileItemFactory)27 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)27 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)24 FileItem (org.apache.commons.fileupload.FileItem)23 FileUploadException (org.apache.commons.fileupload.FileUploadException)16 HashMap (java.util.HashMap)9 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 JSONObject (org.json.JSONObject)6 ApplicationContext (org.springframework.context.ApplicationContext)6 File (java.io.File)5 InputStream (java.io.InputStream)5 ServletException (javax.servlet.ServletException)5 ILogEventService (org.cerberus.crud.service.ILogEventService)5 MessageEvent (org.cerberus.engine.entity.MessageEvent)5 Answer (org.cerberus.util.answer.Answer)5 Iterator (java.util.Iterator)4 AnswerItem (org.cerberus.util.answer.AnswerItem)4 BufferedReader (java.io.BufferedReader)3