2

I'm doing a POST to a restlet and need to return a zip file. But although the created file is zip, the method returns gibberish.

I tried wrapping the FileRepresentation as was suggested here:

new org.restlet.engine.application.EncodeRepresentation(org.restlet.data.Encoding.ZIP, representation);

And also tried adding a Produces annotation like this:

@Produces({"application/x-zip-compressed"})

But neither works. The representation returns as gibberish string, and the Content-Type header stays application/octet-stream. What am I missing?

These are the request headers. Note the Accept-Encoding: gzip, deflate:

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json 
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,he;q=0.6

The response headers:

Vary: Accept-Encoding
Last-Modified: Wed, 06 May 2015 14:49:03 GMT 
Content-Disposition: attachment; filename=_backup_20150506.zip; size=162191 
Date: Wed, 06 May 2015 14:49:03 GMT 
Accept-Ranges: bytes 
Server: Restlet-Framework/2.2.1 
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept 
Set-Cookie: JSESSIONID=5F10BBBDC58D5C3D6C0474FA12C44FB9; Path=/AppName/; Domain=localhost 
Content-Encoding: gzip 
Content-Type: application/octet-stream 
Transfer-Encoding: chunked 

EDIT: I also tried changing the media type when creating the representation:

MediaType mt = MediaType.APPLICATION_ZIP;
FileRepresentation fr = new FileRepresentation(file, mt);

The response content type changed to Content-Type: application/zip but the returned value is still a gibberish string.

Community
  • 1
  • 1
Eddy
  • 3,533
  • 13
  • 59
  • 89

1 Answers1

2

The right way to do that is what you used:

public class MyServerResource extends ServerResource {
    @Post
    public Representation test(Representation repr) {
        FileRepresentation outputRepresentation
             = new FileRepresentation(new File("(...)"),
                             MediaType.APPLICATION_ZIP);
        return outputRepresentation;
    }
}

So this should work.

Using curl with such code, here is what I have:

$ curl -X POST http://localhost:8182/test > mycontent.zip
$ unzip mycontent.zip 
Archive:  mycontent.zip
 extracting: test.txt

In addition, here is what I have with the verbose mode of curl:

curl -X POST --verbose http://localhost:8182/test
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8182 (#0)
> POST /test HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8182
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-type: application/zip
< Last-modified: Thu, 07 May 2015 08:08:59 GMT
< Content-length: 134
* Server Restlet-Framework/2.3.1 is not blacklisted
< Server: Restlet-Framework/2.3.1
< Accept-ranges: bytes
< Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
< Date: Thu, 07 May 2015 08:19:26 GMT
< 

Notice that you can use the header Disposition if you want to configure hints within the download dialog of your browser.

Otherwise, "enable GZIP compression of the JSON response entity on Resltet" corresponds to automatic compression of the whole response content by Restlet. Browsers support this and can directly uncompress the content before displaying it. I don't think that isn't really what you need / expect. If it's the case, you could be interested in this link: https://templth.wordpress.com/2015/02/23/optimizing-restlet-server-applications/.

Hope it helps you, Thierry

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • it doesn't work. Seems like the problem may be related to the content-type which is sent automatically. Not sure. – Eddy May 08 '15 at 11:09
  • Really strange! Would be interested in m'y test project? FYI I used standard Restlet application, not a JAX-RS Restlet one... If you use JAX-RS, I could maker a try with this technology. – Thierry Templier May 08 '15 at 11:20