EzDevInfo.com

playframework-2.0 interview questions

Top playframework-2.0 frequently asked interview questions

How to use Twitter Bootstrap 2 with play framework 2.x

I know that current Play! distribution has a helper for Bootstrap 1.4. What should I do if I want to use the current version of Bootstrap?


Source: (StackOverflow)

Scala Compiler not found in Intellij IDEA 11 with Play 2.0 project

I created a project using the typical play new, and then I used play idea to generate the .iml file. When I imported the .iml file into Intellij IDEA, I could not figure out how to get the compiler library to recognize. Below are some screenshots:

enter image description here

enter image description here

Please help!


Source: (StackOverflow)

Advertisements

Play 2.x : Reactive file upload with Iteratees

I will start with the question: How to use Scala API's Iteratee to upload a file to the cloud storage (Azure Blob Storage in my case, but I don't think it's most important now)

Background:

I need to chunk the input into blocks of about 1 MB for storing large media files (300 MB+) as an Azure's BlockBlobs. Unfortunately, my Scala knowledge is still poor (my project is Java based and the only use for Scala in it will be an Upload controller).

I tried with this code: Why makes calling error or done in a BodyParser's Iteratee the request hang in Play Framework 2.0? (as a Input Iteratee) - it works quite well but eachElement that I could use has size of 8192 bytes, so it's too small for sending some hundred megabyte files to the cloud.

I must say that's quite a new approach to me, and most probably I misunderstood something (don't want to tell that I misunderstood everything ;> )

I will appreciate any hint or link, which will help me with that topic. If is there any sample of similar usage it would be the best option for me to get the idea.


Source: (StackOverflow)

Is there an eclipse plugin for Play2?

I know there is one for play1, but I don't find any for play2.

I hope the plugin can compile the templates to scala code automatically. It's not convenient now.


Source: (StackOverflow)

Play Framework 2: JavaScript gets duplicated as a result of minification (google closure compiler)

I have a weird issue related to JavaScript minification, the problem is that when placing JavaScript files such as test1.js, test2.js inside /assets/javascript/test folder and using following options for closure compiler:

val defaultOptions = new CompilerOptions()
    defaultOptions.closurePass = true
    defaultOptions.setProcessCommonJSModules(false)
    defaultOptions.setPrettyPrint(true)
    CompilationLevel.WHITESPACE_ONLY.setOptionsForCompilationLevel(defaultOptions)

They get minified, but this will cause both test1.min.js and test2.min.js files to share same codes as minified... this will cause some additional JavaScript bugs when files are used within same pages and takes up more disk space.

However, if CompilerOptions is not set, JavaScript compiles properly without duplication but the compilation level is too strict and our JavaScript stops working because it says errors about some things written in those files. Overall, minification process is activated too late to fix all project js files to go through with default compiler options so only option now is to configure it to work in a way which use light minification but without js duplication.. any help on this issue will be highly appreciated!

I'm using Play Framework 2.1.1 with Java project.


Source: (StackOverflow)

How to integrate Play Framework 2.0 into Gradle build management using Maven dependencies?

Play framework 2.0 is a full-stack standalone framework for creating web applications. Probably, many people need to integrate it into their build management, nevertheless. Unfortunately, I did not find much information about his.

Here is my use case: I want to create a new project, which uses Scala and Play 2.0. I do NOT want to use sbt. I want to use Gradle, and dependency management should be done via Maven repositories.

I have only found this play module: http://www.playframework.org/modules/maven-1.0/home which supports dependency management via Maven.

I am looking for something like these examples in Grails: https://github.com/grails/grails-gradle-plugin or http://grails.org/doc/latest/guide/commandLine.html#4.5%20Ant%20and%20Maven

Of course, I could write scripts / tasks which call "play console commands". Though, I do not like this solution. Is there a better way to use Gradle / Maven for build management? If this is the only solution, then I would use Gradle, which then calls Play commands (i.e. sbt internally), right? Does this even work, or will there emerge other problems?

Thanks in advance...

Best regards, Kai


Source: (StackOverflow)

Scala streaming library differences (Reactive Streams/Iteratee/RxScala/Scalaz...)

I'm following the Functional Reactive Programming in Scala course on Coursera and we deal with RxScala Observables (based on RxJava).

As far as I know, the Play Iteratee's library looks a bit like RxScala Observables, where Observables a bit like Enumerators and Observers are bit like Iteratees.

