0

How do I get a handle on data being submitted via a REST request so I can safely process part of it using StAX.

I have a class called ProcessHustleRequest which is generated by JAXB. However it currently contains an element called "attachmentBinary" with BinaryData that is too big to load into memory so I wish to drop down into StAX in order to avoid pulling the entire document into memory.

My XML looks like this:

<?xml version="1.0" encoding="utf-8"?> <ProcessRequest> <DeliveryDate>2015-12-13</DeliveryDate>  <AttachmentBinary>iVBORw0KGgoAAAANSUhEUgAAAFoA</AttachmentBinary> <AttachmentBinary>iVBORw0KGgoAAAANSUhEUgAAAFoA</AttachmentBinary> </ProcessRequest>

My server side Jersey resource is defined as follows:

 @POST
 @Path("/hustle")
 @Consumes(MediaType.TEXT_XML)
 @Produces(MediaType.TEXT_XML)
 public Response consumeXMLtoHustle(ProcessHustleRequest processHustle) {

  // I currently pull in attachmentBinary as a String or some other object and //run out of memory.

  try {
   String tempImage = "E:\\temp\\attachment.bin";
   Base64.decodeToFile(processHustle.getAttachmentBinary(), tempImage);
   processTicket.setHasImage(true);
   processTicket.setImageFileName(tempImage);
  } catch (Exception e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }


  return Response.ok().entity(processHustleResponse).build();

 }

I've seen an example here that might be relevant: JAXB - unmarshal OutOfMemory: Java Heap Space

but I don't know how to get a handle on the XML file being sent to me. Presumable if I could, then I should just remove JAXB references to attachmentBinary and just pull in that element only using STaX.

My suspicion is that I might also be able to implement something based on this answer How to avoid OutOfMemoryError when uploading a large file using Jersey client

 @POST
    @Path("/hustle/{attachmentName}")
    @Consumes(MediaType.APPLICATION_OCTET_STREAM)
    public void uploadAttachment(@PathParam("attachmentName") String attachmentName, InputStream attachmentInputStream) {
        // do something with the input stream
    }
Community
  • 1
  • 1
Toby Derrum
  • 299
  • 1
  • 3
  • 22

0 Answers0