Search code examples
javafile-uploadckeditorseamapache-commons

Custom ckeditor fileUpload in java with apache fileUpload parseRequest returns empty list


I have a seam 2.2.2 application and I'm trying to customize ckeditor fileUpload plugin to it.

The solution I've come up is:

1) init the editor for all elements with 'editor' style class:

var elements = CKEDITOR.document.find('.editor');
for(var i = 0; i< elements["$"].length; i++){
        CKEDITOR.replace(elements["$"][i], {
            filebrowserUploadUrl: rootPath + "/cops/filebrowserUploadUrl.seam"
        });
    }

2) Set filebrowserUploadUrl.seam, to do nothing but to execute:

#{attachmentController.sendImageToServer()}

3) implement the back-end with apache commons fileUpload:

public void sendImageToServer()
{       
    HttpServletRequest request = ServletContexts.instance().getRequest();
    DiskFileItemFactory factory = new DiskFileItemFactory();

    File repository = (File) request.getAttribute("javax.servlet.context.tempdir");
    factory.setRepository(repository);
    ServletFileUpload upload = new ServletFileUpload(factory);

    try
    {
        List<FileItem> items = upload.parseRequest(request);
        processItems(items); //set the file data to specific att
        saveOpenAttachment(); //save the file to disk
    }

This method is called all right. I can debbug an upload parameter (with some binary data) inside the request but upload.parseRequest(request) returns an empty list. I have searched this problem and I did everything I could do but I am not able to tell if the application custom FaceletViewHandler is causing this. Although if I could find the solution to this problem I would be very satisfied, I'm feeling this is not a good solution. Maybe the integration of Seam with facelets could give me a better solution. I really don't know. Any suggestion?


Solution

  • Final solution was like Seam multipart filter handle requests:

    ServletRequest request = (ServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    try
    {
      if (!(request instanceof MultipartRequest))
      {
         request = unwrapMultipartRequest(request);
      }
    
      if (request instanceof MultipartRequest)
      {
         MultipartRequest multipartRequest = (MultipartRequest) request;
    
         String clientId = "upload";
         setFileData(multipartRequest.getFileBytes(clientId));
         setFileContentType(multipartRequest.getFileContentType(clientId));
         setFileName(multipartRequest.getFileName(clientId));
         saveOpenAttachment();
      }
    }
    

    Now I handle the request like Seam does, and do not need the web:multipart-filter config that was breaking other types of request.