Created
October 26, 2011 20:02
-
-
Save engintekin/1317626 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; | |
} | |
} |
Good stuff!
It is definitely a much needed addition to Play, which surprisingly does not come with built in support for compression output.
Agree with @knaak that accept request must be checked. But that easily done. Also, we use a way to trigger gzip on or off on server side. This would help during development, if you want to see output on console.
Why doesn't this work with Play 2.1.1? Is this for a different version?
@mwhawkins it seems to be in response to this v1-related question: http://stackoverflow.com/questions/10778127/how-to-enable-gzip-compression-on-play-framework-1-app-on-heroku-cedar
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very useful, thank you.
You should check that the request accepts gzip though