restpostwso2msf4jwso2-msf4j

MSF4J POST method receiving partial data


I'm new to MSF4J and I need to write a REST API that accepts a large XML data through POST. I am using request.getMessegeBody() method to get the data. I discovered that it's now deprecated but I couldn't find the newer version of it so I decided to use it anyway.

The problem is, when I send data to the microservice for the first time, it doesn't get the whole data. All the subsequent requests will get the full message body except the first.

When I try passing the request through ESB, ESB receives the whole body but when it reaches the endpoint it will be truncated.

I have also tried sending requests from different rest clients but for the first time it always gets the incomplete message body

@POST
@Consumes({ "application/xml", "application/json", "text/xml" })
@Path("test/")
public Response getReqNotification(@Context Request request) throws Exception {
    Response.ResponseBuilder respBuilder =         
    Response.status(Response.Status.OK).entity(request);
    ByteBuf b = request.getMessageBody();
    byte[] bb = new byte[b.readableBytes()];
    b.duplicate().readBytes(bb);
    System.out.println(new String(bb));

    return respBuilder.build();
}

I expect it to print the full message(which is about 2000 bytes long) every time when I send a request, but I'm only getting around 800 bytes when I first run the microservice.

I hope ill get assistance here. I have tried elsewhere but wso2 doesn't have much documentation (⌣_⌣”)


Solution

  • I still don't really understand what I was doing wrong but with the help of this link I have managed to come up with the following code and it works fine.

    The major cha is that I now use request.getMessageContentStream() instead of the depricated request.getMessageBody()

    @Consumes({ "application/xml", "application/json", "text/xml" })
    @Path("test/")
    public Response getReqNotification(@Context Request request) throws Exception {
            Response.ResponseBuilder respBuilder =         
            Response.status(Response.Status.OK).entity(request);
            String data = "";
    
            BufferedInputStream bis = new BufferedInputStream(request.getMessageContentStream());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                int d;
                while ((d = bis.read()) != -1) {
                    bos.write(d);
                }
                data = bos.toString();
            } catch (IOException ex) {
                    ex.printStackTrace();
            } finally {
                try {
                    bos.close();
                } catch (IOException e) {
                }
            }
            System.out.println(data);
    //////do stuff
    
            return respBuilder.build();
    }