Receiving a client-side multicast response (ClosableHttpResponse)

I have a java controller that needs to send me some text data and different byte arrays. So I create n multipart request and stream it from HttpServletResponse.

Now my problem is how to parse the response on the client side and extract multiple parts.

SNIPPET SERVICE CODE: -

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        // Prepare payload
        builder.addBinaryBody("document1", file);
        builder.addBinaryBody("document2", file2);
        builder.addPart("stringData", new StringBody(jsonData, ContentType.TEXT_PLAIN));

        // Set to request body

        HttpEntity entity = builder.build();
        postRequest.setEntity(entity);

      

CLIENT CODE SNIPET: -

        HttpPost httpPost = new HttpPost(finalUrl);

        StringEntity entity = new StringEntity(json);
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-type", APPLICATION_JSON_TYPE);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        InputStream in = new BufferedInputStream(response.getEntity().getContent());

      

I checked CloseableHttpResponse and HttpEntity, but neither provide a method to parse a multiple request.

EDIT 1: This is my example response that I get on the client side: -

--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe
Content-Disposition: form-data; name="numeric"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
01010110
--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe
Content-Disposition: form-data; name="stringmessage"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding:8bit
testmessage
--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe
Content-Disposition: form-data; name="binarydata"; filename="file1"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
HI, THIS IS MY BINARY DATA
--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe
Content-Disposition: form-data; name="ending"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
ending
--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe--

      

+4


source to share


2 answers


I finally got a workaround.

I will be using javax mail MimeMultipart.

Below is the code for the solution: -



    ByteArrayDataSource datasource = new ByteArrayDataSource(in, "multipart/form-data");
    MimeMultipart multipart = new MimeMultipart(datasource);

    int count = multipart.getCount();
    log.debug("count " + count);
    for (int i = 0; i < count; i++) {
        BodyPart bodyPart = multipart.getBodyPart(i);
        if (bodyPart.isMimeType("text/plain")) {
            log.info("text/plain " + bodyPart.getContentType());
            processTextData(bodyPart.getContent());
        } else if (bodyPart.isMimeType("application/octet-stream")) {
            log.info("application/octet-stream " + bodyPart.getContentType());
            processBinaryData(bodyPart.getInputStream()));
        } else {
            log.warn("default " + bodyPart.getContentType());
        }
    }

      

Please let me know if anyone has a standard solution.

+8


source


Mime4j from Apache is one way to parse responses from the client side. It is common practice to use such a tool.

You can refer to this link - http://www.programcreek.com/java-api-examples/index.php?api=org.apache.james.mime4j.MimeException



You can download the jar from this link - http://james.apache.org/download.cgi#Apache_Mime4J

Hope this helps. Greetings

0


source







All Articles