Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning ZipInputStream as a Jax-RS Response object

I am trying to return a ZipInputStream containing two different outputstreams as a javax.ws.rs.core.Response Stream. When I make a web service call to retrieve the stream, I notice that I get an empty stream back. I have tried returning a GZipInputStream before, and I have received the expected stream on the client side. Could there be an issue with ZipInputStream that prevents it from being returned properly? I am using javax 2.4 (servlet-api) This is how my jax-rs service looks like (I have simplified it a bit):

 @GET
 @Produces({"application/zip", MediaType.APPLICATION_XML})
 public Response getZipFiles(@PathParam("id") final Integer id){

    //Get required resources here
    ByteArrayOutputStream bundledStream = new ByteArrayOutputStream();
    ZipOutputStream out = new ZipOutputStream(bundledStream);
    out.putNextEntry(new ZipEntry("Item A"));
    out.write(outputStream.toByteArray());
    out.closeEntry();

    out.putNextEntry(new ZipEntry("Item B"));
    out.write(defectiveBillOutputStream.toByteArray());
    out.closeEntry();

    out.close();
    bundledStream.close();

    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bundledStream.toByteArray()));
    return Response.ok(zis).build();
 }

And this is the code that calls the service. I am using axis 1.4:

 HttpMethodBase getBillGroup = null;
 String id = "1234";
 String absoluteUrl = baseURL + BASE_SERVICE_PATH.replace("@id@",id) ;
 getZip = new GetMethod(absoluteUrl);

 HttpClient httpClient =  new HttpClient();
 try {
      httpClient.executeMethod(getZip);
 }
 catch (Exception e) {
      LOGGER.error("Error during retrieval " + e.getMessage());

 }

 InputStream dataToConvert =  getZip.getResponseBodyAsStream();
 ZipInputStream in = new ZipInputStream(dataToConvert);
 ZipEntry itemA = in.getNextEntry();
 //Do more things

On the last line, itemA should have been the first entry added to the stream in the Jax-RS Service, but I am getting a null back. Any idea what might be causing this?

like image 801
theseeker Avatar asked Nov 13 '22 12:11

theseeker


1 Answers

In the first block use a ByteArrayInputStream instead of a ZipInputStream, which iterates complex zip entries.

like image 172
Joop Eggen Avatar answered Nov 16 '22 04:11

Joop Eggen