2012-10-28 22 views
5

में प्रतिक्रिया निकाय GZIP मैं Playframework 2.x एप्लिकेशन पर काम कर रहा हूं। मेरे एप्लिकेशन में नियंत्रक जेएसओएन प्रतिक्रिया ब्राउज़र/एंडपॉइंट पर वापस लौटते हैं। मैं जानना चाहता था कि प्रतिक्रिया निकायों के जीजेआईपी संपीड़न को सक्षम करने का एक आसान तरीका है या नहीं।PlayFramework 2.0

+1

प्ले एक सामने पीछे रहती हैं अंत (apache, nginx, आदि) तो वहां, सरल, प्रत्यक्ष, और प्रभावी – virtualeyes

+0

ऐसा करें यदि आप इसे एक उत्तर के रूप में रखते हैं तो मैं इसे स्वीकार करूंगा :) – rOrlig

+0

निश्चित रूप से, मैं अपना दृष्टिकोण, सेक शामिल करूंगा – virtualeyes

उत्तर

2

gzip'ing काफी एक अपाचे सामने अंत के साथ पूरा केक है।

अपाचे 2.4 gzip Location ब्लॉक के माध्यम से निपटने सामग्री प्रकार की एक बुनियादी सेट का उपयोग कैसा लग सकता है पर:

<Location /> 
    ... 
    AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon 
    SetOutputFilter DEFLATE 
</Location> 
3

वर्तमान में खेल 2.0.4 में गैर संपत्तियों के लिए कोई आसान तरीका नहीं है।

जावा एपीआई के लिए आप इस्तेमाल कर सकते हैं:

public static Result actionWithGzippedJsonResult() throws IOException { 
    Map<String, String> map = new HashMap<String, String>(); 
    map.put("hello", "world"); 
    final String json = Json.toJson(map).toString(); 
    return gzippedOk(json).as("application/json"); 
} 

/** Creates a response with a gzipped string. Does NOT change the content-type. */ 
public static Results.Status gzippedOk(final String body) throws IOException { 
    final ByteArrayOutputStream gzip = gzip(body); 
    response().setHeader("Content-Encoding", "gzip"); 
    response().setHeader("Content-Length", gzip.size() + ""); 
    return ok(gzip.toByteArray()); 
} 

//solution from James Ward for Play 1 and every request: https://gist.github.com/1317626 
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; 
} 
1

Play framework 2.2+ में यह GzipFilter उपयोग करना संभव है। एसबीटी के माध्यम से उपलब्ध:

libraryDependencies ++= filters 

आप एक स्केला पुरुष हैं, यह Gzip class को देख के लायक है।

0
प्ले 2.5 के साथ

, के रूप में उल्लेख here:

यहाँ gzip फ़िल्टर शामिल करने के लिए (एकाधिक फिल्टर जोड़ने का प्रदर्शन करने के लिए एक नमूना CORS फिल्टर के साथ) एक नमूना कोड है:

import javax.inject.Inject; 

import play.api.mvc.EssentialFilter; 
import play.filters.cors.CORSFilter; 
import play.filters.gzip.GzipFilter; 
import play.http.HttpFilters; 


public class Filters implements HttpFilters { 

    @Inject 
    CORSFilter corsFilter; 

    @Inject 
    GzipFilter gzipFilter; 

    @Override 
    public EssentialFilter[] filters() { 
     return new EssentialFilter[] { corsFilter, gzipFilter }; 
    } 

}