2

Related to this question which is about how to send a binary file to a client. I am doing this, actually my method @Produces("application/zip"), and it works well for the browser client. Now I'm trying to write some automated tests against the rest service, using the Wink client. So my question is not how to send the file to the client, but for how to consume the file, as a java rest client (in this case Apache Wink).

My resource method looks something like the below... Once I have a Wink ClientResponse object, how can I get the file from it so I can work with it?

  @GET
  @Path("/file")
  @Produces("application/zip")
  public javax.ws.rs.core.Response getFile() {

    filesToZip.put("file.txt", myText);

    ResponseBuilder responseBuilder = null;
    javax.ws.rs.core.Response response = null;
    InputStream in = null;
    try {

      in = new FileInputStream( createZipFile( filesToZip ) );
      responseBuilder = javax.ws.rs.core.Response.ok(in, MediaType.APPLICATION_OCTET_STREAM_TYPE);
      response = responseBuilder.header("content-disposition", "inline;filename="file.zip").build();

    } catch( FileNotFoundException fnfe) {
          fnfe.printStackTrace();
    }

    return response;

The method that actually creates the zip file looks like this

  private String createZipFile( Map<String,String> zipFiles ) {

    ZipOutputStream zos = null;
    File file = null;

    String createdFileCanonicalPath = null;

    try {

      // create a temp file -- the ZIP Container
      file = File.createTempFile("files", ".zip");
      zos = new ZipOutputStream( new FileOutputStream(file));

      // for each entry in the Map, create an inner zip entry

      for (Iterator<Map.Entry<String, String>> it = zipFiles.entrySet().iterator(); it.hasNext();){

        Map.Entry<String, String> entry = it.next();
        String innerFileName = entry.getKey();
        String textContent = entry.getValue();

        zos.putNextEntry( new ZipEntry(innerFileName) );
        StringBuilder sb = new StringBuilder();
        byte[] contentInBytes = sb.append(textContent).toString().getBytes();
        zos.write(contentInBytes, 0, contentInBytes.length);
        zos.closeEntry();        
      }

      zos.flush();
      zos.close();

      createdFileCanonicalPath = file.getCanonicalPath(); 

    } catch (SecurityException se) {
      se.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (zos != null) {
          zos.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return createdFileCanonicalPath;

  }
Community
  • 1
  • 1
rogodeter
  • 460
  • 9
  • 19

1 Answers1

1

You can consume it simply as input stream and use ZipInputStream to unzip it.

Here's example using Apache HTTP Client:

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);
    get.addHeader(new BasicHeader("Accept", "application/zip"));
    HttpResponse response = httpclient.execute(get);
    InputStream is = response.getEntity().getContent();
    ZipInputStream zip = new ZipInputStream(is);
Tarlog
  • 10,024
  • 2
  • 43
  • 67
  • Thanks @Tarlog, that worked for me. I thought I'd need to extend the InputStreamAdapter but it worked as you described. Only difference is I am using the Apache Wink client so it looks more like.. > InputStream is = resp.getEntity(new EntityType(){}); > ZipInputStream zos = new ZipInputStream(is); ..or.. > InputStream is = resp.getEntity(InputStream.class); > ZipInputStream zos = new ZipInputStream(is); where resp is an Apache Wink ClientResponse object – rogodeter Mar 14 '13 at 16:27