Scala Play 2.5 How to make Java's Context/Session/Request accessible to a view?
<p>I'm working on this Scala <a href="https://github.com/bravegag/play-authenticate-usage-scala" rel="nofollow noreferrer">play-authenticate-usage-scala</a> project where I build on top of a Java framework <a href="https://github.com/joscha/play-authenticate" rel="nofollow noreferrer">play-authenticate</a> (I know, life is not perfect ...). While running the application I see the exception included below.</p>

<p>Carefully inspecting the stack trace leads me to the view <code>views._providerPartial.scala.html</code> that is using the <code>forProviders.scala.html</code> template that in turns uses classes that depend on Java's Play <code>play.mvc.Http.{ Session, Context, etc}</code> and thus, the error because my sample application brings the Scala <code>play.api.mvc._</code> ones.</p>

<p>I know I can do in Scala:</p>

<pre><code>import play.core.j.JavaHelpers

val jContext : play.mvc.Http.Context = JavaHelpers.createJavaContext(request)
val jSession : play.mvc.Http.Session = context.session() 
</code></pre>

<p>The question is how can I make these above implicitly available to that view. </p>

<pre><code>[error] application - 

! @72ef8b5j8 - Internal server error, for (GET) [/login] -&gt;

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[RuntimeException: There is no HTTP Context available from here.]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
    at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
    at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
    at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:346)
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.lang.RuntimeException: There is no HTTP Context available from here.
    at play.mvc.Http$Context.current(Http.java:62)
    at play.mvc.Http$Context$Implicit.session(Http.java:330)
    at com.feth.play.module.pa.views.html.forProviders_Scope0$forProviders$$anonfun$apply$1.apply(forProviders.template.scala:37)
    at com.feth.play.module.pa.views.html.forProviders_Scope0$forProviders$$anonfun$apply$1.apply(forProviders.template.scala:35)
    at play.twirl.api.TemplateMagic$.defining(TemplateMagic.scala:13)
    at com.feth.play.module.pa.views.html.forProviders_Scope0$forProviders.apply(forProviders.template.scala:35)
    at views.html._providerPartial_Scope0$_providerPartial.apply(_providerPartial.template.scala:38)
    at views.html.login_Scope0$login_Scope1$login.apply(login.template.scala:74)
    at controllers.Application$$anonfun$4$$anonfun$apply$4.apply(Application.scala:57)
    at controllers.Application$$anonfun$4$$anonfun$apply$4.apply(Application.scala:57)
</code></pre>

<p><strong>UPDATE</strong> I attempted the following but didn't work. Basically Java side expects all these in a <code>ThreadLocal</code>. The specific <code>ThreadLocal</code> instance is publicly located in <code>play.mvc.Http.Context.current</code>. Therefore I prepared a template helper called <code>_adaptScalaToJava.scala.html</code> like this:</p>

<pre><code>@import be.objectify.deadbolt.scala._
@import play.core.j.JavaHelpers

@()(implicit request: AuthenticatedRequest[Any])

@play.mvc.Http.Context.current.set(JavaHelpers.createJavaContext(request))
</code></pre>

<p>it sets directly the jContext in the current thread and is used like this:</p>

<pre><code>@_adaptScalaToJava
@forProviders(playAuth, skipCurrent) { p =&gt;
    &lt;li&gt;
    @if(p.getKey() == "openid") {
        &lt;a href="javascript:void(0);" onclick="askOpenID('@p.getUrl()');"&gt;
    } else {
        &lt;a href="@p.getUrl()"&gt;
    }
    @_providerIcon(p.getKey())&lt;/a&gt;
    &lt;/li&gt;
}
</code></pre>

<p>but even though compiles find it still fires the same exception ...</p>