I'm working on this Scala play-authenticate-usage-scala project where I build on top of a Java framework play-authenticate (I know, life is not perfect ...). While running the application I see the exception included below.
Carefully inspecting the stack trace leads me to the view views._providerPartial.scala.html
that is using the forProviders.scala.html
template that in turns uses classes that depend on Java's Play play.mvc.Http.{ Session, Context, etc}
and thus, the error because my sample application brings the Scala play.api.mvc._
ones.
I know I can do in Scala:
import play.core.j.JavaHelpers
val jContext : play.mvc.Http.Context = JavaHelpers.createJavaContext(request)
val jSession : play.mvc.Http.Session = context.session()
The question is how can I make these above implicitly available to that view.
[error] application -
! @72ef8b5j8 - Internal server error, for (GET) [/login] ->
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)
UPDATE I attempted the following but didn't work. Basically Java side expects all these in a ThreadLocal
. The specific ThreadLocal
instance is publicly located in play.mvc.Http.Context.current
. Therefore I prepared a template helper called _adaptScalaToJava.scala.html
like this:
@import be.objectify.deadbolt.scala._
@import play.core.j.JavaHelpers
@()(implicit request: AuthenticatedRequest[Any])
@play.mvc.Http.Context.current.set(JavaHelpers.createJavaContext(request))
it sets directly the jContext in the current thread and is used like this:
@_adaptScalaToJava
@forProviders(playAuth, skipCurrent) { p =>
<li>
@if(p.getKey() == "openid") {
<a href="javascript:void(0);" onclick="askOpenID('@p.getUrl()');">
} else {
<a href="@p.getUrl()">
}
@_providerIcon(p.getKey())</a>
</li>
}
but even though compiles find it still fires the same exception ...
Placing only this line before the troubling code solves the issue:
@play.mvc.Http.Context.current.set(play.core.j.JavaHelpers.createJavaContext(request))
@forProviders(playAuth, skipCurrent) { p =>
trying to make it a bit more clean i.e. that it isn't reset all the time, it is still a mystery for me why this won't work:
@if (play.mvc.Http.Context.current.get() == null) {
@play.mvc.Http.Context.current.set(play.core.j.JavaHelpers.
createJavaContext(request))
}
Seems like the play.mvc.Http.Context.current
is not null
... bottom line is I need to connect with the IDE and debug the code.
UPDATE Actually this is the official way to do it:
@play.core.j.JavaHelpers.withContext(request) { jContext =>
@forProviders(playAuth, skipCurrent) { p =>
...
}
}