Skip to content

Instantly share code, notes, and snippets.

@richdougherty
richdougherty / Global.scala
Created February 7, 2014 22:05
Removing leading slashes before routing requests
import play.api._
import play.api.mvc._
object Global extends GlobalSettings {
case class RewrittenRequestHeader(val path: String, delegate: RequestHeader) extends RequestHeader {
def id = delegate.id
def tags = delegate.tags
def uri = delegate.uri
def method = delegate.method
@richdougherty
richdougherty / Global.scala
Created February 7, 2014 22:38
Strip duplicate leading slashes before both routing and handling requests
import play.api._
import play.api.mvc._
import scala.concurrent.Future
object RewrittenRequestHeader {
def rewrite(header: RequestHeader): RequestHeader = {
if (header.path.startsWith("//")) {
val newPath = header.path.substring(1)
RewrittenRequestHeader(newPath, header)
} else {
@richdougherty
richdougherty / Global.scala
Created February 7, 2014 22:45
Removing leading slashes before handling requests
import play.api._
import play.api.mvc._
object Global extends GlobalSettings {
case class RewrittenRequestHeader(val path: String, delegate: RequestHeader) extends RequestHeader {
def id = delegate.id
def tags = delegate.tags
def uri = delegate.uri
def method = delegate.method
@richdougherty
richdougherty / Controller.java
Created February 10, 2014 23:27
Handle ResultExceptions thrown by actions by adding a @ResultExceptionHandling annotation
@Transactional
@ResultExceptionHandling
public static Result list(int page, String sortBy, String order, String filter) {
if (page == 2) throw new ResultException(ok("transaction rolled back"));
return ok(
list.render(
Computer.page(page, 10, sortBy, order, filter),
sortBy, order, filter
)
@richdougherty
richdougherty / Application.java
Created February 11, 2014 02:20
Show non-500 Results for Exceptions
import actions.*;
...
@Transactional
@ResultExceptionHandling
public static Result list(int page, String sortBy, String order, String filter) {
if (page == 2) throw new ResultException(ok("transaction rolled back"));
return ok(
list.render(
@richdougherty
richdougherty / Application.scala
Created February 12, 2014 04:29
Using native libraries in Play
package controllers
import play.api._
import play.api.mvc._
import com.almworks.sqlite4java._
import java.io.File
object Application extends Controller {
@richdougherty
richdougherty / gist:9337523
Created March 4, 2014 00:13
Java template language resolution in master
+-------------------------------------------------------------------------------+-----------------+
| Name | Time (ms) |
+-------------------------------------------------------------------------------+-----------------+
| +---play.api.Configuration.getString(String, Option) | 39,867 100 % |
| | | |
| +---play.api.Play$.langCookieName(Application) | 25,392 64 % |
| | | | |
| | +---play.api.Play.langCookieName(Application) | |
| | | | |
| | +---play.Play.langCookieName() | |
@richdougherty
richdougherty / README.md
Created March 18, 2014 20:49
Using a Java class for an SBT task

An example of using a Java class for an SBT build task.

We take advantage of the following fact:

The project directory is another project inside your project which knows how to build your project. The project inside project can (in theory) do anything any other project can do. Your build definition is an sbt project.

(See http://www.scala-sbt.org/release/docs/Getting-Started/Full-Def.html)

Test results

Non-blocking - 38 req/s

  def nonBlocking(delay: Long) = Action.async {
    val result = Promise[SimpleResult]
    Akka.system.scheduler.scheduleOnce(delay.millis) {
      result.success(Ok)
 }
package scala.concurrent
import scala.util.Try
case class FutureTry[+A](future: Future[A]) {
def map[B](f: Try[A] => Try[B])(implicit ec: ExecutionContext): FutureTry[B] = {
FutureTry(future.transform(f))
}
def flatMap[B](f: Try[A] => FutureTry[B])(implicit ec: ExecutionContext): FutureTry[B] = {
FutureTry(future.transformWith(t => f(t).future))