alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (Render.scala)

This example Play Framework source code file (Render.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

accept, api, concurrent, future, http, mediarange, mvc, nil, none, notacceptable, partialfunction, play, play framework, requestheader, result, seq

The Render.scala Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.api.mvc

import play.api.http.MediaRange
import play.api.mvc.Results._
import play.api.http.HeaderNames._
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext

trait Rendering {

  object render {

    /**
     * Tries to render the most acceptable result according to the request’s Accept header value.
     * {{{
     * def myAction = Action { implicit req =>
     *   val value = ...
     *   render {
     *     case Accepts.Html() => Ok(views.html.show(value))
     *     case Accepts.Json() => Ok(Json.toJson(value))
     *   }
     * }
     * }}}
     *
     * @param f A partial function returning a `Result` for a given request media range
     * @return A result provided by `f`, if it is defined for the current request media ranges, otherwise NotAcceptable
     */
    def apply(f: PartialFunction[MediaRange, Result])(implicit request: RequestHeader): Result = {
      def _render(ms: Seq[MediaRange]): Result = ms match {
        case Nil => NotAcceptable
        case Seq(m, ms @ _*) =>
          f.applyOrElse(m, (m: MediaRange) => _render(ms))
      }

      // “If no Accept header field is present, then it is assumed that the client accepts all media types.”
      val result =
        if (request.acceptedTypes.isEmpty) _render(Seq(new MediaRange("*", "*", Nil, None, Nil)))
        else _render(request.acceptedTypes)
      result.withHeaders(VARY -> ACCEPT)
    }

    /**
     * Tries to render the most acceptable result according to the request’s Accept header value.
     *
     * This function can be used if you want to do asynchronous processing in your render function.
     * {{{
     * def myAction = Action.async { implicit req =>
     *   val value = ...
     *   render.async {
     *     case Accepts.Html() => loadData.map(data => Ok(views.html.show(value, data))))
     *     case Accepts.Json() => Future.successful(Ok(Json.toJson(value)))
     *   }
     * }
     * }}}
     *
     * @param f A partial function returning a `Future[Result]` for a given request media range
     * @return A result provided by `f`, if it is defined for the current request media ranges, otherwise NotAcceptable
     */
    def async(f: PartialFunction[MediaRange, Future[Result]])(implicit request: RequestHeader): Future[Result] = {
      def _render(ms: Seq[MediaRange]): Future[Result] = ms match {
        case Nil => Future.successful(NotAcceptable)
        case Seq(m, ms @ _*) =>
          f.applyOrElse(m, (m: MediaRange) => _render(ms))
      }

      // “If no Accept header field is present, then it is assumed that the client accepts all media types.”
      val result =
        if (request.acceptedTypes.isEmpty) _render(Seq(new MediaRange("*", "*", Nil, None, Nil)))
        else _render(request.acceptedTypes)
      result.map(_.withHeaders(VARY -> ACCEPT))
    }
  }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework Render.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.