Skip to content

Instantly share code, notes, and snippets.

@klaeufer
Created March 19, 2011 23:25
Show Gist options
  • Save klaeufer/877902 to your computer and use it in GitHub Desktop.
Save klaeufer/877902 to your computer and use it in GitHub Desktop.
Simple Unfiltered example for providing OPTIONS, HEAD, and conditional GET
class RootPlan extends Plan {
val logger = Logger(classOf[RootPlan])
val creationDate = new java.util.Date
val eTag = hashCode.toString
val Head = Ok ~> Vary("Accept-Charset", "Accept-Encoding", "Accept-Language", "Accept")
val Caching =
CacheControl("max-age=3600") ~>
LastModified(DateUtils.formatDate(creationDate)) ~>
ETag(eTag)
def intent = {
case OPTIONS(_) => Ok ~> Allow("GET", "HEAD", "OPTIONS")
case HEAD(Path(Seg(Nil))) => {
logger.debug("HEAD /")
Head
}
case req @ GET(Path(Seg(Nil))) => {
logger.debug("GET /")
val cached =
req match {
case IfNoneMatch(xs) => xs contains eTag
case IfModifiedSince(xs) => {
val sinceDate = DateUtils.parseDate(xs mkString ", ")
! creationDate.after(sinceDate)
}
case _ => false
}
if (cached)
Head ~> Caching ~> NotModified
else
Head ~> Caching ~> ResponseString(
"To register: " +
"curl -X PUT -d 'user[password]=pass' -d 'user[email]=you@host' -d 'user[full_name]=Your%20Name' -v http://localhost:8080/users/you")
}
}
}
@klaeufer
Copy link
Author

klaeufer commented Mar 26, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment