Skip to content

Instantly share code, notes, and snippets.

@klaeufer
Created March 19, 2011 23:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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")
}
}
}
@softprops
Copy link

very cool @klaeufer! @n8han's got some similar code in a somewhat older repo. Check out sling around line https://github.com/n8han/Sling/blob/master/src/main/scala/sling/App.scala#L86

@n8han
Copy link

n8han commented Mar 20, 2011

Sling predates Unfiltered and I did the conversion hastily so it might be a little weird, but yes.

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

n8han commented Mar 25, 2011

@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