-
-
Save dreampowder/0a91bd3829e0bfa2341c259d38e95c8d to your computer and use it in GitHub Desktop.
Gzip your response in Play framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@With(Compress.class) | |
public class Application extends Controller { | |
public static void index() { | |
render();//response will be gzipped | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import play.*; | |
import play.mvc.*; | |
import java.io.*; | |
import java.util.*; | |
import java.util.zip.GZIPOutputStream; | |
public class Compress extends Controller { | |
@Finally | |
public static void compress() throws IOException { | |
String text = response.out.toString(); | |
// if ("text/xml".equals(response.contentType)) { | |
// text = new com.googlecode.htmlcompressor.compressor.XmlCompressor().compress(text); | |
// } else if ("text/html; charset=utf-8".equals(response.contentType)) { | |
// text = new com.googlecode.htmlcompressor.compressor.HtmlCompressor().compress(text); | |
// } | |
final ByteArrayOutputStream gzip = gzip(text); | |
response.setHeader("Content-Encoding", "gzip"); | |
response.setHeader("Content-Length", gzip.size() + ""); | |
response.out = gzip; | |
} | |
public static ByteArrayOutputStream gzip(final String input) | |
throws IOException { | |
final InputStream inputStream = new ByteArrayInputStream(input.getBytes()); | |
final ByteArrayOutputStream stringOutputStream = new ByteArrayOutputStream((int) (input.length() * 0.75)); | |
final OutputStream gzipOutputStream = new GZIPOutputStream(stringOutputStream); | |
final byte[] buf = new byte[5000]; | |
int len; | |
while ((len = inputStream.read(buf)) > 0) { | |
gzipOutputStream.write(buf, 0, len); | |
} | |
inputStream.close(); | |
gzipOutputStream.close(); | |
return stringOutputStream; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment