Scalatra 2.2
-
Upload
ivan-porto-carrero -
Category
Technology
-
view
2.605 -
download
0
description
Transcript of Scalatra 2.2
![Page 1: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/1.jpg)
Scalatra 2.2
NOVEMBER 2012
![Page 2: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/2.jpg)
Short introduc,on And overview of New features
2
![Page 3: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/3.jpg)
● Rou5ng ● Params
● Json4s
● Akka ● Atmosphere
● Commands
What?
![Page 4: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/4.jpg)
class HelloWorld extends ScalatraServlet { get("/hello/:name") { <h1>Hello, {params("name")}</h1> } }
Hello, Scalatra
![Page 5: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/5.jpg)
class HelloWorld extends ScalatraServlet { get("/hello/:name") { <h1>Hello, {params("name")}</h1> } }
● Matches GET requests to /hello/*
● Captures named parameters from the route (here, :name).
● Matching requests are dispatched to the ac5on.
Hello, Scalatra: Routes
![Page 6: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/6.jpg)
class HelloWorld extends ScalatraServlet { get("/hello/:name") { <h1>Hello, {params("name")}</h1> } }
● When a request matches a route, the ac5on is run.
● A response is generated from the ac5on's result. Supports XML literals, Byte arrays, Strings, Files, InputStreams.
● Rendering of ac5on results is customizable.
Hello, Scalatra: Ac,ons
![Page 7: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/7.jpg)
class HelloWorld extends ScalatraServlet { get("/hello/:name") { <h1>Hello, {params("name")}</h1> } }
● ScalatraServlet provides the HTTP DSL. ● Scalatra 2.x is built on Java Servlets, the standard web server API for the JVM.
Hello Scalatra: ScalatraServlet
![Page 8: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/8.jpg)
● Based on Sinatra, which I like to call "the universal web DSL". Clones exist in:
• Ruby • PHP • Python • JavaScript
• Java • F# • Erlang • Haskell • Clojure • Bash • Many others
Why Scalatra?
![Page 9: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/9.jpg)
● Stateless by default, sessions if you need them.
● Lean by default, integra5ons if they're popular.
• Scalate • JSON4s
• Akka • Atmosphere
• Embrace standards
• HTTP • JVM
• Scala • Beginner friendly. • Scale with your app and your exper5se.
Scalatra Philosophy
![Page 10: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/10.jpg)
get("/hello/:name") { "Hi, "+params("name") }
● Routes start with an HTTP method. • Get / Head / Op5ons / Connect • Post / Put / Patch / Delete / Trace
● Advanced usage: can call at any5me to register routes on the fly.
Rou,ng in depth: Methods
![Page 11: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/11.jpg)
get("/hello/:name") { "Hi, "+params("name") }
● Matches the servlet path. • Pro5p: compose large apps with mul5ple servlets
● Declare route params with a leading ':’ • Matches everything up to next '/', '?', or '#'
Rou,ng in depth: Params
![Page 12: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/12.jpg)
Rou,ng in depth: Params
![Page 13: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/13.jpg)
get("/hello/*") { multiParams("splat").mkString(",") }
● Splats get stored in the captures param.
● If you expect multiple values, you can access it through multiParams.
• params is just a convenient view of multiParams
Rou,ng in depth: Splats
![Page 14: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/14.jpg)
get("""/hello/(\w+)""".r) { "Hello, "+params("captures") }
● Use if you have a real hairy route that standard syntax can't handle.
● No named groups yet. Java didn't add named regexes until version 7!
Rou,ng in depth: regex
![Page 15: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/15.jpg)
def userAgent = request.getHeader("User-‐Agent”)
get("/hello/:name", !userAgent.contains("Lynx")) { status = 403 "You and your fancy, schmancy browser. Get out.” }
● Both paths and boolean expressions are converted to route matchers. Implicitly
● A route matches if and only if all route matchers match.
Rou,ng in depth: booleans
![Page 16: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/16.jpg)
● The return value of an action becomes the body of the response.
Ac,ons in depth: return value
![Page 17: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/17.jpg)
Ac,ons in depth: render pipeline
protected def renderPipeline: RenderPipeline = { case bytes: Array[Byte] => response.outputStream.write(bytes) case is: java.io.InputStream => using(is) { util.io.copy(_, response.outputStream) } }
• It's just a partial function. • Override it. Use orElse to delegate to the default.
● It's just a partial function.
● Override it. Use orElse to delegate to the default.
![Page 18: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/18.jpg)
Ac,ons in depth: mutable
![Page 19: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/19.jpg)
get("/hello/:name") { status = HttpServletResponse.SC_GONE contentType = "text/html” <h1>Goodbye, cruel world</h1> }
● Status and headers are typically set in an imperative fashion.
● Response is thread local. Relax.
Ac,ons in depth: mutable
![Page 20: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/20.jpg)
● Capture input
● Validate input
Commands
![Page 21: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/21.jpg)
● Serialize case classes to json
● Handles xml as well as json
Integra,ons: JSON4S
![Page 22: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/22.jpg)
● Takes advantage of servlet 3.0 async support
Integra,ons: Akka
![Page 23: Scalatra 2.2](https://reader033.fdocuments.net/reader033/viewer/2022051111/5561ed36d8b42ab1068b548a/html5/thumbnails/23.jpg)
● WebSocket
● Server side events
● Like socket.io but not socket.io
Integra,ons: Atmosphere