Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:02:15+00:00 2026-06-14T01:02:15+00:00

I need to check for the existence of a YMD uri date param on

  • 0

I need to check for the existence of a YMD uri date param on every request, and, if exist, store it in the Request for later access in various parts of the application where implicit request has been made available.

Request interception seems an obvious choice:
https://github.com/playframework/Play20/wiki/ScalaInterceptors

However, I am not seeing a way to add to the Request (actually RequestHeader is what’s available), assume it’s read only/immutable.

I am able to add data to the Request via Action Composition; however, this approach is limited to the composed action, and not every action (why I’d like the above global Before Interceptor approach to somehow work). For example, an Authentiucated action wrapper allows me to store the logged in user’s id in the Request.

trait Secured { 
  private case class Success[A](uid: Int, r: Request[A]) extends WrappedRequest(r)

  def Authenticated[A](p: BodyParser[A])
    (f: Success[A] => Result)(implicit group: RoleGroup) = {

    def apply(maybeUser: Option[String])(implicit r: Request[A]) = {
      maybeUser map {id =>
        Cache.orElse(group.to_s+"-"+id, 3600){
          repo.user.get(id.int, group) map(_.active) getOrElse false
      } fold ( onFail, f(Success(id.int, r)) )
    }
  }

I would like to do the same for a possible uri date param, but have it apply to all Actions, or, again, have it applied before the Play routes mechanism is triggered via global interceptor.

Ideas much appreciated, thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:02:16+00:00Added an answer on June 14, 2026 at 1:02 am

    To simulate injecting arbitrary data into the Request, you can extend WrappedRequest:

    case class Outcome[A](
      r: Request[A], 
      uid: Int, 
      startDate: String,
      endDate: String,
      uriDate: JodaTime) extends WrappedRequest(r)
    

    and then create Action provider traits that return an instance of your custom request wrapper. For example, a base Task action could look like:

    trait TaskRequest 
      extends DateParser {
    
      def Task[A](p: BodyParser[A])(f: Outcome[A] => Result) = {
        Action(p) { implicit r =>
          f( toOutcome[A](r, r.session.get("userID").map(_.toInt) getOrElse 0) )
        }
      }
      def Task(f: Outcome[AnyContent] => Result): Action[AnyContent] = {
        import play.api.mvc.BodyParsers._
        Task(parse.anyContent)(f)
      }
    
      protected def toOutcome[A](r: Request[A], uid: Int) = {
        val(start,uriDate) = (startDate(r), toDate(r.uri))
        val end = seasonEnd(start)
        Outcome(r, uid, start.toString("yyyyMMdd"), end.toString("yyyyMMdd"), uriDate)
      }
    
      val ymdMatcher = "\\d{8}".r  
      protected def startDate(uri: String) = {
        ymdMatcher.findFirstIn(uri). // set season start based on ymd URI param if exist 
        map(_.startDate) getOrElse seasonStart(new JodaTime)
      }
    
      protected def toDate(uri: String) =
        ymdMatcher.findFirstIn(r.uri).map(_.to_date) getOrElse new JodaTime
    }
    

    and then derive an Authenticated action from Task:

    trait Secured
      extends TaskRequest { 
    
      def Authenticated[A](p: BodyParser[A])
        (f: Outcome[A] => Result)(implicit group: RoleGroup) = {
    
        def apply(maybeUser: Option[String])(implicit r: Request[A]) = {
          maybeUser map {id =>
            Cache.orElse(group.to_s+"-"+id, 3600){
              repo.user.get(id.int, group) map(_.active) getOrElse false
          } fold ( onFail, f( toOutcome(r, id.int)) )
        }
      }
    

    and then use in your controllers:

    def index = Authenticated { implicit request=>
      // voila, current season start date since no uri date param exist
      request.startDate
    }
    

    I could call the date manipulations functions on a per action basis, sure, but that would only apply to Action scope. How would I access the startDate in the template layer? I couldn’t, but with a custom request wrapper you can inject anything you want and easily reference the data anywhere. So instead of bringing play’s request into scope:

    @(model: Foo, title: String)(implicit request: play.api.mvc.Request[_])
    

    you reference your custom request wrapper:

    @(model: Foo, title: String)(implicit request: controllers.Outcome[_])
    

    Does the job quite nicely, global interception I have yet to find a use for (maybe re-route some legacy URIs, but nothing as powerful as the WrappedRequest approach)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to check the existence of an input argument. I have the following
I need to check the existence of following characters in a given string: characters
I need to check for the existence of a file in a directory. The
We need to check for the existence of a SQL server across all domains
For form validation to check email existence I need custom validation I tried money
I already have a script that check for the cookies existence, but i need
Is this a possible function? I need to check if a variable is existent
I need to check for a condition on each page I visit on the
I need to check for duplicates before saving to the database in the create
I need to check some settings for all users on Windows clients in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.