Link to home
Start Free TrialLog in
Avatar of Manikandan Thiagarajan
Manikandan ThiagarajanFlag for India

asked on

I want to Zip the Web Service Response

I want to Zip the Web Service Response

how could i add it below the code

@POST
    @Path("fetchProject")
      @Consumes("application/json")
      ProjectsVO fetchProject(String jsonStr);
Avatar of Valeri
Valeri
Flag of Bulgaria image

Avatar of Manikandan Thiagarajan

ASKER

@POST
    @Path("fetchProject")
      @Consumes("application/json")
      ProjectsVO fetchProject(String jsonStr);
No, Here i want to add @gzip what is the syntax tell me
"Concerning GZIP encoding, each JAX-RS provider has different approaches"
Jersey provides a filter to accomplish the encoding transparently:
http://jersey.java.net/nonav/apidocs/latest/jersey/index.html

RESTEasy provides an annotation for that:
http://docs.jboss.org/resteasy/2.0.0.GA/userguide/html/gzip.html
conn.setRequestMethod("GET");

                  conn.setReadTimeout(600 * 1000);
                  conn.connect();
                  long end2 = System.currentTimeMillis();
                  System.out.println("time taken connection : " + (end2 - end1) + " ms");
                  if (conn.getResponseCode() != 200) {
                        throw new RuntimeException("Failed : HTTP error code : "+ conn.getResponseCode());
                  }
                  is = conn.getInputStream();
                  isr = new InputStreamReader(is);
                  br = new BufferedReader(isr);
                  stringBuilder = new StringBuilder();

                  String line = null;
                  long startRead = System.currentTimeMillis();
                  System.out.println("time taken  ========: " + (startRead-end2) + " ms");
                  while ((line = br.readLine()) != null) {
                        stringBuilder.append(line + "\n");
                  }
could you give me the java code for unzip from webservice response from the above line
I don't have my own code. I've never done this. found this on i-net :

// The response is gzip encoded, so decompress the response.
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.writeTo(out);
        byte[] barr = out.toByteArray();
        InputStream gzipStream = new GZIPInputStream(new ByteArrayInputStream(barr));
        Reader decoder = new InputStreamReader(gzipStream, "UTF-8");
        BufferedReader buffered = new BufferedReader(decoder);
        int n = 0;
        char[] cbuf = new char[1024];
        Writer w = new StringWriter();
        while ((n = buffered.read(cbuf)) != -1) {
            w.write(cbuf,0,n);
        }
        // the writer now contains unzipped message.
        System.out.println(w.toString());
could you please apply with my code
ASKER CERTIFIED SOLUTION
Avatar of Valeri
Valeri
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial