Last active
August 29, 2015 14:25
-
-
Save joecwu/5a7ed0cc1ee4daa9d0c5 to your computer and use it in GitHub Desktop.
Blog Scala Dispatch handle gzip json http://blog.joecwu.com/2015/07/scala-dispatch-gzip-json.html
This file contains hidden or 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
import java.io.{FileOutputStream, ByteArrayOutputStream, ByteArrayInputStream} | |
import java.util.zip.GZIPInputStream | |
import java.util.zip.GZIPOutputStream | |
import java.io._ | |
object GzipHelper { | |
implicit def StringGzipConvertor(str:String) = new { | |
def toGzipBytes(charset:String="UTF-8") = compress(str,charset) | |
def toGzipBytesString(charset:String="UTF-8") = new String(compress(str,charset).map(_.toChar)) | |
def toUngzipString(charset:String="UTF-8") = uncompress(str.toCharArray.map(_.toByte),charset) | |
} | |
def compress(data : String, charset:String = "UTF-8"): Array[Byte] = { | |
compressBytes(data.getBytes(charset)) | |
} | |
def compressBytes(dataBytes: Array[Byte]): Array[Byte] = { | |
val baos = new ByteArrayOutputStream | |
val gzos = new GZIPOutputStream(baos) | |
gzos.write(dataBytes) | |
gzos.finish | |
gzos.close | |
baos.close | |
baos.toByteArray | |
} | |
def uncompress(data : Array[Byte], charset:String = "UTF-8") : String = { | |
val bais = new ByteArrayInputStream(data) | |
val gzis = new GZIPInputStream(bais) | |
val baos = new ByteArrayOutputStream | |
val buf = new Array[Byte](8192) | |
var read = gzis.read(buf) | |
while (read > 0) { | |
baos.write(buf, 0, read) | |
read = gzis.read(buf) | |
} | |
gzis.close() | |
baos.close() | |
baos.toString(charset) | |
} | |
} |
This file contains hidden or 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
val payload = "Payload" | |
val svc = url("http://blog.joecwu.com").addHeader("Accept-Encoding","gzip").setBody(payload.toGzipBytes()) | |
val resp = Http(svc OK GzipJson) |
This file contains hidden or 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
val svc = url("http://blog.joecwu.com").addHeader("Accept-Encoding","gzip") | |
val resp = Http(svc OK GzipJson) |
This file contains hidden or 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
import java.util.zip.GZIPInputStream | |
import com.ning.http.client.Response | |
import org.json4s._ | |
import org.json4s.native.JsonMethods._ | |
object GzipJson extends (Response => JValue) { | |
def apply(r: Response) = { | |
if(r.getHeader("content-encoding")!=null && r.getHeader("content-encoding").equals("gzip")){ | |
(parse(new GZIPInputStream(r.getResponseBodyAsStream), true)) | |
}else | |
(dispatch.as.String andThen (s => parse(StringInput(s), true)))(r) | |
} | |
} |
This file contains hidden or 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
import dispatch._, Defaults._ | |
val svc = url("http://blog.joecwu.com") | |
val resp = Http(svc OK as.String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment