Skip to content

Instantly share code, notes, and snippets.

@bryanjswift
Created June 11, 2011 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanjswift/1020263 to your computer and use it in GitHub Desktop.
Save bryanjswift/1020263 to your computer and use it in GitHub Desktop.
Basic Servlet trait and helper objects to make HttpServletRequest and HttpServletResponse interactions easier
package com.example
import javax.servlet.http.{Cookie, HttpServlet, HttpServletRequest => Request, HttpServletResponse => Response}
import scala.util.matching.Regex
trait Servlet extends HttpServlet {
override def doGet(request:Request, response:Response) = doGet(new HttpHelper(request, response))
override def doPost(request:Request, response:Response) = doPost(new HttpHelper(request, response))
def doGet(http:HttpHelper) { }
def doPost(http:HttpHelper) { }
val config = new Configuration("config.json")
class HttpHelper(val request: Request, val response: Response) {
// pretty impossible to not match this RE
private val uriMatch = Servlet.uriRE.findFirstMatchIn(request.getRequestURI).get
val uri = uriMatch.group("uri")
val format = uriMatch.group("format") match {
case null => "html"
case s:String => s
case _ => "html"
}
val parts = uri.split("/").filter(_.length > 0)
def apply(param:String, default:String = "") = {
val value = request.getParameter(param)
if (value == null || value == "" || value == default) None else Some(value)
}
def cookie(key: String): Option[String] = {
val cookies = request.getCookies
if (cookies == null) {
None
} else {
cookies.find(c => c.getName == key).map(c => c.getValue)
}
}
def cookies = new Cookies(request, response)
def header(name: String): Option[String] = {
val headerNames = request.getHeaderNames.asInstanceOf[java.util.Enumeration[String]]
if (headerNames == null) {
None
} else {
headerNames.find(h => h == name).map(h => request.getHeader(h))
}
}
def header(name: String, value: String): Unit = {
response.setHeader(name, value)
}
}
private class RichEnumeration[T](enumeration: java.util.Enumeration[T]) extends Iterator[T] {
def hasNext: Boolean = enumeration.hasMoreElements()
def next: T = enumeration.nextElement()
}
private implicit def enumerationToRichEnumeration[T](enumeration: java.util.Enumeration[T]): RichEnumeration[T] = {
new RichEnumeration(enumeration)
}
}
class Cookies(private val request: Request, private val response: Response) {
def -(key: String): Cookies = {
val c = new Cookie(key, "")
c.setDomain("com.example")
c.setMaxAge(0)
c.setPath("/")
response.addCookie(c)
this
}
def +(key: String, value: String): Cookies = {
this + (key, value, 60 * 60 * 24 * 365)
}
def +(key: String, value: String, age: Int): Cookies = {
val c = new Cookie(key, value)
c.setDomain("com.example")
c.setMaxAge(age)
c.setPath("/")
response.addCookie(c)
this
}
}
object Servlet {
private val uriRE = new Regex("(.*?)(\\.(.*))?$", "uri", "junk", "format")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment