Skip to content

Instantly share code, notes, and snippets.

@richdougherty
richdougherty / RequestURIHandlingSpec.scala
Created February 7, 2014 23:18
Test for Play #2342
package play.it.http
import play.api.mvc._
import play.api.mvc.Results._
import play.api.test._
import play.api.libs.ws.Response
object RequestURIHandlingSpec extends PlaySpecification {
"Request-URI handling" should {
@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))
@richdougherty
richdougherty / Pipeline.scala
Created April 30, 2014 03:55
Pipeline example
import play.api.libs.json._
import play.api.libs.json.extensions._
/**
* Like a function that takes an A as input and produces a B as output.
* But we make a special type so we can define our own ++, map and
* flatMap operations.
*/
trait Pipeline[-A,+B] {
original =>
- repo-test $ ./activator run
[info] Loading global plugins from /Users/rich/.sbt/0.13/plugins
[info] Updating {file:/Users/rich/.sbt/0.13/plugins/}global-plugins...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] downloading http://repo1.maven.org/maven2/com/github/mpeltonen/sbt-idea_2.10_0.13/1.6.0/sbt-idea-1.6.0.jar ...
[info] [SUCCESSFUL ] com.github.mpeltonen#sbt-idea;1.6.0!sbt-idea.jar (2058ms)
[info] downloading http://repo.typesafe.com/typesafe/ivy-releases/com.typesafe.sbteclipse/sbteclipse-plugin/scala_2.10/sbt_0.13/2.4.0/jars/sbteclipse-plugin.jar ...
[info] [SUCCESSFUL ] com.typesafe.sbteclipse#sbteclipse-plugin;2.4.0!sbteclipse-plugin.jar (1727ms)
[info] downloading http://repo1.maven.org/maven2/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar ...
[info] [SUCCESSFUL ] commons-io#commons-io;2.0.1!commons-io.jar (1736ms)