Created
October 23, 2013 07:53
-
-
Save gnufied/7114310 to your computer and use it in GitHub Desktop.
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
package eventfax.protocol | |
import scala.collection.jcl.TreeMap | |
import java.util.Date | |
import java.text.SimpleDateFormat | |
import java.text.DateFormat | |
import scala.collection.jcl.ArrayList | |
import scala.collection.mutable.Map | |
class HttpResponse(val pristine: Boolean) { | |
def this() = this(false) | |
val statusMap = Map( | |
200 -> "OK", | |
201 -> "Created", | |
202 -> "Accepted", | |
204 -> "No Content", | |
205 -> "Reset Content", | |
206 -> "Partial Content", | |
301 -> "Moved Permanently", | |
302 -> "Found", | |
304 -> "Not Modified", | |
400 -> "Bad Request", | |
401 -> "Unauthorized", | |
402 -> "Payment Required", | |
403 -> "Forbidden", | |
404 -> "Not Found", | |
411 -> "Length Required", | |
500 -> "Internal Server Error", | |
501 -> "Not Implemented", | |
502 -> "Bad Gateway", | |
503 -> "Service Unavailable", | |
504 -> "Gateway Timeout" | |
) | |
var header = Map[String,String]() | |
if(!pristine) { | |
header("Server") = "PushServer/1.1" | |
val f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z") | |
header("Date") = f.format(new Date()) | |
} | |
var body: Option[String] = None | |
var status: Option[Int] = Some(200) | |
def apply(key: String,value: String) = { | |
header(key) = value | |
} | |
/** | |
* Return chunked version of input string | |
*/ | |
def chunked(data: String) = { | |
var responseList = List[String](data.length.toHexString,data) | |
val finalResponse = responseList.mkString("\r\n") + "\r\n" | |
finalResponse | |
} | |
/** | |
* Send multipart responses | |
*/ | |
def multipart(data: String,separator: String) = { | |
val finalResponse = new ArrayList[String]() | |
finalResponse.add("--" + separator) | |
header("Content-Length") = data.length.toString | |
header.get("Content-Type") foreach { (x: String) => finalResponse.add("Content-Type: " + x)} | |
header.get("Content-Length") foreach { (x: String) => finalResponse.add("Content-Length: " + x)} | |
val finalString = finalResponse.mkString("\r\n") + "\r\n\r\n" + data + "\r\n" | |
finalString | |
} | |
def stopChunk() = { | |
"0\r\n" | |
} | |
def outData = { | |
val finalResponse = new ArrayList[String]() | |
finalResponse.add("HTTP/1.1 " + status.get + " " + statusMap(status.get)) | |
if(header.get("Transfer-Encoding").isEmpty && !body.isEmpty) | |
header("Content-Length") = body.get.length.toString | |
// add the general headers first | |
header.get("Cache-Control").foreach {(x: String) => finalResponse.add("Cache-Control: " + x)} | |
header.get("Connection").foreach {(x: String) => finalResponse.add("Connection: " + x )} | |
header.get("Date").foreach {(x: String) => finalResponse.add("Date: " + x)} | |
header.get("Pragma").foreach {(x: String) => finalResponse.add("Pragma: " + x)} | |
header.get("Trailer").foreach {(x: String) => finalResponse.add("Trailer: " + x)} | |
header.get("Transfer-Encoding").foreach {(x: String) => finalResponse.add("Transfer-Encoding: " + x)} | |
// response headers | |
header.get("Accept-Ranges").foreach {(x: String) => finalResponse.add("Accept-Ranges: " + x)} | |
header.get("Age").foreach {(x: String) => finalResponse.add("Age: " + x)} | |
header.get("ETag").foreach {(x: String) => finalResponse.add("ETag: " + x)} | |
header.get("Server").foreach {(x: String) => finalResponse.add("Server: " + x)} | |
header.get("Location").foreach {(x: String) => finalResponse.add("Location: " + x)} | |
//entity header | |
header.get("Allow") foreach {(x: String) => finalResponse.add("Allow: " + x)} | |
header.get("Content-Encoding") foreach {(x: String) => finalResponse.add("Content-Encoding: " + x)} | |
header.get("Content-Length") foreach {(x: String) => finalResponse.add("Content-Length: " + x)} | |
header.get("Content-Language") foreach {(x: String) => finalResponse.add("Content-Language: " + x)} | |
header.get("Content-Location") foreach {(x: String) => finalResponse.add("Content-Location: " + x)} | |
header.get("Content-MD5") foreach {(x: String) => finalResponse.add("Content-MD5: " + x)} | |
header.get("Content-Range") foreach {(x: String) => finalResponse.add("Content-Range: " + x)} | |
header.get("Content-Type") foreach {(x: String) => finalResponse.add("Content-Type: " + x)} | |
header.get("Expires") foreach {(x: String) => finalResponse.add("Expires: " + x)} | |
header.get("Last-Modified") foreach {(x: String) => finalResponse.add("Last-Modified: " + x )} | |
header.get("extension-header") foreach {(x: String) => finalResponse.add("extension-header: " + x)} | |
val finalString = finalResponse.mkString("\r\n") + "\r\n\r\n" + body.getOrElse("") | |
finalString | |
} | |
override def toString = { | |
var finalResponse = List[String]() | |
finalResponse = ("HTTP/1.1 " + status.get + " " + statusMap(status.get)) :: finalResponse | |
if(header.get("Transfer-Encoding").isEmpty) | |
header("Content-Length") = body.get.length.toString | |
header.foreach(keyValue => { | |
finalResponse = (keyValue._1 + ": " + keyValue._2) :: finalResponse | |
}) | |
val finalString = finalResponse.reverse.mkString("\r\n") + "\r\n\r\n" + body.getOrElse("") | |
finalString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment