0

I'm currently working on a RESTful API using the play! framework 2.x for an academic project.

I tried to use the Apache Oltu library but as they make intensive usage of HttpServletRequest/Response I wasn't able to use it. Then I found a play Request wrapper to HttpServletRequest but it was for play 1.x. Because I have no knowledge of servlets I wasn't able to write a wrapper by myself so I searched the web for something else.

I'm trying to use the oauth2play2scala library (which is a port of Oltu for play 2.x) to implement an OAuth provider, but I'm facing the problem that the library was written for the Scala API of play while I'm exclusively using Java.

As you can see in the example code from the oauth2play2scala repository, I need to pass the play.api.mvc.Request instance to the OAuthAuthzRequest constructor. All the classes in the play.api package are used in scala, while the classes out of this packages are usable in Java. In order to construct a OAuthAuthzRequest, I need:

  • to retrieve the play.api.mvc.Request instance (from Scala to Java) OR
  • to find a wrapper in order to use a play.mvc.Request (Java) as a play.api.mvc.Request (Scala)
  • another alternative that I didn't thinked about :D

Thanks in advance

4
  • Maybe you could look at first chapter of Play doc: playframework.com/documentation/2.2.x/JavaActions Commented Dec 18, 2014 at 0:04
  • 1
    The problem is that this request() method is returning a play.mvc.Http.Request while I need a play.api.mvc.Request. The nuance being that request() return the Java implementation of play requests while the one I need is the Scala implementation of play requests. But I need to retreive the Scala implementation in a Java source code. I already had read the entire tutorials from the official website plus a book on Play 2, I looked in the Scala doc too but I got nothing so no need to redirect me to the tutorial ;) Commented Dec 18, 2014 at 0:11
  • If I would have wanted to retrive the Java implementation of Request in a Scala controller (eg. in order to interface with a library) I would have called play.mvc.Http.Context.current().request(). What I want here is the opposite: getting the Scala implementation of Request in a Java controller (in order to interface with oauth2play2scala with was designed for use with the Scala API of play). Or a wrapper, or a converter method, or a tricky solution. Commented Dec 18, 2014 at 0:16
  • 1
    Don't worry so much about downvotes ... it is something crazy called democracy :D I fixed that for you :D. I too think it is a design flaw in Play to have such a strong abstract mismatch specially when a lot of project for legacy reasons need mixed Java-Scala implementations. In my case I want to build a Web App using Scala Play but using a Java Play plugin and thus, I have the same issue as you do. Will post any possible answer I find. Commented Dec 25, 2016 at 10:22

1 Answer 1

2

As of Play 2.5.10 and after skimming over the API (you need a good IDE :)) I found that you can simply do:

val jRequest: play.mvc.Http.Request = ... // your Java request
val sRequest: play.mvc.api.Request = jRequest._underlyingRequest

See Http.Request Javadoc and the note For internal Play-use only but I need it in my use-case. However, there is probably no guarantee that the underlying is always populated, testing that now ... and worked! The important bit when tunneling Scala -> Java -> Scala is to make sure that the jContext is first created using JavaHelpers.createJavaContext(request.asInstanceOf[Request[RequestBody]]) since this createJavaContext(Request[RequestBody]) overloaded implementation is the only one that populates the _underlyingRequest.

See a working example here play-authenticate-usage-scala and a related discussion in the playframework.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.