There's also the Scalaz Stream library, and maybe some others?


So I'd like to know the main differences between all these libraries. In which case one could be better than another?


PS: I wonder why Play Iteratees library has not been choosed by Martin Odersky for his course since Play is in the Typesafe stack. Does it mean Martin prefers RxScala over Play Iteratees?


Edit: the Reactive Streams initiative has just been announced, as an attempt to standardize a common ground for achieving statically typed, high-performance, low latency, asynchronous streams of data with built-in non-blocking back pressure


Source: (StackOverflow)

Route to static file in Play! 2.0

I'm trying to make a route to a specific static file but everything I'm trying ends with an error.

I've made 3 different attempts:

1.

GET /file   staticFile:/public/html/file.html

The error I get:

Compilation error
string matching regex `\z' expected but `:' found

2.

GET /file   controllers.Assets.at(path="/public/html", "file.html")

The error I get:

Compilation error
Identifier expected

3.

GET /file   controllers.Assets.at(path="/public/html", file="file.html")

The error I get: (and this is the weirdest)

Compilation error
not enough arguments for method at: (path: String, file: String)play.api.mvc.Call. Unspecified value parameter file.

The weird part about the 3rd error is that it's thrown in a different file (app/views/main.scala.html) on the following line:

<link rel="stylesheet" media="screen" rel='nofollow' href="@routes.Assets.at("stylesheets/main.css")">

All of these methods were found in the official documentation and/or threads here on stackoverflow. What am I missing here?

Thanks.


Source: (StackOverflow)

Securing REST API on Play framework and OAuth2

I am developing an application with Play 2.0 and Scala that exposes some REST API. These APIs will be used by different applications, web, mobile or desktop, so the OAuth protocol (OAuth2) seems the most suitable.

Also I would initially use an external OAuth Provider such as Facebook.

My question is: what is the exact flow to authorize the individual REST call? What should I expect on the server side for each call and what I should check with the external provider?

With OAuth1 I knew that the client sent the token with all the signed request, but with Oauth2 I think not so, I imagine that if a token is not signed is not trusted and therefore I do not think this is the flow.


Source: (StackOverflow)

Discovery of Akka actors in cluster

I’ve been trying to wrap my head around the concepts of Akka and actor-based systems recently. While I have a pretty good understanding of the Akka fundamentals by now I’m still struggling with a few things when it comes to clustering and remote actors.

I’ll try to illustrate the issue using the WebSocket chat example that comes with Play Framework 2.0: There's an actor that holds the WebSockets and that keeps a list of the currently connected users. The actors basically represents the chat room both technically and logically. This works perfectly fine as long as there’s a single chat room running on a single server.

Now I'm trying to understand how this example would have to be extended when we are talking about many dynamic chat rooms (new rooms can be opened/closed at any time) running on a cluster of servers (with single nodes being added or removed according to current demand). In such a case user A could connect to server 1 while user B connects to server 2. Both might be talking on the same chat room. On each server there would still be an actor (for each chat room?) that holds the WebSocket instances to receive and publish events (messages) to the right users. But logically there should only be one chat room actor on either server 1 or server 2 that holds the list of currently connected users (or similar tasks).

How would you go about to achieve this, preferably in ”pure akka” and without adding an additional messaging system like ZeroMQ or RabbitMQ?

This is what I’ve come up with so far, please let me know whether this makes any sense:

  1. User A connects to server 1 and an actor is allocated that holds his WebSocket.
  2. The actor checks (using Router? EventBus? Something else?) whether a ”chat room actor” for the active chat room exists on any of the connected cluster nodes. Since it doesn't it will request the creation of a new chat room actor somehow and will send and receive future chat messages to/from this actor.
  3. User B connects on server 2 and an actor is allocated for his WebSocket as well.
  4. It also checks whether an actor for the requested chat room exists somewhere and finds it on server 1.
  5. The chat room actor on server 1 now acts as the hub for the given chat room, sending messages to all ”connected” chat member actors and distributing incoming ones.

If server 2 goes down, the chat room actor would have to be re-created on/moved to server 2 somehow, although this is not my primary concern right now. I'm wondering most about how this dynamic discovery of actors spread about various, basically independent machines could be done using Akka’s toolset.

I’ve been looking at Akka’s documentation for quite some time now, so maybe I’m missing the obvious here. If so, please enlighten me :-)


Source: (StackOverflow)

Request.params is gone in Play Framework 2.0

Is there a way to access all request parameters, regardless of HTTP method? I have poured over the documentation and the api without finding a way in Play Framework 2.0.

I have a search on a site that accepts POST and GET. The custom tracking on the site examines all parameters passed in to determine the correct way to store tracking data.

In the Play Framework 1.2.x, I was able to access parameters from a request in a Controller with

request.params.get("keywords")
request.params.get("location") 
request.params.all()

With Play Framework 2.0, this is no longer the case. The Request no longer has the method params, only queryString and queryString only works with GET and not POST.

It is not feasible to define every single possible tracking parameter into the Controller Action, they are dynamic.


UPDATE: A possible work around is using Body Parsers.

Depending on the content type of the request, the appropriate parser is used, e.g. application/form-url-encoded vs application/json

This is the crude Map that combines POST parameters and GET parameters, with GET parameters taking precedence.

val params: collection.mutable.Map[String, Seq[String]] = collection.mutable.Map() 
params ++= request.body.asFormUrlEncoded.getOrElse[Map[String, Seq[String]]] { Map.empty } 
params ++= request.queryString 

Source: (StackOverflow)

How to handle optional query parameters in Play framework

Lets say I have an already functioning Play 2.0 framework based application in Scala that serves a URL such as:

http://localhost:9000/birthdays

which responds with a listing of all known birthdays

I now want to enhance this by adding the ability to restrict results with optional "from" (date) and "to" request params such as

http://localhost:9000/birthdays?from=20120131&to=20120229

(dates here interpreted as yyyyMMdd)

My question is how to handle the request param binding and interpretation in Play 2.0 with Scala, especially given that both of these params should be optional.

Should these parameters be somehow expressed in the "routes" specification? Alternatively, should the responding Controller method pick apart the params from the request object somehow? Is there another way to do this?


Source: (StackOverflow)

Can't understand Iteratee, Enumerator, Enumeratee in Play 2.0 [closed]

I have just started to learn the Play 2.0 Framework. The one thing I just can't understand is the Iteratee, Enumerator and Enumeratee pattern described in the play tutorial. I have very little experience in functional languages.

What does this pattern accomplish?

How does it help me write non-blocking/reactive code?

some simple examples would help.


Source: (StackOverflow)

NoSuchMethodError in javax.persistence.Table.indexes()[Ljavax/persistence/Index

I have a Play Framework application and I was using Hibernate 4.2.5.Final (which is retrieved via the Maven dependency manager). I decided to upgrade to Hibernate 4.3.0.Final, recompile my application successfully, and ran it.

I got the exception below, and haven't been able to figure out why. I downgraded back to 4.2.5 and this issue did not occur. I then, tried upgrading Hibernate with each Final release after 4.2.5. That is, I went from 4.2.5.Final to 4.2.6.Final, to 4.2.7.Final, to 4.2.8.Final and then to 4.3.Final. The issue does not occur until I upgrade to 4.3.0.Final.

Java version information

java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

And exception:

play.api.UnexpectedException: Unexpected exception[NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;]
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:152) ~[play_2.10.jar:2.2.1]
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.1]
    at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.1]
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:110) ~[play_2.10.jar:2.2.1]
    at scala.util.Success.flatMap(Try.scala:200) ~[scala-library.jar:na]
Caused by: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
    at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:936) ~[hibernate-core-4.3.0.Final.jar:4.3.0.Final]
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:781) ~[hibernate-core-4.3.0.Final.jar:4.3.0.Final]
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3762) ~[hibernate-core-4.3.0.Final.jar:4.3.0.Final]
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3716) ~[hibernate-core-4.3.0.Final.jar:4.3.0.Final]
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410) ~[hibernate-core-4.3.0.Final.jar:4.3.0.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) ~[hibernate-core-4.3.0.Final.jar:4.3.0.Final]

Source: (StackOverflow)

Play framework 2.0 continuous integration setup

I am looking for ideas for a Play 2.0 continuous integration setup. It would contain typical jobs like build after a git push, nightly builds with deployment to a test Heroku instance etc. Also code quality and test coverage metrics generation would be handy.

At the moment the stack looks like Play 2.0 with Java but that might change to Scala.

For "traditional" Java web app I would use Hudson/Jenkins. I found a Hudson plugin for Play but it doesn't seem to support Play 2.0. Is Hudson suitable tool here in general or what is your setup for Play 2.0 applications?


Source: (StackOverflow)