Expand my Community achievements bar.

Unable to parse Multipart form data file of DAM Upload

Avatar

Level 3

Hi,

When we upload file in upload, request will go to CreateAssetServlet. So before reaching there we are placing one filter for antivirus scan. But I am unable to parse file from multipart form data. When I iterate the Part, I am getting inputstream, but I notice I was able to read only file name(place log for inputstream data length),not the actual binary.

Anyone have idea how to parse the file from multipart form data?

Thanks,

Swati

8 Replies

Avatar

Administrator

Checking internally.



Kautuk Sahni

Avatar

Level 10

"But I am unable to parse file from multipart form data"

Here is an example DoPost to show you how to handle a file that is posted to a Sling Servlet:

@Override

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {

       

      try

         {

         final boolean isMultipart = org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(request);

         PrintWriter out = null;

         

           out = response.getWriter();

           if (isMultipart) {

             final java.util.Map<String, org.apache.sling.api.request.RequestParameter[]> params = request.getRequestParameterMap();

             for (final java.util.Map.Entry<String, org.apache.sling.api.request.RequestParameter[]> pairs : params.entrySet()) {

               final String k = pairs.getKey();

               final org.apache.sling.api.request.RequestParameter[] pArr = pairs.getValue();

               final org.apache.sling.api.request.RequestParameter param = pArr[0];

               final InputStream stream = param.getInputStream();

              

                   //Save the uploaded file into the Adobe CQ DAM

                out.println("The Sling Servlet placed the uploaded file here: " + writeToDam(stream,param.getFileName()));

             

             }

           }

         }

         

         catch (Exception e) {

             e.printStackTrace();

         }

     

     }

However - although this code works perfectly fine, i have never tried to get it working with anti-virus software.

Avatar

Level 2

IIUC the proposed filter is expected to read the multi-part content-body and run an antivirus on the content read. Is that right? If so, I'd be curious to know how will the content-body be 'forwarded' to downstream servlets (CreateAssetServlet in this case) so that they can continue processing it...

Also, is creating workflow/observation-listener to trigger the antivirus scan _after_ the asset is created in the repository not an option on the table?

Avatar

Level 3

Hi ,

smacdonald2008

I already tried with the above code, but the requestparametermap is coming empty. I have debugged for the attributes and getting that specific attribute to parse the uploaded file.

But now the problem is with filterchain. I am  triing to forward the chain request to next level , at the end of the process and inside null checks , but I am getting 400 bad request error. I am new to sling filters, unable to resolve the issue.

@ Ashish - Correct I am trying to scan digital asset uploaded in dam, before storing inside the jcr repository.

Thanks,

Swati

Avatar

Level 3

Update:

I am able to parse the multipart form data with below line of code,

Iterator<Part> partsIterator = (Iterator<Part>) slingRequest.getAttribute("request-parts-iterator");

But unable to parse whole parts.

When I checked the network response, I can see three parts available in the request. But I am getting only two parts.

1.name="_charset_"

2.name="fileName"

3.name="file"

In code when I parsed using above iterator I am getting only charset and filename but not the file. And the actual file is available in the third parameter for antivirus scan.

Thanks,

Swati

Avatar

Level 3

Thanks, Scott.

Below is the some more info for your reference,

Iterator<Part> partsIteratorone = (Iterator<Part>) request.getAttribute("request-parts-iterator");

logger.info("Request Parts Iterartor{}" , request.getAttribute("request-parts-iterator"));

boolean scanFileResponse = false;

Part part = partsIteratorone.next();

Part test=partsIteratorone.next();

Part testone =partsIteratorone.next();

filterChain.doFilter(request, response);

In the third iterator we are getting file and we are able to scan file too but when we forward the chainfilter to createAssetServlet, then we are getting below message in logs,

14.11.2018 20:48:41.076 *DEBUG* [0:0:0:0:0:0:0:1 [1542208721074] GET /content/dam.1.json HTTP/1.1] com.day.cq.dam.core.impl.servlet.DamContentDispositionFilter checking for acceptance

14.11.2018 20:48:41.108 *DEBUG* [0:0:0:0:0:0:0:1 [1542208721105] POST /content/dam.createasset.html HTTP/1.1] com.day.cq.dam.core.impl.servlet.DamContentDispositionFilter checking for acceptance

14.11.2018 20:48:41.114 *DEBUG* [0:0:0:0:0:0:0:1 [1542208721105] POST /content/dam.createasset.html HTTP/1.1] com.day.cq.dam.core.impl.servlet.CreateAssetServlet Processing a non-streaming request

But when I comment third iterator (which is file parameter) then it is going as Processing a streaming request to CreateAssetServlet.

Thanks,

Swati

Avatar

Level 10

We will post new 6.4 article soon on uploading assets and handling them in a 6.4 servlet. A video will be part of that too.