Skip to content

Instantly share code, notes, and snippets.

View jroper's full-sized avatar

James Roper jroper

View GitHub Profile
// TEST 1:
// Without scrolling down, can you work out whether bar() is the return value, or a side effecting
// method call?
def test1(): Boolean = {
if (foo) {
bar()
} else {
/*
It is a truth universally acknowledged, that a single man in possession
of a good fortune, must be in want of a wife.
@jroper
jroper / Attempts.scala
Created June 9, 2015 14:16
Reader monads
import scalaz.Reader
case class User(id: Int, name: String)
case class Interest(name: String)
trait Database
trait Attempt1 {
// How every explanation of Reader monad I've seen/read goes:
@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 / 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 / GlobalStaticVar.scala
Last active September 25, 2017 21:12
Taking global static variables to a new level on the JVM...
/**
* Provides a global (cross classloader) static var.
*
* Where might you use this? Anywhere where you want to be evil ;)
*
* But, my use case was in a dynamic classloading environment where you want to load a library that
* depends on a native library. Only one classloader can ever load and link the classes associated
* with the native library, so if a second classloader (for example if a dynamic reload was done)
* wanted to use it to, it couldn't. This provided a cross classloader mechanism for storing the
* classloader that loaded the native library.
@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 / 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

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 / gist:7019715
Created October 17, 2013 05:54
Oracle JDK TLS renegotiation bug when want_auth is added and session caching is used

There is a bug in the Oracle JDK TLS implementation.

A server might have an existing SSL session that has no client authentication associated with it. For example, it might be a web server, and a client has accessed the index page. Authentication is not required for the index page, so the server hasn't requested it.

At some point in future, the user might attempt to access a secured area of the site. A typical use case might be that they click an authenticate button. If the server wants to use SSL client certificates to authenticate the user, then at this point the server can send a renegotiation request, asking for a client certificate.

Java SSL negotiation and renogotiation has two modes when it comes to requesting a client certificate, one is "want auth", and the other is "need auth". Want auth means that if the client doesn't provide a certificate, the session will continue, without any client certificates. Need auth means that if the client doesn't provide a certificate, the session will be termi

@jroper
jroper / gist:7019306
Created October 17, 2013 04:44
Reads for recursive search paths
// Reads for recursive path
def recursiveSearchReads[T : Reads](path: JsPath) = Reads[Seq[T]] { json =>
path.apply(json).map(_.validate[T]).foldLeft[JsResult[Seq[T]]](JsSuccess(Nil)) {
case (JsError(a), JsError(b)) => JsError(a ++ b)
case (err: JsError, _) => err
case (_, err: JsError) => err
case (JsSuccess(ts, _), JsSuccess(t, _)) => JsSuccess(ts :+ t)
}.repath(path)
}