Skip to content

Instantly share code, notes, and snippets.

View jroper's full-sized avatar

James Roper jroper

View GitHub Profile
import play.api._
import play.api.mvc._
object Global extends GlobalSettings {
override def onRequestReceived(header: RequestHeader): (RequestHeader, Handler) = {
val rewritten = if (header.uri.startsWith("//")) {
val newPath = header.uri.substring(1).replaceAll("\\?.*$", "")
header.copy(path = newPath)
} else {
@jroper
jroper / json.md
Last active August 29, 2015 14:02
RFC7159 Encoding

RFC7159 introduced a change to the RFC4627, as mentioned in Appendix A:

Changed the definition of "JSON text" so that it can be any JSON
  value, removing the constraint that it be an object or array.

This meant that the heuristic in Section 3 of RFC4627 was no longer valid:

Since the first two characters of a JSON text will always be ASCII
characters [RFC0020], it is possible to determine whether an octet

stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking

@jroper
jroper / Router.scala
Created July 2, 2014 07:50
Simple Play routing DSL with string interpolation
import java.util.regex.Pattern
import play.core.Routes
import play.api.mvc._
object Router extends Routes {
def routes = {
// Static paths
case Route("GET", p"") => controllers.Application.index
case Route("GET", p"/items") => controllers.Items.list
@jroper
jroper / After.scala
Created March 5, 2015 21:55
ERQX router before and after Play 2.4 upgrade
class BlogRouter(controller: BlogController) extends SimpleRouter {
object ? { def unapply[A](a: A) = Some(a, a) }
def routes = {
// Index
case GET(p"/" | p"") ? Page(page) => controller.index(page)
// View single blog post
case GET(p"/${int(year)}<\d{4}>/${int(month)}<\d{2}>/${int(day)}<\d{2}>/$permalink.html") =>
@jroper
jroper / dsl.scala
Created March 5, 2015 22:06
Play query string DSL possibilities
// In the path string:
case GET(p"/foo?param1=${param1}&param2=${param2}")
// Problems: doesn't support extracting lists of parameters or optional parameters
// Using ? and & extractors to combine multiple string interpolated query string parameters:
case GET(p"/foo" ? q"required=${required}" & q?"optional=${optional}" & q*"many=${many}")
// I think I like this one, q extracts String, q? extracts Option[String], q* extracts List[String]
// One question, how strict should we make it?
@jroper
jroper / ScalaFlectSpec.scala
Created April 22, 2012 15:24
ScalaFlect - The new strongly typed reflection library I'm writing for using in query DSLs
package au.id.jazzy.scalaflect
import org.specs2.mutable.Specification
object ScalaFlectSpec extends Specification {
"ScalaFlect" should {
"be able to look up simple case class properties" in {
val exampleCaseClass = new ScalaFlect(classOf[ExampleCaseClass])
val member = exampleCaseClass.reflect(_.property)
member.toString mustEqual "property"
@jroper
jroper / Schrödinger.scala
Created June 4, 2012 15:59
Scala would be more fun if it had one of these...
final case class Schrödinger[+A, +B](a: A, b: B) extends Either[A, B] {
private var isleft = true
private var isright = true
lazy val actual : Either[A, B] = {
if (new Random().nextBoolean()) {
isright = false
Left(a)
} else {
isleft = false
Right(b)
@jroper
jroper / gist:3034154
Created July 2, 2012 16:38
Play sessions
running(fakeApplication(), new Runnable() {
@Override
public void run() {
FakeRequest req = fakeRequest();
for (int i = 0; i < 5; i++) {
req = req.withSession("key" + i, "value" + i);
}
for (int i = 0; i < 5; i++) {
if (!req.getWrappedRequest().session().get("key" + i).isDefined()) {
// Whether we reach this point or not depends on HashMap ordering
@jroper
jroper / playlighthouselinker.js
Created September 21, 2012 06:47
Play Lighthouse Linker Greasemonkey Script
// ==UserScript==
// @name Play 2 Lighthouse Linker
// @description Links Play 2 Lighthouse ticket references to lighthouse
// @namespace http://playframework.org
// @version 1.0
// @author James Roper <james@jazzy.id.au>
// @include https://github.com/playframework/Play20/*
// @require http://code.jquery.com/jquery-1.8.2.min.js
// @run-at document-end
// ==/UserScript==
@jroper
jroper / child.threaddump
Created October 6, 2012 04:26
Thread dumps of hung play builds
2012-10-06 14:18:46
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.10-b01-428 mixed mode):
"DestroyJavaVM" prio=5 tid=7f8444003000 nid=0x10472b000 waiting on condition [00000000]
java.lang.Thread.State: RUNNABLE
"com.google.common.base.internal.Finalizer" daemon prio=5 tid=7f844bdd7000 nid=0x10e4ab000 in Object.wait() [10e4aa000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <7f40c3a90> (a java.lang.ref.ReferenceQueue$Lock)