Streaming over HTTP PUT with JAX-RS

I have a workflow that involves doing an HTTP POST from my Java client to my web server. The message body has a specification object. Then I stream this from my web server to Apache ZooKeeper (which runs in its own process on the server), which starts a big hairy count. I am struggling to figure out how to stream bytes to my web server. I need it to bounce back because I have an HTTP GET request on my web server from my Java client that is waiting to stream back to bytes. I can't wait for the whole computation to complete, I want the bytes to be sent back to the client as soon as possible.

Most of the online examples for JAX-RS that do HTTP PUT on the client and server side do not have examples for streaming code. I'll post what I have, but it doesn't work.

Here is my ZooKeeper Java code that calls JAX-RS on the PUT client side. I'm really not sure how to do this, I have never tried passing data using JAX-RS.

final Client client = ClientBuilder.newClient();
final WebTarget createImageTarget = client.target("groups/{imageGroupUuid:" + Regex.UUID + "}");
StreamingOutput imageResponse = createImageTarget.request(MediaType.APPLICATION_OCTET_STREAM).put(Entity.entity(createRandomImageDataBytes(imageConfigurationObject), MediaType.APPLICATION_OCTET_STREAM), StreamingOutput.class);

      

Here is my webserver code that handles HTTP PUT. This is just a stub because I am not sure about the client side HTTP PUT.

@PUT
@PATH("groups/{uuid:" + Regex.UUID + "}")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public void updateData(StreamingOutput streamingOutput)
{

}

      

+3


source to share


1 answer


Try something like this:



@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/{arg}")
public Response get(@PathParam("arg") {

    //get your data based on "arg"

    StreamingOutput stream = new StreamingOutput() {
        @Override
        public void write(OutputStream os) throws IOException, WebApplicationException {
            Writer writer = new BufferedWriter(new OutputStreamWriter(os));

            for (org.neo4j.graphdb.Path path : paths) {
                writer.write(path.toString() + "\n");
            }
            writer.flush();
        }
    };

    return Response.ok(stream).build();
}

@PUT
@Consumes("application/octet-stream")
public Response putFile(@Context HttpServletRequest request,
                     @PathParam("fileId") long fileId,
                     InputStream fileInputStream) throws Throwable {
    // Do something with the fileInputStream
    // etc
}

      

+3


source







All Articles