Skip to content

Instantly share code, notes, and snippets.

@klaeufer
Created March 19, 2011 23:25
Show Gist options
  • Select an option

  • Save klaeufer/877902 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
Author

Very cool, much more industrial-strength than my toy example. Can someone tell me where /\ comes from? Is it some kind of conjunction? Thanks!

@n8han
Copy link
Copy Markdown

n8han commented Mar 25, 2011

@klaeufer
Copy link
Copy Markdown
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