0

I posted questions about this topic twice (here and herehere but never got a satisfying answer, it felt like I could not articulate the question well enough. I have since figured it out but my understanding of it is weak. It was chance that led me to this solution. What I am doing it is generating results on a seperate process spawned by my webserver. As those results are generated I want to use Jersey to stream the bytes to the webserver and eventually that will get passed back to my client (at a later time). I had the hardest time figuring out how to make my data stream from my process back to my webserver. I guessed that using a StreamingOuput type for my Entity would work. There are not examples on the web of this, can anyone explain what is going on in Jersey land. I was stunned that I was able to read the data out on the webserver as an InputStream.

Running in my servlet container on the server

@PUT
@Path("groups/{myUuid:" + Regex.UUID + "}")
public void updateSpectraData(@PathParam("myUuid") UUID myUuid, InputStream inputStream)
{
   float[] floatHolder = new float[512];
   byte[] bytes = ByteStreams.toByteArray(inputStream);
   ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
   byteBuffer.asFloatBuffer().get(floatHolder);
   System.out.println(Arrays.toString(floatHolder));
}

The code below is running in a separate process spawned by the server.

private void doPut(byte[] data, Object ctx)
{
   final Client client = ClientBuilder.newClient();
   ObjectMapper objectMapper = new ObjectMapper();
   MyTask myTask = objectMapper.readValue(data, MyTask.class);
   MyConifguration config = getConfig();
   WebTarget createTaskTarget = client.target(myTask.getURI());
   Builder request = createTaskTarget.request(MediaType.APPLICATION_OCTET_STREAM);
   request.put(Entity.entity(createData(config), MediaType.APPLICATION_OCTET_STREAM), InputStream.class);
}

Here is the function which creates the data, the return type is what is important:

private StreamingOutput createData(MyConfiguration config)
{
   return new StreamingOutput()
   {
    ...code...
   }
}
Community
  • 1
  • 1
smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113

1 Answers1

1

Jersey will try to provide you with whatever you provide as the input argument based on the content-type of the request. You could also probably use a byte[] instead of InputStream. If the content-type was application/octet-stream and your input argument was a serializable object, then Jersey would assume that the incoming data uses Java serialization, same goes for application/xml (it will use JAXB). If you want to make this more explicit you can put @Consumes and @Produces on your servlet method.

rolfl
  • 17,539
  • 7
  • 42
  • 76
LINEMAN78
  • 2,562
  • 16
  • 19